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?
createTimeTriggerEveryNMinutes()
has missing a{
, the for statement has<
instead of<
. If this errors aren't present in you Apps Script project, please fix them here. (Google Apps Script should not allow you to save the project with syntax errors). P.S. Also, I suggest to avoid using title casing for naming regular variables, instead use camelCase, i.e. instead ofTriggers
usetriggers
. Use title case for naming classes. Ref. google.github.io/styleguide/jsguide.htmltriggerDeleteHandler
is not called byfreezeSunday
.