Chapter 2 Read, repeat, branch ・ MISSION 6
Mission 6: Write to a cell (setValue)
Task
Write the amount for the ballpoint pen (unit price × quantity) in D2.
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).
File +
Code.gs
Chapter 2 ・ MISSION 6
Write to a cell (setValue)
Write the amount for the ballpoint pen (unit price × quantity) in D2.
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.