Understanding Triggers in Google Apps Script — What They Are and How to Use Them

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:

  1. Simple Triggers: Run automatically without any manual setup. Examples include onOpen, onEdit, and onFormSubmit.
  2. Installable Triggers: Manually installed via the Triggers Dashboard and provide more flexibility (like specifying exact times and events).

📘 Types of Triggers

Trigger TypeEventHow It WorksExample Usage
onOpenFile OpenRuns when a user opens a Sheet, Doc, or FormAdd a custom menu to a Google Sheet
onEditSheet EditRuns whenever a user edits a cell in a Google SheetAutomatically update calculations
onFormSubmitForm SubmissionRuns when a Google Form is submittedEmail a summary of form responses
onInstallAdd-on InstallRuns when an add-on is installedConfigure setup logic for add-ons
Time-drivenScheduleRuns 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

  1. Open the Script Editor in Google Sheets/Docs/Forms.
  2. Click Triggers (⏰) icon on the left sidebar.
  3. Click “+ Add Trigger”.
  4. Choose the function and the event (like “On Form Submit” or “On Edit”).
  5. 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:

  1. Open the Triggers Dashboard.
  2. Click + Add Trigger.
  3. Select the sendDailyReport function.
  4. Choose Time-drivenDay timer8: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 TypeUse Case
onOpenAdd custom menu to Sheets/Docs/Forms
onEditRun scripts on specific cell edits (like checkboxes)
onFormSubmitSend thank-you emails after form submissions
Time-DrivenSchedule daily reports or clean up old files
Custom Add-onsConfigure add-ons during install

📘 Best Practices for Using Triggers

  1. Keep it Simple: Avoid long scripts with complex logic in triggers.
  2. Use try...catch for Errors: If an error occurs, the script will fail silently.
  3. Use Logs: Use Logger.log() to track script progress for debugging.
  4. Limit Execution Time: Triggers have time limits (typically 6 minutes), so keep scripts efficient.
  5. 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:

  1. Go to Triggers+ Add Trigger.
  2. Select weeklyEmailReport as the function.
  3. Choose Time-drivenWeeklyMonday 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. 😊