儲存在 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
的註解者清單。如果使用者已列於檢視者清單中,這個方法會在檢視者清單中宣傳使用者。
// 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)); }
參數
名稱 | 類型 | 說明 |
---|---|---|
emailAddress | String | 要新增的使用者電子郵件地址。 |
回攻員
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
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()); }
參數
名稱 | 類型 | 說明 |
---|---|---|
user | User | 代表要新增的使用者。 |
回攻員
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
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)); }
參數
名稱 | 類型 | 說明 |
---|---|---|
emailAddresses | String[] | 要新增的使用者電子郵件地址陣列。 |
回攻員
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/drive
addEditor(emailAddress)
addEditor(user)
addEditors(emailAddresses)
addViewer(emailAddress)
addViewer(user)
addViewers(emailAddresses)
getAccess(email)
取得已授予指定使用者的權限。
參數
名稱 | 類型 | 說明 |
---|---|---|
email | String | 應檢查其權限的使用者電子郵件地址 |
回攻員
Permission
:授予使用者的權限
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
getAccess(user)
取得已授予指定使用者的權限。
參數
名稱 | 類型 | 說明 |
---|---|---|
user | User | 代表應檢查其權限的使用者 |
回攻員
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 網域可能暫時適用更嚴格的配額。
參數
名稱 | 類型 | 說明 |
---|---|---|
contentType | String | 要轉換的 MIME 類型。對多數 blob 而言,'application/pdf' 是唯一的有效選項。如果是 BMP、GIF、JPEG 或 PNG 格式的圖片,'image/bmp' 、'image/gif' 、'image/jpeg' 或 'image/png' 中的任一個也是有效的格式。 |
回攻員
Blob
:資料做為 blob。
getBlob()
getDateCreated()
getDescription()
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()
getLastUpdated()
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()
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()
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()
getSharingPermission()
取得將權限授予可存取 File
的使用者,以及任何明確獲得存取權的個別使用者。
回攻員
Permission
:授予可存取 File
的使用者的權限
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
getSize()
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()
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
isStarred()
isTrashed()
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()); }
參數
名稱 | 類型 | 說明 |
---|---|---|
destination | Folder | 要複製檔案的目錄。 |
回攻員
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()); }
參數
名稱 | 類型 | 說明 |
---|---|---|
name | String | 要套用至新副本的檔案名稱。 |
回攻員
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()); }
參數
名稱 | 類型 | 說明 |
---|---|---|
name | String | 要套用至新副本的檔案名稱。 |
destination | Folder | 要複製檔案的目錄。 |
回攻員
File
:新副本。
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/drive
moveTo(destination)
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'); }
參數
名稱 | 類型 | 說明 |
---|---|---|
emailAddress | String | 要移除的使用者電子郵件地址。 |
回攻員
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
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())); }
參數
名稱 | 類型 | 說明 |
---|---|---|
user | User | 代表要移除的使用者。 |
回攻員
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/drive
removeEditor(emailAddress)
removeEditor(user)
removeViewer(emailAddress)
removeViewer(user)
revokePermissions(emailAddress)
revokePermissions(user)
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());
參數
名稱 | 類型 | 說明 |
---|---|---|
content | String | 檔案的新內容。 |
回攻員
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/drive
setDescription(description)
setName(name)
setOwner(emailAddress)
setOwner(user)
setSecurityUpdateEnabled(enabled)
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);
參數
名稱 | 類型 | 說明 |
---|---|---|
accessType | Access | 哪個類別的使用者應該可以存取 File |
permissionType | Permission | 對於可存取 File 的使用者所能授予的權限 |
回攻員
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/drive