Reading & Writing
How to pull data out of a cell and push new data back in.
Imagine your Google Sheet is a big grid of lockers. Every locker has a number (like A1). Today, we learn how to open a locker to see what's inside and how to put a new prize in its place.
- How to target a specific cell using
getRange. - How to "Read" data with
getValue. - How to "Write" data with
setValue.
This is for the builder who wants their script to interact with the user. If you want a script that reads a name from a sheet and then writes a personalized message next to it, this is for you!
Think of Filling out a Form:
- Reading: You read the question "What is your name?".
- Thinking: Your brain processes the answer.
- Writing: You pick up a pen and write "Alex" in the box.
Google Apps Script is your hands and eyes. It reads the sheet and writes the answers.
To do anything with a cell, you need to point at it first. We use getRange("A1").
But pointing isn't enough! After pointing, you must say WHAT you want to do.
- Point + getValue() = Read it.
- Point + setValue("Hello") = Write to it.
Let's write code that reads a name from A1 and writes "Hello" in B1.
// 1. Read from A1
const name = sheet.getRange("A1").getValue();
// 2. Write to B1
sheet.getRange("B1").setValue("Hello, " + name);
Before you run this, type your name into cell A1 of your Google Sheet!
Check your spreadsheet! You will see cell B1 magically update with a message. It happens almost instantly.
[A1]: Pizza
[B1]: Robot says: Pizza
Click to see common reading/writing bugs
1. Forgotten getValue: If you write const x = sheet.getRange("A1");, you only get the locker, not the prize! You MUST add .getValue() to see inside.
2. Empty Quotes: Make sure "A1" is inside quotes. If you type getRange(A1), the computer will look for a variable named A1 instead of the cell.
3. Uppercase/Lowercase: setValue must have a capital 'V'. setvalue will give an error.
Type a number in A1. Write a script that reads that number, multiplies it by 2, and writes the answer in A2. (Hint: const result = data * 2;)
Schools use this to automatically calculate grades. The script reads your scores from different columns, does the math, and writes your final grade in the last column automatically!
You’ve mastered the art of interaction! You can now move data around like a pro. This is the core skill of every Google Apps Script developer.
Status: Data Mover 📦 | Level: Cell Wizard 🪄