雲端硬碟操作

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

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

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

本頁面說明可在外掛程式中加入的 Drive 專屬小工具動作。

提高互動

擴充雲端硬碟功能的 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;
  }
}