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 已在用户的云端硬盘中加星标,则为 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!'
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

参数

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