Show IntroductionAccording to the documentation, triggers let Google Apps Script run a function automatically when a certain event, like opening a document, occurs. Apps Script supports two types of triggers, simple and installable. Simple triggers are a set of reserved functions built into Apps Script, like the function onOpen(e), which is executed when a user opens a Google Docs, Sheets, Slides, or Forms file. Installable triggers, on the other hand, offer more capabilities than simple triggers but must be activated before use. They can call services that require an authorization, they offer several additional types of events including time-driven (clock) triggers, and they can be controlled programmatically. Both types of triggers let Apps Script run a function automatically if a certain event occurs. When a trigger fires, Apps Script passes in the function an event object as an argument, typically called “e”. The event object contains information about the context that caused the trigger to fire. For example, the simple onEdit(e) trigger for a Google Sheets script that we will see below, uses the event object to determine which cell was edited. Unlike VBA, in Google Apps Script there are only a few events that can be used to detect changes within an application. In this post, we will analyze two of the most useful ones, the onEdit and the onOpen. Examples will be given on Google Sheets application. We will also learn how to install a trigger, both manually and programmatically. GAS
codeIn the code below there are seven GAS functions:
onEdit exampleThe above image shows what happens when the onEdit trigger runs. If the user changes the contents of the cell A6, a message box will pop-up in the browser. Of course, this simple event was written only for demonstration purposes. onOpen exampleWhen the user double-clicks to open the selected spreadsheet, the current date/time will be filled in the last row of column B of the active sheet. This event can be very useful for logging purposes. How to install triggersThe manual wayTo manually install a trigger, you will have to go to the Script editor. There, from the menu, select Edit and then Current’s project triggers. In the window that will pop up, click on the No triggers set up. Click here to add one now. hyperlink. In the form that pop-ups, select the function you want to run and the appropriate event. In this example, the greeting function was selected from the dropdown list, along with the From spreadsheet and On open event. To save the trigger, click on the Save button. That’s it! The programmatic wayThe programmatic way is probably as easy as the manual one. You just have to use the createSpreadsheetOpenTrigger function and adjust it to your needs (e.g. change the function that will be executed when the spreadsheet opens). To set the trigger, simply click the Run option on the menu of Script editor and select the createSpreadsheetOpenTrigger function from the sub-menu. The function will run and the trigger will be set. Authorization procedureNote: the first time that you will try to install a trigger, either manually or programmatically, you will have to set authorization permissions. So, a message box will pop-up asking you to authorize the trigger. Just click the Review Permissions button. Select your Google account to continue. Finally, in the next form, click on the Allow button. Congratulations! You have successfully installed a trigger! Read here about the restrictions that installable triggers have. Read alsoHow To Create & Use A Custom Function In Google Sheets Page last updated: 17/06/2019 Page last modified: February 20, 2020 Hi, I am Christos, a Mechanical Engineer by profession (Ph.D.) and a Software Developer by obsession (10+ years of experience)! I founded this site back in 2011 intending to provide solutions to various engineering and programming problems. Add Content Block How do I use triggers in Google Sheets?Select the event type. For example, pick "On edit" if you want the trigger to fire whenever the Spreadsheet is edited or pick a date and time if you're setting up a Time-driven trigger that should fire at some specific time in the future. Choose how often you want to be notified if your trigger fails to run.
What is a trigger in Google?In Google Tag Manager, a trigger listens to your web page or mobile app for certain types of events like form submissions, button clicks, or page views. The trigger tells the tag to fire when the specified event is detected. Every tag must have at least one trigger in order to fire.
What are installable triggers?An installable edit trigger runs when a user modifies a value in a spreadsheet. An installable change trigger runs when a user modifies the structure of a spreadsheet itself—for example, by adding a new sheet or removing a column. An installable form submit trigger runs when a user responds to a form.
How do I trigger emails in Google Sheets?Try it. Step 1: Set up the spreadsheet. Click the button below to make a copy of the Gmail/Sheets Mail Merge sample spreadsheet. Make a copy. ... . Step 2: Create an email template. In your Gmail account, create an email draft. ... . Step 3: Send emails. In the spreadsheet, click Mail Merge > Send Emails.. |