Google 雲端硬碟中的資料夾。您可以透過 Drive 存取或建立資料夾。
// Log the name of every folder in the user's Drive. const folders = DriveApp.getFolders(); while (folders.hasNext()) { const folder = folders.next(); Logger.log(folder.getName()); }
方法
內容詳盡的說明文件
add Editor(emailAddress)
add Editor(user)
add Editors(emailAddresses)
add Viewer(emailAddress)
add Viewer(user)
add Viewers(emailAddresses)
create File(blob)
使用任意資料的指定 Blob,在目前資料夾中建立檔案。
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
blob | Blob | 新檔案的資料。 |
回攻員
File:新檔案。
授權
使用這個方法的指令碼需要透過下列一或多個範圍授權:
-
https://www.googleapis.com/auth/drive
create File(name, content)
在目前資料夾中建立具有指定名稱和內容的文字檔。如果 content 大於 50 MB,則會擲回例外狀況。
// Create a text file with the content "Hello, world!" DriveApp.getRootFolder().createFile('New Text File', 'Hello, world!');
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
name | String | 新檔案的名稱。 |
content | String | 新檔案的內容。 |
回攻員
File:新檔案。
授權
使用這個方法的指令碼需要透過下列一或多個範圍授權:
-
https://www.googleapis.com/auth/drive
create File(name, content, mimeType)
在目前資料夾中建立檔案,並指定名稱、內容和 MIME 類型。如果 content 大於 10 MB,系統會擲回例外狀況。
// Create an HTML file with the content "Hello, world!" DriveApp.getRootFolder().createFile('New HTML File', '<b>Hello, world!</b>', MimeType.HTML);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
name | String | 新檔案的名稱。 |
content | String | 新檔案的內容。 |
mime | String | 新檔案的 MIME 類型。 |
回攻員
File:新檔案。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/drive
create Folder(name)
create Shortcut(targetId)
create Shortcut For Target Id And Resource Key(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(), ); }
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
target | String | 目標檔案或資料夾的 ID。 |
target | String | 目標檔案或資料夾的資源鍵。 |
回攻員
File:新捷徑。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/drive
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 Date Created()
get Description()
get Editors()
取得這個 Folder 的編輯者清單。如果執行指令碼的使用者沒有 Folder 的編輯權限,這個方法會傳回空陣列。
// Gets a folder by its ID. // TODO(developer): Replace the folder ID with your own. const folder = DriveApp.getFolderById('1234567890abcdefghijklmnopqrstuvwxyz'); // Gets the list of editors and logs their names to the console. const editors = folder.getEditors(); for (const editor of editors) { console.log(editor.getName()); }
回攻員
User[] - 如果使用者具有編輯存取權,則為這個 Folder 的編輯者清單,否則為空陣列。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
get Files()
取得目前資料夾中所有子項檔案的集合。
回攻員
File:目前資料夾中所有子項檔案的集合。
授權
使用這個方法的指令碼需要透過下列一或多個範圍授權:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
get Files By Name(name)
取得目前資料夾中所有具有指定名稱的子項檔案集合。
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
name | String | 要尋找的檔案名稱。 |
回攻員
File:所有子項檔案的集合,這些檔案位於目前資料夾中,且具有指定名稱。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
get Files By Type(mimeType)
取得目前資料夾中具有指定 MIME 類型的一組子檔案。
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
mime | String | 要尋找的檔案 MIME 類型。 |
回攻員
File:所有屬於目前資料夾子項且具有指定 MIME 類型的檔案集合。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
get Folders()
取得目前資料夾的所有子資料夾集合。
回攻員
Folder:目前資料夾的所有子資料夾集合。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
get Folders By Name(name)
取得目前資料夾的所有子項資料夾,這些資料夾具有指定名稱。
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
name | String | 要尋找的資料夾名稱。 |
回攻員
Folder:所有子資料夾的集合,這些子資料夾位於目前資料夾中,且具有指定名稱。
授權
使用這個方法的指令碼需要透過下列一或多個範圍授權:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
get Id()
get Last Updated()
get Name()
get Owner()
取得這個 Folder 的擁有者。
// Gets a folder by its ID. // TODO(developer): Replace the folder ID with your own. const folder = DriveApp.getFolderById('1234567890abcdefghijklmnopqrstuvwxyz'); // Gets the owner of the folder and logs the name to the console. const folderOwner = folder.getOwner(); console.log(folderOwner.getName());
回攻員
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
get Parents()
取得 Folder 的直屬上層資料夾集合。
回攻員
Folder:資料夾集合,是 Folder 的直屬父項
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
get Resource Key()
get Security Update Eligible()
判斷這個 Folder 是否符合資格,可套用安全性更新。套用更新後,透過連結分享時,必須提供資源金鑰才能存取檔案。
如要存取透過連結共用的部分檔案或資料夾,雲端硬碟需要資源金鑰。這項異動是安全性更新的一部分。系統預設會為符合資格的檔案和資料夾開啟更新功能。如要為符合資格的檔案開啟或關閉資源金鑰規定,請使用 set。
進一步瞭解 Google 雲端硬碟安全性更新。
回攻員
Boolean:資源金鑰規定是否可套用至 Folder。
授權
使用這個方法的指令碼需要透過下列一或多個範圍授權:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
get Security Update Enabled()
取得這個 Folder 是否需要資源金鑰才能透過連結存取。系統預設會為符合資格的檔案和資料夾啟用這項要求。
如要為符合資格的檔案開啟或關閉資源金鑰規定,請使用
setSecurityUpdateEnabled。
進一步瞭解 Google 雲端硬碟安全性更新。
回攻員
Boolean:是否為這個 Folder 啟用資源金鑰規定。
授權
使用這個方法的指令碼需要透過下列一或多個範圍授權:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
get Sharing Access()
get Sharing Permission()
取得可存取 Folder 的使用者所獲授權,以及明確獲授權的個別使用者。
回攻員
Permission:授予可存取 Folder 的使用者權限
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
get Size()
get Url()
get Viewers()
取得這個 Folder 的檢視者和加註者清單。如果執行指令碼的使用者沒有 Folder 的編輯權限,這個方法會傳回空陣列。
// Gets a folder by its ID. // TODO(developer): Replace the folder ID with your own. const folder = DriveApp.getFolderById('1234567890abcdefghijklmnopqrstuvwxyz'); // Gets the list of viewers and logs their names to the console. const viewers = folder.getViewers(); for (const viewer of viewers) { console.log(viewer.getName()); }
回攻員
User[]:如果使用者具有編輯存取權,則為這項 Folder 的檢視者和加註者清單,否則為空陣列。
授權
使用這個方法的指令碼需要透過下列一或多個範圍授權:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
is Starred()
is Trashed()
move To(destination)
remove Editor(emailAddress)
remove Editor(user)
remove Viewer(emailAddress)
remove Viewer(user)
revoke Permissions(emailAddress)
revoke Permissions(user)
search Files(params)
取得目前資料夾中符合指定搜尋條件的所有子項檔案集合。如需搜尋條件的詳細資訊,請參閱 Google 雲端硬碟 SDK 說明文件。請注意,Google 雲端硬碟服務使用的是 Drive API 第 2 版,因此部分查詢欄位與第 3 版不同。查看 v2 和 v3 之間的欄位差異。
params 引數是可包含字串值的查詢字串,因此請務必正確逸出引號 (例如 "title contains 'Gulliver\\'s
Travels'" 或 'title contains "Gulliver\'s Travels"')。
// Logs the name of every file that are children of the current folder and modified after February 28, // 2022 whose name contains "untitled."" const files = DriveApp.getRootFolder().searchFiles( 'modifiedDate > "2022-02-28" and title contains "untitled"'); while (files.hasNext()) { const file = files.next(); console.log(file.getName()); }
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
params | String | 搜尋條件,詳情請參閱 Google 雲端硬碟 SDK 說明文件。 |
回攻員
File:目前資料夾中符合搜尋條件的所有子檔案集合。
授權
使用這個方法的指令碼需要透過下列一或多個範圍授權:
-
https://www.googleapis.com/auth/drive.readonly -
https://www.googleapis.com/auth/drive
search Folders(params)
取得目前資料夾的所有子項資料夾,並比對指定的搜尋條件。如需搜尋條件的詳細資訊,請參閱 Google 雲端硬碟 SDK 說明文件。請注意,Google 雲端硬碟服務使用的是 Drive API 第 2 版,因此部分查詢欄位與第 3 版不同。查看 v2 和 v3 之間的欄位差異。
params 引數是可包含字串值的查詢字串,因此請務必正確逸出引號 (例如 "title contains 'Gulliver\\'s
Travels'" 或 'title contains "Gulliver\'s Travels"')。
// Logs the name of every folder that are children of the current folder and you own and is starred. const folders = DriveApp.getRootFolder().searchFolders('starred = true and "me" in owners'); while (folders.hasNext()) { const folder = folders.next(); console.log(folder.getName()); }
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
params | String | 搜尋條件,詳情請參閱 Google 雲端硬碟 SDK 說明文件。 |
回攻員
Folder:目前資料夾的所有子資料夾,且符合搜尋條件。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/drive.readonly -
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)
設定可存取 Folder 的使用者類別,以及授予這些使用者的權限 (明確獲准存取的個別使用者除外)。
// 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 | 哪些類別的使用者應可存取 Folder |
permission | Permission | 可存取 Folder 的使用者應具備的權限 |
回攻員
授權
使用這個方法的指令碼需要透過下列一或多個範圍授權:
-
https://www.googleapis.com/auth/drive