By Raj

How to Automate Google Sheets with Apps Script (Beginner Guide)

Google Apps Script is JavaScript that runs inside Google Workspace. You can use it to automate Google Sheets, Gmail, Calendar, Drive, and more—without installing anything. Scripts live in the cloud next to your spreadsheet and can run on demand or on a schedule. This guide walks you through opening the editor, writing a simple script, running it, and understanding the core APIs so you can build your first automation.

1. Open the script editor

In any Google Sheet: Extensions → Apps Script. A new tab opens with a code editor. You'll see a default function like myFunction(). This is where you write your script. The script is bound to this spreadsheet—it can read and write its sheets, and later you can add triggers so it runs automatically (e.g. on edit or on a schedule).

2. Write a simple script

Example: write "Hello" in cell A1 of the active sheet. This shows the basic pattern: get the spreadsheet, get the sheet, get a range, set a value.

function myFunction() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  sheet.getRange("A1").setValue("Hello");
}

SpreadsheetApp is the entry point for the current spreadsheet. getActiveSpreadsheet() returns the spreadsheet that owns this script. getActiveSheet() returns the sheet tab the user is currently viewing (or the first sheet). getRange("A1") returns a range object for that cell; setValue("Hello") writes the value.

Reading data from the sheet

To read instead of write, use getValue() for one cell or getValues() for a range (returns a 2D array). Example: read A1:B10 and loop over the rows.

const data = sheet.getRange("A1:B10").getValues();
for (let i = 0; i < data.length; i++) {
  const colA = data[i][0];
  const colB = data[i][1];
  // Do something with colA, colB
}

Writing multiple cells at once

For better performance, write a 2D array with setValues() instead of calling setValue() in a loop. The range must match the array dimensions.

const rows = [
  ["Name", "Email"],
  ["Alice", "alice@example.com"],
  ["Bob", "bob@example.com"]
];
sheet.getRange(1, 1, rows.length, 2).setValues(rows);

3. Run it

Click Run (play icon) in the script editor. The first time you run a function that touches the spreadsheet, Google will ask you to authorize the script (review permissions and click Allow). After that, go back to your Sheet—cell A1 should say "Hello". You've just automated your first action.

What you can do next

From here you can: read and write ranges, loop over rows, format cells with getRange().setBackground() / setFontWeight(), create triggers (time-based or on form submit), and connect to Gmail, Calendar, Drive, or external APIs via UrlFetchApp. If you're stuck or want custom automation built for you, I can help—Get a quote.