使用插件扩展 Google 表格
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Google 表格是一款基于云端的电子表格解决方案,可实现实时协作,并提供强大的工具来直观呈现、处理和传达数据。
您可以使用插件扩展 Google 表格的功能,以构建自定义的工作流程改进方案、建立与第三方系统的连接,并将 Google 表格数据与其他 Google Workspace 应用(例如 Google 幻灯片)集成。
您可以在 Google Workspace Marketplace 中查看其他人构建的 Google 表格插件。
可以采取的措施
以下列举了您可使用扩展 Google 表格的插件执行的一些操作:
Google 表格插件是使用 Apps 脚本构建的。如需详细了解如何使用 Apps 脚本访问和管理 Google 表格,请参阅扩展 Google 表格。
工作表结构
Google 表格电子表格由一个或多个工作表组成。每个工作表本质上都是一个二维网格,其中包含可用于存储文本、数字、链接或其他值的单元格。一个或多个相邻单元格组成的群组称为范围。
Apps 脚本电子表格服务提供了多个类来表示 Google 表格中的组织结构(例如 Sheet
和 Range
)。您可以使用这些类来读取和修改 Google 表格数据及行为。
触发器
借助 Apps 脚本触发器,脚本项目可以在满足特定条件时(例如打开电子表格或安装插件时)执行指定函数。
如需详细了解哪些触发器可用于 Google 表格插件以及使用这些触发器时有哪些限制,请参阅插件触发器。
自定义函数
Google 表格具有许多内置函数,例如 SUM
和 AVERAGE
,这些函数可从 Google 表格单元格内调用。
Google 表格插件可以定义其他自定义函数来补充这些内置函数。当用户安装该插件后,插件中包含的任何已定义的自定义函数都会立即生效。插件可以仅包含自定义函数定义。自定义函数定义主要通过发布包含这些定义的插件与他人共享。
创建插件自定义函数
在插件脚本项目中定义的任何函数都可以用作自定义函数。实现该函数并安装该插件后,您就可以像调用任何其他内置 Google 表格函数一样调用该自定义函数:在 Google 表格单元格中,输入 =
,然后输入函数名称和任何必需的参数。如果没有错误,函数返回的结果会放置在工作表单元格中,并根据需要溢出到相邻的单元格。
在插件中创建自定义函数时,您应遵循以下一般自定义函数指南:
此外,插件中定义的自定义函数还有一些特殊注意事项:
- 为函数命名时,请尝试创建一个唯一名称,该名称可能与插件的名称相关。如果两个或更多已安装的插件定义了名称相同的自定义函数,用户只能使用其中一个。
- 插件应明确说明其提供的自定义函数。
请务必为自定义函数提供准确的 JSDoc 注释,以便 Apps 脚本可以向用户显示自动补全信息。此外,请考虑在插件本身或插件支持网页上提供自定义函数的其他文档。
- 如果自定义函数在 30 秒内未完成,则会失败并显示
Internal error executing the custom function
错误。通过限制在自定义函数中执行的处理量来打造良好的用户体验。尽可能优化函数。
- 自定义函数无法使用需要授权的 Apps 脚本服务,如果尝试使用,则会失败并显示
You do not have permission to call X service
错误。仅在自定义函数中使用允许的服务。
- 工作表中的每个自定义函数都会导致对 Apps 脚本服务器进行单独的调用。如果用户尝试在过多的单元格中使用自定义函数,这些函数的执行速度可能会变慢。为缓解此问题,请尽可能简化自定义函数。如果需要该函数执行复杂或扩展的处理,请勿使用自定义函数,而是通过菜单项、对话框或侧边栏互动来提供相应功能。
Google 表格宏
借助宏,您可以记录在 Google 表格中执行的操作,然后使用键盘快捷键重复这些操作。在工作表中创建宏时,该宏会作为宏函数添加到绑定到该工作表的 Apps 脚本项目中。如需详细了解宏,请参阅 Google 表格宏。
很遗憾,Google 表格宏无法与插件一起分发。如果您在插件的清单中添加宏定义,则该插件的用户无法使用该宏定义。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-31。
[null,null,["最后更新时间 (UTC):2025-07-31。"],[[["\u003cp\u003eGoogle Sheets add-ons enhance spreadsheets with custom workflows, external system connections, and integration with other Google Workspace apps.\u003c/p\u003e\n"],["\u003cp\u003eAdd-ons, built using Apps Script, enable data manipulation, custom menus, dialogs, and functions, triggered by specific events.\u003c/p\u003e\n"],["\u003cp\u003eCustom functions extend Google Sheets' built-in formulas, accessible directly within cells, but require adherence to naming and usage guidelines.\u003c/p\u003e\n"],["\u003cp\u003eGoogle Sheet add-ons provide extended functionality but cannot currently distribute or utilize pre-recorded macros.\u003c/p\u003e\n"]]],["Google Sheets add-ons, built with Apps Script, extend functionality by allowing users to read, edit, visualize, and format data. They can access the Google Sheets API, create custom menus, dialogs, sidebars, and custom functions. Add-ons can also use triggers to run functions on specific events. Custom functions, when created within an add-on, become immediately available to users after installation, following specific naming and usage guidelines. Macros, while recordable actions in Sheets, are not distributable with add-ons.\n"],null,["# Extending Google Sheets with add-ons\n\n[Google Sheets](https://workspace.google.com/products/sheets/) is a cloud-based\nspreadsheet solution with real-time collaboration and powerful tools to\nvisualize, process and communicate data.\n\nYou can extend Sheets with add-ons that build customized workflow improvements,\nestablish connectivity to third-party systems, and integrate your Sheets data\nwith other Google Workspace applications (like\nGoogle Slides).\n\nYou can see the Sheets add-ons others have built on the\n[Google Workspace Marketplace](https://workspace.google.com/marketplace/category/works-with-spreadsheet).\n\nWhat you can do\n---------------\n\nHere are a few things you can do with an add-on that extends Google Sheets:\n\n- You can read, edit, visualize, and format data in Google Sheets spreadsheets using the built-in Apps Script [Spreadsheet service](/apps-script/reference/spreadsheet). The service also lets you create and modify conditional formatting and data validation rules.\n- You can use the Apps Script [advanced Sheets service](/apps-script/advanced/sheets) to access the [Google Sheets API](/workspace/sheets/api) directly.\n- You can create [custom menus](/workspace/add-ons/concepts/menus) and define multiple [custom dialogs and sidebars](/workspace/add-ons/concepts/dialogs) interfaces using standard HTML and CSS.\n- You can include [custom function](#custom_functions) definitions in your add-on.\n- You can use add-ons [triggers](#triggers) that run specified functions when certain triggering events occur.\n\nSheets add-ons are built using Apps Script. To learn more about how to access\nand manage Google Sheets with Apps Script, see\n[Extending Google Sheets](/apps-script/guides/sheets).\n\nSheet structure\n---------------\n\nA Google Sheets spreadsheet consists of one or more sheets. Each sheet is\nessentially a 2D grid of cells into which text, numbers, links, or other\nvalues can be stored. A group of one or more adjacent cells is called a\n*range*.\n\nThe Apps Script [Spreadsheet service](/apps-script/reference/spreadsheet)\nprovides several classes to represent organizational structures in Sheets\n(such as [`Sheet`](/apps-script/reference/spreadsheet/sheet) and\n[`Range`](/apps-script/reference/spreadsheet/range)). You can use these\nclasses to read and modify Sheets data and behavior.\n\nTriggers\n--------\n\nApps Script [triggers](/workspace/add-ons/concepts/editor-triggers) let a script project\nexecute a specified function when certain conditions are met, such as when a\nspreadsheet is opened or when an add-on is installed.\n\nSee [add-on triggers](/workspace/add-ons/concepts/editor-triggers) for more information\non what triggers can be used with Sheets add-ons and what restrictions apply\nto their use.\n\nCustom functions\n----------------\n\nGoogle Sheets has a number of\n[built-in functions](https://support.google.com/docs/table/25273?ref_topic=1361471)\nlike `SUM` and `AVERAGE` that can be invoked from within a Google Sheet cell.\nSheets add-ons can define additional\n[custom functions](/apps-script/guides/sheets/functions) to supplement these\nbuilt-in functions. When a user\ninstalls the add-on, any defined custom functions included with the add-on\nbecome available immediately. It is possible for an add-on to consist of\nonly custom function definitions. Custom function definitions are primarily\nshared with others by publishing an add-on containing the definitions.\n\n### Creating add-on custom functions\n\nAny function defined in an add-on script project can be used as a custom\nfunction. Once the function is implemented and the add-on is installed, you\ncan call the custom function like any other built-in Sheets function: in a\nSheet cell, enter the `=` followed by the name of the function and any required\nparameters. If there are no errors, the result returned by the function is\nplaced in the Sheet cell, overflowing to neighboring cells as necessary.\n\nWhen creating custom functions in an add-on you should follow\nthe general custom function guidelines:\n\n- [Function naming guidelines](/apps-script/guides/sheets/functions#naming)\n- [Defining function arguments](/apps-script/guides/sheets/functions#arguments)\n- [Defining the function return value](/apps-script/guides/sheets/functions#return_values)\n- [Custom function data types](/apps-script/guides/sheets/functions#data_types)\n- [Enabling autocomplete using JSDoc](/apps-script/guides/sheets/functions#autocomplete)\n- [Services custom functions can use](/apps-script/guides/sheets/functions#using_apps_script_services)\n- [Optimizing custom functions](/apps-script/guides/sheets/functions#optimization)\n\nIn addition, custom functions defined in add-ons have some special\nconsiderations:\n\n- When naming your function, try to create a unique name, perhaps related to the name of your add-on. If two or more installed add-ons define custom functions with the same name, users can only use one of them.\n- Your add-on should clearly communicate what custom functions it provides. Be sure to provide accurate JSDoc comments for your custom functions so that Apps Script can present [autocomplete](/apps-script/guides/sheets/functions#autocomplete) information to the user. In addition, consider providing additional documentation of the custom functions either in the add-on itself or on an add-on support web page.\n- Custom functions that don't complete in under 30 seconds fail with an `Internal error executing the custom function` error. Build a good user experience by limiting the amount of processing you do in a custom function. [Optimize](/apps-script/guides/sheets/functions#optimization) the function where you can.\n- Custom functions can't use Apps Script services that require authorization, and fail with a `You do not have permission to call X service` error if this is attempted. Only use the [permitted services](/apps-script/guides/sheets/functions#using_apps_script_services) in your custom function.\n- Each custom function in a sheet results in a separate call to the Apps Script servers. If a user attempts to use custom functions in too many cells, the functions may execute slowly. To mitigate this, keep your custom functions as simple as possible. If you need the function to perform complex or extended processing, don't use a custom function---provide that functionality via a menu item, dialog, or sidebar interaction instead.\n\nSheets macros\n-------------\n\n[Macros](https://support.google.com/docs/answer/7665004) let you record actions\ntaken in Google Sheets and repeat them later with a keyboard shortcut. When a\nmacro is created in a sheet, it is added as a *macro function* in an Apps\nScript project [bound](/apps-script/guides/bound) to that sheet. For more\ninformation about macros, see\n[Google Sheets macros](/apps-script/guides/sheets/macros).\n\nUnfortunately, Sheets macros *can't* be distributed with add-ons. If you include\na macro definition in an add-on's manifest, it is unavailable to users of that\nadd-on."]]