Formulas run inside cells. Google Apps Script runs behind the spreadsheet: it is JavaScript that reads and writes cells, reacts to edits, and runs on a schedule. This guide walks through opening the editor and building three small scripts.
Opening the script editor
Every Google Sheet has an attached script project. Open it via Extensions > Apps Script. You will see a file called Code.gs with an empty myFunction. It is standard JavaScript executed on Google's servers rather than in your browser.
Your first script: reading and writing cells
Access to Google Sheets goes through SpreadsheetApp. Replace the default function with:
function helloSheet() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const cell = sheet.getRange("A1");
cell.setValue("Hello from Apps Script");
}
Save (Ctrl/Cmd + S) and click Run. On the first run, Google prompts you to authorize the script because it modifies your spreadsheet. Once approved, A1 displays the text. getRange accepts standard A1 notation ("A1", "B2:D10", "A:A").
Reading a range and looping over it
To process multiple cells at once, read the entire range in one call instead of cell by cell. This script reads column A and logs the total:
function sumColumnA() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const values = sheet.getRange("A1:A10").getValues(); // returns a 2D array
let total = 0;
for (const row of values) {
total += row[0]; // row[0] is column A's value in that row
}
Logger.log(total);
}
getValues() always returns a 2D array (rows containing columns), even for a single column, which is why the loop reads row[0]. Open View > Logs (or the Execution log pane) after running to see the output. Reading a range into an array, transforming it in JavaScript, and writing it back in one setValues() call is the main pattern for fast Apps Script code.
Writing a custom function (=YOURFUNCTION in a cell)
A function becomes usable inside a cell formula (like =SUM() or =XLOOKUP()) when it accepts cell values as arguments and returns a value:
/**
* Converts a price and a tax rate into a total.
* @param {number} price The base price.
* @param {number} rate The tax rate, e.g. 0.2 for 20%.
* @return The price including tax.
* @customfunction
*/
function PRICEWITHTAX(price, rate) {
return price * (1 + rate);
}
The @customfunction JSDoc tag adds autocomplete support in the sheet. Type =PRICEWITHTAX(A2, 0.2) into any cell and it recalculates whenever A2 changes, just like a built-in function.
Automating with triggers
The scripts above only run when you click Run. A trigger runs them automatically. The two most common types are:
Time-driven triggers run on a schedule (hourly, daily, weekly), useful for recurring reports:
function createDailyTrigger() {
ScriptApp.newTrigger("sendDailyReport")
.timeBased()
.everyDays(1)
.atHour(8)
.create();
}
Run createDailyTrigger once. It registers sendDailyReport (a function you define separately) to run every day at 8 a.m. even when the spreadsheet is closed.
On-edit triggers run whenever a cell is edited, useful for lightweight validation or auto-formatting. Naming the function onEdit registers it automatically with no trigger setup needed:
function onEdit(e) {
const range = e.range;
if (range.getColumn() === 2 && range.getValue() < 0) {
range.setBackground("#f8d7da"); // highlight negative values in column B
}
}
e is the edit event object: e.range is the edited cell and e.value is its new value. Keep onEdit fast, because slow handlers add noticeable lag while typing in the sheet.
A practical first automation
This example highlights any row in a sheet named Budget whenever the amount in column B exceeds 500:
function onEdit(e) {
const sheet = e.range.getSheet();
if (sheet.getName() !== "Budget") return;
const row = e.range.getRow();
const amount = sheet.getRange(row, 2).getValue(); // column B = amount
const rowRange = sheet.getRange(row, 1, 1, 3); // columns A:C for this row
if (amount > 500) {
rowRange.setBackground("#fff3cd");
} else {
rowRange.setBackground(null);
}
}
Paste this into the script editor attached to your budget sheet, save it, and edit any amount over 500. The row background updates on edit without maintaining conditional-formatting rules.
Where to go next
- Check the function reference before writing a custom function. Built-in functions calculate faster and require no script permissions.
- If you are new to spreadsheets, start with Google Sheets from Zero to One.
- Bookmark Google's Spreadsheet service reference for the full
SpreadsheetAppandRangeAPI.