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.
- 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.
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!
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.
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.
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:
// This code will run when a form is sent!
Logger.log("New data has arrived!");
}
This script finds the very last row (the one just added) and says hello!
To make this code run automatically, you must set a trigger in the Script Editor:
- Click the Triggers icon (looks like an alarm clock) on the left sidebar.
- Click + Add Trigger (bottom right).
- Choose
handleNewResponseas the function to run. - Set "Select event type" to On form submit.
- Click Save!
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.
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)?
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!
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 ⚙️