Module 09 / 15

The Automatic Postman

Make your script wake up the moment a user submits a Form.

Google Forms are great for asking questions, but Apps Script makes them intelligent. Today, we’ll learn how to connect a Form to a Sheet and write a script that "listens" for new answers.

What You Will Learn
  • How to link a Google Form to a Spreadsheet.
  • What a "Trigger" is (The Robot's Alarm Clock).
  • How to handle new data as it arrives.
Who Is This For?

This is for the "App Builder." If you want to create a feedback system, a quiz that grades itself, or a registration form for your blog, you need to know how to handle user input!

Real Life Connection

Think of a Smart Mailbox.

Usually, a mailbox just sits there. But a Smart Mailbox has a sensor. As soon as the postman drops a letter, a bell rings in your house and your coffee machine starts making a drink for you.

In Google, the Form is the mailbox, and the Trigger is the sensor that rings the bell.

What is a Trigger?

Normally, a script only runs when you click the "Run" button. But a Trigger is an event that tells the script: "Wake up and do your job NOW!"

The most common trigger is "On Form Submit." It means every time someone clicks "Submit" on your form, your script runs automatically.

The Form-to-Script Journey
User Fills Form 📝
⬇️
Data Saved to Sheet 📊
⬇️
TRIGGER FIRES 🔥
⬇️
Script Runs Action 🚀
Setting Up the Connection

Step 1: Create a Google Form with one question: "What is your favorite fruit?"
Step 2: In the Form, go to Responses and click Link to Sheets.
Step 3: Open that Sheet, go to Extensions > Apps Script and paste this code:

function onFormSubmit() {
  // This code will run when a form is sent!
  Logger.log("New data has arrived!");
}
The "Hello Submitter" Script

This script finds the very last row (the one just added) and says hello!

function handleNewResponse() { const sheet = SpreadsheetApp.getActiveSheet(); // Get the last row that was added const lastRow = sheet.getLastRow(); // Read the answer from the 2nd column (Column B) const answer = sheet.getRange(lastRow, 2).getValue(); Logger.log("The user submitted: " + answer); }
How to Activate the Trigger

To make this code run automatically, you must set a trigger in the Script Editor:

  1. Click the Triggers icon (looks like an alarm clock) on the left sidebar.
  2. Click + Add Trigger (bottom right).
  3. Choose handleNewResponse as the function to run.
  4. Set "Select event type" to On form submit.
  5. Click Save!
Why isn't it firing?
Click to fix trigger issues

1. Wrong Function: When setting up the trigger, make sure you selected the correct function name from the list.

2. Column Math: Remember that Column A is 1, Column B is 2, and so on. If your answer is in Column C, use getRange(lastRow, 3).

3. Missing Save: If you change your code, you MUST save the script before the trigger can use the new version.

The "Auto-Reply" Challenge

Can you modify the script so that it writes "Thank you for your answer!" in the column right next to the user's response (Column 3)?

Who Else Uses This?

Online Stores use this constantly! When you buy something, a Form/Input trigger fires a script that checks if they have the item in stock and then sends you a receipt. It all starts with a simple submission!

Module Summary

You’ve learned how to make your code wait for people! By combining Forms, Sheets, and Triggers, you have created a system that reacts to the world without you touching a single button.

Status: Event Listener 👂 | Level: System Automator ⚙️

Type NEXT to unlock the next module 🚀