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 是否符合資格,可套用安全性更新,在透過連結共用時,必須提供資源金鑰才能存取。
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 文件) 中開啟 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 的留言者清單。如果使用者已在觀眾名單中,這個方法會將使用者從觀眾名單中升級。

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

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

參數

名稱類型說明
emailAddressString要新增的使用者電子郵件地址。

回攻員

File — This File, for chaining.

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

  • 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 — This File, for chaining.

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

  • 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 — This File, for chaining.

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

addEditor(emailAddress)

將指定使用者新增至 File 的編輯者清單。如果使用者已在觀眾名單中,這個方法會將使用者從觀眾名單中移除。

參數

名稱類型說明
emailAddressString要新增的使用者電子郵件地址。

回攻員

File — This File, for chaining.

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

addEditor(user)

將指定使用者新增至 File 的編輯者清單。如果使用者已在觀眾名單中,這個方法會將使用者從觀眾名單中移除。

參數

名稱類型說明
userUser要新增的使用者代表。

回攻員

File — This File, for chaining.

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

addEditors(emailAddresses)

將指定的使用者陣列新增至 File 的編輯者清單。如果使用者已在觀眾名單中,這個方法會將他們從觀眾名單中移除。

參數

名稱類型說明
emailAddressesString[]要新增的使用者電子郵件地址陣列。

回攻員

File — This File, for chaining.

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

addViewer(emailAddress)

將指定使用者新增至 File 的檢視者清單。如果使用者已在編輯者名單中,這個方法不會有任何作用。

參數

名稱類型說明
emailAddressString要新增的使用者電子郵件地址。

回攻員

File — This File, for chaining.

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

addViewer(user)

將指定使用者新增至 File 的檢視者清單。如果使用者已在編輯者名單中,這個方法不會有任何作用。

參數

名稱類型說明
userUser要新增的使用者代表。

回攻員

File — This File, for chaining.

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

addViewers(emailAddresses)

將指定的使用者陣列新增至 File 的檢視者清單。如果使用者已在編輯者清單中,這個方法對他們不會有任何影響。

參數

名稱類型說明
emailAddressesString[]要新增的使用者電子郵件地址陣列。

回攻員

File - This 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 的建立日期。

回攻員

DateFile 的建立日期

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getDescription()

取得 File 的說明。

回攻員

StringFile的說明

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

  • 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。

回攻員

StringFile 的 ID

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getLastUpdated()

取得上次更新 File 的日期。

回攻員

DateFile上次更新的日期

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

  • 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 的名稱。

回攻員

StringFile的名稱

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

  • 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 的資源金鑰,這是存取透過連結共用項目時的必要條件。

回攻員

StringFile 的資源鍵。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

  • 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|null:檔案的縮圖。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getUrl()

取得可用於在 Google 應用程式 (例如雲端硬碟或 Google 文件) 中開啟 File 的網址。

回攻員

String:可用於在 Google 雲端硬碟或 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 具有編輯權限的使用者與其他使用者共用,或變更權限。

回攻員

Booleantrue 如果允許具備編輯權限的使用者與其他使用者共用檔案或變更權限;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 是否位於使用者雲端硬碟的垃圾桶中。

回攻員

Booleantrue 如果 File 位於使用者雲端硬碟的垃圾桶中; 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 — This File, for chaining.

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

  • 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 — This File, for chaining.

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

  • 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 — This File, for chaining.

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

removeEditor(emailAddress)

File 的編輯者清單中移除指定使用者。如果使用者屬於具有一般存取權的使用者類別,例如 File 是與使用者的整個網域共用,或是 File 位於使用者可存取的共用雲端硬碟中,這個方法就無法禁止使用者存取 File

如果是雲端硬碟檔案,系統也會從檢視者清單中移除該使用者。

參數

名稱類型說明
emailAddressString要移除的使用者電子郵件地址。

回攻員

File — This File, for chaining.

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

removeEditor(user)

File 的編輯者清單中移除指定使用者。如果使用者屬於具有一般存取權的使用者類別,例如 File 是與使用者的整個網域共用,或是 File 位於使用者可存取的共用雲端硬碟中,這個方法就無法禁止使用者存取 File

如果是雲端硬碟檔案,系統也會從檢視者清單中移除該使用者。

參數

名稱類型說明
userUser要移除的使用者代表。

回攻員

File — This File, for chaining.

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

  • 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 - This File,用於鏈結。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

revokePermissions(user)

撤銷授予特定使用者的 File 存取權。如果使用者屬於具有一般存取權的使用者類別 (例如 File 是與使用者的整個網域共用),這個方法不會禁止使用者存取 File

參數

名稱類型說明
userUser應撤銷存取權的使用者代表。

回攻員

File — This File, for chaining.

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

setContent(content)

使用指定取代內容覆寫檔案內容。如果 content 大於 10 MB,則會擲回例外狀況。

// 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 - This File,用於鏈結。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

setDescription(description)

設定 File 的說明。

參數

名稱類型說明
descriptionStringFile 的新說明

回攻員

File - this File,用於鏈結

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

setName(name)

設定 File 的名稱。

參數

名稱類型說明
nameStringFile 的新名稱

回攻員

File - this File,用於鏈結

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

setOwner(emailAddress)

變更 File 的擁有者。這個方法也會授予先前的擁有者 File 的明確編輯權限。

參數

名稱類型說明
emailAddressString應成為新擁有者的使用者電子郵件地址

回攻員

File - this File,用於鏈結

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

setOwner(user)

變更 File 的擁有者。這個方法也會授予先前的擁有者 File 的明確編輯權限。

參數

名稱類型說明
userUser應成為新擁有者的使用者代表

回攻員

File - this File,用於鏈結

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

setSecurityUpdateEnabled(enabled)

設定 File 是否需要資源金鑰才能透過連結存取。系統預設會為符合資格的檔案和資料夾啟用這項功能。

進一步瞭解 Google 雲端硬碟安全性更新

參數

名稱類型說明
enabledBoolean是否為 File 啟用資源金鑰規定。

回攻員

File - This File,用於鏈結。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

setShareableByEditors(shareable)

設定是否允許具備 File 編輯權限的使用者與其他使用者共用檔案或變更權限。新 File 的預設值為 true

參數

名稱類型說明
shareableBooleantrue是否允許具備編輯權限的使用者與其他使用者共用檔案或變更權限;false如果不是

回攻員

File - this 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 - this File,用於鏈結

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

setStarred(starred)

設定使用者雲端硬碟中的 File 是否已加上星號。新 File 的預設值為 false

參數

名稱類型說明
starredBoolean如果應在使用者雲端硬碟中為 File 加上星號,則為 true;否則為 false

回攻員

File - this File,用於鏈結

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

setTrashed(trashed)

設定 File 是否位於使用者雲端硬碟的垃圾桶中。只有擁有者可以將「File」移至垃圾桶。新 File 的預設值為 false

參數

名稱類型說明
trashedBooleantrue 是否應移至使用者雲端硬碟的垃圾桶;false 如果不是,File

回攻員

File - this File,用於鏈結

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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