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如果這是 Shortcut,請傳回其指向項目的 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)File將指定使用者從 File 的註解者清單中移除。
removeCommenter(user)File將指定使用者從 File 的註解者清單中移除。
removeEditor(emailAddress)File將指定使用者從 File 的編輯者清單中移除。
removeEditor(user)File將指定使用者從 File 的編輯者清單中移除。
removeViewer(emailAddress)File將特定使用者從「File」的檢視者和加註者清單中移除。
removeViewer(user)File將特定使用者從「File」的檢視者和加註者清單中移除。
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 的建立日期。

回攻員

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

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

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

getUrl()

取得可用於在雲端硬碟或文件等 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 (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 加上星號。

回攻員

Booleantrue 表示 File 已在使用者的雲端硬碟中加上星號;如果未在使用者的雲端硬碟中加上星號,則為 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 大於 10 MB,就會擲回例外狀況。

// 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

參數

名稱類型說明
starredBooleantrue 表示在使用者雲端硬碟中應為 File 加上星號;如果不是,則為 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