使用外掛程式擴充 Google 試算表
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Google 試算表是雲端試算表解決方案,提供即時協作功能和強大工具,可將資料視覺化、處理及傳達資料。
您可以透過外掛程式擴充 Google 試算表的功能,打造自訂工作流程、建立與第三方系統的連線,以及將 Google 試算表資料與其他 Google Workspace 應用程式 (例如 Google 簡報) 整合。
您可以在 Google Workspace Marketplace 中查看其他人開發的 Google 試算表外掛程式。
建議做法
以下列舉幾項 Google 試算表外掛程式的用途:
試算表外掛程式是使用 Apps Script 建構而成。如要進一步瞭解如何使用 Apps Script 存取及管理 Google 試算表,請參閱「擴充 Google 試算表」。
工作表結構
Google 試算表由一或多個工作表組成。每張工作表基本上都是儲存格的 2D 格線,可儲存文字、數字、連結或其他值。一或多個相鄰儲存格的群組稱為「範圍」。
Apps Script 的 試算表服務提供多個類別,代表 Google 試算表中的組織結構 (例如 Sheet
和 Range
)。您可以使用這些類別讀取及修改試算表資料和行為。
觸發條件
Apps Script 觸發條件可讓指令碼專案在符合特定條件時執行指定函式,例如開啟試算表或安裝外掛程式時。
如要進一步瞭解哪些觸發條件可用於 Google 試算表外掛程式,以及使用時有哪些限制,請參閱外掛程式觸發條件。
自訂函式
Google 試算表內建多項函式,例如 SUM
和 AVERAGE
,可從 Google 試算表儲存格內叫用。試算表外掛程式可以定義其他自訂函式,補充這些內建函式。使用者安裝外掛程式後,外掛程式中定義的任何自訂函式都會立即生效。外掛程式可能只包含自訂函式定義。如要與他人分享自訂函式定義,主要方法是發布含有定義的外掛程式。
建立外掛程式自訂函式
外掛程式指令碼專案中定義的任何函式,都可以做為自訂函式使用。實作函式並安裝外掛程式後,您就可以呼叫自訂函式,就像呼叫任何其他內建的 Google 試算表函式一樣:在試算表儲存格中輸入 =
,然後輸入函式名稱和任何必要參數。如果沒有錯誤,函式傳回的結果會放在 Google 試算表儲存格中,並視需要溢位至相鄰儲存格。
在外掛程式中建立自訂函式時,請遵守一般自訂函式規範:
此外,外掛程式中定義的自訂函式有一些特殊考量:
- 為函式命名時,請盡量建立不重複的名稱,或許可以與外掛程式名稱相關。如果安裝的兩個以上外掛程式定義了名稱相同的自訂函式,使用者只能使用其中一個。
- 外掛程式應清楚說明提供的自訂函式。
請務必為自訂函式提供正確的 JSDoc 註解,這樣 Apps Script 才能向使用者顯示自動完成資訊。此外,請考慮在外掛程式本身或外掛程式支援網頁中,提供自訂函式的額外文件。
- 如果自訂函式未在 30 秒內完成,就會失敗並顯示
Internal error executing the custom function
錯誤。請限制自訂函式中的處理量,打造良好的使用者體驗。盡可能最佳化函式。
- 自訂函式無法使用需要授權的 Apps Script 服務,如果嘗試使用,會失敗並顯示
You do not have permission to call X service
錯誤。自訂函式只能使用允許的服務。
- 試算表中的每個自訂函式都會分別呼叫 Apps Script 伺服器。如果使用者嘗試在過多儲存格中使用自訂函式,函式執行速度可能會變慢。為減輕這類影響,請盡可能簡化自訂函式。如果需要函式執行複雜或擴充的處理作業,請勿使用自訂函式,而是透過選單項目、對話方塊或側欄互動提供該功能。
試算表巨集
巨集可讓您錄製在 Google 試算表執行的動作,並在之後使用鍵盤快速鍵重複這些動作。在試算表中建立巨集後,系統會將巨集新增為與該試算表繫結的 Apps Script 專案中的巨集函式。如要進一步瞭解巨集,請參閱「Google 試算表巨集」。
很抱歉,試算表巨集無法與外掛程式一併發布。如果在外掛程式的資訊清單中加入巨集定義,該外掛程式的使用者就無法使用巨集。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-31 (世界標準時間)。
[null,null,["上次更新時間: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."]]