カレンダーの操作

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

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

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

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

カレンダーの操作

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

試行された操作 コールバック関数は
参加者を追加する CalendarEventActionResponse
会議データを設定する CalendarEventActionResponse
添付ファイルの追加 CalendarEventActionResponse

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

  • ユーザーがカレンダーの予定を開いている間、アクションがトリガーされます。
  • アドオンの addOns.calendar.currentEventAccess マニフェスト フィールドが WRITE または READ_WRITE に設定されている。
  • このアドオンには、https://www.googleapis.com/auth/calendar.addons.current.event.write カレンダーのスコープが含まれています。

また、アクション コールバック関数による変更は、ユーザーがカレンダーの予定を保存するまで保存されません。

コールバック関数を使用して参加者を追加する

次の例は、編集対象のカレンダーの予定に特定の参加者を追加するボタンを作成する方法を示しています。

  /**
   * Build a simple card with a button that sends a notification.
   * This function is called as part of the eventOpenTrigger that builds
   * a UI when the user opens an event.
   *
   * @param e The event object passed to eventOpenTrigger function.
   * @return {Card}
   */
  function buildSimpleCard(e) {
    var buttonAction = CardService.newAction()
        .setFunctionName('onAddAttendeesButtonClicked');
    var button = CardService.newTextButton()
        .setText('Add new attendee')
        .setOnClickAction(buttonAction);

    // Check the event object to determine if the user can add
    // attendees and disable the button if not.
    if (!e.calendar.capabilities.canAddAttendees) {
      button.setDisabled(true);
    }

    // ...continue creating card sections and widgets, then create a Card
    // object to add them to. Return the built Card object.
  }

  /**
   * Callback function for a button action. Adds attendees to the
   * Calendar event being edited.
   *
   * @param {Object} e The action event object.
   * @return {CalendarEventActionResponse}
   */
  function onAddAttendeesButtonClicked (e) {
    return CardService.newCalendarEventActionResponseBuilder()
        .addAttendees(["aiko@example.com", "malcom@example.com"])
        .build();
  }

コールバック関数を使用した会議データの設定

これにより、開かれたイベントに会議データが設定されます。この会議データでは、ユーザーが目的のソリューションを選択したことによってアクションがトリガーされていないため、会議ソリューション ID を指定する必要があります。

次の例は、編集中のイベントの会議データを設定するボタンを作成する方法を示しています。

  /**
   * Build a simple card with a button that sends a notification.
   * This function is called as part of the eventOpenTrigger that builds
   * a UI when the user opens a Calendar event.
   *
   * @param e The event object passed to eventOpenTrigger function.
   * @return {Card}
   */
  function buildSimpleCard(e) {
    var buttonAction = CardService.newAction()
        .setFunctionName('onSaveConferenceOptionsButtonClicked')
        .setParameters(
          {'phone': "1555123467", 'adminEmail': "joyce@example.com"});
    var button = CardService.newTextButton()
        .setText('Add new attendee')
        .setOnClickAction(buttonAction);

    // Check the event object to determine if the user can set
    // conference data and disable the button if not.
    if (!e.calendar.capabilities.canSetConferenceData) {
      button.setDisabled(true);
    }

    // ...continue creating card sections and widgets, then create a Card
    // object to add them to. Return the built Card object.
  }

  /**
   * Callback function for a button action. Sets conference data for the
   * Calendar event being edited.
   *
   * @param {Object} e The action event object.
   * @return {CalendarEventActionResponse}
   */
  function onSaveConferenceOptionsButtonClicked(e) {
    var parameters = e.commonEventObject.parameters;

    // Create an entry point and a conference parameter.
    var phoneEntryPoint = ConferenceDataService.newEntryPoint()
      .setEntryPointType(ConferenceDataService.EntryPointType.PHONE)
      .setUri('tel:' + parameters['phone']);

    var adminEmailParameter = ConferenceDataService.newConferenceParameter()
        .setKey('adminEmail')
        .setValue(parameters['adminEmail']);

    // Create a conference data object to set to this Calendar event.
    var conferenceData = ConferenceDataService.newConferenceDataBuilder()
        .addEntryPoint(phoneEntryPoint)
        .addConferenceParameter(adminEmailParameter)
        .setConferenceSolutionId('myWebScheduledMeeting')
        .build();

    return CardService.newCalendarEventActionResponseBuilder()
        .setConferenceData(conferenceData)
        .build();
  }

コールバック関数を使用して添付ファイルを追加する

次の例は、編集するカレンダーの予定に添付ファイルを追加するボタンを作成する方法を示しています。

  /**
   * Build a simple card with a button that creates a new attachment.
   * This function is called as part of the eventAttachmentTrigger that
   * builds a UI when the user goes through the add-attachments flow.
   *
   * @param e The event object passed to eventAttachmentTrigger function.
   * @return {Card}
   */
  function buildSimpleCard(e) {
    var buttonAction = CardService.newAction()
        .setFunctionName('onAddAttachmentButtonClicked');
    var button = CardService.newTextButton()
        .setText('Add a custom attachment')
        .setOnClickAction(buttonAction);

    // Check the event object to determine if the user can add
    // attachments and disable the button if not.
    if (!e.calendar.capabilities.canAddAttachments) {
      button.setDisabled(true);
    }

    // ...continue creating card sections and widgets, then create a Card
    // object to add them to. Return the built Card object.
  }

  /**
   * Callback function for a button action. Adds attachments to the Calendar
   * event being edited.
   *
   * @param {Object} e The action event object.
   * @return {CalendarEventActionResponse}
   */
  function onAddAttachmentButtonClicked(e) {
    return CardService.newCalendarEventActionResponseBuilder()
             .addAttachments([
               CardService.newAttachment()
                 .setResourceUrl("https://example.com/test")
                 .setTitle("Custom attachment")
                 .setMimeType("text/html")
                 .setIconUrl("https://example.com/test.png")
             ])
        .build();
  }

添付ファイルのアイコンを設定する

添付ファイルのアイコンは、Google のインフラストラクチャでホストされている必要があります。詳しくは、アタッチメント アイコンを指定するをご覧ください。