使用 Action 物件,在 Google Workspace 外掛程式中建構互動式行為。
動作物件會定義使用者與外掛程式 UI 中的小工具 (例如按鈕) 互動時發生的情況。
在小工具中新增動作
如要將動作附加至小工具,請使用小工具處理常式函式,這個函式也會定義觸發動作的條件。觸發時,動作會執行指定的回呼函式。回呼函式會收到 event 物件,其中包含使用者在用戶端互動的相關資訊。您必須實作回呼函式,並讓該函式傳回特定回應物件。
範例:在點選按鈕時顯示新資訊卡
如要在外掛程式中新增按鈕,點選後可建立及顯示新資訊卡,請按照下列步驟操作:
- 建立按鈕 小工具。
- 如要設定卡片建構動作,請新增按鈕小工具處理常式函式
setOnClickAction。 - 建立要執行的 Apps Script 回呼函式,並在小工具處理常式函式中指定為
action。在本例中,回呼函式應建構所需資訊卡,並傳回ActionResponse物件。回應物件會指示外掛程式顯示回呼函式建立的資訊卡。
以下範例顯示如何建立按鈕小工具。這項動作會代表外掛程式,要求目前檔案的 drive.file 範圍。
/**
* Adds a section to the Card Builder that displays a "REQUEST PERMISSION"
* button. When it's clicked, the callback triggers file scope permission flow.
* This is used in the add-on when the home-page displays basic data.
*/
function addRequestFileScopeButtonToBuilder(cardBuilder) {
var buttonSection = CardService.newCardSection();
// If the add-on does not have access permission, add a button that
// lets the user provide that permission on a per-file basis.
var buttonAction = CardService.newAction()
.setFunctionName("onRequestFileScopeButtonClickedInEditor");
var button = CardService.newTextButton()
.setText("Request permission")
.setBackgroundColor("#4285f4")
.setTextButtonStyle(CardService.TextButtonStyle.FILLED)
.setOnClickAction(buttonAction);
buttonSection.addWidget(button);
cardBuilder.addSection(buttonSection);
}
/**
* Callback function for a button action. Instructs Docs to display a
* permissions dialog to the user, requesting `drive.file` scope for the
* current file on behalf of this add-on.
*
* @param {Object} e The parameters object that contains the document's ID
* @return {editorFileScopeActionResponse}
*/
function onRequestFileScopeButtonClickedInEditor(e) {
return CardService.newEditorFileScopeActionResponseBuilder()
.requestFileScopeForActiveDocument().build();
}
REST API 的檔案存取互動
擴充編輯器並使用 REST API 的 Google Workspace 外掛程式,可以加入額外的 widget 動作來要求檔案存取權。這項動作需要相關聯的動作回呼函式傳回專門的回應物件:
| 嘗試執行的動作 | 回呼函式應會傳回 |
|---|---|
| 要求檔案存取權 | EditorFileScopeActionResponse |
如要使用這個小工具動作和回應物件,必須符合下列所有條件:
- 外掛程式使用 REST API。
- 外掛程式會使用
CardService.newEditorFileScopeActionResponseBuilder().requestFileScopeForActiveDocument().build();方法,顯示要求檔案範圍的對話方塊。 - 外掛程式的資訊清單包含
https://www.googleapis.com/auth/drive.file編輯器範圍和onFileScopeGranted觸發條件。
要求存取目前文件
如要要求存取目前文件的檔案,請按照下列步驟操作:
- 建立首頁資訊卡,檢查外掛程式是否具有
drive.file範圍。 - 如果外掛程式尚未獲得
drive.file範圍,請建立要求使用者授予目前文件drive.file範圍的方法。
範例:在 Google 文件中取得目前文件的存取權
以下範例會為 Google 文件建構介面,顯示目前文件的大小。如果外掛程式沒有 drive.file 授權,系統會顯示按鈕,以便啟動檔案範圍授權。
/**
* Build a card that checks selected items' quota usage. Checking
* quota usage requires user-permissions, so this add-on provides a button
* to request `drive.file` scope for items the add-on doesn't yet have
* permission to access.
*
* @param e The event object passed containing information about the
* current document.
* @return {Card}
*/
function onDocsHomepage(e) {
return createAddOnView(e);
}
function onFileScopeGranted(e) {
return createAddOnView(e);
}
/**
* For the current document, display either its quota information or
* a button that lets the user provide permission to access that
* file to retrieve its quota details.
*
* @param e The event containing information about the current document
* @return {Card}
*/
function createAddOnView(e) {
var docsEventObject = e['docs'];
var builder = CardService.newCardBuilder();
var cardSection = CardService.newCardSection();
if (docsEventObject['addonHasFileScopePermission']) {
cardSection.setHeader(docsEventObject['title']);
// This add-on uses the recommended, limited-permission `drive.file`
// scope to get granular per-file access permissions.
// See: https://developers.google.com/drive/api/v2/about-auth
// If the add-on has access permission, read and display its quota.
cardSection.addWidget(
CardService.newTextParagraph().setText(
"This file takes up: " +
getQuotaBytesUsed(docsEventObject['id'])));
} else {
// If the add-on does not have access permission, add a button that
// lets the user provide that permission on a per-file basis.
cardSection.addWidget(
CardService.newTextParagraph().setText(
"The add-on needs permission to access this file's quota."));
var buttonAction = CardService.newAction()
.setFunctionName("onRequestFileScopeButtonClicked");
var button = CardService.newTextButton()
.setText("Request permission")
.setOnClickAction(buttonAction);
cardSection.addWidget(button);
}
return builder.addSection(cardSection).build();
}
/**
* Callback function for a button action. Instructs Docs to
* display a permissions dialog to the user, requesting `drive.file` scope for
* the current file on behalf of this add-on.
*
* @param {Object} e The parameters object that contains the document's ID
* @return {editorFileScopeActionResponse}
*/
function onRequestFileScopeButtonClicked(e) {
return CardService.newEditorFileScopeActionResponseBuilder()
.requestFileScopeForActiveDocument().build();
}