function createTriggers(){
// Trigger every 6 hours
ScriptApp.newTrigger('myFunction')
.timeBased()
.everyHours(6)
.create();
// Trigger everyday at 10:00 hours. ScriptApp.newTrigger('dailyCatchup')
.timeBased()
.atHour(10)
.everyDays(1)
.inTimezone("Asia/Kolkata")
.create();
// Trigger every Monday at 09:00 hours ScriptApp.newTrigger('myFunction')
.timeBased()
.onWeekDay(ScriptApp.WeekDay.MONDAY)
.atHour(9)
.create();
}
Deleting all triggers
function deleteallTriggers() {
// Loop over all triggers.
var allTriggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < allTriggers.length; i++) { ScriptApp.deleteTrigger(allTriggers[i]);
}
}
Capturing trigger ID
var trigger = ScriptApp.newTrigger('myFunction')
.timeBased()
.everyHours(6)
.create();
var triggerId = trigger.getUniqueId();
The following code will create a trigger, capture its triggerID and store that triggerID to the B2 column of a spreadsheet
function createTrigger(){
var trigger = ScriptApp.newTrigger('myFunction')
.timeBased()
.everyHours(6)
.create();
var triggerId = trigger.getUniqueId();
var ss = SpreadsheetApp.openByUrl("
https://docs.google.com/spreadsheets/d/1SCKp4r_3xllf1eZQFvE0LDXffdwCSdFfvEdUWAkaAsg/edit#gid=0
");
var sheet = ss.getSheetByName("Values");
var cell = sheet.getRange("B2");
cell.setValue(triggerId);
Logger.log(trigger);
Logger.log(triggerId);
Logger.log(cell.getValue());
}
Delete specific trigger (using Trigger ID)
function deleteTrigger(triggerId) {
var allTriggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < allTriggers.length; i++) {
// If the current trigger is the correct one, delete it.
if (allTriggers[i].getUniqueId() === triggerId) { ScriptApp.deleteTrigger(allTriggers[i]);
break;
}
}
}
The following code will read the triggerID value stored in the B2 column of the spreads
function deleteTrigger() {
var ss = SpreadsheetApp.openByUrl("
https://docs.google.com/spreadsheets/d/1SCKp4r_3xllf1eZQFvE0LDXffdwCSdFfvEdUWAkaAsg/edit#gid=0
");
var sheet = ss.getSheetByName("Values");
var cell = sheet.getRange("B2");
var triggerId = cell.getValue();
Logger.log(triggerId);
var allTriggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < allTriggers.length; i++) {
// If the current trigger is the correct one, delete it.
if (allTriggers[i].getUniqueId() === triggerId) { ScriptApp.deleteTrigger(allTriggers[i]);
break;
}
}
}