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 不同。查看第 2 版和第 3 版的欄位差異

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 不同。查看第 2 版和第 3 版的欄位差異

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

已淘汰的方法