Class DriveApp

DriveApp

允许脚本在 Google 云端硬盘中创建、查找和修改文件和文件夹。如需访问共享云端硬盘中的文件或文件夹,请使用高级云端硬盘服务

// Logs the name of every file in the user's Drive.
var files = DriveApp.getFiles();
while (files.hasNext()) {
  var file = files.next();
  console.log(file.getName());
}

属性

媒体资源类型说明
AccessAccess此枚举表示可以访问某个文件或文件夹的用户所属的类,以及已被明确授予访问权限的任何单个用户。
PermissionPermission一个枚举,表示向可以访问文件或文件夹的用户授予的权限,而不仅仅是已明确授予访问权限的任何单个用户。

方法

方法返回类型简介
continueFileIterator(continuationToken)FileIterator使用来自上一个迭代器的接续令牌恢复文件迭代。
continueFolderIterator(continuationToken)FolderIterator使用来自上一个迭代器的接续令牌恢复文件夹迭代。
createFile(blob)File在用户云端硬盘的根目录中基于指定的 Blob 任意数据创建一个文件。
createFile(name, content)File在用户云端硬盘的根目录中创建一个具有指定名称和内容的文本文件。
createFile(name, content, mimeType)File在用户的云端硬盘根目录中创建具有指定名称、内容和 MIME 类型的文件。
createFolder(name)Folder在用户云端硬盘的根目录中创建一个具有指定名称的文件夹。
createShortcut(targetId)File为所提供的云端硬盘内容 ID 创建快捷方式,并将其返回。
createShortcutForTargetIdAndResourceKey(targetId, targetResourceKey)File为所提供的云端硬盘内容 ID 和资源密钥创建快捷方式,并将其返回。
enforceSingleParent(value)void针对影响项父级的所有调用启用或停用 forceSingleParent 行为。
getFileById(id)File获取具有指定 ID 的文件。
getFileByIdAndResourceKey(id, resourceKey)File获取具有指定 ID 和资源密钥的文件。
getFiles()FileIterator获取用户云端硬盘中所有文件的集合。
getFilesByName(name)FileIterator获取用户云端硬盘中具有指定名称的所有文件的集合。
getFilesByType(mimeType)FileIterator获取用户云端硬盘中具有指定 MIME 类型的所有文件的集合。
getFolderById(id)Folder获取具有指定 ID 的文件夹。
getFolderByIdAndResourceKey(id, resourceKey)Folder获取具有指定 ID 和资源键的文件夹。
getFolders()FolderIterator获取用户云端硬盘中所有文件夹的集合。
getFoldersByName(name)FolderIterator获取用户的云端硬盘中具有指定名称的所有文件夹的集合。
getRootFolder()Folder获取位于用户云端硬盘根目录下的文件夹。
getStorageLimit()Integer获取允许用户在云端硬盘中存储的字节数。
getStorageUsed()Integer获取用户当前在云端硬盘中存储的字节数。
getTrashedFiles()FileIterator获取用户云端硬盘回收站中所有文件的集合。
getTrashedFolders()FolderIterator获取用户云端硬盘回收站中所有文件夹的集合。
searchFiles(params)FileIterator获取用户的云端硬盘中与指定搜索条件匹配的所有文件的集合。
searchFolders(params)FolderIterator获取用户的云端硬盘中与指定搜索条件匹配的所有文件夹的集合。

详细文档

continueFileIterator(continuationToken)

使用来自上一个迭代器的接续令牌恢复文件迭代。如果在一次执行中处理的迭代器超过最长执行时间,则此方法很有用。延续令牌的有效期通常为一周。

// Continues getting a list of all 'Untitled document' files in the user's Drive.
// Creates a file iterator named 'previousIterator'.
const previousIterator = DriveApp.getFilesByName('Untitled document');

// Gets continuation token from the previous file iterator.
const continuationToken = previousIterator.getContinuationToken();

// Creates a new iterator using the continuation token from the previous file iterator.
const newIterator = DriveApp.continueFileIterator(continuationToken);

// Resumes the file iteration using a continuation token from 'firstIterator' and
// logs the file name.
if (newIterator.hasNext()) {
  const file = newIterator.next();
  console.log(file.getName());
}

参数

名称类型说明
continuationTokenString来自上一个文件迭代器的延续令牌。

弃踢回攻

FileIterator - 生成接续令牌时保留在先前迭代器中的文件的集合。


continueFolderIterator(continuationToken)

使用来自上一个迭代器的接续令牌恢复文件夹迭代。如果在一次执行中处理的迭代器超过最长执行时间,则此方法很有用。延续令牌的有效期通常为一周。

// Continues getting a list of all folders in user's Drive.
// Creates a folder iterator named 'previousIterator'.
const previousIterator = DriveApp.getFolders();

// Gets continuation token from the previous folder iterator.
const continuationToken = previousIterator.getContinuationToken();

// Creates a new iterator using the continuation token from the previous folder iterator.
const newIterator = DriveApp.continueFolderIterator(continuationToken);

// Resumes the folder iteration using a continuation token from the previous iterator and logs
// the folder name.
if (newIterator.hasNext()) {
  const folder = newIterator.next();
  console.log(folder.getName());
}

参数

名称类型说明
continuationTokenString来自上一个文件夹迭代器的延续令牌。

弃踢回攻

FolderIterator - 生成继续令牌时保留在上一个迭代器中的文件夹集合。


createFile(blob)

此类函数会在用户的云端硬盘根目录中基于指定的 Blob 任意数据创建一个文件。

// Create an image file in Google Drive using the Maps service.
var blob = Maps.newStaticMap().setCenter('76 9th Avenue, New York NY').getBlob();
DriveApp.createFile(blob);

参数

名称类型说明
blobBlobSource新文件的数据。

弃踢回攻

File - 新文件。

授权

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

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

createFile(name, content)

在用户云端硬盘的根目录中创建一个具有指定名称和内容的文本文件。如果 content 大于 50 MB,则会抛出异常。

// Create a text file with the content "Hello, world!"
DriveApp.createFile('New Text File', 'Hello, world!');

参数

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

弃踢回攻

File - 新文件。

授权

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

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

createFile(name, content, mimeType)

在用户的云端硬盘根目录中创建具有指定名称、内容和 MIME 类型的文件。如果 content 大于 10 MB,则会抛出异常。

// Create an HTML file with the content "Hello, world!"
DriveApp.createFile('New HTML File', '<b>Hello, world!</b>', MimeType.HTML);

参数

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

弃踢回攻

File - 新文件。

授权

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

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

createFolder(name)

在用户云端硬盘的根目录中创建一个具有指定名称的文件夹。

参数

名称类型说明
nameString新文件夹的名称。

弃踢回攻

Folder - 新文件夹。

授权

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

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

createShortcut(targetId)

为所提供的云端硬盘内容 ID 创建快捷方式,并将其返回。

参数

名称类型说明
targetIdString目标文件或文件夹的文件 ID。

弃踢回攻

File - 新的快捷方式。

授权

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

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

createShortcutForTargetIdAndResourceKey(targetId, targetResourceKey)

为所提供的云端硬盘内容 ID 和资源密钥创建快捷方式,并将其返回。资源密钥是访问通过链接共享的目标文件或文件夹时需要传递的额外参数。

// Creates shortcuts for all folders in the user's drive that have a specific name.
// TODO(developer): Replace 'Test-Folder' with a valid folder name in your drive.
const folders = DriveApp.getFoldersByName('Test-Folder');

// Iterates through all folders named 'Test-Folder'.
while (folders.hasNext()) {
  const folder = folders.next();

  // Creates a shortcut to the provided Drive item ID and resource key, and returns it.
  DriveApp.createShortcutForTargetIdAndResourceKey(folder.getId(), folder.getResourceKey());
}

参数

名称类型说明
targetIdString目标文件或文件夹的 ID。
targetResourceKeyString目标文件或文件夹的资源密钥。

弃踢回攻

File - 新的快捷方式。

授权

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

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

enforceSingleParent(value)

针对影响项父级的所有调用启用或停用 forceSingleParent 行为。

如需了解详情,请参阅 简化 Google 云端硬盘的文件夹结构和共享模型博客。

// Enables enforceSingleParent behavior for all calls affecting item parents.
DriveApp.enforceSingleParent(true);

参数

名称类型说明
valueBooleanforceSingleParent 标记的新状态。

授权

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

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

getFileById(id)

获取具有指定 ID 的文件。如果文件不存在或用户无权访问该文件,则会抛出脚本异常。

// Gets a list of all files in Google Drive with the given name.
// TODO(developer): Replace 'Test' with your file name.
const files = DriveApp.getFilesByName('Test');

if (files.hasNext()) {
  // Gets the ID of each file in the list.
  const fileId = files.next().getId();

  // Gets the file name using its ID and logs it to the console.
  console.log(DriveApp.getFileById(fileId).getName());
}

参数

名称类型说明
idString文件的 ID。

弃踢回攻

File - 具有指定 ID 的文件。

授权

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

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

getFileByIdAndResourceKey(id, resourceKey)

获取具有指定 ID 和资源密钥的文件。资源键是在访问通过链接共享的文件时需要传递的额外参数。

如果文件不存在或用户无权访问,则会抛出脚本异常。

// Gets a list of all files in Drive with the given name.
// TODO(developer): Replace 'Test' with your file name.
const files = DriveApp.getFilesByName('Test');
if (files.hasNext()) {

  // Gets the first file in the list.
  const file = files.next();

  // Gets the ID and resource key.
  const key = file.getResourceKey();
  const id = file.getId();

  // Logs the file name to the console using its ID and resource key.
  console.log(DriveApp.getFileByIdAndResourceKey(id, key).getName());
}

参数

名称类型说明
idString文件的 ID。
resourceKeyString文件夹的资源密钥。

弃踢回攻

File - 具有指定 ID 的文件。

授权

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

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

getFiles()

获取用户云端硬盘中所有文件的集合。

弃踢回攻

FileIterator - 用户云端硬盘中所有文件的集合。

授权

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

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

getFilesByName(name)

获取用户云端硬盘中具有指定名称的所有文件的集合。

参数

名称类型说明
nameString要查找的文件的名称。

弃踢回攻

FileIterator - 用户云端硬盘中具有指定名称的所有文件的集合。

授权

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

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

getFilesByType(mimeType)

获取用户云端硬盘中具有指定 MIME 类型的所有文件的集合。

参数

名称类型说明
mimeTypeString要查找的文件的 MIME 类型。

弃踢回攻

FileIterator - 用户云端硬盘中具有指定 MIME 类型的所有文件的集合。

授权

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

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

getFolderById(id)

获取具有指定 ID 的文件夹。如果文件夹不存在或用户无权访问它,则会抛出脚本异常。

参数

名称类型说明
idString文件夹的 ID。

弃踢回攻

Folder - 具有指定 ID 的文件夹。

授权

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

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

getFolderByIdAndResourceKey(id, resourceKey)

获取具有指定 ID 和资源键的文件夹。资源键是在访问通过链接共享的文件夹时需要传递的额外参数。

如果文件夹不存在或用户无权访问它,则会抛出脚本异常。

参数

名称类型说明
idString文件夹的 ID。
resourceKeyString文件夹的资源密钥。

弃踢回攻

Folder - 具有指定 ID 的文件夹。

授权

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

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

getFolders()

获取用户云端硬盘中所有文件夹的集合。

弃踢回攻

FolderIterator - 用户云端硬盘中所有文件夹的集合。

授权

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

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

getFoldersByName(name)

获取用户的云端硬盘中具有指定名称的所有文件夹的集合。

参数

名称类型说明
nameString要查找的文件夹的名称。

弃踢回攻

FolderIterator - 用户云端硬盘中具有指定名称的所有文件夹的集合。

授权

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

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

getRootFolder()

获取位于用户云端硬盘根目录下的文件夹。

// Gets the user's My Drive folder and logs its name to the console.
console.log(DriveApp.getRootFolder().getName());

// Logs the Drive owner's name to the console.
console.log(DriveApp.getRootFolder().getOwner().getName());

弃踢回攻

Folder - 用户云端硬盘的根文件夹。

授权

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

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

getStorageLimit()

获取允许用户在云端硬盘中存储的字节数。

// Gets the number of bytes the user can store in Drive and logs it to the console.
console.log(DriveApp.getStorageLimit());

弃踢回攻

Integer - 允许用户在云端硬盘中存储的字节数。

授权

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

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

getStorageUsed()

获取用户当前在云端硬盘中存储的字节数。

// Gets the number of bytes the user is currently storing in Drive and logs it to the console.
console.log(DriveApp.getStorageUsed());

弃踢回攻

Integer - 用户当前在云端硬盘中存储的字节数。

授权

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

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

getTrashedFiles()

获取用户云端硬盘回收站中所有文件的集合。

// Gets a list of all the files in the trash of the user's Drive.
const trashFiles = DriveApp.getTrashedFiles();

// Logs the trash file names to the console.
while (trashFiles.hasNext()) {
  const file = trashFiles.next();
  console.log(file.getName());
}

弃踢回攻

FileIterator - 回收站中的文件集合。

授权

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

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

getTrashedFolders()

获取用户云端硬盘回收站中所有文件夹的集合。

// Gets a collection of all the folders in the trash of the user's Drive.
const trashFolders = DriveApp.getTrashedFolders();

// Logs the trash folder names to the console.
while (trashFolders.hasNext()) {
  const folder = trashFolders.next();
  console.log(folder.getName());
}

弃踢回攻

FolderIterator - 回收站中的文件夹集合。

授权

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

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

searchFiles(params)

获取用户的云端硬盘中与指定搜索条件匹配的所有文件的集合。如需详细了解搜索条件,请参阅 Google 云端硬盘 SDK 文档。请注意,云端硬盘服务使用 v2 的 Drive API,并且某些查询字段与 v3 不同。查看 v2 和 v3 之间的字段差异

params 参数是可能包含字符串值的查询字符串,因此请注意正确转义引号(例如 "title contains 'Gulliver\\'s Travels'"'title contains "Gulliver\'s Travels"')。

// Logs the name of every file in the user's Drive that modified after February 28,
// 2022 whose name contains "untitled.""
var files = DriveApp.searchFiles(
    'modifiedDate > "2022-02-28" and title contains "untitled"');
while (files.hasNext()) {
  var file = files.next();
  console.log(file.getName());
}

参数

名称类型说明
paramsString搜索条件(Google 云端硬盘 SDK 文档中详述)。

弃踢回攻

FileIterator - 用户云端硬盘中符合搜索条件的所有文件的集合。

授权

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

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

searchFolders(params)

获取用户的云端硬盘中与指定搜索条件匹配的所有文件夹的集合。如需详细了解搜索条件,请参阅 Google 云端硬盘 SDK 文档。请注意,云端硬盘服务使用 v2 的 Drive API,并且某些查询字段与 v3 不同。查看 v2 和 v3 之间的字段差异

params 参数是可能包含字符串值的查询字符串,因此请注意正确转义引号(例如 "title contains 'Gulliver\\'s Travels'"'title contains "Gulliver\'s Travels"')。

// Logs the name of every folder in the user's Drive that you own and is starred.
var folders = DriveApp.searchFolders('starred = true and "me" in owners');
while (folders.hasNext()) {
  var folder = folders.next();
  console.log(folder.getName());
}

参数

名称类型说明
paramsString搜索条件(Google 云端硬盘 SDK 文档中详述)。

弃踢回攻

FolderIterator - 用户云端硬盘中与搜索条件匹配的所有文件夹的集合。

授权

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

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

已废弃的方法