Google 云端硬盘中的文件。可以通过 DriveApp
访问或创建文件。
// Trash every untitled spreadsheet that hasn't been updated in a week. var files = DriveApp.getFilesByName('Untitled spreadsheet'); while (files.hasNext()) { var file = files.next(); if (new Date() - file.getLastUpdated() > 7 * 24 * 60 * 60 * 1000) { file.setTrashed(true); } }
方法
详细文档
addCommenter(emailAddress)
将指定用户添加到 File
的评论者列表中。如果用户已显示在查看者列表中,则此方法会将该用户从查看者列表中升级。
// Gets a list of all files in Google Drive with the given name. // TODO(developer): Replace 'cloudysanfrancisco@gmail.com' with the email address that you // want to add as a commenter. while (files.hasNext()) { const file = files.next(); email = 'cloudysanfrancisco@gmail.com'; console.log(file.addCommenter(email)); }
参数
名称 | 类型 | 说明 |
---|---|---|
emailAddress | String | 要添加的用户的电子邮件地址。 |
返程
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/drive
addCommenter(user)
将指定用户添加到 File
的评论者列表中。如果用户已显示在查看者列表中,则此方法会将该用户从查看者列表中升级。
// Gets a list of all files in Google Drive with the given name. // TODO(developer): Replace the file name with your own. const files = DriveApp.getFilesByName('Test'); // Adds the active user as a commenter. while (files.hasNext()) { const file = files.next(); file.addCommenter(Session.getActiveUser()); }
参数
名称 | 类型 | 说明 |
---|---|---|
user | User | 要添加的用户。 |
返程
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/drive
addCommenters(emailAddresses)
将给定用户数组添加到 File
的评论者列表。如果其中有任何用户已经在查看者列表中,此方法会将他们从查看者列表中升级。
// Gets a list of all files in Google Drive with the given name. // TODO(developer): Replace the file name with your own. const files = DriveApp.getFilesByName('Test'); while (files.hasNext()) { const file = files.next(); // TODO(developer): Replace 'cloudysanfrancisco@gmail.com' and // 'baklavainthebalkans@gmail.com' with the email addresses to add as commenters. const emails = ['cloudysanfrancisco@gmail.com','baklavainthebalkans@gmail.com']; console.log(file.addCommenters(emails)); }
参数
名称 | 类型 | 说明 |
---|---|---|
emailAddresses | String[] | 要添加的用户的电子邮件地址数组。 |
返程
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/drive
addEditor(emailAddress)
addEditor(user)
addEditors(emailAddresses)
addViewer(emailAddress)
addViewer(user)
addViewers(emailAddresses)
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
getAs(contentType)
将此对象中的数据作为 blob 转换为指定内容类型返回。此方法会在文件名中添加适当的扩展名,例如“myfile.pdf”。不过,前提是文件名中最后一个句点后面的部分(如果有)是应替换的现有扩展名。因此,“ShoppingList.12.25.2014”将变为“ShoppingList.12.25.pdf”。
如需查看转化的每日配额,请参阅 Google 服务的配额。新创建的 Google Workspace 网域可能暂时受到更严格的配额约束。
参数
名称 | 类型 | 说明 |
---|---|---|
contentType | String | 要转换为的 MIME 类型。对于大多数 blob,'application/pdf' 是唯一的有效选项。对于 BMP、GIF、JPEG 或 PNG 格式的图片,'image/bmp' 、'image/gif' 、'image/jpeg' 或 'image/png' 中的任意一项也有效。 |
返程
Blob
- 数据作为 blob。
getBlob()
getDateCreated()
getDescription()
getDownloadUrl()
获取可用于下载文件的网址。只有有权在 Google 云端硬盘中打开文件的用户才能访问该网址。您可以在浏览器中使用此网址下载文件,但无法用于通过 UrlFetchApp
提取文件。如果您希望脚本中包含文件内容,请使用 getBlob()
。
// Gets a list of all files in Google Drive with the given name. // TODO(developer): Replace the file name with your own. const files = DriveApp.getFilesByName('Test'); // Loops through the files and logs the download URLs to the console. while (files.hasNext()) { const file = files.next(); console.log(file.getDownloadUrl()); }
返程
String
- 可用于下载文件的网址。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
getEditors()
获取此 File
的编辑器列表。如果执行脚本的用户没有 File
的编辑权限,此方法会返回一个空数组。
// Gets a list of all files in Google Drive with the given name. // TODO(developer): Replace the file name with your own. const files = DriveApp.getFilesByName('Test'); // Loops through the files. while (files.hasNext()) { const file = files.next(); // Adds the email addresses in the array as editors of each file. // TODO(developer): Replace 'cloudysanfrancisco@gmail.com' // and 'baklavainthebalkans@gmail.com' with valid email addresses. file.addEditors(['cloudysanfrancisco@gmail.com', 'baklavainthebalkans@gmail.com']); // Gets a list of the file editors. const editors = file.getEditors(); // For each file, logs the editors' email addresses to the console. for (const editor of editors) { console.log(editor.getEmail()); } }
返程
User[]
- 如果用户对此 File
拥有编辑权限,则返回编辑器列表。如果用户没有编辑权限,则返回空数组。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
getId()
getLastUpdated()
getMimeType()
获取文件的 MIME 类型。
// Gets a list of all files in Google Drive with the given name. // TODO(developer): Replace the file name with your own. const files = DriveApp.getFilesByName('Test'); // Loops through the files and logs the MIME type to the console. while (files.hasNext()) { const file = files.next(); console.log(file.getMimeType()); }
返程
String
- 文件的 MIME 类型。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
getName()
getOwner()
获取文件所有者。
// Gets a list of all files in Google Drive with the given name. // TODO(developer): Replace the file name with your own. const files = DriveApp.getFilesByName('Test'); // Loops through the files and logs the names of the file owners to the console. while (files.hasNext()) { const file = files.next(); console.log(file.getOwner().getName()); }
返程
User
- 文件所有者。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
getParents()
获取文件夹的集合,这些文件夹是 File
的直接父级。
返程
FolderIterator
- 文件夹的集合,这些文件夹是 File
的直接父级
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
getResourceKey()
getSecurityUpdateEligible()
获取此 File
是否有资格应用通过资源共享时需要访问资源密钥的安全更新。
云端硬盘需要资源密钥才能访问使用链接共享的某些文件或文件夹。此更改属于安全更新的一部分。默认情况下,符合条件的文件和文件夹会启用此更新。如需为符合条件的文件启用或停用资源密钥要求,请使用 setSecurityUpdateEnabled
。
详细了解 Google 云端硬盘的安全更新。
返程
Boolean
- 表示是否可以为 File
应用资源键要求。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
getSecurityUpdateEnabled()
获取此 File
在使用链接进行共享时是否需要资源键进行访问。默认情况下,系统会为符合条件的文件和文件夹启用此要求。如需为符合条件的文件启用或停用资源密钥要求,请使用
setSecurityUpdateEnabled
。
详细了解 Google 云端硬盘的安全更新。
返程
Boolean
- 表示是否为此 File
启用了资源键要求。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
getSharingAccess()
getSharingPermission()
获取向可以访问 File
的用户以及明确获得访问权限的各位用户的权限。
返程
Permission
- 授予可以访问 File
的用户的权限
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
getSize()
getTargetId()
如果是快捷方式,则返回它所指向的项的 ID。
否则,它会返回 null
。
// The ID of the file for which to make a shortcut and the ID of // the folder to which you want to add the shortcut. // TODO(developer): Replace the file and folder IDs with your IDs. const fileId = 'abc123456'; const folderId = 'xyz987654'; // Gets the folder to add the shortcut to. const folder = DriveApp.getFolderById(folderId); // Creates a shortcut of the file and moves it to the specified folder. const shortcut = DriveApp.createShortcut(fileId).moveTo(folder); // Logs the target ID of the shortcut. console.log(`${shortcut.getName()}=${shortcut.getTargetId()}`);
返程
String
- 目标项 ID。
getTargetMimeType()
如果是快捷方式,则返回它所指向的项的 MIME 类型。
否则,它会返回 null
。
// The ID of the file for which to make a shortcut and the ID of // the folder to which you want to add the shortcut. // TODO(developer): Replace the file and folder IDs with your IDs. const fileId = 'abc123456'; const folderId = 'xyz987654'; // Gets the folder to add the shortcut to. const folder = DriveApp.getFolderById(folderId); // Creates a shortcut of the file and moves it to the specified folder. const shortcut = DriveApp.createShortcut(fileId).moveTo(folder); // Logs the MIME type of the file that the shortcut points to. console.log(`MIME type of the shortcut: ${shortcut.getTargetMimeType()}`);
返程
String
- 目标内容的 MIME 类型。
getTargetResourceKey()
如果文件是快捷方式,则返回其所指向项的资源键。资源键是需要传递的附加参数,用于访问通过链接共享的文件。
如果文件不是快捷方式,它将返回 null
。
// Gets a file by its ID. // TODO(developer): Replace 'abc123456' with your file ID. const file = DriveApp.getFileById('abc123456'); // If the file is a shortcut, returns the resource key of the file that it points to. console.log(file.getTargetResourceKey());
返程
String
- 目标项的资源键;如果文件不是快捷方式,则为 null
。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
getThumbnail()
获取该文件的缩略图,如果不存在缩略图,则返回 null
。
// Gets a list of all files in Google Drive with the given name. // TODO(developer): Replace the file name with your own. const files = DriveApp.getFilesByName('Test'); // Loops through the files. while (files.hasNext()) { const file = files.next(); // Logs the thumbnail image for each file to the console as a blob, // or null if no thumbnail exists. console.log(file.getThumbnail()); }
返程
Blob
- 文件的缩略图。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
getUrl()
getViewers()
获取此 File
的观看者和评论者列表。如果执行脚本的用户没有 File
的修改权限,此方法会返回一个空数组。
// Gets a list of all files in Google Drive with the given name. // TODO(developer): Replace the file name with your own. const files = DriveApp.getFilesByName('Test'); // Loops through the files. while (files.hasNext()) { const file = files.next(); // For each file, logs the viewers' email addresses to the console. const viewers = file.getViewers(); for (viewer of viewers) { console.log(viewer.getEmail()); } }
返程
User[]
- 如果用户对此 File
拥有编辑权限,则返回查看者和评论者列表。如果用户没有编辑权限,则返回空数组。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
isStarred()
isTrashed()
makeCopy()
创建文件的副本。
// Gets a list of all files in Google Drive with the given name. // TODO(developer): Replace the file name with your own. const files = DriveApp.getFilesByName('Test'); // Loops through the files. while (files.hasNext()) { const file = files.next(); // Creates a copy of each file and logs the file name to the console. console.log(file.makeCopy().getName()); }
返程
File
- 新的副本。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/drive
makeCopy(destination)
在目标目录中创建文件的副本。
// Gets a list of all files in Google Drive with the given name. // TODO(developer): Replace the file name with your own. const files = DriveApp.getFilesByName('Test'); // Loops through the files. while (files.hasNext()) { const file = files.next(); // Creates a copy of each file and adds it to the specified folder. // TODO(developer): Replace the folder ID with your own. const destination = DriveApp.getFolderById('123456abcxyz'); const copiedFile = file.makeCopy(destination); // Logs the file names to the console. console.log(copiedFile.getName()); }
参数
名称 | 类型 | 说明 |
---|---|---|
destination | Folder | 可将文件复制到的目录。 |
返程
File
- 新的副本。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/drive
makeCopy(name)
创建文件的副本,并使用提供的名称为其命名。
// Gets a list of all files in Google Drive with the given name. // TODO(developer): Replace the file name with your own. const files = DriveApp.getFilesByName('Test'); // Loops through the files. while (files.hasNext()) { const file = files.next(); // Creates a copy of each file and sets the name to 'Test-Copy.' const filename = file.makeCopy('Test-Copy'); // Logs the copied file's name to the console. console.log(filename.getName()); }
参数
名称 | 类型 | 说明 |
---|---|---|
name | String | 应用于新副本的文件名。 |
返程
File
- 新的副本。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/drive
makeCopy(name, destination)
在目标目录中创建文件的副本,并使用提供的名称为其命名。
// Gets a list of all files in Google Drive with the given name. // TODO(developer): Replace the file name with your own. const files = DriveApp.getFilesByName('Test'); // Loops through the files. while (files.hasNext()) { const file = files.next(); // Creates a copy of each file, sets the file name, and adds the copied file // to the specified folder. // TODO(developer): Replace the folder ID with your own. const destination = DriveApp.getFolderById('123456abcxyz'); const copiedFile = file.makeCopy('Test-Copy', destination); // Logs the file names to the console. console.log(copiedFile.getName()); }
参数
名称 | 类型 | 说明 |
---|---|---|
name | String | 应用于新副本的文件名。 |
destination | Folder | 可将文件复制到的目录。 |
返程
File
- 新的副本。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/drive
moveTo(destination)
removeCommenter(emailAddress)
从 File
的评论者列表中移除指定用户。如果用户属于拥有常规访问权限的某一类用户,例如用户与整个网域共享 File
,则此方法不会阻止用户访问 File
。
// Gets a list of all files in Google Drive with the given name. // TODO(developer): Replace the file name with your own. const files = DriveApp.getFilesByName('Test'); // Loops through the files. while (files.hasNext()) { const file = files.next(); // Removes the given user from the list of commenters for each file. // TODO(developer): Replace the email with the email of the user you want to remove. file.removeCommenter('cloudysanfrancisco@gmail.com'); }
参数
名称 | 类型 | 说明 |
---|---|---|
emailAddress | String | 要移除的用户的电子邮件地址。 |
返程
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/drive
removeCommenter(user)
从 File
的评论者列表中移除指定用户。如果用户属于拥有常规访问权限的某一类用户,例如用户与整个网域共享 File
,则此方法不会阻止用户访问 File
。
// Gets a list of all files in Google Drive with the given name. // TODO(developer): Replace the file name with your own. const files = DriveApp.getFilesByName('Test'); // Loops through the files. while (files.hasNext()) { const file = files.next(); // Removes the given user from the list of commenters for each file. console.log(file.removeCommenter(Session.getActiveUser())); }
参数
名称 | 类型 | 说明 |
---|---|---|
user | User | 表示要移除的用户。 |
返程
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/drive
removeEditor(emailAddress)
removeEditor(user)
removeViewer(emailAddress)
removeViewer(user)
revokePermissions(emailAddress)
revokePermissions(user)
setContent(content)
使用给定替换项覆盖文件的内容。如果 content
大于 10MB,则会抛出异常。
// Creates a text file with the content 'Hello, world!' file = DriveApp.createFile('New Text File', 'Hello, world!'); // Logs the content of the text file to the console. console.log(file.getBlob().getDataAsString()); // Updates the content of the text file to 'Updated text!' file.setContent('Updated text!') // Logs content of the text file to the console. console.log(file.getBlob().getDataAsString());
参数
名称 | 类型 | 说明 |
---|---|---|
content | String | 文件的新内容。 |
返程
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/drive
setDescription(description)
setName(name)
setOwner(emailAddress)
setOwner(user)
setSecurityUpdateEnabled(enabled)
setSharing(accessType, permissionType)
设置哪类用户可以访问 File
以及向哪些用户授予了明确的访问权限。
// 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 | 哪种类别的用户可以访问File |
permissionType | Permission | 应授予可以访问File 的用户的权限 |
返程
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/drive