Google 雲端硬碟中的檔案。您可以透過 Drive 存取或建立檔案。
// 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); } }
方法
內容詳盡的說明文件
add Commenter(emailAddress)
將指定使用者新增至 File 的留言者清單。如果使用者已在觀眾名單中,這個方法會將使用者從觀眾名單中升級。
const files = DriveApp.getFilesByName('Test'); // Loops through the files while (files.hasNext()) { const file = files.next(); file.addCommenter('hello@example.com'); }
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
email | String | 要新增的使用者電子郵件地址。 |
回攻員
File — This File, for chaining.
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/drive
add Commenter(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()); }
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
user | User | 要新增的使用者代表。 |
回攻員
File — This File, for chaining.
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/drive
add Commenters(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)); }
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
email | String[] | 要新增的使用者電子郵件地址陣列。 |
回攻員
File — This File, for chaining.
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/drive
add Editor(emailAddress)
add Editor(user)
add Editors(emailAddresses)
add Viewer(emailAddress)
add Viewer(user)
add Viewers(emailAddresses)
get Access(email)
取得授予特定使用者的權限。這個方法不支援傳回 Google 群組的權限,或透過 Google 群組沿用的權限。
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
email | String | 要檢查權限的使用者電子郵件地址。不支援 Google 網路論壇。 |
回攻員
Permission - 授予使用者的權限。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
get Access(user)
取得授予特定使用者的權限。這個方法不支援傳回 Google 群組的權限,或透過 Google 群組繼承的權限。
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
user | User | 代表要檢查權限的使用者。 |
回攻員
Permission - 授予使用者的權限。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
get As(contentType)
以轉換為指定內容類型的 Blob 形式,傳回這個物件內的資料。這個方法會在檔案名稱中加入適當的副檔名,例如「myfile.pdf」。不過,系統會假設檔案名稱中最後一個半形句點 (如有) 後方的部分是現有副檔名,應予以取代。因此,「ShoppingList.12.25.2014」會變成「ShoppingList.12.25.pdf」。
如要查看每日轉換配額,請參閱「Google 服務配額」。新建立的 Google Workspace 網域可能暫時會受到較嚴格的配額限制。
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
content | String | 要轉換成的 MIME 類型。對於大多數 Blob 而言,'application/pdf' 是唯一有效的選項。如果是 BMP、GIF、JPEG 或 PNG 格式的圖片,'image/bmp'、'image/gif'、'image/jpeg' 或 'image/png' 也有效。如果是 Google 文件,'text/markdown' 也有效。 |
回攻員
Blob:以 Blob 形式呈現的資料。
get Blob()
get Date Created()
get Description()
get Download Url()
取得可用於下載檔案的網址。只有有權開啟 Google 雲端硬碟檔案的使用者,才能存取該網址。您可以在瀏覽器中使用這個網址下載檔案,但無法使用 Url 擷取檔案。如要在指令碼中使用檔案內容,請使用 get。
// 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
get Editors()
取得這個 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
get Id()
get Last Updated()
get Mime Type()
取得檔案的 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
get Name()
get Owner()
取得檔案擁有者。
// 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
get Parents()
取得 File 的直屬父項資料夾集合。
回攻員
Folder:資料夾集合,是 File 的直屬父項
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
get Resource Key()
get Security Update Eligible()
判斷這個 File 是否符合資格,可套用安全性更新。套用更新後,透過連結分享時,必須提供資源金鑰才能存取檔案。
如要存取透過連結共用的部分檔案或資料夾,雲端硬碟需要資源金鑰。這項異動是安全性更新的一部分。系統預設會為符合資格的檔案和資料夾開啟更新功能。如要為符合資格的檔案開啟或關閉資源金鑰規定,請使用 set。
進一步瞭解 Google 雲端硬碟安全性更新。
回攻員
Boolean:資源金鑰規定是否可套用至 File。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
get Security Update Enabled()
取得這個 File 是否需要資源金鑰才能透過連結存取。系統預設會為符合資格的檔案和資料夾開啟這項要求。
如要為符合資格的檔案開啟或關閉資源金鑰規定,請使用
setSecurityUpdateEnabled。
進一步瞭解 Google 雲端硬碟安全性更新。
回攻員
Boolean:是否為這個 File 啟用資源金鑰規定。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
get Sharing Access()
get Sharing Permission()
取得可存取 File 的使用者所獲授權,以及已明確獲授權的個別使用者。
回攻員
Permission:授予可存取 File 的使用者權限
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
get Size()
get Target Id()
如果是捷徑,則傳回所指向項目的 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。
get Target Mime Type()
如果是捷徑,則傳回所指項目的 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 類型。
get Target Resource Key()
如果檔案是捷徑,則會傳回所指項目的資源鍵。資源金鑰是您需要傳遞的額外參數,才能存取透過連結共用的檔案。
如果檔案不是捷徑,則會傳回 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
get Thumbnail()
取得檔案的縮圖,如果沒有縮圖,則會取得 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
get Url()
get Viewers()
取得這個 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
is Starred()
is Trashed()
make Copy()
建立檔案副本。
// 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
make Copy(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()); }
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
destination | Folder | 要將檔案複製到哪個目錄。 |
回攻員
File:新副本。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/drive
make Copy(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()); }
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
name | String | 新副本應套用的檔案名稱。 |
回攻員
File:新副本。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/drive
make Copy(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()); }
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
name | String | 新副本應套用的檔案名稱。 |
destination | Folder | 要將檔案複製到哪個目錄。 |
回攻員
File:新副本。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/drive
move To(destination)
remove Commenter(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'); }
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
email | String | 要移除的使用者電子郵件地址。 |
回攻員
File — This File, for chaining.
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/drive
remove Commenter(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())); }
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
user | User | 要移除的使用者代表。 |
回攻員
File — This File, for chaining.
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/drive
remove Editor(emailAddress)
remove Editor(user)
remove Viewer(emailAddress)
remove Viewer(user)
revoke Permissions(emailAddress)
revoke Permissions(user)
set Content(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());
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
content | String | 檔案的新內容。 |
回攻員
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/drive
set Description(description)
set Name(name)
set Owner(emailAddress)
set Owner(user)
set Security Update Enabled(enabled)
set Sharing(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);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
access | Access | 哪些類別的使用者應可存取 File |
permission | Permission | 可存取 File 的使用者應獲得的權限 |
回攻員
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/drive