2

I would like my function freezeSunday to run at exactly at 1pm on Sunday. Since it does not appear that you can trigger a function to run at a precise time (only at a randomly selected time within a range), I wrote the following code.

function createTimeTriggerEveryNMinutes() {
 ScriptApp.newTrigger("freezeSunday")
   .timeBased()
   .everyMinutes(1)
   .create();
}
function freezeSunday() {
  const date = new Date();
  const minute = date.getMinutes();
  if (minute == 0) {
    var spreadsheet = SpreadsheetApp.getActive();
    spreadsheet.getRange('A37:Z69').activate();
    spreadsheet.getRange('A2:Z34').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
    }
}
function triggerDeleteHandler() {
  var Triggers = ScriptApp.getProjectTriggers();
  for (var i = 0; i < Triggers.length; i++) {
    if (Triggers[i].getHandlerFunction() == "freezeSunday") {
      ScriptApp.deleteTrigger(Triggers[i])
    }
  }
}

The first function sets a trigger for the freezeSunday function to run every minute. The freezeSunday function only performs its action when the minute is 0. Otherwise it does nothing. The last function deletes the "every minute" trigger. All of this works fine if I run the first function, createTimeTriggerEveryNMinues, manually. The freezeSunday function starts running every minute, and at 1pm it performs its action.

However, if I set a trigger to run createTimeTriggerEveryNMinutes sometime between 12-1pm instead of running it manually, things don't work. The freezeSunday function never runs, and the trigger for it in my list of triggers displays "Disabled" in red text. If I click on the info icon for that, it says "Trigger disabled for unknown reason."

Is it not permitted to set a trigger for a function from another triggered function?

0

1 Answer 1

1

I think I have discovered the issue. In the code below, if I use the createDoubleTrigger function to create the triggers for createTimeTriggerEveryNMinutes and triggerDeleteHandler, everything works correctly.

If instead I create triggers for createTimeTriggerEveryNMinutes and triggerDeleteHandler using the pull-down menus in the Apps Script interface, then freezeSunday never runs, and the trigger for it created by createTimeTriggerEveryNMinutes shows "Disabled."

I think the general solution is to always created triggers programatically.

function createDoubleTrigger() {
  ScriptApp.newTrigger("createTimeTriggerEveryNMinutes")
    .timeBased()
    .onWeekDay(ScriptApp.WeekDay.SUNDAY)
    .atHour(12)
    .nearMinute(40)
    .everyWeeks(1)
    .create();
  ScriptApp.newTrigger("triggerDeleteHandler")
    .timeBased()
    .onWeekDay(ScriptApp.WeekDay.SUNDAY)
    .atHour(13)
    .nearMinute(20)
    .everyWeeks(1)
    .create();
}
function createTimeTriggerEveryNMinutes() {
 ScriptApp.newTrigger("freezeSunday")
   .timeBased()
   .everyMinutes(1)
   .create();
}
function freezeSunday() {
  const date = new Date();
  const minute = date.getMinutes();
  if (minute == 0) {
    var spreadsheet = SpreadsheetApp.getActive();
    spreadsheet.getRange('A2:Z34').copyTo(spreadsheet.getRange('A37:Z69'), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
  }
}
function triggerDeleteHandler() {
  var Triggers = ScriptApp.getProjectTriggers();
  for (var i = 0; i < Triggers.length; i++) {
    if (Triggers[i].getHandlerFunction() == "freezeSunday") {
      ScriptApp.deleteTrigger(Triggers[i])
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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