鼓勵採取行動

Action 物件可讓您在 Google Workspace 外掛程式中建構互動式行為。可定義使用者與外掛程式 UI 中的小工具 (例如按鈕) 互動時會發生的情況。

動作會使用小工具處理常式函式附加至特定小工具,該函式也會定義觸發動作的條件。觸發之後,該動作會執行指定的回呼函式。回呼函式會傳遞包含使用者用戶端互動資訊的事件物件。您必須實作回呼函式,並讓函式傳回特定的回應物件。

舉例來說,假設您想要一個按鈕,當使用者點選該按鈕時,該按鈕便會建構並顯示新資訊卡。為此,您必須建立新的按鈕小工具,並使用按鈕小工具處理常式函式 setOnClickAction(action) 設定卡片建構 Action。您定義的 Action 會指定在點選按鈕時執行的 Apps Script 回呼函式。在這種情況下,您可以實作回呼函式來建構所需資訊卡,並傳回 ActionResponse 物件。回應物件會指示外掛程式顯示建構的回呼函式。

本頁說明您可以加入外掛程式中的雲端硬碟專屬小工具動作。

提高互動次數

擴充雲端硬碟的 Google Workspace 外掛程式可以納入額外的雲端硬碟專屬小工具動作。這個動作需要相關的動作回呼函式傳回特殊回應物件:

已嘗試執行動作 回呼函式應會傳回
要求存取所選檔案的檔案 DriveItemsSelectedActionResponse

如要使用這些小工具動作和回應物件,必須符合以下所有條件:

  • 使用者選取一或多個雲端硬碟項目時,就會觸發動作。
  • 外掛程式的資訊清單中包含 https://www.googleapis.com/auth/drive.file 雲端硬碟範圍

要求存取所選檔案的檔案存取權

以下範例說明如何建構 Google 雲端硬碟的結構定義介面,在使用者選取一或多個雲端硬碟項目時觸發。範例會測試每個項目,確認外掛程式是否已獲得存取權限;如果沒有,它使用 DriveItemsSelectedActionResponse 物件要求使用者提供權限。項目授予權限後,外掛程式就會顯示該項目的雲端硬碟配額用量。

/**
 * Build a simple 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 contextual information about
 *    the Drive items selected.
 * @return {Card}
 */
function onDriveItemsSelected(e) {
  var builder =  CardService.newCardBuilder();

  // For each item the user has selected in Drive, display either its
  // quota information or a button that allows the user to provide
  // permission to access that file to retrieve its quota details.
  e['drive']['selectedItems'].forEach(
    function(item){
      var cardSection = CardService.newCardSection()
          .setHeader(item['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 (item['addonHasFileScopePermission']) {
        // If the add-on has access permission, read and display its
        // quota.
        cardSection.addWidget(
          CardService.newTextParagraph().setText(
              "This file takes up: " + getQuotaBytesUsed(item['id'])));
      } else {
        // If the add-on does not have access permission, add a button
        // that allows the user to 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")
          .setParameters({id: item.id});

        var button = CardService.newTextButton()
          .setText("Request permission")
          .setOnClickAction(buttonAction);

        cardSection.addWidget(button);
      }

      builder.addSection(cardSection);
    });

  return builder.build();
}

/**
 * Callback function for a button action. Instructs Drive to display a
 * permissions dialog to the user, requesting `drive.file` scope for a
 * specific item on behalf of this add-on.
 *
 * @param {Object} e The parameters object that contains the item's
 *   Drive ID.
 * @return {DriveItemsSelectedActionResponse}
 */
function onRequestFileScopeButtonClicked (e) {
  var idToRequest = e.parameters.id;
  return CardService.newDriveItemsSelectedActionResponseBuilder()
      .requestFileScope(idToRequest).build();
}

/**
 * Use the Advanced Drive Service
 * (See https://developers.google.com/apps-script/advanced/drive),
 * with `drive.file` scope permissions to request the quota usage of a
 * specific Drive item.
 *
 * @param {string} itemId The ID of the item to check.
 * @return {string} A description of the item's quota usage, in bytes.
 */
function getQuotaBytesUsed(itemId) {
  try {
    return Drive.Files.get(itemId,{fields: "quotaBytesUsed"})
        .quotaBytesUsed + " bytes";
  } catch (e) {
    return "Error fetching how much quota this item uses. Error: " + e;
  }
}