日曆動作

Action 物件可讓您在 Google Workspace 外掛程式中建構互動式行為。這些函式會定義使用者與外掛程式 UI 中的小工具 (例如按鈕) 互動時發生的情況。

動作會使用小工具處理常式函式附加至指定小工具,該函式也會定義觸發動作的條件。觸發時,動作會執行指定的回呼函式。回呼函式會收到 event 物件,其中包含使用者在用戶端互動的相關資訊。您必須實作回呼函式,並讓該函式傳回特定回應物件。

舉例來說,假設您想要在點選按鈕時建構並顯示新資訊卡,為此,您必須建立新的按鈕小工具,並使用按鈕小工具處理常式函式 setOnClickAction(action) 設定建構資訊卡 Action。您定義的 Action 會指定在點選按鈕時執行的 Apps Script 回呼函式。在這種情況下,您會實作回呼函式來建構所需資訊卡,並傳回 ActionResponse 物件。回應物件會指示外掛程式顯示回呼函式建構的資訊卡。

本頁面說明可加入外掛程式的日曆專屬小工具動作。

Google 日曆互動

擴充 Google 日曆的 Google Workspace 外掛程式可以包含其他日曆專屬的小工具動作。如要執行這些動作,相關動作的回呼函式必須傳回專屬的回應物件:

嘗試執行的動作 回呼函式應會傳回
新增參與者 CalendarEventActionResponse
設定會議資料 CalendarEventActionResponse
新增附件 CalendarEventActionResponse

如要使用這些小工具動作和回應物件,必須符合下列所有條件:

  • 使用者開啟日曆活動時,系統會觸發這項動作。
  • Google Workspace 外掛程式的資訊清單欄位設為 WRITEREAD_WRITEaddOns.calendar.currentEventAccess
  • 外掛程式包含 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 基礎架構上。詳情請參閱「提供附件圖示」。