Apps Script: How to automate a script

In this post we’re going to look at how to set up a trigger in Apps Script and look at some of the options available. So, first of all, what’s a trigger?

A trigger automatically calls a function in the script project based on some kind of event. This could be something changing on a Sheet, a Form being submitted, a Slides document being opened, or it being a particular time or date.

It’s a wonderful way to automate your scripts! Let’s look at how we set them up.

Accessing the triggers page

In the script editor, click on the clock icon in the menu on the left-hand side of the screen. This will open the Triggers page.

Trigger options

On the Triggers page, you will see all the triggers that are currently active on this script project. Of course, at the moment there are none.

To add a new trigger, click the “Add trigger” button. This will open the Add trigger screen.

Here you will see a set of possible options. Going from top to bottom on the left:

Function: This is where you select the function on your script you want to trigger automatically. Just click on the drop-down menu and select the one you want (if you have more than one in the script project).

Deployment: The deployment type I’ve never changed and I’ve only had the option of the default “Head”.

Event source: This gives you the option of the script being triggered by an event on the spreadsheet, being triggered at a particular time or on a particular date, or from a calendar event.

Event type: If the Event source is “From spreadsheet”, you will see the 4 types of spreadsheet triggers:

  • On open (triggered when the spreadsheet is opened)
  • On edit (when a cell is edited)
  • On change (when the value of a cell is changed)
  • On form submit (when a form is submitted).

Time-based triggers

If you select the Time-driven option as an event source, the event type drop-down will change to two time-based options.

The first is the type of timer.

And the second will depend on what you choose for the first. So, if you choose “Hour timer” you will be presented with different hourly intervals to choose from.

If you select the week timer, you will see an additional drop-down menu. One for the day of the week you want to trigger on, and at what time of that day you want to do it. Note, it gives you an hour window, so is not a specific time.


Calendar updated trigger

The third event source is a Google calendar. You can trigger a script from a calendar event being updated.


Failure notifications

On the right-hand side, you can select when you want to receive failure notifications. By default it’s daily and you receive a daily summary of them, but I prefer to receive them immediately as they rarely fail and I want to be able to fix the problem as soon as I can.

You have four options, immediately, hourly, daily, and weekly.


To save the trigger, click “Save”.

You will now see the trigger on the Triggers page, showing things like who owns it, what event is it triggered from, and which function is triggered.


How to edit a trigger

To edit a trigger, clic on the pencil icon on the right-hand side of the trigger, on the Triggers page.


How to delete a trigger

To delete a trigger, click on the 3-dot menu and select “Delete trigger”.


How to set up an “on form submit” trigger

OK, let’s look at a common example. Here, we’re going to set up an “on form submit” trigger which will call a function when a Google Form is submitted, for example, to send an automatic email with the content of that submission to someone.

From the script editor, click on the clock icon.

Click “Add trigger”.

Select the function you want to run from the trigger.

Click on the event type “On open” and select “on form submit”.

Finally, click “Save” to save the trigger. This will now call the


Other notes on triggers

You can’t edit other user’s triggers, you have to be the account owner.

A common use of the onOpen trigger is to create menus in Google Sheets, Docs or Slides.

For more information on triggers, check out the Google documentation:

https://developers.google.com/apps-script/guides/triggers/installable

Want to learn more about Google Workspace and Apps Script? The books below are available on Amazon. Just click on a book! (Affiliate links).

JavaScript Fundamentals for Apps Script users

a

Google Apps Script Projects 1
Google Apps Script Projects 2