Tindakan kalender

Objek Action memungkinkan Anda mem-build perilaku interaktif ke dalam Add-on Google Workspace. Parameter ini menentukan hal yang terjadi saat pengguna berinteraksi dengan widget (misalnya, tombol) di UI add-on.

Tindakan dikaitkan ke widget tertentu menggunakan fungsi pengendali widget, yang juga menentukan kondisi yang memicu tindakan. Saat dipicu, tindakan akan menjalankan fungsi callback yang ditetapkan. Fungsi callback diberi objek peristiwa yang membawa informasi tentang interaksi sisi klien pengguna. Anda harus menerapkan fungsi callback dan memintanya menampilkan objek respons tertentu.

Misalnya, Anda menginginkan tombol yang mem-build dan menampilkan kartu baru saat diklik. Untuk melakukannya, Anda harus membuat widget tombol baru dan menggunakan fungsi pengendali widget tombol setOnClickAction(action) untuk menyetel pembuatan kartu Action. Action yang Anda tentukan menentukan fungsi callback Apps Script yang dijalankan saat tombol diklik. Dalam hal ini, Anda akan menerapkan fungsi callback untuk membuat kartu yang diinginkan dan menampilkan objek ActionResponse. Objek respons memberi tahu add-on untuk menampilkan kartu fungsi callback yang dibuat.

Halaman ini menjelaskan tindakan widget khusus Kalender yang dapat Anda sertakan dalam add-on.

Interaksi kalender

Add-on Google Workspace yang memperluas Kalender dapat menyertakan beberapa tindakan widget tambahan khusus Kalender. Tindakan ini memerlukan fungsi callback tindakan terkait untuk menampilkan objek respons khusus:

Tindakan dicoba Fungsi callback akan menampilkan
Menambahkan tamu CalendarEventActionResponse
Menyetel data konferensi CalendarEventActionResponse
Menambahkan lampiran CalendarEventActionResponse

Untuk menggunakan tindakan widget dan objek respons ini, semua hal berikut harus terpenuhi:

  • Tindakan ini akan dipicu saat pengguna membuka acara Kalender.
  • Kolom manifes addOns.calendar.currentEventAccess add-on ditetapkan ke WRITE atau READ_WRITE.
  • Add-on tersebut menyertakan https://www.googleapis.com/auth/calendar.addons.current.event.write Cakupan Kalender.

Selain itu, setiap perubahan yang dibuat oleh fungsi callback tindakan tidak akan disimpan hingga pengguna menyimpan acara Kalender.

Menambahkan tamu dengan fungsi callback

Contoh berikut menunjukkan cara membuat tombol yang menambahkan peserta tertentu ke acara Kalender yang sedang diedit:

  /**
   * 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();
  }

Menyetel data konferensi dengan fungsi callback

Tindakan ini menetapkan data konferensi pada peristiwa terbuka. Untuk data konferensi ini, ID solusi konferensi harus ditentukan, karena tindakan tidak dipicu oleh pengguna yang memilih solusi yang diinginkan.

Contoh berikut menunjukkan cara membuat tombol yang menetapkan data konferensi untuk peristiwa yang sedang diedit:

  /**
   * 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();
  }

Menambahkan lampiran dengan fungsi callback

Contoh berikut menunjukkan cara membuat tombol yang menambahkan lampiran ke acara Kalender yang sedang diedit:

  /**
   * 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();
  }

Menyetel ikon lampiran

Ikon lampiran harus dihosting di infrastruktur Google. Lihat Menyediakan ikon lampiran untuk mengetahui detailnya.