エディタのアクション

Action オブジェクトを使用してインタラクティブな Google Workspace アドオンに組み込まれます。

アクション オブジェクトでは、ユーザーがウィジェットを操作したときの動作を定義します。 (ボタンなど)。

ウィジェットにアクションを追加する

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

例: ボタンがクリックされたときに新しいカードを表示する

新しいカードをビルドして表示するボタンをアドオンに追加する場合 クリックされたら、次の手順を行います。

  1. ボタン ウィジェットを作成します。
  2. カード作成アクションを設定するには、ボタン ウィジェット ハンドラ関数を追加します。 setOnClickAction(action)
  3. 実行する Apps Script コールバック関数を作成し、 (action) ウィジェットハンドラ関数内で使用できますこの場合、 必要なカードを作成し、ActionResponse オブジェクトを返します。レスポンス オブジェクトは、コールバック関数が作成したカードを表示するようアドオンに指示します。

次の例は、ボタン ウィジェットの作成方法を示しています。このアクションでは、 アドオンの代わりに現在のファイルの drive.file スコープ。

/**
 * Adds a section to the Card Builder that displays a "REQUEST PERMISSION" button.
 * When it's clicked, the callback triggers file scope permission flow. This is used in
 * the add-on when the home-page displays basic data.
 */
function addRequestFileScopeButtonToBuilder(cardBuilder) {
    var buttonSection = CardService.newCardSection();
    // 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.
    var buttonAction = CardService.newAction()
      .setFunctionName("onRequestFileScopeButtonClickedInEditor");

    var button = CardService.newTextButton()
      .setText("Request permission")
      .setBackgroundColor("#4285f4")
      .setTextButtonStyle(CardService.TextButtonStyle.FILLED)
      .setOnClickAction(buttonAction);

    buttonSection.addWidget(button);
    cardBuilder.addSection(buttonSection);
}

/**
 * Callback function for a button action. Instructs Docs to display a
 * permissions dialog to the user, requesting `drive.file` scope for the 
 * current file on behalf of this add-on.
 *
 * @param {Object} e The parameters object that contains the document’s ID
 * @return {editorFileScopeActionResponse}
 */
function onRequestFileScopeButtonClickedInEditor(e) {
  return CardService.newEditorFileScopeActionResponseBuilder()
      .requestFileScopeForActiveDocument().build();

REST API のファイル アクセス インタラクション

エディタを拡張し、REST API を使用する Google Workspace アドオンには、 ファイルへのアクセスをリクエストする追加のウィジェット アクション。この操作に必要なのは、 関連するアクション コールバック関数を呼び出して、特別なレスポンス オブジェクトを返します。

アクションを試行しました コールバック関数は
current_document のファイル アクセス権をリクエストする EditorFileScopeActionResponse

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

  • このアドオンは REST API を使用します。
  • アドオンはリクエスト ファイルのスコープ ダイアログを表示 CardService.newEditorFileScopeActionResponseBuilder().requestFileScopeForActiveDocument().build(); メソッドを使用します。
  • このアドオンには https://www.googleapis.com/auth/drive.file エディタのスコープと マニフェストで onFileScopeGranted トリガーを指定します。

現在のドキュメントのファイル アクセス権をリクエストする

現在のドキュメントのファイル アクセス権をリクエストする手順は次のとおりです。

  1. アドオンに drive.file スコープがあるかどうかを確認するホームページ カードを作成します。
  2. アドオンに drive.file スコープが付与されていない場合は、 現在のドキュメントに drive.file スコープを付与するようユーザーにリクエストする方法。

例: Google ドキュメントで現在のドキュメントへのアクセス権を取得する

次のサンプルでは、Google ドキュメント用のインターフェースを作成し、サイズ 編集することもできます。アドオンに drive.file の承認がない場合は、 ファイル スコープの承認を開始するボタンが表示されます。

/**
 * 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 information about the
 *   current document.
 * @return {Card}
 */
function onDocsHomepage(e) {
  return createAddOnView(e);
}

function onFileScopeGranted(e) {
  return createAddOnView(e);
}

/**
 * For the current document, display either its quota information or
 * a button that allows the user to provide permission to access that
 * file to retrieve its quota details.
 *
 * @param e The event containing information about the current document
 * @return {Card}
 */
function createAddOnView(e) {
  var docsEventObject = e['docs'];
  var builder =  CardService.newCardBuilder();

  var cardSection = CardService.newCardSection();
  if (docsEventObject['addonHasFileScopePermission']) {
    cardSection.setHeader(docsEventObject['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 the add-on has access permission, read and display its quota.
    cardSection.addWidget(
      CardService.newTextParagraph().setText(
          "This file takes up: " + getQuotaBytesUsed(docsEventObject['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");

    var button = CardService.newTextButton()
      .setText("Request permission")
      .setOnClickAction(buttonAction);

    cardSection.addWidget(button);
  }
  return builder.addSection(cardSection).build();
}

/**
 * Callback function for a button action. Instructs Docs to display a
 * permissions dialog to the user, requesting `drive.file` scope for the
 * current file on behalf of this add-on.
 *
 * @param {Object} e The parameters object that contains the document’s ID
 * @return {editorFileScopeActionResponse}
 */
function onRequestFileScopeButtonClicked(e) {
  return CardService.newEditorFileScopeActionResponseBuilder()
      .requestFileScopeForActiveDocument().build();
}