Welcome to this how-to series for Google Apps Script users! Whether you’re a seasoned developer or just dipping your toes into the world of automation, Google Apps Script is a powerful tool that can help you streamline tasks, automate workflows, and integrate Google services seamlessly. In this series, we’re exploring practical tips and tricks to help you get the most out of Apps Script, from writing your first script to deploying advanced automation solutions.
In today’s post, we’re diving into a topic that’s essential for anyone looking to automate repetitive tasks: how to schedule a Google Apps Script to run automatically. Imagine having a script that sends daily email reminders, updates a Google Sheet with fresh data, or syncs information between Google Drive and an external database—all without lifting a finger. By the end of this guide, you’ll know exactly how to set up time-driven triggers to make your scripts run like clockwork.
Let’s get started!
If Apps Script and Google Sheets are your go-to tools for automation, data storage and interface design, you have probably designed a few great automations or integrations for your own use, your business or your clients. Where to take this next?
Five, a rapid application development environment, takes Google Sheets and Apps Script to the next level. With Five, you can:
✅ Set up and connect to a data source, such as a SQL database, an API or OData endpoint.
✅ Write automations or integrations in JavaScript and trigger or schedule functions based on client- or server-side events, such as onEdit
, onCancel
, or onPress
– the same way you do in Google Sheets and Apps Script.
✅ Get an auto-generated, responsive user interface that looks and feels much more professional than a spreadsheet.
✅ Use role-based access control (RBAC) to create user roles with different CRUD permissions.
✅ Authenticate users through Single Sign-On, 2-Factor Authentication or username and password to share data securely.
Say goodbye to clunky spreadsheet automations. Automate processes in a professional, cloud-native web app instead. Schedule jobs, share data securely, and integrate with 3rd party platforms – all from within Five, an all-in-one tool for building and launching custom software.
Check out some of our sample applications below to better understand the user interface of applications developed in Five or sign up for a free trial to get started.
Check out our AI-powered email extractor and learn how to build it here. Or check out this project management tool developed entirely in Five with just a few lines of code.
Time-driven triggers, also known as clock triggers, are a powerful feature in Google Apps Script that allow you to automate the execution of your scripts at specific times or on recurring intervals. These triggers fall under the category of installable triggers, which are designed to handle events that aren’t directly tied to user actions (like opening a document or submitting a form). Instead, time-driven triggers enable your scripts to run automatically based on a schedule, making them ideal for tasks that need to happen regularly without manual intervention.
Time-driven triggers function similarly to cron jobs in Unix-based systems. You can configure them to execute your script:
One important thing to note is that add-ons (Apps Script projects published to the Google Workspace Marketplace) are subject to stricter limitations—they can only use time-driven triggers once per hour at most.
When you set up a time-driven trigger, Apps Script may slightly randomize the execution time to optimize system performance. For example, if you create a trigger to run daily at 9 AM, Apps Script might choose a time between 9 AM and 10 AM initially. However, once the time is set, it remains consistent from day to day, ensuring that 24 hours elapse between each execution. This randomization helps distribute the load on Google’s servers while maintaining the reliability of your scheduled tasks.
Time-driven triggers are incredibly versatile and can be used for a wide range of automation tasks, such as:
In the next section, we’ll walk through the steps to create and manage time-driven triggers for your Apps Script projects. Let’s dive in!
In this section, we’ll walk through the steps to set up a time-driven trigger in Google Apps Script that invokes a function every minute. This is particularly useful for tasks that require frequent execution, such as monitoring data, sending notifications, or updating records.
We’ll use the provided onTrigger
function as an example, which posts a message with the current date and time to a predefined set of spaces (e.g., Google Chat spaces).
First, ensure you have a function in your Apps Script project that you want to trigger. Here’s the example function provided:
function onTrigger() {
// Retrieve stored space IDs from script properties
var spaceIds = PropertiesService.getScriptProperties().getKeys();
// Create a message with the current date and time
var message = { 'text': 'Hi! It\'s now ' + (new Date()) };
// Loop through each space ID and post the message
for (var i = 0; i < spaceIds.length; ++i) {
postMessage(spaceIds[i], message);
}
}
This function retrieves a list of space IDs from the script properties, creates a message with the current date and time, and posts it to each space using a hypothetical postMessage
function (remember to define this function).
You can replace this logic with any action you want to perform every minute.
onTrigger
(or the name of your function).Head
(this refers to the latest version of your script).Time-driven
.Minute timer
.Every minute
.3. Click Save.
If this is your first time setting up a trigger, you’ll be prompted to authorize the script. Follow these steps:
Once the trigger is set up, it will automatically run the onTrigger
function every minute. You can verify that it’s working by:
postMessage
function above.By following these steps, you’ve successfully set up a time-driven trigger to run your function every minute. This powerful feature allows you to automate repetitive tasks and focus on more important aspects of your projects.
Happy scripting!