ออบเจ็กต์ Action
ช่วยให้คุณสร้างลักษณะการทำงานแบบอินเทอร์แอกทีฟในส่วนเสริมของ Google Workspace ได้ โดยจะกำหนด
สิ่งที่เกิดขึ้นเมื่อผู้ใช้โต้ตอบกับวิดเจ็ต (เช่น ปุ่ม) ใน
UI ของส่วนเสริม
การดำเนินการจะแนบไปกับวิดเจ็ตที่กำหนดโดยใช้ฟังก์ชันตัวแฮนเดิลวิดเจ็ต ซึ่งยังกำหนดเงื่อนไขที่ทริกเกอร์การดำเนินการด้วย เมื่อทริกเกอร์แล้ว การดำเนินการจะเรียกใช้ ฟังก์ชัน Callback ที่กำหนด ฟังก์ชันเรียกกลับจะส่งออบเจ็กต์เหตุการณ์ที่มี ข้อมูลเกี่ยวกับการโต้ตอบฝั่งไคลเอ็นต์ของผู้ใช้ คุณต้องใช้ ฟังก์ชันเรียกกลับและให้ฟังก์ชันดังกล่าวแสดงผลออบเจ็กต์การตอบกลับที่เฉพาะเจาะจง
เช่น สมมติว่าคุณต้องการปุ่มที่สร้างและแสดงการ์ดใหม่เมื่อ
คลิก โดยคุณต้องสร้างวิดเจ็ตปุ่มใหม่และใช้ฟังก์ชันตัวแฮนเดิลวิดเจ็ตปุ่ม
setOnClickAction(action)
เพื่อตั้งค่าAction
การสร้างการ์ด
Action
ที่คุณกำหนดจะระบุฟังก์ชันเรียกกลับของ Apps Script
ซึ่งจะทำงานเมื่อมีการคลิกปุ่ม ในกรณีนี้ คุณ
จะใช้ฟังก์ชันเรียกกลับเพื่อสร้างการ์ดที่ต้องการและแสดงผลออบเจ็กต์
ActionResponse
ออบเจ็กต์การตอบกลับจะบอกให้ส่วนเสริมแสดงการ์ดที่ฟังก์ชัน
Callback สร้างขึ้น
หน้านี้จะอธิบายการดำเนินการของวิดเจ็ตที่เฉพาะเจาะจงของปฏิทินที่คุณรวมไว้ในส่วนเสริมได้
การโต้ตอบกับปฏิทิน
ส่วนเสริม Google Workspace ที่ขยายปฏิทิน สามารถรวมการดำเนินการวิดเจ็ตเพิ่มเติมบางอย่างที่เฉพาะเจาะจงสำหรับปฏิทินได้ การดำเนินการเหล่านี้ ต้องใช้ฟังก์ชันเรียกกลับของการดำเนินการที่เกี่ยวข้อง เพื่อส่งคืนออบเจ็กต์การตอบกลับเฉพาะ
การดำเนินการที่พยายาม | ฟังก์ชัน Callback ควรแสดงผล |
---|---|
การเพิ่มผู้เข้าร่วม | CalendarEventActionResponse |
การตั้งค่าข้อมูลการประชุม | CalendarEventActionResponse |
การเพิ่มไฟล์แนบ | CalendarEventActionResponse |
หากต้องการใช้การดำเนินการของวิดเจ็ตและออบเจ็กต์การตอบกลับเหล่านี้ เงื่อนไขต่อไปนี้ทั้งหมดต้องเป็นจริง
- ระบบจะทริกเกอร์การดำเนินการขณะที่ผู้ใช้เปิดกิจกรรมในปฏิทิน
- ตั้งค่าฟิลด์
addOns.calendar.currentEventAccess
Manifest ของส่วนเสริมเป็น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();
}
การตั้งค่าข้อมูลการประชุมด้วยฟังก์ชันเรียกกลับ
การดำเนินการนี้จะตั้งค่าข้อมูลการประชุมในกิจกรรมที่เปิดอยู่ สำหรับข้อมูลการประชุมนี้ ต้องระบุรหัสโซลูชันการประชุม เนื่องจากผู้ใช้ไม่ได้เป็นผู้ทริกเกอร์การดำเนินการโดยการเลือกโซลูชันที่ต้องการ
ตัวอย่างต่อไปนี้แสดงวิธีสร้างปุ่มที่ตั้งค่าข้อมูลการประชุม สำหรับกิจกรรมที่กำลังแก้ไข
/**
* 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 ดูรายละเอียดได้ที่ระบุไอคอน ไฟล์แนบ