行動を促す

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

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

たとえば、必要なときに新しいカードを作成して表示するボタンを作成するとします。 クリックします。そのためには、新しいボタン ウィジェットを作成し、そのボタン ウィジェットを ハンドラ関数 setOnClickAction(action) カード作成 Action を設定します。「 定義した Action で Apps Script を指定 ボタンがクリックされたときに実行されるコールバック関数です。今回は コールバック関数を実装して必要なカードを作成し、 ActionResponse 渡されます。応答オブジェクトはアドオンに対し、コールバックをカードに表示するよう指示します。 構築しました。

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

インタラクションの促進

ドライブを拡張する Google Workspace アドオンには、 追加でドライブ固有のウィジェット アクションを実行できます。この操作を行うには、 アクション コールバック関数 特殊なレスポンス オブジェクトを返します。

アクションを試行しました コールバック関数は
選択したファイルに対するアクセス権をリクエストする DriveItemsSelectedActionResponse

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

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

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

次の例は、Google Cloud のコンテキストに応じたインターフェースを構築する方法を示しています。 ユーザーが 1 つ以上のドライブ アイテムを選択したときにトリガーされるドライブ。「 この例では、各アイテムをテストして、アドオンにアクセス権が付与されているかどうかを確認します。 そうでない場合は、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;
  }
}