行動を促す

Action オブジェクトを使用すると、インタラクティブな Google Workspace アドオンに組み込まれます。定義 ユーザーが Google Chat でウィジェット(ボタンなど)を 表示されます。

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

たとえば、必要なときに新しいカードを作成して表示するボタンを作成するとします。 クリックします。そのためには、新しいボタン ウィジェットを作成し、そのボタン ウィジェットを ハンドラ関数 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;
  }
}