Google Workspace 插件的触发器
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Apps 脚本触发器会在指定事件发生时执行指定的脚本函数(即触发函数)。只有特定事件才能触发触发器,并且每款 Google Workspace 应用支持的事件集各不相同。
当触发器触发时,系统会创建一个事件对象。此 JSON 结构包含有关所发生事件的详细信息。事件对象结构中的信息会根据触发器类型以不同的方式进行整理。
创建事件对象后,Apps 脚本会将其作为参数传递给触发函数。触发函数是一个回调函数,您必须自行实现该函数,以采取适当的措施来响应事件。例如,在扩展 Gmail 的 Google Workspace 加载项中,您可以定义一个触发器,用于在用户打开邮件会话时创建新的卡片界面。在这种情况下,您需要实现一个上下文回调函数,以使用 event 对象中传递的数据创建构成新界面的卡片。
本页面提供了有关在 Google Workspace 加载项项目中使用触发器的指南。
清单触发器
与编辑器插件不同,Google Workspace 插件目前无法使用 Apps 脚本简单触发器,而是使用专门为 Google Workspace 插件设计的触发器:清单触发器。
清单触发器完全在 Google Workspace 插件的清单中定义。清单触发器的示例包括:
- 用于构建和显示插件首页的首页触发器。
- 日历 eventOpen 触发器,用于在打开日历活动时显示新卡片或执行其他操作。
- 日历 eventUpdate 触发器,可在用户修改并保存日历活动时显示新卡片或执行其他操作。
- 当用户在云端硬盘中选择一个或多个文件或文件夹时,显示新卡片或执行其他操作的 Drive onItemsSelected 触发器。
- Gmail 写邮件触发器,用于在用户于 Gmail 写邮件窗口中打开插件时显示插件卡片。
- Gmail 上下文触发器,用于在用户打开 Gmail 邮件时显示新卡片或执行其他操作。
- 编辑器 onFileScopeGranted 触发器,当用户在当前编辑器文档中授予
drive.file
OAuth 范围的授权时,会显示一张新卡片。
在上述列表中,只有首页触发器是非情境触发器;其余均为情境触发器。如需详细了解清单触发器定义,请参阅清单。
除了清单触发器之外,Google Workspace 加载项还可以使用 Apps 脚本可安装的触发器。
限制
清单触发器在使用方面存在一些限制。
- 这些触发器仅在 Google Workspace 插件项目中使用;在任何其他应用中都没有用途。
- 由于这些触发器是在插件清单中定义的,而不是在其代码中定义的,因此您无法使用 Apps 脚本
Script
服务来创建或修改这些触发器。
- Gmail 上下文触发器目前只能包含一个
unconditional
条件。
这意味着,无论电子邮件的内容如何,系统都会针对每封电子邮件触发上下文触发器。
- 每个插件只能针对每个用户、每个文档拥有一个触发器。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-31。
[null,null,["最后更新时间 (UTC):2025-07-31。"],[[["\u003cp\u003eApps Script triggers execute a specific function when a defined event occurs within a Google Workspace application.\u003c/p\u003e\n"],["\u003cp\u003eGoogle Workspace add-ons utilize manifest triggers, defined in the add-on's manifest, to respond to events like opening a document or composing an email.\u003c/p\u003e\n"],["\u003cp\u003eManifest triggers can be contextual, reacting to specific user actions within a document or application, or non-contextual, like those that build the add-on's homepage.\u003c/p\u003e\n"],["\u003cp\u003eWhile manifest triggers offer event-driven functionality, they have limitations, including restrictions on their use and creation methods.\u003c/p\u003e\n"]]],["Apps Script triggers initiate a script function when specific events occur, creating an event object with event details. This object is then passed to a user-implemented trigger function, enabling actions based on the event. Google Workspace add-ons use manifest triggers, defined in the add-on's manifest, to trigger actions like displaying cards on homepage, when opening/updating Calendar events, selecting Drive items, or opening a message in Gmail. Contextual triggers and installable triggers are also supported but with some restrictions.\n"],null,["# Triggers for Google Workspace add-ons\n\n[Apps Script triggers](/apps-script/guides/triggers) cause a specified script\nfunction (the *trigger function*) to execute whenever a specified event\noccurs. Only certain events can cause triggers to fire, and each\nGoogle Workspace application supports a different set of events.\n\nWhen a trigger fires, an *event object* is created. This JSON structure\ncontains details about the event that occurred. The information in the event\nobject structure is organized differently based on the trigger type.\n\nOnce the event object is created, Apps Script passes it as a parameter to the\ntrigger function. The trigger function is a callback function that you must\nimplement yourself, to take whatever actions are appropriate to respond to the\nevent. For example, in a\nGoogle Workspace add-on that extends Gmail, you can\ndefine a trigger that creates a new card interface when the user opens a message\nthread. In this case, you implement a contextual callback function to create\nthe cards making up the new UI using the data passed in the\n[event object](/workspace/add-ons/concepts/event-objects).\n\nThis page provides guidelines on using triggers in\nGoogle Workspace\nadd-on projects.\n\nManifest triggers\n-----------------\n\nUnlike Editor add-ons, Google Workspace add-ons\ncurrently can't use Apps Script [simple triggers](/apps-script/guides/triggers)\nInstead, they use triggers designed specifically for\nGoogle Workspace add-ons: *manifest triggers*.\n\nManifest triggers are defined completely in the\nGoogle Workspace add-on's\n[manifest](/workspace/add-ons/concepts/workspace-manifests). Examples of manifest triggers include the\nfollowing:\n\n- **Homepage triggers** that build and display the add-on homepage.\n- **Calendar eventOpen triggers** that display a new card or take other actions when a Calendar event is opened.\n- **Calendar eventUpdate triggers** that display a new card or take other actions when a user edits and saves a Calendar event.\n- **Drive onItemsSelected triggers** that display a new card or take other actions when a user selects one or more files or folders in Drive.\n- **Gmail compose triggers** that display an add-on card when the user opens the add-on in the Gmail compose window.\n- **Gmail contextual triggers** that display a new card or take other actions when the user opens a Gmail message.\n- **Editor onFileScopeGranted triggers** that display a new card when users grant authorization for the `drive.file` OAuth scope in the current Editor document.\n\nIn the above list, only homepage triggers are non-contextual; the rest are\ncontextual triggers. See [Manifest](/workspace/add-ons/concepts/workspace-manifests)\nfor more information about manifest trigger definitions.\n\nIn addition to manifest triggers, Google Workspace\nadd-ons can also use Apps Script\n[installable triggers](/apps-script/guides/triggers/installable).\n\n### Restrictions\n\nManifest triggers have certain restrictions to their use.\n\n- These triggers are only used in Google Workspace add-on projects; they have no purpose in any other application.\n- Since they are defined in the add-on manifest and not in its code, you can't use the Apps Script [`Script`](/apps-script/reference/script) service to create or modify these triggers.\n- Gmail contextual triggers currently can only have an `unconditional` criteria. This means contextual triggers fire for every email message, regardless of content.\n- Each add-on can only have one trigger of each type, per user, per document."]]