Google Apps Script Triggers Explained 👈🏽

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
i'm shauna greco from sapers and in this video i'll show you pretty much everything you need to know about google apps script triggers [Music] first things first what are triggers triggers in google apps script is what actually runs your code so in this case here i have a menu item set up as a trigger and that is now launching or kicking off my code and executing whatever i have programmed to do in apps script we have two separate categories of triggers one would be the simple triggers so these are the triggers like on open that fire when a document is opened and then we have the installable triggers which offer more capabilities more things that you can do with your code in order to use the simple triggers we create a function that uses referred or reserved function names so these are predefined by google and we just have to use them whenever we want to use any specific trigger so the one i most commonly use and which you've probably also seen in other tutorial videos is on open this fires or triggers whenever the person opens up a spreadsheet google docs document google slides or google forms where this trigger is used then on install that is a trigger you use um for when someone installs an add-on for the editor then on edit that is what you use when someone changes something into spreadsheet and you want that to trigger a specific script and on selection change also for within spreadsheet when someone changes a selection in that spreadsheet and you want code to start running then do get and do post are a bit special in the sense that do get is when a person visits a website or an http get request is set sent to any given website and do post on the other hand is when a program sends an http post request so those are the simple triggers and you might see that in as a parameter i've mentioned here the event now um this is something you might have never seen out there in the wild that and a trigger actually uses the event parameter this is something optional but if you need to somehow um you know use that event to do something within that function within that trigger then there is a parameter available but as i said most likely you will only see on open closing bracket or parentheses without any parameters in between as i mentioned before the simple trigger that i use most often is the on open and what you see here is a code snippet actually taken from that very exact youtube automation script that i showed at the beginning of this tutorial and what it does is it gets the spreadsheet app and it changes the ui so it adds this menu and the menu item get stats which i can then click on in my spreadsheet and that triggers my automation script that is also within the same file to then actually run and you see here this get stats that is the name of the function that this trigger will then run once i click on that menu item get stats learn how to automate tasks processes and data handling in google sheets head over to courses.saparis.io and check out our google sheets automation course simple triggers are great they're easy use but they do also come with some restrictions that we just need to be aware of and need to deal with and maybe start using installable triggers if one of these instructions restrictions has an impact on what we're trying to do so first up what we have to keep in mind is that script or specific script that we want to run must be bound to a document or else be an app add-on which can be installed so the takeaway here is script is bound to one specific google sheets or google docs and so on and so forth then if the people if any given user who's trying to run a script only has let's say viewing or commenting right the script won't run so people need editing rights in order for these triggers to actually fire to run the code then script executions and api requests do not cause triggers to run here an example so we have our range set value which is a method which we could use in a script that will not trigger the on edit trigger will not have the on edit trigger do something so on edit will only work if a user edits the document but not if the document was programmatically edited by let's say saying range set value now on to some further restrictions a simple trigger cannot access services that require authorizations so let's say you want to create a mail merge well that would not work because a simple trigger can't set out sent out emails because in order for emails to be sent out the gmail app authorization will be triggered and simple triggers cannot deal with that they cannot have these services do something then simple triggers can access files other than the one they are bound to so they're restricted to that specific file that they are bound to and they can't run longer than 30 seconds and lastly they're subject to apps script trigger quota limits if you need further information on that have a look at the developer guidelines that i also will be linking below if you need to know exactly what those quotas are for the different apps or services that you will be using the simple triggers on now let's focus on the second category the installable triggers what are they for what do they offer us they are similar to the simple triggers but they have some added capabilities like you can call services that require authorization some something that we cannot do with our simple triggers they offer several several additional types of events including the time driven triggers which is something that i will be also showing afterwards further on in this tutorial they can also be controlled programmatically also there i will show you a code snippet how that is done but they do also come with some restrictions so why don't we have a look at those again here similar or actually exactly the same to the simple triggers installable triggers will not run if someone only has viewing or commenting rights on that specific file then the script executions and api requests do not cause triggers to run similar like with the simple triggers here are a different example so we have our form response dot submit method and that will not trigger the form submit trigger or that will not trigger the forms okay so that's an infinite loop but anyway um so here you actually need a person to submit in order for the submit trigger to run doing it programmatically with form response.submit method will not work then there are also some further restrictions um which we're going to have a look at now this one is quite special the scripts run under the account of the person who created it so let's say um jane creates a script with an installable trigger but adam runs it that means that let's say in a case of a mail merge the emails that are sent out they're not sent out with adam's account but instead with jane's account so that is definitely something to take into account when you're using installable triggers that the account of the creator will be used not the account of the executor a given account cannot see triggers installed from a second account even though the first accounts can still activate those triggers so depending on your use case this might be something that you will have to keep or might have to consider okay so those were the capabilities of the simple triggers and the installable triggers as well as their age restrictions that they have now let's have a look at um kind of like a sub category of installable triggers so to now dive a bit into these um different installable triggers let's start with where do we access them so i'm here in the script editor and if you click here on the clock symbol you're brought to the triggers interface and click here on add trigger and then you pop up this new window appears here and i could say choose which function to run um on open would it make sense in this case it would be get stats so let's say i want in this scenario i want i don't know every day at a certain time i want the statistics from youtube from our youtube videos to get to be fetched and written to any or to the google sheets file we have prepared so i say get stats and then what i'm interested here is select event source so i'm going to say not from spreadsheet but instead time driven and then i'm going to select a type of time based trigger here i have a couple of options let's say i don't know a day timer i want that to run every day at uh in the evening sometimes and 9 to 10 pm now the interesting thing is here um google says that we're giving a time window from nine to ten so we're not specifying exactly at nine google will run this code somewhere within that time so it's you know it's subject to kind of like a random time it could be 9 15 p.m um but no matter what random time in that time window that is selected that will then be consistently um every single day so that's just something to keep in mind um there you go i could say save and then that would execute every day at in this time window next we're on to another subcategories of installable triggers these are called the google workspace application triggers it's the one down here you see select event type on open wait a minute that isn't that the simple trigger on open actually it's not it's like the simple trigger so it's used in the same situation but in this case if we used installable trigger called on open then we could also call services that require authorization so you remember how i said that um let's say a mail merge it could not run if you say on the simple trigger someone opens a document and you want an email to be sent out just simply because that person opened up that document that would not work with an installable trigger on the other hand the on open installable trigger that would work because it has access to um to the gmail app to get the authorization to run any given code that you want to run in the gmail app previously i showed you how you can edit or how you can add installable triggers manually by going into the script editor on the trigger section then you know adding i don't know a time a time based trigger now what you see here is how you can programmatically add or create a trigger in this case we're creating again a time driven trigger that will run our code in this case just my function but in your case it would be whatever function name you've given and it will run this function every six hours so that is how you can programmatically create your own installable triggers now what happens if let's say one of your installable triggers fires but somehow the code runs fails to run or there is some other error and the script cannot be executed as intended you will not see a message on your screen instead what happens is that apps script sends you an email and you can choose the frequency of of the email notifications that you receive uh receive the default actually is notify me daily and you will receive a short message with the information uh if it's script bound if i remember correctly it will even send you the link to the script so then you can check that email and and you know find the error and get your trigger executing the way it should be did you learn something about triggers in this video which you didn't know before then let me know in the comment section below what exactly it was and would you mind subscribing to this youtube channel by hitting the subscribe button below because every week i publish new videos about google workspace and at least once a month a new google apps script video and i would not want you to miss out on any of those videos [Music]
Info
Channel: saperis
Views: 3,368
Rating: 5 out of 5
Keywords: google apps script triggers, apps script triggers, how to use google apps script triggers, run google apps script, how to run apps script, google apps script trigger, google apps script, apps script, gas, google workspace, google sheets, automate google sheets, coding, chanel greco, Saperis
Id: KC7pBjD3GGw
Channel Id: undefined
Length: 14min 31sec (871 seconds)
Published: Tue Apr 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.