Thao tác trên lịch

Các đối tượng Action cho phép bạn tạo hành vi tương tác thành Tiện ích bổ sung của Google Workspace. Chúng xác định những 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 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 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 callback đã chỉ định. Hàm callback được truyền một đối tượng sự kiện mang thông tin về các 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 có một nút tạo và hiển thị thẻ mới khi được nhấp vào. Để làm được việc 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) để thiết lập Action khi tạo thẻ. Action mà bạn xác định sẽ chỉ định một hàm callback Apps Script. Hàm này 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ẻ mà 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.

Hoạt động tương tác trên lịch

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

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

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

  • 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 một nút sẽ thêm một người tham dự cụ thể 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 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ị truyền hình bằng hàm callback

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

Ví dụ sau đây cho biết cách tạo một nút thiết lập dữ liệu hội nghị truyền hình 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 bằng hàm callback

Ví dụ sau đây cho biết cách tạo một 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. Hãy xem phần Cung cấp biểu tượng đính kèm để biết thông tin chi tiết.