建構 Google 雲端硬碟介面
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
使用者使用 Google 雲端硬碟時,Google Workspace 外掛程式可提供自訂介面。您可以藉此提供使用者其他相關資訊、自動執行工作,以及將第三方系統連結至 Google 雲端硬碟。
存取外掛程式 UI
如果 Google 雲端硬碟使用者介面右側的圖示欄中顯示 Google Workspace 外掛程式的圖示,Google Workspace 外掛程式可以定義首頁介面、項目選取介面,或同時定義這兩者:
建構外掛程式的雲端硬碟介面
如要建構擴充 Google 雲端硬碟的介面,請按照下列一般步驟操作:
- 決定是否要讓外掛程式擁有專屬的雲端硬碟首頁。
此外,您也可以決定是否要在使用者選取雲端硬碟項目時,提供內容相關介面。
- 在外掛程式指令碼專案的資訊清單中,加入適當的
addOns.common
和 addOns.drive
欄位,包括任何必要的 Google 雲端硬碟範圍。
- 如果您提供專為 Google 雲端硬碟設計的首頁,請實作
drive.homepageTrigger
函式來建構這個介面。您也可以選擇使用
common.homepageTrigger
介面,管理多個 Google Workspace 主機。
- 如果您提供 Google 雲端硬碟情境項目選取介面,就必須實作
drive.onItemsSelectedTrigger
情境觸發函式,才能建構這個介面。詳情請參閱「選取項目時的雲端硬碟情境介面」。
- 實作相關聯的回呼函式,以便回應使用者的 UI 互動,例如點選按鈕。
雲端硬碟首頁
Google 雲端硬碟支援顯示 Google Workspace 外掛程式的首頁。如要在 Google 雲端硬碟中顯示外掛程式的通用首頁,請確保外掛程式的資訊清單中有 addOns.drive
欄位。
或者,您也可以在外掛程式資訊清單中新增 drive.homepageTrigger
,提供 Google 雲端硬碟專用的首頁。
無論選擇哪一種做法,您都必須在外掛程式的指令碼專案中提供首頁觸發函式的名稱。系統會在需要時自動呼叫這個函式,以建構雲端硬碟首頁。您必須實作這個函式,才能建構並傳回單一 Card
或組成首頁的 Card
物件陣列。首頁觸發函式會將事件物件當做參數傳遞,其中包含用戶端平台等一般資訊。您可以運用事件物件資料,量身打造首頁。
根據所選項目顯示相關介面
Google 雲端硬碟會根據內容比對觸發條件,判斷使用者選取一或多個 Google 雲端硬碟項目時,應顯示哪個介面 (如有)。觸發條件觸發時,會執行外掛程式資訊清單中 drive.onItemsSelectedTrigger.runFunction
欄位指定的內容相關觸發條件函式。
如要為 Google 雲端硬碟建立內容相關項目選取介面,請完成下列步驟:
確認外掛程式的資訊清單包含
https://www.googleapis.com/auth/drive.addons.metadata.readonly
scope
確認資訊清單包含 drive.onItemsSelectedTrigger
區段。
實作「drive.onItemsSelectedTrigger
」欄位中指定的函式。這個函式會接受 事件物件做為引數,且必須傳回單一 Card
物件或 Card
物件陣列。
與任何資訊卡一樣,您必須實作所有用於提供介面小工具互動的任何回呼函式。舉例來說,如果您在介面中加入按鈕,該按鈕應附加 Action,並實作點選按鈕時執行的回呼函式。
事件物件
系統會建立事件物件,並在呼叫 drive.homepageTrigger
或 drive.onItemsSelectedTrigger
觸發函式時,將該物件傳遞至這些函式。觸發函式可以使用這個事件物件中的資訊,判斷如何建構外掛程式資訊卡,或以其他方式控制外掛程式行為。
如要瞭解事件物件的完整結構,請參閱「事件物件」。如果雲端硬碟是外掛程式的實際主機應用程式,背景事件物件會包含「雲端硬碟事件物件」欄位,其中含有雲端硬碟專屬的用戶端資訊。
項目選取觸發條件的內容相關雲端硬碟事件物件,包含觸發條件觸發時使用者選取的項目資訊。使用者在雲端硬碟中選取多個項目時,其中一個項目會被視為主要感興趣的項目,這個項目稱為「游標所在項目」。
以下範例顯示傳遞至 drive.onItemsSelectedTrigger
函式的 Google 雲端硬碟事件物件:
{
"commonEventObject": { ... },
"drive": {
"activeCursorItem":{
"addonHasFileScopePermission": true,
"id":"0B_sX1fXRRU6Ac3RhcnRlcl9maWxl",
"iconUrl": "https://drive-thirdparty.googleusercontent.com...",
"mimeType":"application/pdf",
"title":"How to get started with Drive"
},
"selectedItems": [
{
"addonHasFileScopePermission": true,
"id":"0B_sX1fXRRU6Ac3RhcnRlcl9maWxl",
"iconUrl":"https://drive-thirdparty.googleusercontent.com...",
"mimeType":"application/pdf",
"title":"How to get started with Drive"
},
...
]
},
...
}
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-31 (世界標準時間)。
[null,null,["上次更新時間:2025-07-31 (世界標準時間)。"],[[["\u003cp\u003eGoogle Workspace add-ons extend Google Drive functionality by offering custom interfaces for automation, insights, and third-party integrations.\u003c/p\u003e\n"],["\u003cp\u003eAdd-ons can provide a homepage within Google Drive and a contextual interface that appears when Drive items are selected.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers build these interfaces using Apps Script, defining triggers and functions in the add-on's manifest file.\u003c/p\u003e\n"],["\u003cp\u003eAdd-ons utilize event objects, including Drive-specific data, to tailor their behavior based on user interactions and selected items.\u003c/p\u003e\n"],["\u003cp\u003eWhen multiple items are selected in Google Drive, add-ons can access both the active cursor item and the full list of selected items for context.\u003c/p\u003e\n"]]],["Google Workspace add-ons enhance Google Drive with custom interfaces, triggered by user actions. Add-ons can display a homepage when the icon is clicked or a contextual interface upon item selection. Developers define these interfaces by implementing `homepageTrigger` or `onItemsSelectedTrigger` functions in the add-on's manifest. These functions build and return card-based UIs. Contextual triggers utilize event objects containing information about selected items, including an active cursor item for single-item interactions, to tailor the add-on's behavior.\n"],null,["# Building Google Drive interfaces\n\nGoogle Workspace add-ons can provide customized\ninterfaces when the user is using Google Drive. This lets you provide the user\nwith additional relevant information, automate tasks, and connect third-party\nsystems to Google Drive.\n\nAccessing the add-on UI\n-----------------------\n\nYou can open a Google Workspace add-on in\nGoogle Drive if its icon appears in the\nicon column on the right side of the Drive user interface. A\nGoogle Workspace add-on can\ndefine a homepage interface, an item select interface, or both:\n\n- If a user clicks the add-on icon while in Google Drive, the add-on executes the corresponding [`drive.homepageTrigger`](/apps-script/manifest/drive-addons#Drive.FIELDS.homepageTrigger) function (if present). This function builds and returns a [homepage card](/workspace/add-ons/concepts/homepages) to Google Drive for display. If no [`drive.homepageTrigger`](/apps-script/manifest/drive-addons#Drive.FIELDS.homepageTrigger) function is defined, a generic homepage card is displayed instead.\n- If the user selects one or more items in Google Drive and then clicks the add-on icon, or selects items while the add-on is open, the add-on executes the corresponding [`drive.onItemsSelectedTrigger`](/apps-script/manifest/drive-addons#Drive.FIELDS.onItemsSelectedTrigger) contextual function (if present). This function builds the add-on's Google Drive [contextual \"items selected\" interface](#drive_contextual_interface_for_items_selected) and returns it to Google Drive for display.\n\nBuilding the add-on Drive interface\n-----------------------------------\n\nYou can build interfaces that extend Google Drive by following these general\nsteps:\n\n1. Decide whether you want your add-on to have a Drive-specific [homepage](/workspace/add-ons/concepts/homepages). Also decide if you want to provide a contextual interface for when the user selects Drive items.\n2. Add the appropriate [`addOns.common`](/apps-script/manifest/addons#AddOns.FIELDS.common) and [`addOns.drive`](/apps-script/manifest/addons#AddOns.FIELDS.drive) fields to the add-on script project [manifest](/workspace/add-ons/concepts/workspace-manifests), including any [Drive scopes](/workspace/add-ons/concepts/workspace-scopes#drive_scopes) required.\n3. If you are providing a [Drive-specific homepage](#drive_homepages), implement the [`drive.homepageTrigger`](/apps-script/manifest/drive-addons#Drive.FIELDS.homepageTrigger) function to build this interface. You can also choose to use the [`common.homepageTrigger`](/apps-script/manifest/addons#Common.FIELDS.homepageTrigger) interface for multiple Google Workspace hosts.\n4. If you are providing a Drive contextual item selection interface, you must implement a [`drive.onItemsSelectedTrigger`](/apps-script/manifest/drive-addons#Drive.FIELDS.onItemsSelectedTrigger) contextual trigger function to build this interface. See [Drive contextual interface for items selected](#drive_contextual_interface_for_items_selected)) for details.\n5. Implement the associated callback functions needed to respond to the user's UI interactions, such as button clicks.\n\n### Drive homepages\n\nGoogle Drive supports displaying Google Workspace add-on\n[homepages](/workspace/add-ons/concepts/homepages).\nTo show your add-on's common homepage in Google Drive simply make sure there\nis a [`addOns.drive`](/apps-script/manifest/addons#AddOns.FIELDS.drive) field\nin the add-on's manifest.\n\nAlternatively, add a\n[`drive.homepageTrigger`](/apps-script/manifest/drive-addons#Drive.FIELDS.homepageTrigger)\nto the add-on manifest to provide a Drive-specific homepage.\n\nIn either case, you must provide the name of a homepage trigger function in your\nadd-on's script project. This function is automatically called to build the\nDrive homepage when it is needed. You must implement this function to build and\nreturn a single\n[`Card`](/apps-script/reference/card-service/card) or an array of\n[`Card`](/apps-script/reference/card-service/card)\nobjects that make up the homepage. The homepage trigger function is passed an\n[event object](/workspace/add-ons/concepts/event-objects)\nas a parameter that contains some general information such as the client's\nplatform. You can use the event object data to tailor the construction of the\nhomepage.\n\n### Drive contextual interface for items selected\n\nGoogle Drive relies on a contextual trigger to determine what interface\n(if any) to display when the user selects one or more Google Drive items. When the\ntrigger fires, it executes the contextual trigger function specified by the\n[`drive.onItemsSelectedTrigger.runFunction`](/apps-script/manifest/drive-addons#OnItemsSelectedTrigger.FIELDS.runFunction)\nfield in the add-on [manifest](/workspace/add-ons/concepts/workspace-manifests).\n\nTo create a contextual item selection interface for Drive, you must do the\nfollowing:\n\n1. Ensure the add-on's manifest includes the\n `https://www.googleapis.com/auth/drive.addons.metadata.readonly`\n [scope](/workspace/add-ons/concepts/workspace-scopes#drive_scopes)\n\n2. Ensure the manifest includes a\n [`drive.onItemsSelectedTrigger`](/apps-script/manifest/drive-addons#Drive.FIELDS.onItemsSelectedTrigger)\n section.\n\n3. Implement the function named in the\n [`drive.onItemsSelectedTrigger`](/apps-script/manifest/drive-addons#Drive.FIELDS.onItemsSelectedTrigger)\n field.\n This function accepts an [event object](#event_objects) as an argument\n and must return either a single\n [`Card`](/apps-script/reference/card-service/card) object or an array of\n [`Card`](/apps-script/reference/card-service/card) objects.\n\n4. As with any card, you must implement any callback functions used to provide\n widget interactivity for the interface. For example, if you include a button\n in the interface, it should have an attached\n [Action](/apps-script/reference/card-service/action)\n and an implemented callback function that run when the button is clicked.\n\n### Event objects\n\nAn [event object](/workspace/add-ons/concepts/event-objects) is created and passed\nto the [`drive.homepageTrigger`](/apps-script/manifest/drive-addons#Drive.FIELDS.homepageTrigger)\nor [`drive.onItemsSelectedTrigger`](/apps-script/manifest/drive-addons#Drive.FIELDS.onItemsSelectedTrigger)\ntrigger function when those functions are called. The trigger function can use\nthe information in this event object to determine how to construct add-on cards\nor otherwise control the add-on behavior.\n\nThe full structure of event objects is described in\n[Event objects](/workspace/add-ons/concepts/event-objects).\nWhen Drive is the acting host app of the add-on, contextual event objects\ninclude the\n[Drive event object](/workspace/add-ons/concepts/event-objects#drive_event_object)\nfield that carries Drive-specific client information.\n\nContextual Drive event objects for item selection triggers include information\nabout the items the user has selected when the trigger fires. When a user\nselects more than one item in Drive, one of the items is deemed to be one of\nprimary interest; this item is referred to as the *active cursor item*.\n| **Note:** If the add-on's behavior is meant to apply to multiple selected items, use the information provided in the [`drive.selectedItems`](/workspace/add-ons/concepts/event-objects#drive_event_object) array in the event object to identify them all. \n|\n| When an add-on's behavior should only be applied to a single selected item, use the information provided in the [`drive.activeCursorItem`](/workspace/add-ons/concepts/event-objects#drive_event_object) field of the event object to identify the item out of the full selection. Don't attempt to infer which item to use from the [`drive.selectedItems`](/workspace/add-ons/concepts/event-objects#drive_event_object) array.\n\nThe following example shows a\n[Drive event object](/workspace/add-ons/concepts/event-objects#drive_event_object)\nthat is passed to a\n[`drive.onItemsSelectedTrigger`](/apps-script/manifest/drive-addons#Drive.FIELDS.onItemsSelectedTrigger)\nfunction: \n\n {\n \"commonEventObject\": { ... },\n \"drive\": {\n \"activeCursorItem\":{\n \"addonHasFileScopePermission\": true,\n \"id\":\"0B_sX1fXRRU6Ac3RhcnRlcl9maWxl\",\n \"iconUrl\": \"https://drive-thirdparty.googleusercontent.com...\",\n \"mimeType\":\"application/pdf\",\n \"title\":\"How to get started with Drive\"\n },\n \"selectedItems\": [\n {\n \"addonHasFileScopePermission\": true,\n \"id\":\"0B_sX1fXRRU6Ac3RhcnRlcl9maWxl\",\n \"iconUrl\":\"https://drive-thirdparty.googleusercontent.com...\",\n \"mimeType\":\"application/pdf\",\n \"title\":\"How to get started with Drive\"\n },\n ...\n ]\n },\n ...\n }"]]