0

Currently the function runs every day at 11pm but it runs twice. I'm only needing it to run once though.

function functionsToRun ()
{
  appendValues();
  clearCells();
  addZeros();
}

function createTimeDrivenTriggers() 
{
  // Trigger everyday at 11PM
  ScriptApp.newTrigger('functionsToRun')
      .timeBased()
      .atHour(23)
      .everyDays(1)
      .create();
}
1

If you see a trigger firing twice a day, when you expect it to fire just once, you probably have two triggers rather than one.

You should not run the createTimeDrivenTriggers() function repeatedly. Every time you run it, it creates a new trigger. If you run the function twice, you get two triggers that fire every day.

You can manage all your triggers in the My Triggers dashboard. Check the triggers list and delete the ones you do not need.

If your dashboard shows just one trigger, perhaps another trigger was created in another account. Check the My Triggers dashboard with all accounts that have access to the script.

It is unclear why you are creating the trigger programmatically in the first place. See Managing triggers manually.

If you need to programmatically adjust the time the trigger fires from time to time, you should delete existing triggers before creating a new one. Try this:

function createTimeDrivenTriggers() {
  deleteTriggers_(ScriptApp.EventType.CLOCK);
  ScriptApp.newTrigger('functionsToRun')
    //...
}

/**
* Deletes all installable triggers of the type triggerType associated with the current project and current user.
*
* @param {EventType} triggerType One of ScriptApp.EventType.ON_EDIT, .ON_FORM_SUBMIT, .ON_OPEN, .ON_CHANGE, .CLOCK (time-driven triggers) or .ON_EVENT_UPDATED (Calendar events).
*/
function deleteTriggers_(triggerType) {
  // version 1.0, written by --Hyde, 7 May 2020
  var triggers = ScriptApp.getProjectTriggers();
  for (var i = 0, numTriggers = triggers.length; i < numTriggers; i++) {
    if (triggers[i].getEventType() === triggerType) {
      ScriptApp.deleteTrigger(triggers[i]);
    }
  }
}
2
  • I checked my triggers and there is only one in the dashboard. Would removing .atHour(23) fix my issue? It seems the trigger is running at 11:30pm and agian at 12:30pm every day.
    – Toko
    Nov 12 at 22:40
  • Edited the answer. Nov 13 at 9:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.