Chapter 1 Recording and running macros ・ MISSION 3

Mission 3: Record with relative references

📋 Mission list

Task

Record, with relative references, a macro that types "Completed" in the "Status" column (two to the right) of the selected item's row. Start recording with A2 selected.

Office supply order sheet - Google Sheets
Office supply order sheet

For practice, the table goes back to its starting state every time before the code runs (a real spreadsheet doesn't go back. Script changes can sometimes be undone with Ctrl+Z, but not all at once).

Ctrl+S: save · Ctrl+R: run (editor) · F5: reopen

Chapter 1 ・ MISSION 3

Record with relative references

Record, with relative references, a macro that types "Completed" in the "Status" column (two to the right) of the selected item's row. Start recording with A2 selected.

Now
Goal

Reading the code

spreadsheet.getCurrentCell().offset(0, 2).activate();
Selects the cell 0 rows down and 2 columns right of the current cell. Relative references are saved in this form.
spreadsheet.getCurrentCell().setValue('Completed');
Puts "Completed" in the selected cell.

With relative references, selecting is saved as "how many rows and columns from the current cell", such as offset(0, 2). Whichever row you select, the value goes into that row's "Status". Macros you use again and again are often handier with relative references.

How to get started with GAS and common code are covered in an article.What is GAS? Getting started and common code

The code you write runs in an isolated place inside this page. It can't reach outside the page or other sites.

* This is a simulation of how Google Sheets and Apps Script behave and may differ in some ways from the real thing.

* Google Sheets is a trademark of Google LLC. This site is not affiliated with Google.