Chapter 2 Read, repeat, branch ・ MISSION 6

Mission 6: Write to a cell (setValue)

📋 Mission list

Task

Write the amount for the ballpoint pen (unit price × quantity) in D2.

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 writeAmount() {
const sheet = SpreadsheetApp.getActiveSheet();
const price = sheet.getRange('B2').getValue();
const quantity = sheet.getRange('C2').getValue();
sheet.getRange('').(price * quantity);
}
Ctrl+S: save · Ctrl+R: run (editor) · F5: reopen

Chapter 2 ・ MISSION 6

Write to a cell (setValue)

Write the amount for the ballpoint pen (unit price × quantity) in D2.

Now
Goal

Reading the code

const price = sheet.getRange('B2').getValue();
Reads B2 (unit price).
const quantity = sheet.getRange('C2').getValue();
Reads C2 (quantity).
sheet.getRange('D2').setValue(price * quantity);
Writes unit price × quantity to D2.

Read with getValue, calculate, write with setValue. Most GAS code that builds tables is a combination of these three. * means multiply.

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.