Google Apps Script Triggers

In Google Apps Script, triggers are special functions that let your scripts run automatically in response to certain events—either in your Google Workspace documents (Sheets, Docs, Forms, etc.) or on a time-driven schedule. They fall into two broad categories:


1. Simple Triggers

  • Definition: Built-in functions that Apps Script recognizes by name and runs automatically when the corresponding event occurs.
  • Examples:
    • function onOpen(e)
      • Fires when a user opens the spreadsheet, document, or form.
      • Commonly used to add custom menus or sidebars.
    • function onEdit(e)
      • Fires whenever the content of a spreadsheet is edited.
      • Useful for validating entries, syncing data, or auto-formatting.
    • function onFormSubmit(e)
      • Fires when a Google Form linked to the script is submitted.
  • Characteristics:
    • Don’t require explicit setup in the UI; simply include the function in your script.
    • Run under the authority of the current user.
    • Limitations: Can’t call services that require authorization (e.g., MailApp) and can’t access some advanced services.
// Example: Simple onOpen trigger to add a custom menu
function onOpen(e) {
SpreadsheetApp.getUi()
.createMenu('My Tools')
.addItem('Send Reminder', 'sendReminder')
.addToUi();
}

function sendReminder() {
// … your code to send email reminders …
}

2. Installable Triggers

  • Definition: Triggers you explicitly create—either through the Apps Script editor’s Triggers panel or programmatically—allowing more flexibility and fewer restrictions than simple triggers.
  • Types:
    • Time-driven (clock) triggers: Run on a schedule (hourly, daily, weekly, specific date/time).
    • Spreadsheet/Document/Form triggers: More powerful versions of onOpen, onEdit, onFormSubmit that run with your account’s full authorization.
    • Others: Calendar events, Gmail send/receive, and more via advanced services.
  • Setup:
    1. Via UI: In the Apps Script editor, go to Triggers (clock icon) → Add Trigger → choose function, event source, and event type.
    2. Programmatically: Use the ScriptApp service.
// Example: Programmatically create a time-driven trigger
function createDailyTrigger() {
// Deletes any existing triggers for clarity (optional)
deleteTriggers_('dailySummary');

// Creates a new trigger that runs 'dailySummary' every day at 8 AM
ScriptApp.newTrigger('dailySummary')
.timeBased()
.everyDays(1)
.atHour(8)
.create();
}

function dailySummary() {
// … code to compile and email a daily summary …
}

// Helper to delete existing triggers for a function
function deleteTriggers_(fnName) {
ScriptApp.getProjectTriggers()
.filter(t => t.getHandlerFunction() === fnName)
.forEach(t => ScriptApp.deleteTrigger(t));
}
  • Benefits over simple triggers:
    • Can call any service (MailApp, UrlFetchApp, etc.).
    • Run even if the user who installed them isn’t actively using the document.
    • Provide more event details in the event object.

3. Choosing Between Simple and Installable

FeatureSimple TriggerInstallable Trigger
SetupJust name the functionMust install manually
Authorization scopeLimitedFull
Access to servicesRestrictedUnrestricted
Runs under user sessionYesYes
Granularity of schedulingNo (tied to user events)Yes (time-based, etc.)

4. Common Use Cases

  1. Automated Reporting
    • Time-driven trigger to email performance metrics every morning.
  2. Data Validation & Cleanup
    • onEdit simple trigger to enforce data formats or remove unwanted characters.
  3. Form Processing
    • Installable onFormSubmit to parse responses, update other sheets, or send customized follow-up emails.
  4. Custom Menus & Interfaces
    • onOpen triggers to add user-friendly menus, dialogs, or sidebars to Docs/Sheets.

Key Tips

  • Always handle errors gracefully; wrap your trigger code in try…catch and log failures with Logger.log() or Stackdriver logs.
  • Be mindful of quotas: triggers count toward your total executions and service usage.
  • Use descriptive names for your installable triggers so you can manage them easily via ScriptApp.getProjectTriggers().

By leveraging triggers effectively, you can turn passive scripts into powerful, event-driven automations that keep your Google Workspace tools working for you around the clock.