編集者のアクション

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

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

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

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

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

クリックしたときに新しいカードを作成して表示するボタンをアドオンに追加する場合は、次の手順を行います。

  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

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

  • このアドオンは 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();
}