Reputation: 73
Description of how to set 2 different time triggers daily which executes two functions.
Therefore the function createDayTrigger() will create two time triggers see below.
Flow:
1.) Trigger 1 writes at 00:00:00 the current timestamp into cell A2
2.) Trigger 2 writes at 22:00:00 the current timestamp into cell A3
3.) Next day, same game
function TimeTrigger1() {
deleteTrigger(TimeTrigger1);
//This will write the current timestamp into cell A2
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Tickerprüfung');
sheet.getRange('A2').setValue(new Date());
}
// This will delete TimeTrigger1 after the function has been executed
function deleteTrigger(TimeTrigger1) {
var triggers = ScriptApp.getProjectTriggers();
triggers.forEach(function(t) {
if (t.getHandlerFunction() == TimeTrigger1) ScriptApp.deleteTrigger(t);
});
}
function TimeTrigger2() {
deleteTrigger(TimeTrigger2);
// This will write the current timestamp into cell A3
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Tickerprüfung');
sheet.getRange('A3').setValue(new Date());
}
// This will delete TimeTrigger2 after the function has been executed
function deleteTrigger(TimeTrigger2) {
var triggers = ScriptApp.getProjectTriggers();
triggers.forEach(function(t) {
if (t.getHandlerFunction() == TimeTrigger2) ScriptApp.deleteTrigger(t);
});
}
// This will start TimeTrigger1 and TimeTrigger2
function createDayTrigger() {
// For TimeTrigger1 this will set the date to the current date + 1 Day and the time to 00:00:00
var d1 = new Date();
d1.setDate(d1.getDate()+ 1);
d1.setHours(0);
d1.setMinutes(0);
d1.setSeconds(0);
ScriptApp.newTrigger('TimeTrigger1').timeBased().at(d1).create();
// For TimeTrigger2 this will set the date to the current date + 1 Day and the time to 22:00:00
var d2 = new Date();
d2.setDate(d2.getDate()+ 1);
d2.setHours(22);
d2.setMinutes(0);
d2.setSeconds(0);
ScriptApp.newTrigger('TimeTrigger2').timeBased().at(d2).create();
}
// This function has to be started once to get "createDayTrigger()" started.
function startScript() {
ScriptApp.newTrigger('createDayTrigger').timeBased().everyDays(1).atHour(22).create();
}
Upvotes: 0
Views: 106
Reputation: 201358
As one of several methods, if you want to use one trigger for everyDays
, how about the following modified script?
In this modification, the flow is as follows.
createDayTrigger
is set as the trigger of everyDays
which runs at near "21:00".
startScript()
is required to be run only one time. After this function is run, the time-driven trigger is automatically run.createDayTrigger
is run, the functions of TimeTrigger1
and TimeTrigger2
are set as the trigger of at
which run at "22:00" and "00:01".TimeTrigger1
is run, the function of TimeTrigger
is run and the trigger of TimeTrigger1
is cleared.TimeTrigger2
is run, the function of TimeTrigger
is run and the trigger of TimeTrigger2
is cleared.By this cycle, the function of TimeTrigger
can be run by TimeTrigger1
and TimeTrigger2
at "22:00" and "00:01".
function TimeTrigger(functionName) {
deleteTrigger(functionName);
// Script writes two timestamps in cells A2 and A3.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Tickerprüfung');
sheet.getRange('A2').setValue(new Date());
var date = new Date();
date.setHours(22);
date.setMinutes(0);
date.setSeconds(0);
date.setMilliseconds(0);
sheet.getRange('A3').setValue(date);
var date1 = new Date();
date1.setHours(23);
date1.setMinutes(59);
date1.setSeconds(59);
date1.setMilliseconds(00);
sheet.getRange('A4').setValue(date1);
}
function TimeTrigger1() {TimeTrigger("TimeTrigger1")}
function TimeTrigger2() {TimeTrigger("TimeTrigger2")}
function deleteTrigger(functionName) {
var triggers = ScriptApp.getProjectTriggers();
triggers.forEach(function(t) {
if (t.getHandlerFunction() == functionName) ScriptApp.deleteTrigger(t);
});
}
function createDayTrigger() {
var d1 = new Date();
d1.setHours(22);
d1.setMinutes(0);
d1.setSeconds(0);
ScriptApp.newTrigger('TimeTrigger1').timeBased().at(d1).create();
var d2 = new Date();
d2.setDate(d2.getDate() + 1);
d2.setHours(0);
d2.setMinutes(1); // Do you want to run at "00:01"?
d2.setSeconds(0);
ScriptApp.newTrigger('TimeTrigger2').timeBased().at(d2).create();
}
// This function has to be started once to get "createDayTrigger()" started.
function startScript() {
ScriptApp.newTrigger('createDayTrigger').timeBased().everyDays(1).atHour(21).create();
}
d2.setDate(d2.getDate() + 1);
is required to be added. Because when the trigger is set, "00:01" is tomorrow.From your replying comment and updated question, I could understand that you want to set the trigger times to 00:00 and 22:00 of the same day. In the above answer, the 1st trigger and 2nd trigger is 22:00 of today and 00:00 of tomorrow, respectively. But, this cycle is continued by the trigger installed with createDayTrigger
. By this, after the next cycle, 00:00 and 22:00 of the same day are triggered. But it seems that this is not useful. So I proposed the modified script. Please modify my above script as follows.
function createDayTrigger() {
var d1 = new Date();
d1.setDate(d1.getDate() + 1); // <--- Added
d1.setHours(22);
d1.setMinutes(0);
d1.setSeconds(0);
ScriptApp.newTrigger('TimeTrigger1').timeBased().at(d1).create();
var d2 = new Date();
d2.setDate(d2.getDate() + 1);
d2.setHours(0);
d2.setMinutes(1); // Do you want to run at "00:01"?
d2.setSeconds(0);
ScriptApp.newTrigger('TimeTrigger2').timeBased().at(d2).create();
}
createDayTrigger()
is run, the trigger times of 00:00 and 22:00 are set as the same day.In your script reflected my latest modification, the following flow is run.
startScript()
is run, createDayTrigger
is installed at the time-driven trigger at near "21:00" every day.
createDayTrigger()
is run at 21:00 of 2019-10-09, TimeTrigger1
and TimeTrigger2
are installed as the time-driven trigger at "22:00 of 2019-10-10" and "00:00 of 2019-10-10", respectively.When TimeTrigger2
is run at "00:00 of 2019-10-10", 00:00:00,22:00:00,23:59:59
are put in the cells of "A2:A4" of the sheet of Tickerprüfung
.
This values are from your script.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Tickerprüfung');
sheet.getRange('A2').setValue(new Date());
var date = new Date();
date.setHours(22);
date.setMinutes(0);
date.setSeconds(0);
date.setMilliseconds(0);
sheet.getRange('A3').setValue(date);
var date1 = new Date();
date1.setHours(23);
date1.setMinutes(59);
date1.setSeconds(59);
date1.setMilliseconds(00);
sheet.getRange('A4').setValue(date1);
When TimeTrigger1
is run at "22:00 of 2019-10-10", 22:00:00,22:00:00,23:59:59
are put in the cells of "A2:A4" of the sheet of Tickerprüfung
.
This is the flow of the current script reflected my modification in your question.
In your question, you want to achieve the functions of TimeTrigger1
and TimeTrigger2
at 22:00 and 00:00 of the same day. About this, I could confirm that my understanding is correct at this replying. I think that this can be achieved.
Upvotes: 1