App Script yearly trigger

158 views
Skip to first unread message

Clément Milewski

unread,
Mar 17, 2021, 4:41:58 PM3/17/21
to Google Apps Script Community
Greetings,
I wish to set up a yearly trigger for a script to send an email (e.g a company wishing its customers a merry Christmas every year). I am somewhat lost in the logic though. I have a couple ideas, but I am not even sure if the logic is right. My first attempt was :
function myFuncToTrigger () {
  //...
}

function myTrigger () {
  var currentDate = new Date();
  var year = currentDate.getFullYear();
  ScriptApp.newTrigger(myFuncToTrigger ).timeBased().atDate(year01).create();
}

I tried replacing atDate by eveMinutes(4) to check the behaviour, to no avail. I also thought of adding the
ScriptApp.newTrigger () into myFuncToTrigger (), but was afraid that would mean a trigger creation apocalypse (where the code
to create a new trigger is executed every 5 minutes, resulting in 2 triggers executing the code every 5 minutes, creating 2 new triggers etc, doubling every 5 minutes).
Could someone just point me to the right direction please ?

Cheers,

Edward Wu

unread,
Mar 18, 2021, 1:23:55 PM3/18/21
to google-apps-sc...@googlegroups.com
Here's what I use to schedule once-a-year triggers. You need to manually create the first one (i.e. manually set the trigger to run scheduleNextYearsPrep at 12/31/21 at 11pm).
When that runs on 12/31/21, it'll schedule *next* year's trigger. etc.

And prepYearEnd would be the function you want to run at that time.


function scheduleNextYearsPrep(){
  var date = new Date();
  var year = date.getYear()+1;
 
  var d = new Date(year, 11, 31, 23); // remember, month starts at ZERO
ScriptApp.newTrigger("prepYearEnd")
   .timeBased()
   .at(d)
   .create();
  ScriptApp.newTrigger("scheduleNextYearsPrep")
   .timeBased()
   .at(d)
   .create();
}

function prepYearEnd(){
  // do your actions here
}


--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/58102115-1424-49b5-9607-c165047d2d5do%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages