Chapter 2 Read, repeat, branch ・ MISSION 7

Mission 7: Read at once (getValues and two-dimensional arrays)

📋 Mission list

Task

Read A2:C5 at once and write the name of the second item (Notebook) to the execution log.

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).

Apps ScriptUntitled projectDeploy ▾
function showSecondItem() {
const sheet = SpreadsheetApp.getActiveSheet();
const values = sheet.getRange('A2:C5').getValues();
Logger.log(values[][]);
}
Ctrl+S: save · Ctrl+R: run (editor) · F5: reopen

Chapter 2 ・ MISSION 7

Read at once (getValues and two-dimensional arrays)

Read A2:C5 at once and write the name of the second item (Notebook) to the execution log.

Now
Goal (execution log)

InformationNotebook

Reading the code

const values = sheet.getRange('A2:C5').getValues();
Reads A2 to C5 at once. The contents are [[Ballpoint pen, 120, 20], [Notebook, 150, 15], …].
Logger.log(values[1][0]);
Outputs row 2 ([1]), column 1 ([0]). Counting starts at 0.

getValues() returns a range as a two-dimensional array, [[row 1 values…], [row 2 values…]]. Indexes count from 0, so row 2, column 1 of the range read is values[1][0]. Reading at once is much faster than reading cells one by one.

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.