Chapter 2 Read, repeat, branch ・ MISSION 5

Mission 5: Read a cell and write it to the execution log

📋 Mission list

Task

Read the unit price of the ballpoint pen in B2 and write it 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 showPrice() {
const sheet = SpreadsheetApp.getActiveSheet();
const price = sheet.getRange('').();
Logger.log(price);
}
Ctrl+S: save · Ctrl+R: run (editor) · F5: reopen

Chapter 2 startsRead, repeat, branch

Chapter 2 ・ MISSION 5

Read a cell and write it to the execution log

Read the unit price of the ballpoint pen in B2 and write it to the execution log.

Now
Goal (execution log)

Information120.0

Reading the code

const sheet = SpreadsheetApp.getActiveSheet();
Puts the sheet that is open now into sheet. const is "a box whose contents you won't change later".
const price = sheet.getRange('B2').getValue();
Reads the value of B2 into price.
Logger.log(price);
Writes the contents of price to the execution log.

getValue() reads a cell's value and Logger.log() writes it to the execution log. Logger.log shows numbers with a decimal point, such as 120.0 (console.log shows 120). When something does not work as expected, check the values in the log first.

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.