Google Apps Script triggers are automated event listeners that run scripts automatically based on specific actions or scheduled times. With triggers, you can automate repetitive tasks, respond to user actions, and set schedules for scripts to run at specific intervals.
Triggers allow you to create event-driven workflows. For example:
- Run a script every day at 8 AM.
- Automatically send an email when a new row is added to a Google Sheet.
- Notify users when a Google Form is submitted.
- Run clean-up scripts when a document is closed.
This guide will explain what triggers are, the types of triggers, and how to set them up in Google Apps Script. We’ll also walk through examples of useful triggers you can implement right away.
🎉 What Are Triggers in Google Apps Script?
A trigger is an event that runs a script automatically. Triggers “listen” for specific actions (like edits, form submissions, or opening a file) and execute the specified function.
🔹 Types of Triggers
There are two main types of triggers in Apps Script:
- Simple Triggers: Run automatically without any manual setup. Examples include
onOpen
,onEdit
, andonFormSubmit
. - Installable Triggers: Manually installed via the Triggers Dashboard and provide more flexibility (like specifying exact times and events).
📘 Types of Triggers
Trigger Type | Event | How It Works | Example Usage |
---|---|---|---|
onOpen | File Open | Runs when a user opens a Sheet, Doc, or Form | Add a custom menu to a Google Sheet |
onEdit | Sheet Edit | Runs whenever a user edits a cell in a Google Sheet | Automatically update calculations |
onFormSubmit | Form Submission | Runs when a Google Form is submitted | Email a summary of form responses |
onInstall | Add-on Install | Runs when an add-on is installed | Configure setup logic for add-ons |
Time-driven | Schedule | Runs at a specific time (daily, weekly, hourly) | Send daily reports at 8:00 AM |
📘 1️⃣ Simple Triggers
Simple triggers do not require manual setup. Just add the function to your Apps Script file, and it runs automatically.
onOpen Trigger
This trigger runs every time a user opens a Google Sheet, Doc, or Form. It’s useful for creating custom menus.
function onOpen() {
const ui = SpreadsheetApp.getUi();
ui.createMenu('📊 Custom Report')
.addItem('📄 Create Report', 'createReport')
.addToUi();
}
function createReport() {
SpreadsheetApp.getActiveSpreadsheet().toast('Your report has been created!');
}
What It Does:
When you open the spreadsheet, a new menu “📊 Custom Report” is added. This menu contains an item “📄 Create Report”. Clicking it runs the createReport()
function.
onEdit Trigger
This trigger runs whenever you edit a cell in a Google Sheet. You can use it to trigger real-time calculations, validations, or alerts.
function onEdit(e) {
const range = e.range; // The edited range
const editedValue = range.getValue(); // The new value
Logger.log(`Cell ${range.getA1Notation()} was changed to ${editedValue}`);
}
What It Does:
Every time a cell in the sheet is edited, the script logs the cell address and the new value. You can modify this to trigger custom actions, like checking for specific keywords or auto-populating cells.
onFormSubmit Trigger
This trigger runs whenever a user submits a Google Form linked to a Google Sheet.
function onFormSubmit(e) {
const formData = e.values; // Array of form submission data
const email = formData[1]; // Assuming the second field is an email
MailApp.sendEmail(email, 'Thanks for your response!', 'We received your form submission. Thank you!');
}
What It Does:
When a Google Form is submitted, this script automatically sends a thank-you email to the form submitter.
📘 2️⃣ Installable Triggers
Installable triggers require manual setup through the Google Apps Script editor. They allow you to define more complex triggers, such as:
- Time-driven triggers (run every hour, day, week, etc.)
- Specific actions like document edits, form submissions, and file openings.
📘 How to Create Installable Triggers
- Open the Script Editor in Google Sheets/Docs/Forms.
- Click Triggers (⏰) icon on the left sidebar.
- Click “+ Add Trigger”.
- Choose the function and the event (like “On Form Submit” or “On Edit”).
- Save and grant permissions if required.
📘 3️⃣ Time-Driven Triggers (Scheduled Triggers)
Time-driven triggers let you schedule scripts to run on a specific schedule (every minute, hour, day, or week).
Example: Send Daily Email at 8:00 AM
function sendDailyReport() {
const email = 'your-email@example.com';
const subject = 'Daily Report';
const message = 'Here is the daily report for ' + new Date();
MailApp.sendEmail(email, subject, message);
}
How to Set It Up:
- Open the Triggers Dashboard.
- Click + Add Trigger.
- Select the
sendDailyReport
function. - Choose Time-driven → Day timer → 8:00 AM.
What It Does:
The script will run every day at 8:00 AM and send you a daily email report.
📘 Use Cases for Triggers
Trigger Type | Use Case |
---|---|
onOpen | Add custom menu to Sheets/Docs/Forms |
onEdit | Run scripts on specific cell edits (like checkboxes) |
onFormSubmit | Send thank-you emails after form submissions |
Time-Driven | Schedule daily reports or clean up old files |
Custom Add-ons | Configure add-ons during install |
📘 Best Practices for Using Triggers
- Keep it Simple: Avoid long scripts with complex logic in triggers.
- Use
try...catch
for Errors: If an error occurs, the script will fail silently. - Use Logs: Use
Logger.log()
to track script progress for debugging. - Limit Execution Time: Triggers have time limits (typically 6 minutes), so keep scripts efficient.
- Avoid Using Services with Limits: Avoid services like
GmailApp
in triggers, as they may hit daily usage limits.
📘 Example: Automatically Send Weekly Report via Email
function weeklyEmailReport() {
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
const sheet = spreadsheet.getSheetByName('Sales Report');
const range = sheet.getRange('A1:D20');
const data = range.getValues();
let report = 'Weekly Sales Report:\n\n';
data.forEach(row => {
report += row.join(' | ') + '\n';
});
const email = 'your-email@example.com';
MailApp.sendEmail(email, 'Weekly Sales Report', report);
}
How to Set It Up:
- Go to Triggers → + Add Trigger.
- Select
weeklyEmailReport
as the function. - Choose Time-driven → Weekly → Monday at 9:00 AM.
This script sends a summary of the weekly sales every Monday at 9:00 AM.
📘 Conclusion
Google Apps Script triggers are a game-changer for automating repetitive tasks. Whether it’s running scripts on file open, form submissions, or at specific times, triggers offer a wide range of possibilities.
What You Learned:
- What triggers are and how to use them.
- The difference between Simple and Installable triggers.
- How to create scheduled triggers and send daily or weekly reports.
💡 Ready to Automate Your Workflows?
With triggers, you can automate file management, send reminders, and generate reports — all while you sleep! If you’d like more examples or help setting up specific triggers, let me know in the comments. 😊
