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
是否符合条件,以应用安全更新(在使用链接共享时,需要使用资源密钥才能访问)。
云端硬盘需要资源密钥才能访问使用链接共享的部分文件或文件夹。此变更是安全更新的一部分。系统会默认为符合条件的文件和文件夹开启更新。如需为符合条件的文件开启或关闭资源键要求,请使用 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
的修改权限,则为此 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
位于用户可以访问的共享云端硬盘中。
对于云端硬盘文件,这还会将用户从编辑者列表中移除。
参数
名称 | 类型 | 说明 |
---|---|---|
email | String | 要移除的用户的电子邮件地址。 |
返回
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/drive
remove Viewer(user)
revoke Permissions(emailAddress)
revoke Permissions(user)
search Files(params)
获取当前文件夹的子文件夹且符合给定搜索条件的所有文件的集合。如需详细了解搜索条件,请参阅 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 Folders(params)
获取当前文件夹的子文件夹中所有符合给定搜索条件的文件夹的集合。如需详细了解搜索条件,请参阅 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 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