Google 云端硬盘中的一个文件夹。可以从 DriveApp
访问或创建文件夹。
// Log the name of every folder in the user's Drive. var folders = DriveApp.getFolders(); while (folders.hasNext()) { var folder = folders.next(); Logger.log(folder.getName()); }
方法
详细文档
addEditor(emailAddress)
addEditor(user)
addEditors(emailAddresses)
addViewer(emailAddress)
addViewer(user)
addViewers(emailAddresses)
createFile(blob)
根据任意数据的给定 Blob
在当前文件夹中创建文件。
// Create an image file in Google Drive using the Maps service. var blob = Maps.newStaticMap().setCenter('76 9th Avenue, New York NY').getBlob(); DriveApp.getRootFolder().createFile(blob);
参数
名称 | 类型 | 说明 |
---|---|---|
blob | BlobSource | 新文件的数据。 |
返程
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 | 新文件的内容。 |
mimeType | 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()); }
参数
名称 | 类型 | 说明 |
---|---|---|
targetId | String | 目标文件或文件夹的 ID。 |
targetResourceKey | String | 目标文件或文件夹的资源键。 |
返程
File
- 新的快捷方式。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/drive
getAccess(email)
获取向指定用户授予的权限。
参数
名称 | 类型 | 说明 |
---|---|---|
email | String | 应检查其权限的用户的电子邮件地址 |
返程
Permission
- 授予用户的权限
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
getAccess(user)
获取向指定用户授予的权限。
参数
名称 | 类型 | 说明 |
---|---|---|
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 (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 类型的所有文件的集合。
参数
名称 | 类型 | 说明 |
---|---|---|
mimeType | 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
是否有资格应用通过资源共享时需要访问资源密钥的安全更新。
云端硬盘需要资源密钥才能访问使用链接共享的某些文件或文件夹。此更改属于安全更新的一部分。默认情况下,符合条件的文件和文件夹会启用此更新。如需为符合条件的文件启用或停用资源密钥要求,请使用 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 (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 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."" var files = DriveApp.getRootFolder().searchFiles( 'modifiedDate > "2022-02-28" and title contains "untitled"'); while (files.hasNext()) { var 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 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. var folders = DriveApp.getRootFolder().searchFolders('starred = true and "me" in owners'); while (folders.hasNext()) { var 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.) var folder = DriveApp.createFolder('Shared Folder'); folder.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.EDIT);
参数
名称 | 类型 | 说明 |
---|---|---|
accessType | Access | 哪种类别的用户可以访问Folder |
permissionType | Permission | 应授予可以访问Folder 的用户的权限 |
返程
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/drive