日曆動作

Action 物件可讓您建構互動式 行為嵌入 Google Workspace 外掛程式會定義 使用者在 外掛程式 UI

您可以使用 小工具處理常式函式, 這也會定義觸發動作的條件。觸發後 就會執行 回呼函式。 回呼函式會經由 事件物件, 使用者的用戶端互動相關資訊。您必須將 回呼函式並使其傳回特定回應物件。

舉例來說,假設您想新增一個按鈕,並在 點擊。為此,您必須建立新的按鈕小工具,並使用按鈕小工具 處理常式函式 setOnClickAction(action)敬上 設定卡片建構 Action。 您定義的 Action 指定了 Apps Script 使用者點選按鈕時執行的回呼函式。在這個範例中 實作回呼函式來建構所需卡片,並傳回 ActionResponse敬上 物件。回應物件會指示外掛程式顯示回呼 函式

本頁說明您可以在 外掛程式。

日曆互動情形

擴充 Google 日曆的 Google Workspace 外掛程式 則可加入一些額外的 Google 日曆小工具動作。這些動作 需要相關動作回呼函式 傳回專門的回應物件:

已嘗試操作 回呼函式應傳回
新增參與者 CalendarEventActionResponse
設定會議資料 CalendarEventActionResponse
新增附件 CalendarEventActionResponse

如要使用這些小工具動作和回應物件,請執行下列所有項目 須為 true:

  • 使用者開啟日曆活動時,就會觸發動作。
  • 外掛程式的 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,因此必須指定會議解決方案 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 的基礎架構代管。請參閱提供 附件圖示