Các đối tượng Action
cho phép bạn tạo các lớp có tính tương tác
hành vi vào Tiện ích bổ sung của Google Workspace. Chúng xác định
điều gì 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 hành động được đính kèm vào một tiện ích cụ thể bằng một hàm trình xử lý tiện ích, mã này cũng xác định điều kiện kích hoạt hành động đó. Khi được kích hoạt, sẽ thực thi một hành động đã chỉ định. callback function (hàm callback). 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 của người dùng ở phía máy khách. 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
đã nhấp vào. Để thực hiện điều này, bạn phải tạo một tiện ích nút mới và sử dụng tiện ích nút
hàm trình xử lý
setOnClickAction(action)
để thiết lập Action
tạo thẻ. Chiến lược phát hành đĩa đơn
Action
mà bạn xác định sẽ chỉ định một Apps Script
hàm callback 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
triển khai hàm callback để tạo thẻ bạn muốn và trả về một
ActionResponse
. Đối tượng phản hồi yêu cầu tiện ích bổ sung hiển thị thẻ lệnh 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.
Hoạt động tương tác trên lịch
Tiện ích bổ sung của Google Workspace giúp mở rộng Lịch có thể đưa vào một vài thao tác bổ sung trên tiện ích dành riêng cho Lịch. Các hành động này cần có chức năng gọi lại thao tác đượ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 trên tiện ích, hãy làm theo tất cả các bước 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.
addOns.calendar.currentEventAccess
của tiện ích bổ sung trường tệp kê khai được đặt thànhWRITE
hoặcREAD_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 action đều không được lưu cho đến 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 trình bày cách tạo một nút để thêm thông tin cụ thể người tham dự 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 về hội nghị này cần chỉ định mã giải pháp hội nghị truyền hình vì hành động không do người dùng kích hoạt bằng cách chọn giải pháp mong muốn.
Ví dụ sau đây trình bày cách tạo một nút thiết lập dữ liệu hội nghị truyền hình cho 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 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.