Reputation: 11
I have a script that needs to execute at the top of the hour every day.
I set up automatic time triggers that execute a checking script every 10 minutes to see if it's 10 minutes before the next hour. The script sends out text and email to our list and it works (although it takes 5-20 minutes to execute).
The problem is that people are receiving double text and emails.
I suspect the reason is that the checker script sets a script property, but it doesn't actually get set until the completion of the script (similar to writing cells). If the script takes longer than 10 minutes to execute, the script will start executing again without the new property and cause it to run double. My question is: "does the setProperty() happen instantly or does it happen at the end of the script"?
If it happens instantly, then I need to find another reason the script seems to exectute each one twice. Here's my checker script for reference. Note that when I execute function sendComs(time) manually, without the time triggers, it only executes once. So it seems the propblem is something below.
function sendDaily () {
var today = new Date();
var hours = today.getHours();
var mins = today.getMinutes();
var scriptProperties = PropertiesService.getScriptProperties();
var timeLastTriggered = scriptProperties.getProperty('LastTime');
switch (timeLastTriggered) {
case '0':
if (hours >= 4 && mins >= 50 || hours >= 5) {
scriptProperties.setProperties({'LastTime':'5'})
sendComs('5:00 AM');
}
break;
case '5':
if (hours >= 5 && mins >= 50 || hours >= 6) {
scriptProperties.setProperties({'LastTime':'6'});
sendComs('6:00 AM');
}
break;
case '6':
if (hours >= 6 && mins >= 50 || hours >= 7) {
scriptProperties.setProperties({'LastTime':'7'});
sendComs('7:00 AM');
}
break;
case '7':
if (hours >= 7 && mins >= 30 || hours >= 8) {
scriptProperties.setProperties({'LastTime':'7.5'});
sendComs('7:30 AM');
}
break;
case '7.5':
if (hours >= 7 && mins >= 30 || hours >= 8) {
scriptProperties.setProperties({'LastTime':'7.6'});
sendComs('7:40 AM');
}
break;
case '7.6':
if (hours >= 7 && mins >= 30 || hours >= 8) {
scriptProperties.setProperties({'LastTime':'7.7'});
sendComs('7:50 AM');
}
break;
case '7.7':
if (hours >= 7 && mins >= 30 || hours >= 8) {
scriptProperties.setProperties({'LastTime':'7.8'});
sendComs('7:55 AM');
}
break;
case '7.8':
if (hours >= 7 && mins >= 30 || hours >= 8) {
scriptProperties.setProperties({'LastTime':'8'});
sendComs('8:00 AM');
}
break;
case '8':
if (hours >= 8 && mins >= 50 || hours >= 9) {
scriptProperties.setProperties({'LastTime':'9'});
sendComs('9:00 AM');
}
break;
case '9':
if (hours >= 11 && mins >= 50 || hours >= 12) {
scriptProperties.setProperties({'LastTime':'12'});
sendComs('12:00 PM');
}
break;
case '12':
if (hours >= 14 && mins >= 50 || hours >= 15) {
scriptProperties.setProperties({'LastTime':'15'});
sendComs('3:00 PM');
}
break;
case '15':
if (hours >= 17 && mins >= 50 || hours >= 18) {
scriptProperties.setProperties({'LastTime':'18'});
sendComs('6:00 PM');
}
break;
case '18':
if (hours >= 20 && mins >= 50 || hours >= 21) {
scriptProperties.setProperties({'LastTime':'21'});
sendComs('9:00 PM');
}
break;
case '21':
if (hours >= 23 && mins >= 50 || hours < 5) {
scriptProperties.setProperties({'LastTime':'0'});
sendComs('12:00 AM');
}
break;
}
}
Upvotes: 1
Views: 75
Reputation: 2760
Properties are stored instantly.
Your script must have other problems that are causing the double messages.
Upvotes: 0
Reputation: 64040
I think this will answer the question for you. Paste it and run it.
function doesItHappenInstantly() {
var ps=PropertiesService.getScriptProperties();
ps.setProperty("testvalue",0);
for(var i=0;i<5;i++) {
var tv=ps.getProperty("testvalue");
if(tv!=i) {
SpreadsheetApp.getUi().alert("It does not happen instantly");
return;
}
ps.setProperty("testvalue",i+1);
}
SpreadsheetApp.getUi().alert("It happens instantly");
}
Warning: Don't run this with large number of iterations or you will get the error service invoked too many times for one day.
Upvotes: 1