Google 雲端硬碟中的資料夾。您可以透過 DriveApp
存取或建立資料夾。
// Log the name of every folder in the user's Drive. var folders = DriveApp.getFolders(); while (folders.hasNext()) { var folder = folders.next(); Logger.log(folder.getName()); }
方法
內容詳盡的說明文件
addEditor(emailAddress)
addEditor(user)
addEditors(emailAddresses)
addViewer(emailAddress)
addViewer(user)
addViewers(emailAddresses)
createFile(blob)
這個外掛程式能根據指定的 Blob
任意資料,在目前的資料夾中建立檔案。
// Create an image file in Google Drive using the Maps service. var blob = Maps.newStaticMap().setCenter('76 9th Avenue, New York NY').getBlob(); DriveApp.getRootFolder().createFile(blob);
參數
名稱 | 類型 | 說明 |
---|---|---|
blob | BlobSource | 新檔案的資料。 |
回攻員
File
:新檔案。
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/drive
createFile(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
createFile(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 | 新檔案的內容。 |
mimeType | String | 新檔案的 MIME 類型。 |
回攻員
File
:新檔案。
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/drive
createFolder(name)
createShortcut(targetId)
createShortcutForTargetIdAndResourceKey(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()); }
參數
名稱 | 類型 | 說明 |
---|---|---|
targetId | String | 目標檔案或資料夾的 ID。 |
targetResourceKey | String | 目標檔案或資料夾的資源金鑰。 |
回攻員
File
:新的快速鍵。
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/drive
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
getDateCreated()
getDescription()
getEditors()
取得這個 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 (editor of editors) { console.log(editor.getName()); }
回攻員
User[]
:如果使用者俱有編輯權限,則這個 Folder
的編輯者清單;如果沒有編輯權限,則為空白陣列。
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
getFiles()
取得目前資料夾子項的所有檔案的集合。
回攻員
FileIterator
:屬於目前資料夾子項的所有檔案集合。
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
getFilesByName(name)
取得目前資料夾子項的所有檔案集合,且擁有指定名稱。
參數
名稱 | 類型 | 說明 |
---|---|---|
name | String | 要尋找的檔案名稱。 |
回攻員
FileIterator
:屬於目前資料夾子項且具有指定名稱的所有檔案集合。
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
getFilesByType(mimeType)
取得目前資料夾子項,且具備指定 MIME 類型的所有檔案集合。
參數
名稱 | 類型 | 說明 |
---|---|---|
mimeType | String | 要尋找的 MIME 類型檔案。 |
回攻員
FileIterator
:屬於目前資料夾子項,且具備指定 MIME 類型的所有檔案集合。
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
getFolders()
取得目前資料夾子項的所有資料夾集合。
回攻員
FolderIterator
:屬於目前資料夾子項的所有資料夾。
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
getFoldersByName(name)
取得目前資料夾子項的所有資料夾集合,且擁有指定名稱。
參數
名稱 | 類型 | 說明 |
---|---|---|
name | String | 要尋找的資料夾名稱。 |
回攻員
FolderIterator
:屬於目前資料夾子項且具有指定名稱的所有資料夾。
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
getId()
getLastUpdated()
getName()
getOwner()
取得此 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
getParents()
取得一組是 Folder
直接父項的資料夾。
回攻員
FolderIterator
:具備 Folder
立即父項的一組資料夾
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
getResourceKey()
getSecurityUpdateEligible()
取得此 Folder
是否符合資格,可套用透過連結共用時,需要資源金鑰才能存取的安全性更新。
雲端硬碟需要資源金鑰才能存取透過連結共用的部分檔案或資料夾。這項變更屬於安全性更新的一部分。針對符合條件的檔案和資料夾,系統預設會開啟更新。如要開啟或關閉符合資格的檔案的資源金鑰需求,請使用 setSecurityUpdateEnabled
。
進一步瞭解 Google 雲端硬碟的安全性更新。
回攻員
Boolean
:是否可對 Folder
套用資源金鑰要求。
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
getSecurityUpdateEnabled()
取得此 Folder
是否需要透過連結分享時需要資源金鑰才能存取。系統會為符合資格的檔案和資料夾預設啟用這項規定。
如要開啟或關閉符合資格的檔案的資源金鑰需求,請使用
setSecurityUpdateEnabled
。
進一步瞭解 Google 雲端硬碟的安全性更新。
回攻員
Boolean
:指出這個Folder
是否已啟用資源金鑰需求。
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
getSharingAccess()
getSharingPermission()
取得將權限授予可存取 Folder
的使用者,以及任何明確獲得存取權的個別使用者。
回攻員
Permission
:授予可存取 Folder
的使用者的權限
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
getSize()
getUrl()
getViewers()
取得此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 (viewer of viewers) { console.log(viewer.getName()); }
回攻員
User[]
:如果使用者擁有編輯權限,或是擁有空白陣列,此 Folder
的檢視者和加註者清單。
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
isStarred()
isTrashed()
moveTo(destination)
removeEditor(emailAddress)
removeEditor(user)
removeViewer(emailAddress)
removeViewer(user)
revokePermissions(emailAddress)
revokePermissions(user)
searchFiles(params)
取得目前資料夾子項且符合指定搜尋條件的所有檔案的集合。如需搜尋條件的詳細說明,請參閱 Google 雲端硬碟 SDK 說明文件。請注意,雲端硬碟服務使用 v2 的 Drive API,且部分查詢欄位與 v3 不同。查看第 2 版和第 3 版的欄位差異。
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."" var files = DriveApp.getRootFolder().searchFiles( 'modifiedDate > "2022-02-28" and title contains "untitled"'); while (files.hasNext()) { var file = files.next(); console.log(file.getName()); }
參數
名稱 | 類型 | 說明 |
---|---|---|
params | String | 搜尋條件,詳情請參閱 Google 雲端硬碟 SDK 說明文件。 |
回攻員
FileIterator
:屬於目前資料夾子項且符合搜尋條件的所有檔案集合。
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
searchFolders(params)
取得目前資料夾子項且符合指定搜尋條件的所有資料夾集合。如需搜尋條件的詳細說明,請參閱 Google 雲端硬碟 SDK 說明文件。請注意,雲端硬碟服務使用 v2 的 Drive API,且部分查詢欄位與 v3 不同。查看第 2 版和第 3 版的欄位差異。
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. var folders = DriveApp.getRootFolder().searchFolders('starred = true and "me" in owners'); while (folders.hasNext()) { var folder = folders.next(); console.log(folder.getName()); }
參數
名稱 | 類型 | 說明 |
---|---|---|
params | String | 搜尋條件,詳情請參閱 Google 雲端硬碟 SDK 說明文件。 |
回攻員
FolderIterator
:屬於目前資料夾子項且符合搜尋條件的所有資料夾。
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
setDescription(description)
setName(name)
setOwner(emailAddress)
setOwner(user)
setSecurityUpdateEnabled(enabled)
setSharing(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.) var folder = DriveApp.createFolder('Shared Folder'); folder.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.EDIT);
參數
名稱 | 類型 | 說明 |
---|---|---|
accessType | Access | 哪個類別的使用者應該可以存取 Folder |
permissionType | Permission | 對於可存取 Folder 的使用者所能授予的權限 |
回攻員
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/drive