Google Sheets automation
Clean up imported data, build a weekly report, add a custom menu, or write your own formula like =AHREFS_DR(A2).
Apps Script is the scripting tool built into Google Workspace. A few lines of it can clean up a spreadsheet, send emails from a list or file Drive attachments for you. This page has our Apps Script library and a 7-step guide to get any script running in your own account.
Anything you do by hand in Google's apps, on repeat. Here's what that looks like in each one.
Clean up imported data, build a weekly report, add a custom menu, or write your own formula like =AHREFS_DR(A2).
Send personalised emails from a sheet, label incoming mail, or save attachments to Drive the moment they arrive.
Rename and sort files, create folders for new clients, or turn a Docs template into a filled-in contract.
Email someone each time a form is submitted, or put new sign-ups straight onto a calendar.
A good first script if you work in SEO.
Adds a =AHREFS_DR() formula to your sheet. Drop it next to a list of domains and fill it down to get each site's Domain Rating from the Ahrefs API.
The same seven steps work for every script in the library.
Open a script from the library and read three things first: what it does, which Google services it touches, and what permissions it will ask for.
Scripts come as a .gs or .txt file, or as a ZIP with several files. Unzip it first if needed.
In the Google Sheet (or Doc or Form) you want to automate, go to Extensions, then Apps Script. For a standalone script, start a new project at script.google.com.
Replace the sample code in Code.gs, or add a new file if your project already has code. Give the project a name you'll recognise later.
Most scripts have a few values near the top to edit, such as a sheet name, a Drive folder ID or an email address. Keep API keys out of any file you share.
Choose the function and click Run. Google will list the permissions the script needs. Read them, and only continue if they match what the script is supposed to do.
Open Triggers (the clock icon) and add a time-driven or event trigger, so the script runs every morning or every time a form comes in.
A safe first run. It reads the active sheet and logs how many rows sit under the header. It doesn't change or send anything.
function countSpreadsheetRows() {
const sheet = SpreadsheetApp
.getActiveSpreadsheet()
.getActiveSheet();
// Row 1 is the header, so leave it out of the count.
const dataRows = Math.max(0, sheet.getLastRow() - 1);
console.log('Rows below the header: ' + dataRows);
return dataRows;
}
countSpreadsheetRows. The answer shows up in the Execution log.Triggers are what turn a script you run by hand into an automation.
| Trigger | Runs when | Typical use |
|---|---|---|
| Time-driven | On a schedule you set, from every minute to once a month | Daily reports, reminders, data syncs |
| On open | Someone opens the file | Adding a custom menu |
| On edit | A cell in the sheet is changed | Timestamps, input checks |
| On form submit | A Google Form gets a response | Confirmation emails, routing responses |
Google limits how often triggers fire and how long each run can last. The official Apps Script quotas page has the current numbers.
Five messages cover most of the trouble people run into.
| Error | What usually causes it |
|---|---|
| Authorization required | The script needs your permission. Run it once from the editor and approve the access, if it matches what the script does. |
| Cannot read properties of null | The script expected a sheet, file or value that isn't there. Check sheet names and IDs in the settings. |
| Exceeded maximum execution time | One run tried to do too much. Process the data in smaller batches. |
| Service invoked too many times | You've hit a daily Google quota. Cut down repeat calls or run the trigger less often. |
| ReferenceError: X is not defined | A typo in a function name, or a file from the project is missing. |
What people want to know before they run their first script.
Yes. Apps Script comes with every Google account at no extra cost. Google sets daily quotas, such as how many emails a script can send, and Google Workspace accounts get higher limits than personal Gmail accounts.
Not to use the scripts in our library. You paste the code, change a few settings and click Run. You only need JavaScript if you want to change how a script works.
Open the sheet, go to Extensions, then Apps Script. Anything you write there is tied to that sheet, so it can read and change its data, add menus and create custom formulas.
Google shows that warning for scripts that haven't been through its app review, which is normal for scripts you add yourself. Only click through it if you've read the code and trust what it does.
Add a trigger. In the Apps Script editor, open Triggers, click Add Trigger, then pick the function and when it should run, such as every day at 8am or whenever a form is submitted.
It's as safe as the code itself. Read the script, check the permissions it asks for, test it on a copy of your data, and never paste passwords or API keys into a file other people can open.
Browse the scripts in the library, or tell us what you'd like automated in Sheets, Gmail or Drive and we'll look at building it.