אובייקטים של Action מאפשרים לכם להוסיף התנהגות אינטראקטיבית לתוספים של Google Workspace. הם מגדירים מה קורה כשמשתמש מבצע אינטראקציה עם ווידג'ט (לדוגמה, לחצן) בממשק המשתמש של התוסף.
פעולה מצורפת לווידג'ט מסוים באמצעות פונקציית handler של הווידג'ט, שמגדירה גם את התנאי שמפעיל את הפעולה. כשמופעלת פעולה, היא מפעילה פונקציית קריאה חוזרת ייעודית. פונקציית הקריאה החוזרת מקבלת אובייקט אירוע שמכיל מידע על האינטראקציות של המשתמש בצד הלקוח. צריך להטמיע את פונקציית הקריאה החוזרת ולגרום לה להחזיר אובייקט תגובה ספציפי.
לדוגמה, נניח שאתם רוצים לחצן שיוצר ומציג כרטיס חדש כשלוחצים עליו. לשם כך, צריך ליצור ווידג'ט חדש של לחצן ולהשתמש בפונקציית הטיפול של ווידג'ט הלחצן setOnClickAction(action) כדי להגדיר Action לבניית כרטיס. הערך של Action שאתם מגדירים מציין פונקציית קריאה חוזרת של Apps Script שמופעלת כשלוחצים על הלחצן. במקרה כזה, מטמיעים את פונקציית הקריאה החוזרת כדי ליצור את הכרטיס הרצוי ומחזירים אובייקט ActionResponse. אובייקט התגובה אומר לתוסף להציג את הכרטיס שפונקציית הקריאה החוזרת יצרה.
בדף הזה מתוארות פעולות ספציפיות לווידג'טים של היומן שאפשר לכלול בתוסף.
אינטראקציות עם יומן Google
תוספים ל-Google Workspace שמרחיבים את יומן Google יכולים לכלול פעולות נוספות של ווידג'טים שספציפיות ליומן. כדי לבצע את הפעולות האלה, פונקציית הקריאה החוזרת שמשויכת לפעולה צריכה להחזיר אובייקטים מיוחדים של תגובה:
| הפעולה שניסית לבצע | פונקציית הקריאה החוזרת צריכה להחזיר |
|---|---|
| הוספת משתתפים | CalendarEventActionResponse |
| הגדרת נתונים של שיחות ועידה | CalendarEventActionResponse |
| הוספת קבצים מצורפים | CalendarEventActionResponse |
כדי להשתמש בפעולות הווידג'ט ובאובייקטים של התגובה, צריך לעמוד בכל התנאים הבאים:
- הפעולה מופעלת כשהמשתמש פותח אירוע ביומן.
- שדה המניפסט של התוסף ל-Google Workspace
addOns.calendar.currentEventAccessמוגדר לערךWRITEאוREAD_WRITE. - התוסף כולל את
https://www.googleapis.com/auth/calendar.addons.current.event.writeהיקף הגישה ליומן.
השינויים שבוצעו על ידי פונקציית הקריאה החוזרת של הפעולה לא נשמרים עד שהמשתמש שומר את האירוע ביומן.
הוספת משתתפים באמצעות פונקציית התקשרות חוזרת
בדוגמה הבאה אפשר לראות איך ליצור לחצן שמוסיף משתתף ספציפי לאירוע ביומן שנערך. בדוגמה הזו נעשה שימוש במבנה בסיסי של כרטיס:
/**
* Build a basic 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 basic 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 basic 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. פרטים נוספים מופיעים במאמר בנושא הוספת סמלים לקבצים מצורפים.