カレンダーの操作

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

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

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

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

Google カレンダーのインタラクション

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

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

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

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

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

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

次の例は、編集中のカレンダー イベントに特定の参加者を追加するボタンを作成する方法を示しています。この例では、基本的なカード構造を使用しています。

/**
 * Build a basic 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 basic 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 basic 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 のインフラストラクチャでホストする必要があります。詳しくは、添付ファイルのアイコンを提供するをご覧ください。