Class File

文件

Google 云端硬盘中的文件。您可以通过 DriveApp 访问或创建文件。

// Trash every untitled spreadsheet that hasn't been updated in a week.
const files = DriveApp.getFilesByName('Untitled spreadsheet');
while (files.hasNext()) {
  const 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 是否符合应用安全更新的条件。如果应用安全更新,则在通过链接分享此 File 时,需要使用资源密钥才能访问。
getSecurityUpdateEnabled()Boolean获取相应 File 在通过链接共享时是否需要资源密钥才能访问。
getSharingAccess()Access获取哪些类别的用户可以访问 File,明确获得访问权限的任何个人用户除外。
getSharingPermission()Permission获取已授予可访问 File 的用户的权限,明确授予访问权限的任何个人用户除外。
getSize()Integer获取用于在云端硬盘中存储 File 的字节数。
getTargetId()String如果这是快捷方式,则返回其指向的商品的 ID。
getTargetMimeType()String如果这是快捷方式,则返回其指向的商品的 MIME 类型。
getTargetResourceKey()String如果文件是快捷方式,则返回其指向的资源的资源键。
getThumbnail()Blob|null获取文件的缩略图,如果没有缩略图,则返回 null
getUrl()String获取可用于在 Google 应用(例如 Google 云端硬盘或 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)FileFile 设置说明。
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 的评论者列表中。如果用户已在观看者列表中,此方法会将用户从观看者列表中提升出来。

const files = DriveApp.getFilesByName('Test');

// Loops through the files
while (files.hasNext()) {
  const file = files.next();
  file.addCommenter('hello@example.com');
}

参数

名称类型说明
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)

获取授予特定用户的权限。该方法不支持返回 Google 群组的权限或通过 Google 群组继承的权限。

参数

名称类型说明
emailString应检查其权限的用户的电子邮件地址。不支持 Google 群组。

返回

Permission - 授予用户的权限。

授权

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

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

getAccess(user)

获取授予特定用户的权限。该方法不支持返回 Google 群组的权限或通过 Google 群组继承的权限。

参数

名称类型说明
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' 中的任何一个也有效。对于 Google 文档,'text/markdown' 也有效。

返回

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 是否符合应用安全更新的条件。如果应用安全更新,则在通过链接分享此 File 时,需要使用资源密钥才能访问此 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|null - 文件的缩略图。

授权

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

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

getUrl()

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

返回

String - 可用于在 Google 应用(如云端硬盘或 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 (const 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 在用户的云端硬盘中已加星标,则为 true;否则为 false

授权

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

  • 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!'
const 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

参数

名称类型说明
shareableBooleantrue 如果应允许具有修改权限的用户与其他用户共享或更改权限;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.)
const 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

参数

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

返回

File - 此 File,用于链式调用

授权

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

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

setTrashed(trashed)

设置 File 是否位于用户的云端硬盘回收站中。只有所有者可以删除File。新 File 的默认值为 false

参数

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

返回

File - 此 File,用于链式调用

授权

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

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