Google 云端硬盘中的文件夹。您可以通过 Drive 访问或创建文件夹。
// Log the name of every folder in the user's Drive. const folders = DriveApp.getFolders(); while (folders.hasNext()) { const folder = folders.next(); Logger.log(folder.getName()); }
方法
详细文档
add Editor(emailAddress)
add Editor(user)
add Editors(emailAddresses)
add Viewer(emailAddress)
add Viewer(user)
add Viewers(emailAddresses)
create File(blob)
根据给定的任意数据 Blob 在当前文件夹中创建一个文件。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
blob | Blob | 新文件的数据。 |
返回
File - 新文件。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/drive
create File(name, content)
在当前文件夹中创建一个具有指定名称和内容的文本文件。如果 content 大于 50 MB,则抛出异常。
// Create a text file with the content "Hello, world!" DriveApp.getRootFolder().createFile('New Text File', 'Hello, world!');
参数
| 名称 | 类型 | 说明 |
|---|---|---|
name | String | 新文件的名称。 |
content | String | 新文件的内容。 |
返回
File - 新文件。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/drive
create File(name, content, mimeType)
在当前文件夹中创建一个具有指定名称、内容和 MIME 类型的文件。如果 content 大于 10MB,则抛出异常。
// Create an HTML file with the content "Hello, world!" DriveApp.getRootFolder().createFile('New HTML File', '<b>Hello, world!</b>', MimeType.HTML);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
name | String | 新文件的名称。 |
content | String | 新文件的内容。 |
mime | String | 新文件的 MIME 类型。 |
返回
File - 新文件。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/drive
create Folder(name)
create Shortcut(targetId)
create Shortcut For Target Id And Resource Key(targetId, targetResourceKey)
创建指向所提供的云端硬盘内容 ID 和资源密钥的快捷方式,并返回该快捷方式。资源密钥是一种附加参数,需要传递该参数才能访问通过链接共享的目标文件或文件夹。
// Creates shortcuts for all folders in the user's drive that have a specific // name. // TODO(developer): Replace 'Test-Folder' with a valid folder name in your // drive. const folders = DriveApp.getFoldersByName('Test-Folder'); // Iterates through all folders named 'Test-Folder'. while (folders.hasNext()) { const folder = folders.next(); // Creates a shortcut to the provided Drive item ID and resource key, and // returns it. DriveApp.createShortcutForTargetIdAndResourceKey( folder.getId(), folder.getResourceKey(), ); }
参数
| 名称 | 类型 | 说明 |
|---|---|---|
target | String | 目标文件或文件夹的 ID。 |
target | String | 目标文件或文件夹的资源键。 |
返回
File - 新快捷方式。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/drive
get Access(email)
获取授予特定用户的权限。该方法不支持返回 Google 群组的权限或通过 Google 群组继承的权限。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
email | String | 应检查其权限的用户的电子邮件地址。不支持 Google 群组。 |
返回
Permission - 授予用户的权限。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
get Access(user)
获取授予特定用户的权限。该方法不支持返回 Google 群组的权限或通过 Google 群组继承的权限。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
user | User | 要检查其权限的用户的表示形式。 |
返回
Permission - 授予用户的权限。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
get Date Created()
get Description()
get Editors()
获取相应 Folder 的编辑器列表。如果执行脚本的用户没有 Folder 的编辑权限,此方法会返回一个空数组。
// Gets a folder by its ID. // TODO(developer): Replace the folder ID with your own. const folder = DriveApp.getFolderById('1234567890abcdefghijklmnopqrstuvwxyz'); // Gets the list of editors and logs their names to the console. const editors = folder.getEditors(); for (const editor of editors) { console.log(editor.getName()); }
返回
User[] - 如果用户具有编辑权限,则为相应 Folder 的编辑者列表;否则,为空数组。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
get Files()
获取当前文件夹的所有子级文件组成的集合。
返回
File - 当前文件夹的所有子文件的集合。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
get Files By Name(name)
获取当前文件夹的所有子文件(具有指定名称)。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
name | String | 要查找的文件的名称。 |
返回
File - 当前文件夹的所有子文件(具有指定名称)的集合。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
get Files By Type(mimeType)
获取当前文件夹的所有子文件(具有指定 MIME 类型)的集合。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
mime | String | 要查找的文件的 MIME 类型。 |
返回
File - 当前文件夹中具有指定 MIME 类型的所有子文件的集合。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
get Folders()
获取当前文件夹的所有子文件夹的集合。
返回
Folder - 当前文件夹的所有子文件夹的集合。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
get Folders By Name(name)
获取当前文件夹的所有子文件夹(具有指定名称)的集合。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
name | String | 要查找的文件夹的名称。 |
返回
Folder - 当前文件夹的所有子文件夹的集合,这些子文件夹具有给定的名称。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
get Id()
get Last Updated()
get Name()
get Owner()
获取相应 Folder 的所有者。
// Gets a folder by its ID. // TODO(developer): Replace the folder ID with your own. const folder = DriveApp.getFolderById('1234567890abcdefghijklmnopqrstuvwxyz'); // Gets the owner of the folder and logs the name to the console. const folderOwner = folder.getOwner(); console.log(folderOwner.getName());
返回
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
get Parents()
获取 Folder 的直接父文件夹的集合。
返回
Folder - Folder 的直接父文件夹的集合
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
get Resource Key()
get Security Update Eligible()
获取此 Folder 是否符合应用安全更新的条件。如果应用安全更新,则在通过链接分享此 Folder 时,需要使用资源密钥才能访问此 Folder。
云端硬盘需要资源密钥才能访问通过链接共享的某些文件或文件夹。此变更是安全更新的一部分。对于符合条件的文件和文件夹,此更新默认处于开启状态。如需针对符合条件的文件开启或关闭资源密钥要求,请使用 set。
详细了解 Google 云端硬盘安全更新。
返回
Boolean - 资源密钥要求是否可应用于 Folder。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
get Security Update Enabled()
获取相应 Folder 在通过链接共享时是否需要资源密钥才能访问。对于符合条件的文件和文件夹,此要求默认处于启用状态。
如需为符合条件的文件启用或停用资源密钥要求,请使用
setSecurityUpdateEnabled。
详细了解 Google 云端硬盘安全更新。
返回
Boolean - 是否为相应 Folder 启用了资源键要求。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
get Sharing Access()
get Sharing Permission()
获取已授予可访问 Folder 的用户的权限,明确授予访问权限的任何个人用户除外。
返回
Permission - 向可访问 Folder 的用户授予的权限
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
get Size()
get Url()
get Viewers()
获取相应 Folder 的查看者和评论者列表。如果执行脚本的用户没有 Folder 的编辑权限,此方法会返回一个空数组。
// Gets a folder by its ID. // TODO(developer): Replace the folder ID with your own. const folder = DriveApp.getFolderById('1234567890abcdefghijklmnopqrstuvwxyz'); // Gets the list of viewers and logs their names to the console. const viewers = folder.getViewers(); for (const viewer of viewers) { console.log(viewer.getName()); }
返回
User[] - 如果用户具有编辑权限,则为相应 Folder 的查看者和评论者列表;否则,为空数组。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
is Starred()
is Trashed()
move To(destination)
remove Editor(emailAddress)
remove Editor(user)
remove Viewer(emailAddress)
从 Folder 的观看者和评论者列表中移除指定用户。如果用户是编辑者,而不是查看者或评论者,此方法不会产生任何影响。此方法也不会阻止属于具有常规访问权限的用户类别的用户访问 Folder,例如,如果 Folder 与用户的整个网域共享,或者如果 Folder 位于用户可以访问的共享云端硬盘中,则用户可以访问 Folder。
对于云端硬盘文件,此操作还会从编辑者列表中移除相应用户。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
email | String | 要移除的用户的电子邮件地址。 |
返回
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/drive
remove Viewer(user)
revoke Permissions(emailAddress)
revoke Permissions(user)
search Files(params)
获取当前文件夹的所有子文件(这些文件符合给定的搜索条件)。如需详细了解搜索条件,请参阅 Google 云端硬盘 SDK 文档。请注意,Drive 服务使用的是 Drive API v2,并且某些查询字段与 v3 不同。查看 v2 与 v3 之间的字段差异。
params 实参是一个可以包含字符串值的查询字符串,因此请注意正确转义引号(例如 "title contains 'Gulliver\\'s
Travels'" 或 'title contains "Gulliver\'s Travels"')。
// Logs the name of every file that are children of the current folder and modified after February 28, // 2022 whose name contains "untitled."" const files = DriveApp.getRootFolder().searchFiles( 'modifiedDate > "2022-02-28" and title contains "untitled"'); while (files.hasNext()) { const file = files.next(); console.log(file.getName()); }
参数
| 名称 | 类型 | 说明 |
|---|---|---|
params | String | 搜索条件,如 Google 云端硬盘 SDK 文档中所述。 |
返回
File - 当前文件夹中符合搜索条件的所有文件的集合。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
search Folders(params)
获取当前文件夹的所有子文件夹的集合,这些子文件夹符合给定的搜索条件。如需详细了解搜索条件,请参阅 Google 云端硬盘 SDK 文档。请注意,Drive 服务使用的是 Drive API v2,并且某些查询字段与 v3 不同。查看 v2 与 v3 之间的字段差异。
params 实参是一个可以包含字符串值的查询字符串,因此请注意正确转义引号(例如 "title contains 'Gulliver\\'s
Travels'" 或 'title contains "Gulliver\'s Travels"')。
// Logs the name of every folder that are children of the current folder and you own and is starred. const folders = DriveApp.getRootFolder().searchFolders('starred = true and "me" in owners'); while (folders.hasNext()) { const folder = folders.next(); console.log(folder.getName()); }
参数
| 名称 | 类型 | 说明 |
|---|---|---|
params | String | 搜索条件,如 Google 云端硬盘 SDK 文档中所述。 |
返回
Folder - 当前文件夹的所有子文件夹的集合,这些子文件夹符合搜索条件。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
set Description(description)
set Name(name)
set Owner(emailAddress)
set Owner(user)
set Security Update Enabled(enabled)
set Sharing(accessType, permissionType)
设置哪些类别的用户可以访问 Folder,以及除了已明确获得访问权限的任何个人用户之外,这些用户还被授予了哪些权限。
// Creates a folder that anyone on the Internet can read from and write to. // (Domain administrators can prohibit this setting for users of a Google // Workspace domain.) const folder = DriveApp.createFolder('Shared Folder'); folder.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.EDIT);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
access | Access | 哪些类别的用户应该能够访问 Folder |
permission | Permission | 应向可访问 Folder 的用户授予的权限 |
返回
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/drive