Your first Apps Script
Open your sheet, choose Extensions, Apps Script, replace the stub with a function, click Save then Run. The first run opens an authorization dialog; after you allow it, the function runs and anything you Logger.log appears in the Execution log at the bottom.
Syntax
function name() { const sheet = SpreadsheetApp.getActiveSheet(); ... }Examples
function addTotals() {
const sheet = SpreadsheetApp.getActiveSheet();
const values = sheet.getRange("B2:B6").getValues();
let total = 0;
for (const row of values) total += Number(row[0]);
sheet.getRange("B7").setValue(total);
}Result: B7 shows the sum of B2:B6, for example 150 when the cells hold 10, 20, 30, 40, 50
function stamp() {
const sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange("A1").setValue(new Date());
Logger.log("Stamped at " + new Date());
}Result: A1 shows the current date and time; the log shows Stamped at Tue Nov 03 2026 ...
function sayHi() {
SpreadsheetApp.getUi().alert("Hi from your first script");
}Result: A popup appears in the spreadsheet window (only works when run from a sheet, not from the editor alone in some cases)
Common mistakes
- SyntaxError on save: a missing closing brace or quote, check that every { has a }, every ( a ) and strings are closed.
- Cannot read properties of null: getActiveSheet or getActiveSpreadsheet returned null because the script is standalone, so use SpreadsheetApp.openById instead.
- The number in the sheet is treated as text and the total shows 1020304050: wrap values in Number() before adding, or keep the cells numeric.
- You edited the code but the old behavior runs: click Save (Ctrl+S) before Run; unsaved changes are not executed.
Practice Your first Apps Script
These drills check your answer on live data, free, no account needed.
- Total of a listReturn the total of an array of numbers. An empty array totals `0`. Numbers can be negative or have decimals.
Questions
How do I see the output of my script?
Use Logger.log or console.log; the text appears in the Execution log panel under the editor after the run finishes. For a popup in the sheet, use SpreadsheetApp.getUi().alert.
Why does Google warn that the app is not verified?
Your own script is unverified by Google, which is normal. Click Advanced, then Go to (project name) to continue. It only affects you and people you share the script with.
Related
- What is Google Apps ScriptGoogle Apps Script explained for Sheets users: the JavaScript platform built into Google Workspace that automates Sheets, Gmail, Drive and Calendar.
- Logger.log and console.logDebug Google Apps Script with Logger.log and console.log: where output appears, format placeholders, logging arrays and objects with JSON.stringify, logs.
- getRange and getValuesRead Google Sheets data with Apps Script: getRange by A1 notation or row and column numbers, then getValue for one cell or getValues for a 2D array of rows.
- Custom functionsCreate a custom function in Google Sheets with Apps Script: any function becomes a formula like =DOUBLE(A1), ranges arrive as 2D arrays, JSDoc adds help.