การดําเนินการในปฏิทิน

ออบเจ็กต์ Action ช่วยให้คุณสร้างลักษณะการทำงานแบบอินเทอร์แอกทีฟลงในส่วนเสริมของ Google Workspace ได้ โฆษณาประเภทนี้จะกำหนดสิ่งที่จะเกิดขึ้นเมื่อผู้ใช้โต้ตอบกับวิดเจ็ต (เช่น ปุ่ม) ใน UI ของส่วนเสริม

การทำงานจะแนบอยู่กับวิดเจ็ตที่กำหนดโดยใช้ฟังก์ชันตัวแฮนเดิลวิดเจ็ต ซึ่งจะกำหนดเงื่อนไขที่ทริกเกอร์การทำงานดังกล่าวด้วย เมื่อทริกเกอร์แล้ว การดำเนินการดังกล่าวจะเรียกใช้ฟังก์ชัน Callback ที่กำหนด ฟังก์ชัน Callback จะส่งผ่านออบเจ็กต์เหตุการณ์ที่แสดงข้อมูลเกี่ยวกับการโต้ตอบฝั่งไคลเอ็นต์ของผู้ใช้ คุณต้องใช้ฟังก์ชัน Callback และให้ออบเจ็กต์ตอบกลับที่เฉพาะเจาะจง

ตัวอย่างเช่น สมมติว่าคุณต้องการปุ่มที่สร้างและแสดงการ์ดใหม่เมื่อคลิก สำหรับกรณีนี้ คุณต้องสร้างวิดเจ็ตปุ่มใหม่และใช้ฟังก์ชันแฮนเดิลวิดเจ็ตของปุ่ม setOnClickAction(action) เพื่อตั้งค่าการสร้างการ์ด Action Action ที่คุณกำหนดจะระบุฟังก์ชัน Callback ของ Apps Script ที่จะทำงานเมื่อมีการคลิกปุ่ม ในกรณีนี้ คุณใช้ฟังก์ชัน Callback เพื่อสร้างการ์ดที่ต้องการและแสดงผลออบเจ็กต์ ActionResponse ออบเจ็กต์การตอบสนองจะบอกให้ส่วนเสริมแสดงการ์ดที่ฟังก์ชัน Callback สร้างขึ้น

หน้านี้อธิบายการดำเนินการสำหรับวิดเจ็ตปฏิทินโดยเฉพาะซึ่งคุณสามารถรวมไว้ในส่วนเสริมได้

การโต้ตอบกับปฏิทิน

ส่วนเสริมของ Google Workspace ที่ขยายการใช้งานปฏิทินอาจมีการทำงานเพิ่มเติมเกี่ยวกับวิดเจ็ตสำหรับปฏิทินโดยเฉพาะ การดำเนินการเหล่านี้ต้องใช้ฟังก์ชัน Callback ของการดำเนินการที่เชื่อมโยงเพื่อแสดงออบเจ็กต์การตอบกลับแบบพิเศษ

พยายามดำเนินการแล้ว ฟังก์ชัน Callback ควรแสดงผล
การเพิ่มผู้เข้าร่วม CalendarEventActionResponse
การตั้งค่าข้อมูลการประชุม CalendarEventActionResponse
การเพิ่มไฟล์แนบ CalendarEventActionResponse

หากต้องการใช้การทำงานของวิดเจ็ตและออบเจ็กต์การตอบสนอง สิ่งต่อไปนี้ต้องเป็นจริง

  • ระบบจะทริกเกอร์การดำเนินการขณะที่ผู้ใช้เปิดกิจกรรมในปฏิทิน
  • ช่องไฟล์ Manifest addOns.calendar.currentEventAccess ของส่วนเสริมมีการตั้งค่าเป็น WRITE หรือ READ_WRITE
  • ส่วนเสริมนี้ประกอบด้วย https://www.googleapis.com/auth/calendar.addons.current.event.write ขอบเขตของปฏิทิน

นอกจากนี้ ระบบจะไม่บันทึกการเปลี่ยนแปลงที่เกิดขึ้นจากฟังก์ชัน Callback สำหรับการดำเนินการจนกว่าผู้ใช้จะบันทึกกิจกรรมปฏิทิน

การเพิ่มผู้เข้าร่วมด้วยฟังก์ชัน Callback

ตัวอย่างต่อไปนี้แสดงวิธีการสร้างปุ่มสำหรับเพิ่มผู้เข้าร่วมที่เจาะจงไปยังกิจกรรมในปฏิทินที่กำลังแก้ไข

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

การตั้งค่าข้อมูลการประชุมด้วยฟังก์ชัน Callback

การดำเนินการนี้จะตั้งค่าข้อมูลการประชุมของกิจกรรมแบบเปิด สำหรับข้อมูลการประชุมนี้ คุณต้องระบุรหัสโซลูชันการประชุมเนื่องจากผู้ใช้ที่เลือกโซลูชันที่ต้องการไม่ได้เป็นผู้ทริกเกอร์การดำเนินการดังกล่าว

ตัวอย่างต่อไปนี้แสดงวิธีการสร้างปุ่มที่ตั้งค่าข้อมูลการประชุมสำหรับกิจกรรมที่กำลังแก้ไข

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

เพิ่มไฟล์แนบด้วยฟังก์ชัน Callback

ตัวอย่างต่อไปนี้แสดงวิธีการสร้างปุ่มที่เพิ่มไฟล์แนบไปยังกิจกรรมในปฏิทินที่กำลังแก้ไข

  /**
   * 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 โปรดดูรายละเอียดที่ใส่ไอคอนไฟล์แนบ