Google 云端硬盘中的文件夹。您可以通过 DriveApp 访问或创建文件夹。
// 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()); }
方法
详细文档
addEditor(emailAddress)
addEditor(user)
addEditors(emailAddresses)
addViewer(emailAddress)
addViewer(user)
addViewers(emailAddresses)
createFile(blob)
根据给定的任意数据 Blob 在当前文件夹中创建一个文件。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
blob | Blob | 新文件的数据。 |
返回
File - 新文件。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/drive
createFile(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
createFile(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
createFolder(name)
createShortcut(targetId)
createShortcutForTargetIdAndResourceKey(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
getAccess(email)
获取授予特定用户的权限。该方法不支持返回 Google 群组的权限或通过 Google 群组继承的权限。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
email | String | 应检查其权限的用户的电子邮件地址。不支持 Google 群组。 |
返回
Permission - 授予用户的权限。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
getAccess(user)
获取授予特定用户的权限。该方法不支持返回 Google 群组的权限或通过 Google 群组继承的权限。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
user | User | 要检查其权限的用户的表示形式。 |
返回
Permission - 授予用户的权限。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
getDateCreated()
getDescription()
getEditors()
获取相应 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
getFiles()
获取当前文件夹的所有子级文件组成的集合。
返回
FileIterator - 当前文件夹的所有子文件的集合。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
getFilesByName(name)
获取当前文件夹的所有子文件(具有指定名称)。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
name | String | 要查找的文件的名称。 |
返回
FileIterator - 当前文件夹的所有子文件(具有指定名称)的集合。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
getFilesByType(mimeType)
获取当前文件夹的所有子文件(具有指定 MIME 类型)的集合。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
mime | String | 要查找的文件的 MIME 类型。 |
返回
FileIterator - 当前文件夹中具有指定 MIME 类型的所有子文件的集合。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
getFolders()
获取当前文件夹的所有子文件夹的集合。
返回
FolderIterator - 当前文件夹的所有子文件夹的集合。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
getFoldersByName(name)
获取当前文件夹的所有子文件夹(具有指定名称)的集合。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
name | String | 要查找的文件夹的名称。 |
返回
FolderIterator - 当前文件夹的所有子文件夹的集合,这些子文件夹具有给定的名称。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
getId()
getLastUpdated()
getName()
getOwner()
获取相应 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
getParents()
获取 Folder 的直接父文件夹的集合。
返回
FolderIterator - Folder 的直接父文件夹的集合
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
getResourceKey()
getSecurityUpdateEligible()
获取此 Folder 是否符合应用安全更新的条件。如果应用安全更新,则在通过链接分享此 Folder 时,需要使用资源密钥才能访问此 Folder。
云端硬盘需要资源密钥才能访问通过链接共享的某些文件或文件夹。此变更是安全更新的一部分。对于符合条件的文件和文件夹,系统默认会开启此更新。如需为符合条件的文件开启或关闭资源密钥要求,请使用 setSecurityUpdateEnabled。
详细了解 Google 云端硬盘安全更新。
返回
Boolean - 资源密钥要求是否可应用于 Folder。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
getSecurityUpdateEnabled()
获取相应 Folder 在通过链接共享时是否需要资源密钥才能访问。对于符合条件的文件和文件夹,此要求默认处于启用状态。
如需为符合条件的文件启用或停用资源密钥要求,请使用
setSecurityUpdateEnabled。
详细了解 Google 云端硬盘安全更新。
返回
Boolean - 是否为相应 Folder 启用了资源键要求。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
getSharingAccess()
getSharingPermission()
获取已授予可访问 Folder 的用户的权限,明确授予访问权限的任何个人用户除外。
返回
Permission - 授予可访问 Folder 的用户的权限
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
getSize()
getUrl()
getViewers()
获取相应 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
isStarred()
isTrashed()
moveTo(destination)
removeEditor(emailAddress)
removeEditor(user)
removeViewer(emailAddress)
removeViewer(user)
revokePermissions(emailAddress)
revokePermissions(user)
searchFiles(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 文档中所述。 |
返回
FileIterator - 当前文件夹中符合搜索条件的所有文件的集合。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
searchFolders(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 文档中所述。 |
返回
FolderIterator - 当前文件夹的所有子文件夹的集合,这些子文件夹符合搜索条件。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
setDescription(description)
setName(name)
setOwner(emailAddress)
setOwner(user)
setSecurityUpdateEnabled(enabled)
setSharing(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