Thao tác trên lịch

Đối tượng Action cho phép bạn xây dựng hành vi tương tác vào Tiện ích bổ sung của Google Workspace. Chúng xác định điều gì sẽ xảy ra khi người dùng tương tác với một tiện ích (ví dụ: một nút) trong giao diện người dùng của tiện ích bổ sung.

Một thao tác được đính kèm vào một tiện ích nhất định bằng cách sử dụng hàm trình xử lý tiện ích. Hàm này cũng xác định điều kiện kích hoạt thao tác đó. Khi được kích hoạt, hành động sẽ thực thi một hàm gọi lại được chỉ định. Hàm callback được truyền một đối tượng sự kiện chứa thông tin về hoạt động tương tác phía máy khách của người dùng. Bạn phải triển khai hàm callback và yêu cầu hàm này trả về một đối tượng phản hồi cụ thể.

Ví dụ: giả sử bạn muốn một nút tạo và hiển thị thẻ mới khi được nhấp vào. Để làm được điều này, bạn phải tạo một tiện ích nút mới và sử dụng hàm xử lý tiện ích nút setOnClickAction(action) để đặt Action tạo thẻ. Action mà bạn đã xác định sẽ chỉ định hàm callback Apps Script sẽ thực thi khi người dùng nhấp vào nút này. Trong trường hợp này, bạn sẽ triển khai hàm callback để tạo thẻ bạn muốn và trả về một đối tượng ActionResponse. Đối tượng phản hồi yêu cầu tiện ích bổ sung hiển thị thẻ mà hàm gọi lại đã tạo.

Trang này mô tả các thao tác trên tiện ích dành riêng cho Lịch mà bạn có thể đưa vào tiện ích bổ sung của mình.

Lượt tương tác trên Lịch

Tiện ích bổ sung của Google Workspace giúp mở rộng tính năng Lịch có thể bao gồm một số thao tác bổ sung dành riêng cho Lịch trên tiện ích. Những thao tác này yêu cầu hàm gọi lại thao tác liên kết để trả về các đối tượng phản hồi chuyên biệt:

Hành động đã cố gắng thực hiện Hàm callback sẽ trả về
Thêm người tham dự CalendarEventActionResponse
Thiết lập dữ liệu hội nghị CalendarEventActionResponse
Thêm tệp đính kèm CalendarEventActionResponse

Để sử dụng các thao tác trong tiện ích và đối tượng phản hồi này, bạn phải đáp ứng tất cả những điều sau:

  • Hành động này được kích hoạt khi người dùng đang mở một sự kiện trên Lịch.
  • Trường tệp kê khai addOns.calendar.currentEventAccess của tiện ích bổ sung được đặt thành WRITE hoặc READ_WRITE.
  • Tiện ích bổ sung này bao gồm https://www.googleapis.com/auth/calendar.addons.current.event.write phạm vi Lịch.

Ngoài ra, mọi thay đổi do hàm callback hành động thực hiện sẽ không được lưu cho đến khi người dùng lưu sự kiện trên Lịch.

Thêm người tham dự bằng hàm callback

Ví dụ sau đây cho biết cách tạo nút để thêm một người tham dự cụ thể vào một sự kiện trên Lịch Google đang được chỉnh sửa:

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

Thiết lập dữ liệu hội nghị bằng hàm callback

Hành động này sẽ thiết lập dữ liệu hội nghị về sự kiện mở. Đối với dữ liệu hội nghị này, bạn cần chỉ định mã giải pháp hội nghị vì hành động không được kích hoạt bởi người dùng chọn giải pháp mong muốn.

Ví dụ sau đây cho biết cách tạo một nút để đặt dữ liệu hội nghị cho một sự kiện đang được chỉnh sửa:

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

Thêm tệp đính kèm có hàm callback

Ví dụ sau đây cho biết cách tạo nút để thêm tệp đính kèm vào một sự kiện trên Lịch đang được chỉnh sửa:

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

Đặt biểu tượng tệp đính kèm

Biểu tượng tệp đính kèm phải được lưu trữ trên cơ sở hạ tầng của Google. Xem phần Cung cấp biểu tượng tệp đính kèm để biết thông tin chi tiết.