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
add
add
add
add
add
create
根据任意数据的给定 Blob 在当前文件夹中创建文件。
参数
| 名称 | 类型 | 说明 | 
|---|---|---|
| blob | Blob | 新文件的数据。 | 
返回
File - 新文件。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
- 
https://www.googleapis.com/auth/drive
create
在当前文件夹中创建一个文本文件,并为其指定给定名称和内容。如果 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
在当前文件夹中创建一个文件,并为其指定名称、内容和 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
create
create
创建指向所提供的云端硬盘内容 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
获取向特定用户授予的权限。该方法不支持返回 Google 群组的权限或通过 Google 群组继承的权限。
参数
| 名称 | 类型 | 说明 | 
|---|---|---|
| email | String | 应检查权限的用户的电子邮件地址。不支持 Google 群组。 | 
返回
Permission - 向用户授予的权限。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
- 
https://www.googleapis.com/auth/drive.readonly
- 
https://www.googleapis.com/auth/drive
get
获取向特定用户授予的权限。该方法不支持返回 Google 群组的权限或通过 Google 群组继承的权限。
参数
| 名称 | 类型 | 说明 | 
|---|---|---|
| user | User | 应检查权限的用户的表示法。 | 
返回
Permission - 向用户授予的权限。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
- 
https://www.googleapis.com/auth/drive.readonly
- 
https://www.googleapis.com/auth/drive
get
get
get
获取此 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
获取当前文件夹的所有子文件的集合。
返回
File - 当前文件夹的所有子文件的集合。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
- 
https://www.googleapis.com/auth/drive.readonly
- 
https://www.googleapis.com/auth/drive
get
获取当前文件夹的子文件夹中所有具有给定名称的文件的集合。
参数
| 名称 | 类型 | 说明 | 
|---|---|---|
| name | String | 要查找的文件的名称。 | 
返回
File - 当前文件夹的子文件夹且具有给定名称的所有文件的集合。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
- 
https://www.googleapis.com/auth/drive.readonly
- 
https://www.googleapis.com/auth/drive
get
获取当前文件夹的子文件夹且具有给定 MIME 类型的所有文件的集合。
参数
| 名称 | 类型 | 说明 | 
|---|---|---|
| mime | String | 要查找的文件的 MIME 类型。 | 
返回
File - 当前文件夹的子文件夹且具有给定 MIME 类型的所有文件的集合。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
- 
https://www.googleapis.com/auth/drive.readonly
- 
https://www.googleapis.com/auth/drive
get
获取当前文件夹的所有子文件夹的集合。
返回
Folder - 当前文件夹的所有子文件夹的集合。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
- 
https://www.googleapis.com/auth/drive.readonly
- 
https://www.googleapis.com/auth/drive
get
获取当前文件夹的子文件夹中具有给定名称的所有文件夹的集合。
参数
| 名称 | 类型 | 说明 | 
|---|---|---|
| name | String | 要查找的文件夹的名称。 | 
返回
Folder - 当前文件夹的子文件夹且具有给定名称的所有文件夹的集合。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
- 
https://www.googleapis.com/auth/drive.readonly
- 
https://www.googleapis.com/auth/drive
get
get
get
get
获取此 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
获取 Folder 的直接父级文件夹的集合。
返回
Folder - Folder 的直接父级文件夹的集合
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
- 
https://www.googleapis.com/auth/drive.readonly
- 
https://www.googleapis.com/auth/drive
get
get
获取此 Folder 是否符合条件,以应用安全更新(在使用链接共享时,需要使用资源密钥才能访问)。
云端硬盘需要资源密钥才能访问使用链接共享的部分文件或文件夹。此变更是安全更新的一部分。系统会默认为符合条件的文件和文件夹开启更新。如需为符合条件的文件开启或关闭资源键要求,请使用 set。
详细了解 Google 云端硬盘安全更新。
返回
Boolean - 是否可以对 Folder 应用资源键要求。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
- 
https://www.googleapis.com/auth/drive.readonly
- 
https://www.googleapis.com/auth/drive
get
获取此 Folder 在使用链接共享时是否需要资源键才能访问。默认情况下,系统会为符合条件的文件和文件夹启用此要求。如需为符合条件的文件开启或关闭资源密钥要求,请使用 
setSecurityUpdateEnabled。
详细了解 Google 云端硬盘安全更新。
返回
Boolean - 是否为此 Folder 启用了资源键要求。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
- 
https://www.googleapis.com/auth/drive.readonly
- 
https://www.googleapis.com/auth/drive
get
get
获取授予可以访问 Folder 的用户(除了已明确授予访问权限的任何个人用户)的权限。
返回
Permission - 向可以访问 Folder 的用户授予的权限
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
- 
https://www.googleapis.com/auth/drive.readonly
- 
https://www.googleapis.com/auth/drive
get
get
get
获取此 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 的修改权限,则为此 Folder 的查看者和评论者的列表;否则,为空数组。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
- 
https://www.googleapis.com/auth/drive.readonly
- 
https://www.googleapis.com/auth/drive
is
is
move
remove
remove
remove
remove
revoke
revoke
search
获取当前文件夹的子文件夹且符合给定搜索条件的所有文件的集合。如需详细了解搜索条件,请参阅 Google 云端硬盘 SDK 文档。请注意,云端硬盘服务使用的是 v2 版云端硬盘 API,并且某些查询字段与 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
获取当前文件夹的子文件夹中所有符合给定搜索条件的文件夹的集合。如需详细了解搜索条件,请参阅 Google 云端硬盘 SDK 文档。请注意,云端硬盘服务使用的是 v2 版云端硬盘 API,并且某些查询字段与 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
set
set
set
set
set
除了已明确授予访问权限的任何个人用户之外,还设置哪类用户可以访问 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