日曆動作
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Action
物件可讓您在 Google Workspace 外掛程式中建構互動式行為。這些函式會定義使用者與外掛程式 UI 中的小工具 (例如按鈕) 互動時,會發生什麼情況。
動作會使用小工具處理常式函式附加至指定小工具,這個函式也會定義觸發動作的條件。觸發時,動作會執行指定的回呼函式。回呼函式會收到 event 物件,其中包含使用者在用戶端互動的相關資訊。您必須實作回呼函式,並讓該函式傳回特定回應物件。
舉例來說,假設您想要在點選按鈕時建構並顯示新卡片,為此,您必須建立新的按鈕小工具,並使用按鈕小工具處理常式函式 setOnClickAction(action)
設定建構資訊卡 Action
。您定義的 Action
會指定在點選按鈕時執行的 Apps Script 回呼函式。在這種情況下,您會實作回呼函式來建構所需資訊卡,並傳回 ActionResponse
物件。回應物件會指示外掛程式顯示回呼函式建構的資訊卡。
本頁面說明可加入外掛程式的日曆專屬小工具動作。
日曆互動
擴充 Google 日曆的 Google Workspace 外掛程式可以包含幾項額外的日曆專用小工具動作。這些動作需要相關聯的動作回呼函式傳回專屬的回應物件:
如要使用這些小工具動作和回應物件,必須符合下列所有條件:
此外,除非使用者儲存日曆活動,否則系統不會儲存動作回呼函式所做的任何變更。
使用回呼函式新增參與者
以下範例說明如何建立按鈕,將特定出席者新增至正在編輯的日曆活動:
/**
* 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 基礎架構上。詳情請參閱「提供附件圖示」。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-29 (世界標準時間)。
[null,null,["上次更新時間: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."]]