日历操作
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
借助 Action
对象,您可以为 Google Workspace 插件构建互动行为。它们定义了当用户与插件界面中的 widget(例如按钮)互动时会发生什么。
操作通过 widget 处理函数附加到给定的 widget,该函数还定义了触发操作的条件。触发后,操作会执行指定的回调函数。回调函数会收到一个事件对象,其中包含有关用户客户端互动的信息。您必须实现回调函数,并让其返回特定的响应对象。
例如,假设您希望创建一个按钮,当用户点击该按钮时,系统会构建并显示一张新卡片。为此,您必须创建一个新的按钮 widget,并使用按钮 widget 处理函数 setOnClickAction(action)
设置卡片构建器 Action
。您定义的 Action
用于指定在点击按钮时执行的 Apps 脚本回调函数。在这种情况下,您需要实现回调函数来构建所需的卡片,并返回 ActionResponse
对象。响应对象会告知插件显示回调函数构建的卡片。
本页介绍了您可以在插件中添加的特定于日历的小部件操作。
日历互动
扩展 Google 日历的 Google Workspace 加载项可以包含一些额外的日历专用 widget 操作。这些操作需要关联的操作回调函数返回专门的响应对象:
如需使用这些 widget 操作和响应对象,必须满足以下所有条件:
此外,在用户保存日历活动之前,操作回调函数所做的任何更改都不会保存。
使用回调函数添加参与者
以下示例展示了如何创建一个按钮,用于将特定参会者添加到正在修改的日历活动中:
/**
* 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();
}
使用回调函数设置会议数据
此操作会在打开的活动中设置会议数据。对于此会议数据,需要指定会议解决方案 ID,因为该操作不是由用户选择所需解决方案触发的。
以下示例展示了如何创建一个按钮,用于为正在修改的活动设置会议数据:
/**
* 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 的基础架构上。如需了解详情,请参阅提供附件图标。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-29。
[null,null,["最后更新时间 (UTC):2025-08-29。"],[[["\u003cp\u003eActions enable interactive behavior in Google Workspace add-ons by defining responses to user interactions with UI widgets.\u003c/p\u003e\n"],["\u003cp\u003eActions are linked to widgets using handlers, triggering callback functions with event details for custom logic.\u003c/p\u003e\n"],["\u003cp\u003eCalendar add-ons have specific actions for attendee management, conference data, and attachments, requiring proper setup and permissions.\u003c/p\u003e\n"],["\u003cp\u003eCallback functions for Calendar actions return specialized response objects to manipulate event details like attendees and attachments.\u003c/p\u003e\n"],["\u003cp\u003eChanges made by these actions are only saved when the user saves the Calendar event itself.\u003c/p\u003e\n"]]],["Actions in Google Workspace add-ons define interactive behaviors triggered by widget interactions. Widget handler functions link actions to widgets, invoking callback functions upon user interaction. These callbacks, receiving event object data, return specific response objects. Calendar add-ons offer actions to add attendees, set conference data, and add attachments, requiring `CalendarEventActionResponse` returns and specific permissions. Example code demonstrates creating buttons that, when clicked, modify calendar events by adding attendees, setting conference data, or add an attachement.\n"],null,["# Calendar actions\n\n[`Action`](/workspace/add-ons/concepts/actions) objects let you build interactive\nbehavior into Google Workspace add-ons. They define\nwhat happens when a user interacts with a widget (for example, a button) in\nthe add-on UI.\n\nAn action is attached to a given widget using a\n[widget handler function](/workspace/add-ons/concepts/actions#widget_handler_functions),\nwhich also defines the condition that triggers the action. When triggered, the\naction executes a designated\n[callback function](/workspace/add-ons/concepts/actions#callback_functions).\nThe callback function is passed an\n[event object](/workspace/add-ons/concepts/event-objects) that carries\ninformation about the user's client-side interactions. You must implement the\ncallback function and have it return a specific response object.\n\nFor example, say you want a button that builds and displays a new card when\nclicked. For this, you must create a new button widget and use the button widget\nhandler function\n[`setOnClickAction(action)`](/apps-script/reference/card-service/text-button#setOnClickAction(Action))\nto set a card-building [`Action`](/workspace/add-ons/concepts/actions). The\n[`Action`](/workspace/add-ons/concepts/actions) you define specifies an Apps Script\ncallback function that executes when the button is clicked. In this case, you\nimplement the callback function to build the card you want and return an\n[`ActionResponse`](/apps-script/reference/card-service/action-response)\nobject. The response object tells the add-on to display the card the callback\nfunction built.\n\nThis page describes Calendar-specific widget actions you can include in your\nadd-on.\n\nCalendar interactions\n---------------------\n\nGoogle Workspace add-ons that extend Calendar\ncan include a few additional Calendar-specific widget actions. These actions\nrequire the associated action [callback function](/workspace/add-ons/concepts/actions#callback_functions)\nto return specialized response objects:\n\n| Action attempted | Callback function should return |\n|------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------|\n| [Adding attendees](#adding_attendees_with_a_callback_function) | [`CalendarEventActionResponse`](/apps-script/reference/card-service/calendar-event-action-response) |\n| [Setting conference data](#setting_conference_data_with_a_callback_function) | [`CalendarEventActionResponse`](/apps-script/reference/card-service/calendar-event-action-response) |\n| [Adding attachments](#add_attachments_with_a_callback_function) | [`CalendarEventActionResponse`](/apps-script/reference/card-service/calendar-event-action-response) |\n\nTo make use of these widget actions and response objects, all of the following\nmust be true:\n\n- The action is triggered while the user has a Calendar event open.\n- The add-on's [`addOns.calendar.currentEventAccess`](/apps-script/manifest/calendar-addons#Calendar.FIELDS.currentEventAccess) manifest field is set to `WRITE` or `READ_WRITE`.\n- The add-on includes the `https://www.googleapis.com/auth/calendar.addons.current.event.write` [Calendar scope](/workspace/add-ons/concepts/workspace-scopes#calendar_scopes).\n\nIn addition, any changes made by the action callback function aren't saved until\nthe user saves the Calendar event.\n\n### Adding attendees with a callback function\n\nThe following example shows how to create a button that adds a specific\nattendee to a Calendar event being edited: \n\n /**\n * Build a simple card with a button that sends a notification.\n * This function is called as part of the eventOpenTrigger that builds\n * a UI when the user opens an event.\n *\n * @param e The event object passed to eventOpenTrigger function.\n * @return {Card}\n */\n function buildSimpleCard(e) {\n var buttonAction = CardService.newAction()\n .setFunctionName('onAddAttendeesButtonClicked');\n var button = CardService.newTextButton()\n .setText('Add new attendee')\n .setOnClickAction(buttonAction);\n\n // Check the event object to determine if the user can add\n // attendees and disable the button if not.\n if (!e.calendar.capabilities.canAddAttendees) {\n button.setDisabled(true);\n }\n\n // ...continue creating card sections and widgets, then create a Card\n // object to add them to. Return the built Card object.\n }\n\n /**\n * Callback function for a button action. Adds attendees to the\n * Calendar event being edited.\n *\n * @param {Object} e The action event object.\n * @return {CalendarEventActionResponse}\n */\n function onAddAttendeesButtonClicked (e) {\n return CardService.newCalendarEventActionResponseBuilder()\n .addAttendees([\"aiko@example.com\", \"malcom@example.com\"])\n .build();\n }\n\n### Setting conference data with a callback function\n\nThis action sets the conference data on the open event. For this conference data\nthe conference solution ID needs to be specified, because the action was not\ntriggered by the user selecting the desired solution.\n\nThe following example shows how to create a button that sets conference data\nfor an event being edited: \n\n /**\n * Build a simple card with a button that sends a notification.\n * This function is called as part of the eventOpenTrigger that builds\n * a UI when the user opens a Calendar event.\n *\n * @param e The event object passed to eventOpenTrigger function.\n * @return {Card}\n */\n function buildSimpleCard(e) {\n var buttonAction = CardService.newAction()\n .setFunctionName('onSaveConferenceOptionsButtonClicked')\n .setParameters(\n {'phone': \"1555123467\", 'adminEmail': \"joyce@example.com\"});\n var button = CardService.newTextButton()\n .setText('Add new attendee')\n .setOnClickAction(buttonAction);\n\n // Check the event object to determine if the user can set\n // conference data and disable the button if not.\n if (!e.calendar.capabilities.canSetConferenceData) {\n button.setDisabled(true);\n }\n\n // ...continue creating card sections and widgets, then create a Card\n // object to add them to. Return the built Card object.\n }\n\n /**\n * Callback function for a button action. Sets conference data for the\n * Calendar event being edited.\n *\n * @param {Object} e The action event object.\n * @return {CalendarEventActionResponse}\n */\n function onSaveConferenceOptionsButtonClicked(e) {\n var parameters = e.commonEventObject.parameters;\n\n // Create an entry point and a conference parameter.\n var phoneEntryPoint = ConferenceDataService.newEntryPoint()\n .setEntryPointType(ConferenceDataService.EntryPointType.PHONE)\n .setUri('tel:' + parameters['phone']);\n\n var adminEmailParameter = ConferenceDataService.newConferenceParameter()\n .setKey('adminEmail')\n .setValue(parameters['adminEmail']);\n\n // Create a conference data object to set to this Calendar event.\n var conferenceData = ConferenceDataService.newConferenceDataBuilder()\n .addEntryPoint(phoneEntryPoint)\n .addConferenceParameter(adminEmailParameter)\n .setConferenceSolutionId('myWebScheduledMeeting')\n .build();\n\n return CardService.newCalendarEventActionResponseBuilder()\n .setConferenceData(conferenceData)\n .build();\n }\n\n### Add attachments with a callback function\n\nThe following example shows how to create a button that adds an attachment to a\nCalendar event being edited: \n\n /**\n * Build a simple card with a button that creates a new attachment.\n * This function is called as part of the eventAttachmentTrigger that\n * builds a UI when the user goes through the add-attachments flow.\n *\n * @param e The event object passed to eventAttachmentTrigger function.\n * @return {Card}\n */\n function buildSimpleCard(e) {\n var buttonAction = CardService.newAction()\n .setFunctionName('onAddAttachmentButtonClicked');\n var button = CardService.newTextButton()\n .setText('Add a custom attachment')\n .setOnClickAction(buttonAction);\n\n // Check the event object to determine if the user can add\n // attachments and disable the button if not.\n if (!e.calendar.capabilities.canAddAttachments) {\n button.setDisabled(true);\n }\n\n // ...continue creating card sections and widgets, then create a Card\n // object to add them to. Return the built Card object.\n }\n\n /**\n * Callback function for a button action. Adds attachments to the Calendar\n * event being edited.\n *\n * @param {Object} e The action event object.\n * @return {CalendarEventActionResponse}\n */\n function onAddAttachmentButtonClicked(e) {\n return CardService.newCalendarEventActionResponseBuilder()\n .addAttachments([\n CardService.newAttachment()\n .setResourceUrl(\"https://example.com/test\")\n .setTitle(\"Custom attachment\")\n .setMimeType(\"text/html\")\n .setIconUrl(\"https://example.com/test.png\")\n ])\n .build();\n }\n\n#### Setting the attachment icon\n\nThe attachment icon must be hosted on Google's infrastructure. See [Provide\nattachment icons](/workspace/add-ons/calendar/attachment/providing-icons)\nfor details."]]