What Is a Trigger?
A trigger in Google Apps Script is a feature that automatically runs a function when a certain event or condition occurs, like opening and editing a spreadsheet or submitting a form. These triggers streamline workflows by automating repetitive tasks without human intervention. Triggers can be set up to activate in two ways: one is time-based, where the function is actuated at a particular time, and another is event-based, where the trigger activated at a predefined action occurs within a Google Sheet, like adding a new row in the sheet. This is the background action, where the trigger ensures the timely execution of tasks—whether it’s sending notifications, updating data, or running calculations. This functionality makes it an effective tool that enhances efficiency, saves time, and boosts productivity in Google Workspace projects.
Here are a few examples of triggers:
- A trigger runs when a user creates, sends, and deletes a reminder email.
- When data is automatically formatted, the structure of a Google Sheet.
- Running a cleanup operation at 6 AM every morning.
Types of Triggers in Google Apps Script
There are two main types of triggers in Google Apps Script that enable automation: simple and installable triggers.
1. Simple Triggers
Simple triggers are simple and user-friendly and are used for basic tasks with limited capabilities that they have permission to edit. These triggers have a set of built-in functions like onOpen(e), which is executed when a particular event happens, like opening a spreadsheet, slide, or document file.
Examples:
- onOpen: Executes when a user opens a Google sheet, document, or slide.
- Use case: Used for custom menu items for easy navigation
- OnEdit: Executes when a user changes the value in a Google spreadsheet.
- Use case: Automatically fill time and date when data is updated
2. Installable Trigger:
Installable triggers are used for more complex tasks with more capabilities than simple triggers. The triggers require permission of the user and can be controlled programmatically. Installable triggers allow you to automate events like sending emails and running a script on a schedule.
Examples:
- Time-based trigger: Runs the script automatically at scheduled intervals.
- Use case: Daily report to team manager.
- onFormSubmit: Runs script when a Google form is submitted.
- Use case: Keeps track of responses in Google sheet for analysis.
Why Triggers Are Used?
- Automation: Triggers can easily automate everyday work and eliminate repetitive manual tasks For example, you can set up automatic reminders for unpaid invoices and format data in a Google Sheet each time it is updated. Triggers eliminate the need for constant monitoring and also reduce the potential errors in the process. It allows you to focus on more productive and important tasks by automating routine administrative tasks.
- Efficiency: Triggers accelerate the execution of tasks, which eliminates the delay and human intervention. Once configured, they run silently in the background, automatically handling processes until the specific conditions are met. This eliminates the waiting time between activities and keeps operations running continuously, which ensures increased productivity.
- Scalability: Triggers can streamline operations without adding irrelevant complexities, whether you are managing a team or working on a multi-step process. They can manage different tasks from various projects or systems while ensuring that as your operations grow, everything runs smoothly. This mechanism makes it easy to expand and manage workflows across a wide range of users and systems.
Practical Examples of Using Triggers:
Example 1: Automatically Adding Timestamps on Edits
If a user updates any cell in a specific column, a timestamp is added automatically to an adjacent column. This helps to keep records more accurate and efficient without human intervention.
Code:
How it works:
Update any cell in column A. The current data and time are filled in the parallel cell in column B.
Example 2: Set up a reminder Email using a TIme-driven trigger
This trigger automatically sends a reminder email 7 days before from the initial data stored in a Google Sheet.
Code:
How to Set Up Triggers:
- Open Google Apps Script Editor.
- Click on Triggers > Add Trigger.
- Choose the function sendReminders.
- Choose "time-driven" and configure the trigger to run frequency.
Step-by-Step Guide to Setting Up a Trigger
- Open the Google Apps Script project in the Script Editor.
- On the sidebar, find and click on "Triggers" to access the trigger management panel.
- Click the "+ Add Trigger" button at the bottom right corner and get started with setting up a new trigger.
- You will see a drop-down menu where you can select the function you want to run automatically.
For example, if you have a function named "FetchDomain" that fetches domain-related data, then this would be selected through the dropdown menu such that this trigger executes automatically when a specific event occurs.
2. Selecting the Deployment:
After choosing the function, then the next step is to decide the version in which your code should be executed when the trigger runs. You will have two options:
Head (Latest Version): This option will run your code in the most recent version, which ensures that new changes are reflected immediately. This is the ideal option when you continuously develop and want your latest changes to reflect right away.
Specific deployment version: You can choose a particular version of your code if you deploy multiple versions of your script for different reasons. This version is useful when you want the trigger to run with the latest version or if you want stability and prevention from unexpected behavior from recent updates.
3. Choose Event Source
Once you have set up the deployment, the next step is to choose an event source, which defines where your trigger needs events that should run the function. There are many options for event sources, tailored to different use cases:
Time-based Event: Choose this time-based event source if you want to run your function at specific timers or intervals (i.e., daily, weekly, and monthly). This is well-suited for automated reminders, periodic data updates, and scheduled reports.
From Calendar: Choose the calendar event source if you need a script that interacts with Google Calendar. This event source will help you to automate tasks such as creating, updating, and managing events.
From Spreadsheet: This is the best event source if you want a trigger based on changes occurring in Google Sheets. This event source triggers actions when a cell is edited or a new value is inserted in any cell. This is great for sending alerts, automating calculations, and data validation.
By choosing the right deployment and event sources, you can automate tasks and schedule your script when you need it.
4. Event Type Configuration
After choosing an event source, you need to select an event type, a specific action that will trigger your function.
If you choose “From Spreadsheet”
- On open: The event type executes the function whenever the spreadsheet is opened. This is great for creating or setting up custom menus, displaying welcome messages, and initialising settings.
- On edit: Use this event type if you want to trigger the function each time the user edits a spreadsheet. This is best for real-time data validation, updating dependent fields, and automating formatting.
- On Change: Executes when structural changes happen in the spreadsheet, like adding or deleting rows and columns. This is mostly used when any modification is required in the sheet’s layout.
- OnForm Submit: If you are using a spreadsheet for submitting the Google Form responses, this event type executes whenever a response is submitted. This is the ideal option for processing form data automatically, logging responses, and sending confirmation.
If you choose “time-driven”:
If you choose a “time-driven” trigger as your event source, you can control the timing preferences based on how often the function should run. The options include:
- Minutes: Allowing for frequent updates and data syncing tasks by running the function in every 1, 5, 10, 15, or 30 minutes.
- Hourly: Choose a specific hour when you want your function to execute. Such as setting a trigger at each hour at a specific time.
- Daily: Executes at a specific time each day, which makes it perfect for daily reports or updates.
- Weekly: Runs on selected times and days of the week for the function to run, such as every Monday or Friday.
- Monthly: Triggers on specific days of the month, such as the 1st and 15th days of each month.
If you choose "From Calendar":
When you choose “From Calendar,” event type for your trigger, follow these steps:
- Enter the calendar email address: Provide the email address link with your Google calendar that you want to monitor.
- Select the calendar event types: Choose this option when it should run your function, such as event creation, update, and cancellation.
5. Enable Failure Notification
By enabling failure notification, you will get an instant alert of issues like errors and failure to execute. Set up a failure notification trigger:
- Notify daily: Getting a daily notification of any issues, such as trigger failure.
- Notify weekly: Receive a weekly report of any issues related to the trigger.
- Notify immediately: get an immediate alert whenever a failure happens, allowing you to fix issues right away.
- No notifications: Disable failure alerts if you prefer not to receive any notifications.
Example Use Cases
1. Spreadsheet Updates
- Event Source: From spreadsheet
- Event Type: Triggered on edit
- Use: The trigger ensures the verification of information to meet the desired criteria whenever data is entered and modified in a Google sheet. These triggers are used for various purposes, such as automatic correction, calculations, formatting checks, error detection, or most crucially, addressing any inconsistencies as data is added.
2. Calendar Reminders
- Event Source: From calendar
- Use: These triggers are used for sending custom notifications when any events are happening in a Google Sheet. This is best suited for reminding users about tasks, deadlines, and upcoming meetings so they do not miss any important events.
3. Daily Data Processing
- Event Source: Time-driven
- Timing: Set to run daily at a specific time
- Use: This setup ensures that tasks happen at the same time every day without human interaction, such as formatting and data extraction. This is used to automate the process like daily reports, generating summaries, or updates.
Final Thoughts
Triggers in Apps Script are crucial for automating workflows while ensuring that essential actions are performed rapidly by using onOpen, onEdit, and time-driven events. Whether you need to process form submissions, schedule calendar events, generate reports, and update spreadsheets, triggers ensure that you save valuable time by ensuring everything runs smoothly without manual intervention.
By leveraging triggers, you can minimize errors and focus on high-value tasks. Take control of your Apps script triggers to automate your process and make workflows more efficient.