Google Sheets script (Apps Script) is a JavaScript-based platform that lets you automate tasks, build custom functions, and connect Sheets to other Google services—directly from the spreadsheet. You open it via Extensions > Apps Script, write code in the editor, and run it on a trigger (time-based, on-open, or on-form-submit).
What Google Apps Script Actually Is
Apps Script is Google’s cloud scripting language, based on JavaScript (ES3/ES5 with some ES6 support). It runs on Google’s servers, so your code executes even when your browser is closed—provided you set a trigger. It’s the same platform that powers add-ons for Docs, Slides, and Forms. In Sheets, you can use it to read/write cell values, manipulate ranges, send emails, create calendar events, and fetch external APIs.
Unlike Excel’s VBA, which is tied to the desktop application, Apps Script lives in the cloud. That means you don’t need to install anything; you just open the script editor in your browser. The code is stored with the spreadsheet file, and you can share it with collaborators just by sharing the sheet.
How to Open the Script Editor
- Open your Google Sheet in a web browser.
- Click Extensions in the top menu.
- Select Apps Script from the dropdown. A new tab opens with the script editor.
- Write your code in the editor (the default file is named
Code.gs). - Click the Save icon (or press Ctrl+S / Cmd+S on Mac) to save.
- To run a function, select its name from the dropdown next to the Run button, then click Run.
If the function requires authorization (like sending an email), you’ll be prompted to review permissions. You’ll see a warning that Google hasn’t verified the app—this is normal for personal scripts. Click Advanced and then Go to [project name] (unsafe) to proceed, but only if you trust the code.
Writing Your First Script: A Simple Example
Let’s say you want to automatically capitalize the text in column A. Open the script editor and paste this:
function capitalizeColumnA() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var range = sheet.getRange("A1:A100");
var values = range.getValues();
for (var i = 0; i < values.length; i++) {
if (values[i][0]) {
values[i][0] = values[i][0].toString().toUpperCase();
}
}
range.setValues(values);
}
This reads the first 100 rows of column A, converts each cell to uppercase, and writes it back. To run it, select capitalizeColumnA from the function dropdown and click Run. The first run asks for permissions; after that, it executes instantly.
You can also create a custom function that appears in the sheet like a built-in formula. For example, to convert Fahrenheit to Celsius:
function FtoC(fahrenheit) {
return (fahrenheit - 32) * 5 / 9;
}
Then in a cell, type =FtoC(212) and it returns 100. Custom functions are recalculated when the referenced cells change.
Common Automation Tasks You Can Do
- Send emails: Use
MailApp.sendEmail()to send a personalized email to each row in a sheet. - Create calendar events: Use
CalendarAppto add events from a spreadsheet. - Import data from APIs: Use
UrlFetchApp.fetch()to pull JSON data (e.g., weather, stock prices) and write it to cells. - Format rows conditionally: Apply formatting based on cell values beyond what conditional formatting offers.
- Sync with other Google services: Read/write Google Drive files, Docs, or Forms responses.
- Build custom menus: Add a custom menu to the sheet with
onOpen()that runs your functions.
Triggers: Running Scripts Automatically
Triggers let your script run without manual intervention. To set one, go to the script editor, click the clock icon (Triggers) in the left sidebar, then Add Trigger. You can choose:
- Time-driven: Run every hour, day, week, or at a specific time.
- On open: Run when someone opens the spreadsheet.
- On edit: Run when a cell’s value changes.
- On form submit: Run when a linked Google Form receives a response.
Note that time-driven triggers are limited to a certain total runtime per day (currently 90 minutes for consumer accounts). For heavy automation, you might need a Google Workspace account with higher quotas.
Apps Script vs. Excel VBA: Key Differences
If you’re coming from Excel, you’ll notice big differences. VBA runs locally on your PC and can interact with Windows applications; Apps Script runs in the cloud and only interacts with Google services and external HTTP APIs. VBA uses the Visual Basic language; Apps Script uses JavaScript. VBA macros are stored in the workbook file; Apps Script code is stored in the cloud and attached to the spreadsheet’s ID. For simple tasks like formatting, both work fine, but for complex integrations with external databases, VBA has more direct access to local files. However, Apps Script is easier to share and collaborate on because it’s web-based.
Another difference: Apps Script has built-in services for Gmail, Calendar, and Drive, which VBA can’t natively access without extra libraries. Conversely, VBA can control other Office apps like Word and Outlook directly; Apps Script cannot control other Google apps directly—it must use their APIs, which are similar but require more setup.
Common Problems and How to Fix Them
- Authorization errors: If you see “Exception: You do not have permission to call…” it means you haven’t granted the required OAuth scopes. Re-run the function and accept the permission prompt.
- Script runs too slowly: Avoid making multiple calls to
getValue()/setValue()inside loops; instead, read the entire range into an array, modify it, and write it back in one call. - Quota exceeded: Apps Script has daily quotas (e.g., 20,000 URL fetch calls, 100 email recipients per day for consumer accounts). Check the current quotas in the Apps Script dashboard.
- Custom function returns #ERROR: Usually because the function is not returning a value, or it references a cell that is not a number. Check the script editor’s execution log (View > Logs) for error details.
- Trigger not firing: Ensure the trigger is correctly set and the function name is spelled exactly. Also, check that the spreadsheet is not in view-only mode for the script owner.
Costs and Safety Considerations
Apps Script is free for personal Google accounts. If you have a Google Workspace account, the quotas are much higher, but you still don’t pay extra for Apps Script itself. There are no hidden charges; you only pay for other Google services if you exceed their free tiers (e.g., Google Maps API).
Safety-wise, be cautious when granting permissions. Apps Script can access your Gmail, Drive, and other sensitive data if you authorize those scopes. Only run scripts you wrote or that come from a trusted source. Also, make a backup of your spreadsheet before running a script that modifies data, because scripts can overwrite cells irreversibly (there’s no undo for script changes). To be safe, test on a copy of your sheet first.
Frequently Asked Questions
Can I use Apps Script to import data from Excel?
Yes, you can use UrlFetchApp to fetch a file from a URL, or use the DriveApp to convert an Excel file to Google Sheets format. However, if you want to run macros written in VBA, they won’t work directly; you’ll need to rewrite them in Apps Script.
How do I make a button in Google Sheets that runs a script?
Insert a drawing (Insert > Drawing) or an image, then click the three-dot menu on the drawing and select Assign script. Type the function name (e.g., myFunction) and click OK. When you click the drawing, that function runs.
Can Apps Script access other Google Sheets files?
Yes, you can use SpreadsheetApp.openById() or openByUrl() to open a different spreadsheet, provided you have at least view access. You can then read and write to it, subject to permissions.
Is there a way to debug my script?
Yes, the script editor has a built-in debugger. Set breakpoints by clicking the line number, then click the Debug button (the bug icon). You can step through code and inspect variable values. Also, use Logger.log() and view the logs in View > Logs (or Execution log in the new editor).
What is the difference between a custom function and a macro?
A custom function is a user-defined function that you call in a cell like =MYFUNC(); it returns a value. A macro is a recorded sequence of actions (like in Excel) that you can replay; in Sheets, macros are essentially Apps Script functions that you record via Extensions > Macros > Record Macro. Macros can include formatting, but custom functions cannot change the sheet’s structure—they only return values.
Can I share a script with someone without sharing the spreadsheet?
Yes, you can create a standalone script (from script.google.com) and share it as a separate project. You can also publish a script as a web app or add-on, but that requires more setup. For simple sharing, you can copy the code and send it, or share the script project’s link.
Related
- Shortcut for Closing All Windows on Windows and Mac
- Data Entry Information in Marathi: Meaning, Types & More
- Google Sheets Query Unique: How to Get Distinct Values
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- Detect Circular References in Excel: Step-by-StepTo detect a circular reference in Excel for Microsoft 365 on Windows, open the Formulas tab, choose Error…
- How to Auto Archive Emails in Outlook 365 by Age or RuleTo auto archive emails in Outlook 365, choose a method that matches what you mean by archive: create…
- Excel Workbook File Type: Formats and Extensions ExplainedThe standard file type for a modern Excel workbook is .xlsx, used by Microsoft Excel for Microsoft 365…
- How to Open the Trust Center in Excel for Microsoft 365In Excel for Microsoft 365 on Windows, open the Trust Center by selecting File > Options > Trust…
- Excel Number Format for Negative Numbers in ParenthesesTo show negative numbers in parentheses in Excel, select the cells, press Ctrl+1, choose Number > Custom, and…
- Google Sheets IFS: Syntax, Examples, and FixesIn Google Sheets, IFS checks conditions in order and returns the result for the first condition that is…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.