Google 試算表可讓您錄製巨集,複製您定義的一系列特定 UI 互動。錄製巨集後,即可將巨集連結至 Ctrl+Alt+Shift+Number 形式的鍵盤快速鍵。您可以使用該捷徑,快速再次執行巨集步驟,通常是在不同位置或對不同資料執行。您也可以從 Google 試算表的「擴充功能」>「巨集」選單啟用巨集。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-04 (世界標準時間)。"],[[["\u003cp\u003eGoogle Sheets macros automate UI interactions, triggered by keyboard shortcuts or the Extensions menu.\u003c/p\u003e\n"],["\u003cp\u003eMacros are Apps Script functions stored in a bound script project, editable within the Apps Script editor.\u003c/p\u003e\n"],["\u003cp\u003eYou can import existing Apps Script functions as macros or create new ones by editing the script manifest.\u003c/p\u003e\n"],["\u003cp\u003eMacros are limited to 10 keyboard shortcuts per sheet and cannot be used outside of bound Google Sheets scripts.\u003c/p\u003e\n"],["\u003cp\u003eWhen creating macros, prioritize lightweight, repetitive tasks and ensure unique keyboard shortcuts for optimal performance.\u003c/p\u003e\n"]]],[],null,["Google Sheets lets you record\n[macros](https://support.google.com/docs/answer/7665004) that duplicate a\nspecific series of UI interactions that you define. Once you've recorded a\nmacro, you can link it to a keyboard shortcut in the form\n`Ctrl+Alt+Shift+Number`. You can use that shortcut to quickly execute the\nexact macro steps again, typically in a different place or on different data.\nYou can also activate the macro from the Google Sheets **Extensions**\n\\\u003e **Macros** menu.\n\nWhen you record a macro, Google Sheets automatically creates an Apps Script\nfunction (the *macro function* ) that replicates the macro steps. The macro\nfunction is added to an Apps Script project [bound](/apps-script/guides/bound)\nto the sheet, in a file titled `macros.gs`. In the event that there is\nalready a project file bound to the sheet with that name, the macro function\nis appended to it. Google Sheets also automatically updates the script\nproject [manifest](/apps-script/concepts/manifests), recording the name\nand keyboard shortcut assigned to the macro.\n\nSince every recorded macro is defined entirely within Apps Script, you can\nedit them directly within the Apps Script editor. You can even write macros\nfrom scratch in Apps Script, or take functions you've already written and\nturn them into macros.\n\nCreating macros in Apps Script\n\nYou can take functions written in Apps Script and use them as macro functions.\nThe easiest way to do this is by\n[importing an existing function](#importing_functions_as_macros) from the\nGoogle Sheets editor.\n\nAlternatively, you can create macros within the Apps Script editor by\nfollowing these steps:\n\n1. In the Google Sheets UI, select **Extensions** \\\u003e **Apps Script** to open the script bound to the sheet in the Apps Script editor.\n2. Write the macro function. Macro functions should take no arguments and return no values.\n3. Edit your [script manifest](/apps-script/concepts/manifests#editing_a_manifest) to create the macro and link it to the macro function. Assign it a unique keyboard shortcut and name.\n4. Save the script project. The macro is then available for use in the sheet.\n5. Test the macro function in the sheet to verify that functions as intended.\n\nEditing macros\n\nYou can edit macros attached to a sheet by doing the following:\n\n1. In the Google Sheets UI, select **Extensions** \\\u003e **Macros** \\\u003e **Manage macros**.\n2. Find the macro you want to edit and select **more_vert \\\u003e Edit macro**. This opens the Apps Script editor to the project file containing the macro function.\n3. Edit the macro function to change the macro behavior.\n4. Save the script project. The macro is then available for use in the sheet.\n5. Test the macro function in the sheet to verify that functions as intended.\n\nImporting functions as macros\n\nIf there is already a script [bound](/apps-script/guides/bound) to a sheet,\nyou can *import* a function in the script as a new macro and then assign it\na keyboard shortcut. You can do this by\n[editing the manifest](/apps-script/concepts/manifests#editing_a_manifest)\nfile and adding another element to the\n[`sheets.macros[]`](#manifest_structure_for_macros) property.\n\nAlternatively, follow these steps to import a function as a macro from the\nSheets UI:\n\n1. In the Google Sheets UI, select **Extensions** \\\u003e **Macros** \\\u003e **Import**.\n2. Select a function from the list presented and then click **Add function**.\n3. Select clear to close the dialog.\n4. Select **Extensions** \\\u003e **Macros** \\\u003e **Manage macros**.\n5. Locate the function you just imported in the list. Assign a unique keyboard shortcut to the macro. You can also change the macro name here; the name defaults to the name of the function.\n6. Click **Update** to save the macro configuration.\n\nManifest structure for macros\n\nThe following manifest file example snippet shows the section of a\n[manifest](/apps-script/concepts/manifests) that defines Google Sheets macros.\nThe `sheets` section of the manifest defines the name and keyboard shortcut\nassigned to the macro and the name of the macro function.\n**Note:** Manifests include other components that relate to Apps Script properties. The fields under the `sheets` relate directly to Sheets functionality. This example is just a portion of a full manifest file and is not a fully functional manifest. \n\n {\n ...\n \"sheets\": {\n \"macros\": [{\n \"menuName\": \"QuickRowSum\",\n \"functionName\": \"calculateRowSum\",\n \"defaultShortcut\": \"Ctrl+Alt+Shift+1\"\n }, {\n \"menuName\": \"Headerfy\",\n \"functionName\": \"updateToHeaderStyle\",\n \"defaultShortcut\": \"Ctrl+Alt+Shift+2\"\n }]\n }\n }\n\nSee the [Sheets macro manifest resource](/apps-script/manifest/sheets)\nfor more details on how Sheets macro manifests are constructed.\n\nBest practices\n\nWhen creating or managing macros in Apps Script, it is recommended that you\nadhere to the following guidelines.\n\n1. Macros are more performant when they are light-weight. Where possible, limit the number of actions a macro takes.\n2. Macros are best suited for rote operations that need to be repeated frequently with little or no configuration. For other operations, consider using a [custom menu item](/apps-script/guides/menus) instead.\n3. Always remember that macro keyboard shortcuts must be unique, and a given sheet can only have ten macros with shortcuts at any one time. Any additional macros can only be executed from the **Extensions** \\\u003e **Macros** menu.\n4. Macros that make changes to a single cell can be applied to a range of cells by first selecting the full range and then activating the macro. This means it is often unnecessary to create macros that duplicate the same operation across a predefined range of cells.\n\nThings you can't do\n\nThere are a few restrictions on what you can do with macros:\n\nUse macros outside bound scripts\n\nMacros are defined in scripts bound to specific Google Sheets. Macro\ndefinitions are ignored if defined in a\n[standalone script](/apps-script/guides/standalone) or\n[web app](/apps-script/guides/web).\n\nDefine macros in Sheets add-ons\n\nYou cannot distribute macro definitions using a\n[Sheets add-on](/workspace/add-ons/overview). Any macro definitions in a Sheets\nadd-on project are ignored by users of that add-on.\n\nDistribute macros in script libraries\n\nYou cannot distribute macro definitions using Apps Script\n[libraries](/apps-script/guides/libraries).\n\nUse macros outside of Google Sheets\n\nMacros are only a feature in Google Sheets, and do not exist for Google Docs,\nForms, or Slides."]]