Module 08 / 15

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.

What You Will Learn
  • How to target a specific cell using getRange.
  • How to "Read" data with getValue.
  • How to "Write" data with setValue.
Who Is This For?

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!

Real Life Connection

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.

The "Cell Pointer" Strategy

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.
The Interaction Flow
Cell A1
getValue() ➜
SCRIPT
➜ setValue()
Cell B1
The Magic Lines

Let's write code that reads a name from A1 and writes "Hello" in B1.

const sheet = SpreadsheetApp.getActiveSheet();

// 1. Read from A1
const name = sheet.getRange("A1").getValue();

// 2. Write to B1
sheet.getRange("B1").setValue("Hello, " + name);
The "Copy-Paste" Script

Before you run this, type your name into cell A1 of your Google Sheet!

function readAndWrite() { // Get our active sheet const sheet = SpreadsheetApp.getActiveSheet(); // 1. Get the value from cell A1 const myData = sheet.getRange("A1").getValue(); // 2. Put a new message into cell B1 sheet.getRange("B1").setValue("Robot says: " + myData); }
The Result

Check your spreadsheet! You will see cell B1 magically update with a message. It happens almost instantly.

Sheet View:
[A1]: Pizza
[B1]: Robot says: Pizza
Why did it fail?
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.

The "Math Machine" Challenge

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

Who Else Uses This?

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!

Module Summary

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 🪄

Type NEXT to unlock the next module 🚀