Class File

文件

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将指定用户添加到 File 的评论者列表中。
addCommenter(user)File将指定用户添加到 File 的评论者列表中。
addCommenters(emailAddresses)File将给定用户数组添加到 File 的评论者列表。
addEditor(emailAddress)File将指定用户添加到 File 的编辑者列表中。
addEditor(user)File将指定用户添加到 File 的编辑者列表中。
addEditors(emailAddresses)File将给定用户数组添加到 File 的编辑器列表中。
addViewer(emailAddress)File将指定用户添加到 File 的查看者列表中。
addViewer(user)File将指定用户添加到 File 的查看者列表中。
addViewers(emailAddresses)File将给定用户数组添加到 File 的查看者列表中。
getAccess(email)Permission获取向指定用户授予的权限。
getAccess(user)Permission获取向指定用户授予的权限。
getAs(contentType)Blob将此对象中的数据作为 blob 转换为指定内容类型返回。
getBlob()Blob以 blob 形式返回此对象中的数据。
getDateCreated()Date获取 File 的创建日期。
getDescription()String获取 File 的说明。
getDownloadUrl()String获取可用于下载文件的网址。
getEditors()User[]获取此 File 的编辑器列表。
getId()String获取 File 的 ID。
getLastUpdated()Date获取 File 的上次更新日期。
getMimeType()String获取文件的 MIME 类型。
getName()String获取 File 的名称。
getOwner()User获取文件所有者。
getParents()FolderIterator获取文件夹的集合,这些文件夹是 File 的直接父级。
getResourceKey()String获取访问通过链接共享的内容所需的 File 资源键。
getSecurityUpdateEligible()Boolean获取此 File 是否有资格应用通过资源共享时需要资源密钥进行访问的安全更新。
getSecurityUpdateEnabled()Boolean获取此 File 在使用链接进行共享时是否需要资源键进行访问。
getSharingAccess()Access获取哪类用户可以访问 File 以及任何明确获得访问权限的用户除外。
getSharingPermission()Permission获取向可以访问 File 的用户以及明确获得访问权限的各位用户的权限。
getSize()Integer获取用于在云端硬盘中存储 File 的字节数。
getTargetId()String如果它是快捷方式,则返回它所指向的项的 ID。
getTargetMimeType()String如果这是一个快捷方式,则返回它所指向的项的 MIME 类型。
getTargetResourceKey()String如果文件是快捷方式,则返回其所指向项的资源键。
getThumbnail()Blob获取该文件的缩略图,如果不存在缩略图,则返回 null
getUrl()String获取可用于在云端硬盘或文档等 Google 应用中打开 File 的网址。
getViewers()User[]获取此 File 的观看者和评论者列表。
isShareableByEditors()Boolean确定拥有 File 修改权限的用户是否允许与其他用户共享或更改权限。
isStarred()Boolean确定 File 是否在用户的云端硬盘中已加星标。
isTrashed()Boolean确定 File 是否在用户的云端硬盘回收站中。
makeCopy()File创建文件的副本。
makeCopy(destination)File在目标目录中创建文件的副本。
makeCopy(name)File创建文件的副本,并使用提供的名称为其命名。
makeCopy(name, destination)File在目标目录中创建文件的副本,并使用提供的名称为其命名。
moveTo(destination)File将此内容移至所提供的目标文件夹。
removeCommenter(emailAddress)FileFile 的评论者列表中移除指定用户。
removeCommenter(user)FileFile 的评论者列表中移除指定用户。
removeEditor(emailAddress)FileFile 的编辑者列表中移除指定用户。
removeEditor(user)FileFile 的编辑者列表中移除指定用户。
removeViewer(emailAddress)FileFile 的查看者和评论者列表中移除指定用户。
removeViewer(user)FileFile 的查看者和评论者列表中移除指定用户。
revokePermissions(emailAddress)File撤消已授予指定用户的 File 的访问权限。
revokePermissions(user)File撤消已授予指定用户的 File 的访问权限。
setContent(content)File使用给定替换项覆盖文件的内容。
setDescription(description)File设置 File 的说明。
setName(name)File设置 File 的名称。
setOwner(emailAddress)File更改 File 的所有者。
setOwner(user)File更改 File 的所有者。
setSecurityUpdateEnabled(enabled)File设置 File 使用链接共享时是否需要资源键进行访问。
setShareableByEditors(shareable)File设置是否允许具有 File 修改权限的用户与其他用户共享或更改权限。
setSharing(accessType, permissionType)File设置具体有哪些用户可以访问 File 以及除了授予明确的用户访问权限之外,用户还会获得哪些权限。
setStarred(starred)File设置是否在用户的云端硬盘中为 File 加星标。
setTrashed(trashed)File设置 File 是否在用户的云端硬盘回收站中。

详细文档

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));
}

参数

名称类型说明
emailAddressString要添加的用户的电子邮件地址。

返程

File - 此 File,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • 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());
}

参数

名称类型说明
userUser要添加的用户。

返程

File - 此 File,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • 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));
}

参数

名称类型说明
emailAddressesString[]要添加的用户的电子邮件地址数组。

返程

File - 此 File,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive

addEditor(emailAddress)

将指定用户添加到 File 的编辑者列表中。如果用户已显示在查看者列表中,则此方法会将该用户从查看者列表中移除。

参数

名称类型说明
emailAddressString要添加的用户的电子邮件地址。

返程

File - 此 File,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive

addEditor(user)

将指定用户添加到 File 的编辑者列表中。如果用户已显示在查看者列表中,则此方法会将该用户从查看者列表中移除。

参数

名称类型说明
userUser要添加的用户。

返程

File - 此 File,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive

addEditors(emailAddresses)

将给定用户数组添加到 File 的编辑器列表中。如果任何用户已经在查看者列表中,此方法会将他们从查看者列表中升级。

参数

名称类型说明
emailAddressesString[]要添加的用户的电子邮件地址数组。

返程

File - 此 File,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive

addViewer(emailAddress)

将指定用户添加到 File 的查看者列表中。如果用户已在编辑器列表中,则此方法不会产生任何影响。

参数

名称类型说明
emailAddressString要添加的用户的电子邮件地址。

返程

File - 此 File,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive

addViewer(user)

将指定用户添加到 File 的查看者列表中。如果用户已在编辑器列表中,则此方法不会产生任何影响。

参数

名称类型说明
userUser要添加的用户。

返程

File - 此 File,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive

addViewers(emailAddresses)

将给定用户数组添加到 File 的查看者列表中。如果任何用户已经在编辑器列表中,则此方法对他们没有影响。

参数

名称类型说明
emailAddressesString[]要添加的用户的电子邮件地址数组。

返程

File - 此 File,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive

getAccess(email)

获取向指定用户授予的权限。

参数

名称类型说明
emailString应检查其权限的用户的电子邮件地址

返程

Permission - 授予用户的权限

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getAccess(user)

获取向指定用户授予的权限。

参数

名称类型说明
userUser表示应检查其权限的用户

返程

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 网域可能暂时受到更严格的配额约束。

参数

名称类型说明
contentTypeString要转换为的 MIME 类型。对于大多数 blob,'application/pdf' 是唯一的有效选项。对于 BMP、GIF、JPEG 或 PNG 格式的图片,'image/bmp''image/gif''image/jpeg''image/png' 中的任意一项也有效。

返程

Blob - 数据作为 blob。


getBlob()

以 blob 形式返回此对象中的数据。

返程

Blob - 数据作为 blob。


getDateCreated()

获取 File 的创建日期。

返程

Date - File 的创建日期

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getDescription()

获取 File 的说明。

返程

String - File 的说明

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

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()

获取 File 的 ID。

返程

String - File 的 ID

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getLastUpdated()

获取 File 的上次更新日期。

返程

Date - File 的上次更新日期

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

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()

获取 File 的名称。

返程

String - File 的名称

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

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()

获取访问通过链接共享的内容所需的 File 资源键。

返程

String - File 的资源键。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

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()

获取哪类用户可以访问 File 以及任何明确获得访问权限的用户除外。

返程

Access - 哪些类型的用户可以访问 File

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getSharingPermission()

获取向可以访问 File 的用户以及明确获得访问权限的各位用户的权限。

返程

Permission - 授予可以访问 File 的用户的权限

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getSize()

获取用于在云端硬盘中存储 File 的字节数。请注意,Google Workspace 应用文件不会占用云端硬盘存储空间,因此会返回 0 字节。

返程

Integer - 用于在云端硬盘中存储 File 的字节数

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

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()

获取可用于在云端硬盘或文档等 Google 应用中打开 File 的网址。

返程

String - 可用于在云端硬盘或文档等 Google 应用中查看此 File 的网址

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

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

isShareableByEditors()

确定拥有 File 修改权限的用户是否允许与其他用户共享或更改权限。

返程

Boolean - 如果您允许与其他用户共享权限或更改权限,则为 true;否则,返回 false

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

isStarred()

确定 File 是否在用户的云端硬盘中已加星标。

返程

Boolean - 如果用户的云端硬盘中已加星标的 File,否则为 falsetrue

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

isTrashed()

确定 File 是否在用户的云端硬盘回收站中。

返程

Boolean - 如果 File 位于用户云端硬盘的回收站中,则为 true;否则为 false

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

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());
}

参数

名称类型说明
destinationFolder可将文件复制到的目录。

返程

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());
}

参数

名称类型说明
nameString应用于新副本的文件名。

返程

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());
}

参数

名称类型说明
nameString应用于新副本的文件名。
destinationFolder可将文件复制到的目录。

返程

File - 新的副本。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive

moveTo(destination)

将此内容移至所提供的目标文件夹中。

当前用户必须是文件的所有者,或者至少拥有内容的当前父文件夹的编辑权限,才能将内容移至目标文件夹。

参数

名称类型说明
destinationFolder成为新父级的文件夹。

返程

File - 此 File,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive

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');
}

参数

名称类型说明
emailAddressString要移除的用户的电子邮件地址。

返程

File - 此 File,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • 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()));
}

参数

名称类型说明
userUser表示要移除的用户。

返程

File - 此 File,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive

removeEditor(emailAddress)

File 的编辑者列表中移除指定用户。如果用户属于拥有常规访问权限的一类用户(例如,用户的整个网域共享了 File,或者用户有权访问的 File 位于共享云端硬盘中),则此方法不会阻止用户访问 File

对于云端硬盘文件,此操作还会将用户从查看者列表中移除。

参数

名称类型说明
emailAddressString要移除的用户的电子邮件地址。

返程

File - 此 File,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive

removeEditor(user)

File 的编辑者列表中移除指定用户。如果用户属于拥有常规访问权限的一类用户(例如,用户的整个网域共享了 File,或者用户有权访问的 File 位于共享云端硬盘中),则此方法不会阻止用户访问 File

对于云端硬盘文件,此操作还会将用户从查看者列表中移除。

参数

名称类型说明
userUser表示要移除的用户。

返程

File - 此 File,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive

removeViewer(emailAddress)

File 的查看者和评论者列表中移除指定用户。如果用户是编辑者,而不是查看者或评论者,则此方法不会产生任何影响。此外,如果用户属于一类具有常规访问权限的用户,例如用户与其整个网域共享的 File,或者 File 位于用户有权访问的共享云端硬盘中,此方法也不会阻止用户访问 File

对于云端硬盘文件,此操作还会将用户从编辑者列表中移除。

参数

名称类型说明
emailAddressString要移除的用户的电子邮件地址。

返程

File - 用于链接的 File

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive

removeViewer(user)

File 的查看者和评论者列表中移除指定用户。如果用户是编辑者(而非查看者),则此方法不会产生任何影响。此外,如果用户属于一类具备常规访问权限的用户,例如用户与其整个网域共享的 File,或者 File 位于用户有权访问的共享云端硬盘中,此方法也不会阻止用户访问 File

对于云端硬盘文件,此操作还会将用户从编辑者列表中移除。

参数

名称类型说明
userUser表示要移除的用户。

返程

File - 用于链接的 File

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive

revokePermissions(emailAddress)

撤消已授予指定用户的 File 的访问权限。如果用户属于拥有常规访问权限的某一类用户,例如用户与整个网域共享 File,则此方法不会阻止用户访问 File

参数

名称类型说明
emailAddressString应撤销其访问权限的用户的电子邮件地址。

返程

File - 此 File,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive

revokePermissions(user)

撤消已授予指定用户的 File 的访问权限。如果用户属于拥有常规访问权限的某一类用户,例如用户与整个网域共享 File,则此方法不会阻止用户访问 File

参数

名称类型说明
userUser表示要撤消其访问权限的用户。

返程

File - 此 File,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive

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());

参数

名称类型说明
contentString文件的新内容。

返程

File - 此 File,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive

setDescription(description)

设置 File 的说明。

参数

名称类型说明
descriptionStringFile的新说明

返程

File - 此 File,用于链接

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive

setName(name)

设置 File 的名称。

参数

名称类型说明
nameStringFile 的新名称

返程

File - 此 File,用于链接

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive

setOwner(emailAddress)

更改 File 的所有者。此方法还可以为上一个所有者提供对 File 的明确编辑访问权限。

参数

名称类型说明
emailAddressString应成为新所有者的用户的电子邮件地址

返程

File - 此 File,用于链接

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive

setOwner(user)

更改 File 的所有者。此方法还可以为上一个所有者提供对 File 的明确编辑访问权限。

参数

名称类型说明
userUser表示应成为新所有者的用户

返程

File - 此 File,用于链接

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive

setSecurityUpdateEnabled(enabled)

设置 File 使用链接共享时是否需要资源键进行访问。默认情况下,符合条件的文件和文件夹处于启用状态。

详细了解 Google 云端硬盘的安全更新

参数

名称类型说明
enabledBoolean是否针对 File 启用资源键要求。

返程

File - 此 File,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive

setShareableByEditors(shareable)

设置是否允许具有 File 修改权限的用户与其他用户共享或更改权限。新 File 的默认值为 true

参数

名称类型说明
shareableBoolean如果应允许与其他用户共享权限或更改权限的用户,则为 true;否则,为 false

返程

File - 此 File,用于链接

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive

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);

参数

名称类型说明
accessTypeAccess哪种类别的用户可以访问File
permissionTypePermission应授予可以访问File的用户的权限

返程

File - 此 File,用于链接

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive

setStarred(starred)

设置是否在用户的云端硬盘中为 File 加星标。新 File 的默认值为 false

参数

名称类型说明
starredBooleantrue(如果 File 应在用户的云端硬盘中加星标;否则,则为 false

返程

File - 此 File,用于链接

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive

setTrashed(trashed)

设置 File 是否在用户的云端硬盘回收站中。只有所有者才能将 File 移入回收站。新 File 的默认值为 false

参数

名称类型说明
trashedBooleantrue(如果 File 应移至用户云端硬盘的回收站中,否则为 false

返程

File - 此 File,用于链接

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/drive