ユーザー行動を促進する

Action オブジェクトを使用すると、Google Workspace アドオンにインタラクティブな動作を組み込むことができます。ユーザーがアドオン UI のウィジェット(ボタンなど)を操作したときの動作を定義します。

アクションは、ウィジェット ハンドラ関数を使用して特定のウィジェットにアタッチされます。この関数により、アクションをトリガーする条件も定義されます。アクションがトリガーされると、指定されたコールバック関数が実行されます。コールバック関数には、ユーザーのクライアント側操作に関する情報を含むイベント オブジェクトが渡されます。コールバック関数を実装して、特定のレスポンス オブジェクトを返すようにする必要があります。

たとえば、クリック時に新しいカードを作成して表示するボタンがあるとします。そのためには、新しいボタン ウィジェットを作成し、ボタン ウィジェット ハンドラ関数 setOnClickAction(action) を使用して、カード構築の Action を設定する必要があります。定義した Action は、ボタンがクリックされたときに実行される Apps Script コールバック関数を指定します。この場合は、コールバック関数を実装してカードをビルドし、ActionResponse オブジェクトを返します。レスポンス オブジェクトは、コールバック関数が作成したカードを表示するようアドオンに指示します。

このページでは、アドオンに含めることができるドライブ固有のウィジェット アクションについて説明します。

インタラクションの促進

ドライブを拡張する Google Workspace アドオンには、ドライブ固有の追加のウィジェット アクションを含めることができます。このアクションには、特別なレスポンス オブジェクトを返すために、関連するアクションのコールバック関数が必要です。

試行された操作 コールバック関数は
選択したファイルのアクセス権をリクエストする DriveItemsSelectedActionResponse

これらのウィジェット アクションとレスポンス オブジェクトを利用するには、以下の条件をすべて満たしている必要があります。

  • このアクションは、ユーザーが 1 つ以上のドライブ アイテムを選択している間にトリガーされます。
  • このアドオンのマニフェストには https://www.googleapis.com/auth/drive.file ドライブのスコープが含まれています。

選択したファイルのファイル アクセス権をリクエストする

次の例は、ユーザーがドライブの 1 つ以上のアイテムを選択したときにトリガーされる 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;
  }
}