Yes, you can run an Apps Script from Google Sheets directly by opening Extensions > Apps Script, writing or pasting your function, and clicking Run (or by assigning the function to a button or a custom menu). The script editor runs in a separate tab, but it is tightly linked to the spreadsheet it was created from, and any function you define there can be triggered manually, on a time schedule, or by spreadsheet events like edits.
What Apps Script Actually Is
Apps Script is Google’s JavaScript-based scripting platform for automating tasks across Google Workspace apps (Sheets, Docs, Gmail, Drive, and more). In the context of Sheets, it lets you write custom functions, manipulate cell values, send emails, fetch external data, and build add-ons. It runs on Google’s servers, not on your local machine, so it works even when your browser is closed (for time-driven triggers).
You do not need to install anything. The script editor is built into Sheets, and your code is stored as a project tied to that specific spreadsheet. If you copy the spreadsheet, the script comes with it unless you explicitly remove it.
How to Open and Run a Script
Here is the shortest path to running a script from a Google Sheet:
- Open your Google Sheet in a web browser.
- Click Extensions in the top menu, then Apps Script. This opens a new tab with the script editor.
- In the editor, replace the default
myFunction()with your own code. For example:
function sayHello() {
SpreadsheetApp.getActiveSheet().getRange('A1').setValue('Hello from Apps Script!');
}
- Select your function name from the dropdown next to the Run button (it defaults to the first function).
- Click Run. The first time, you will be asked to authorize permissions. Click Review permissions, choose your Google account, and click Allow.
- Go back to your spreadsheet tab. If your function wrote to A1, you should see the text.
If you see an error like Exception: Cannot read property ‘getRange’ of undefined, it usually means the script is not bound to the spreadsheet you expect. Make sure you opened the script from that specific sheet.
Running a Script on a Button or Menu
Clicking Run in the editor works for testing, but for regular use you will want a button or a custom menu. To add a custom menu:
- In the script editor, add a function that builds a menu, and an
onOpen()trigger that runs when the sheet opens. Example:
function onOpen() {
const ui = SpreadsheetApp.getUi();
ui.createMenu('Custom Menu')
.addItem('Run My Script', 'myFunction')
.addToUi();
}
function myFunction() {
SpreadsheetApp.getActive().toast('Script ran!');
}
- Save the script, then reload the spreadsheet. A new menu appears next to Help.
- Click Custom Menu > Run My Script to execute it.
For a button: insert a drawing (Insert > Drawing) or an image, then click the three-dot menu on the image and choose Assign script. Type the function name (e.g., myFunction) without parentheses. Clicking the button will run that function. Note that the button will not work in the Sheets mobile app.
Custom Functions vs. Macros
A common point of confusion is the difference between a custom function (like =MYFUNC(A1)) and a macro. Custom functions are written in Apps Script and can be used in cell formulas, but they have limitations: they cannot set values in other cells, and they cannot use functions that require authorization (like sending emails). Macros, on the other hand, are recorded actions (using the Macro recorder under Extensions > Macros) that replay your manual steps. Macros are stored as Apps Script functions too, but they are meant for repetitive UI actions, not for returning values to a cell.
If you want a function that behaves like a built-in formula, write a custom function and use it in a cell. Example:
function DOUBLE(input) {
return input * 2;
}
Then in a cell, type =DOUBLE(5) and it will return 10. Custom functions recalculate when the referenced cells change, but they are slower than native formulas.
Common Problems and How to Fix Them
Most issues when running a script fall into a few categories:
- Authorization errors: If you see Authorization is required to perform that action, go to the script editor, click the clock icon (Triggers), and re-authorize. Sometimes you need to delete and recreate the trigger.
- Permissions not granted: The first run always asks for permission. If you clicked Deny, you will need to run again and allow. If you are in a Google Workspace domain, an admin might block certain scopes.
- Script not appearing in menu: The
onOpen()trigger only runs when the spreadsheet is opened, not when you reload the script. After editingonOpen(), close and reopen the spreadsheet, or runonOpen()manually once from the editor. - Function not found: If you assigned a button to a function that does not exist, or you renamed it, the button will do nothing. Check the function name is spelled exactly and exists in the script.
- Quota exceeded: Apps Script has daily quotas (e.g., 20,000 URL fetch calls, 2 hours of trigger runtime). If you hit them, wait until the next day or optimize your code.
Triggers: Run Automatically
You can run a script without opening the sheet by setting up a trigger. In the script editor, click the clock icon on the left (Triggers), then Add Trigger. Choose the function, the event source (time-driven, from spreadsheet, or from calendar), and the frequency. For example, you can run a function every hour, or every time a cell is edited. Note that time-driven triggers use your Google account’s timezone, and simple triggers like onEdit() have restrictions (they cannot access services that require authorization).
Costs and Limits
Apps Script is free for personal Google accounts and for Google Workspace accounts. There are no direct charges, but there are quotas that reset daily. For most personal or small business use, these are generous. If you need higher limits, you can sign up for Google Workspace, but even then the quotas are not unlimited. For extremely heavy workloads, consider using Google Cloud services with billing, but that is beyond the scope of a simple script.
One hidden cost: if you use Apps Script to fetch external URLs, those calls count against your quota and can be slow. Also, custom functions that call external APIs are cached for a short time, which can cause stale data.
Alternatives to Apps Script
If Apps Script feels too limited or complex, consider these alternatives:
- Google Sheets built-in functions: For simple calculations, use native formulas like
QUERY,FILTER, orIMPORTDATA. - Google Apps Script add-ons: Many pre-built add-ons (like Advanced Find and Replace) handle common tasks without coding.
- Zapier or Make: These no-code tools can connect Sheets to hundreds of other apps, but they require a paid subscription for high volume.
- Excel with VBA: If you are on Microsoft Excel, VBA macros serve a similar purpose, but they run locally and have different syntax. The equivalent in Excel is Alt+F11 to open the VBA editor.
- Google Sheets API: For external programs (Python, Node.js) to read/write Sheets, the API is more robust than Apps Script for large data operations.
Frequently Asked Questions
Can I run an Apps Script from a Google Sheet on mobile?
You cannot open the script editor or run a script directly from the mobile app. However, if you have a time-driven trigger or an onEdit trigger, the script will run on Google’s servers even when you edit from mobile. To manually run a script from mobile, you would need to use the desktop site in a browser, which is not practical.
Why does my script run but not update the sheet?
This usually happens if you are using a custom function (like =MYFUNC()) that tries to set values in other cells. Custom functions can only return a value to the cell they are in; they cannot modify other cells. Use a regular function and run it via a button or menu instead.
How do I run a script when a specific cell changes?
Use the onEdit() simple trigger. In your script, define function onEdit(e) { ... } and check the edited range. For example, if you want to run when cell B2 changes, check e.range.getA1Notation() === 'B2'. Remember that onEdit() runs automatically and does not require authorization, but it cannot access services that need permissions (like Gmail).
Can I share a sheet with a script without sharing the script?
When you share a spreadsheet, the script is automatically shared as part of the file. However, if you publish the script as an add-on, you can control who uses it. For personal use, anyone with edit access to the sheet can open the script editor and see your code. If you want to hide the code, you need to create a standalone script and use the Sheets API, or publish as a web app.
Is Apps Script safe to use with sensitive data?
Apps Script runs with your Google account permissions, and you must authorize each script. A malicious script could read your emails or delete files if you grant broad permissions. Always review the code before authorizing, and only grant the minimum scopes needed. Google does not manually review scripts, so treat any third-party script as untrusted.
Related
- Google Sheets WEEKNUM Function: Syntax & Examples
- Google Sheets Split Cell Into Rows: How-To
- Data Entry Jobs Chicago: Last Week’s Openings
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- Easiest Way to Superscript in Excel: Quick Steps and ExamplesThe easiest way to superscript text in Excel for Microsoft 365 on Windows is to select the characters…
- Excel Find and Replace: How It Works and When to Use ItYes. Excel for Microsoft 365 on Windows has a Find and Replace function: press Ctrl+H, or choose Home…
- How to Convert an Excel Document to WordTo convert Excel content into Word, copy the cells, chart, or worksheet you need, then paste it into…
- Autofit Button in Excel: Where It Is and How to Use ItIn Excel for Microsoft 365 on Windows, find AutoFit on the Home tab: choose Format in the Cells…
- Excel Conditional Formatting by Date: Rules and ExamplesTo conditionally format dates in Excel for Microsoft 365, select the date cells, then choose Home > Conditional…
- Compare Similarity Between Two Excel SheetsTo compare two Excel sheets, first decide whether the same cells should match or whether records may appear…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.