雲端硬碟操作

Action 物件可讓您建構互動式 行為嵌入 Google Workspace 外掛程式會定義 使用者在 外掛程式 UI

您可以使用 小工具處理常式函式, 這也會定義觸發動作的條件。觸發後 就會執行 回呼函式。 回呼函式會經由 事件物件, 使用者的用戶端互動相關資訊。您必須將 回呼函式並使其傳回特定回應物件。

舉例來說,假設您想新增一個按鈕,並在 點擊。為此,您必須建立新的按鈕小工具,並使用按鈕小工具 處理常式函式 setOnClickAction(action)敬上 設定卡片建構 Action。 您定義的 Action 指定了 Apps Script 使用者點選按鈕時執行的回呼函式。在這個範例中 實作回呼函式來建構所需卡片,並傳回 ActionResponse敬上 物件。回應物件會指示外掛程式顯示回呼 函式

本頁說明雲端硬碟專屬的小工具動作,可加入下列元素: 外掛程式。

提升互動率

擴充雲端硬碟的 Google Workspace 外掛程式包括 額外的雲端硬碟專用小工具動作。這項操作需要 動作回呼函式 傳回特殊的回應物件:

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

如要使用這些小工具動作和回應物件,請執行下列所有項目 須為 true:

  • 使用者選取一或多個雲端硬碟項目時,就會觸發動作。
  • 這個外掛程式包含 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;
  }
}