Reputation: 57
In Google Sheets Script, how do I stop a function from executing more than x e.g. 5 times a day?
For instance, I have a trigger that executes a function every 20 minutes that sends an email based on a condition in the function, however I only want it to send this email a maximum of 5 times a day. How would I do this?
Upvotes: 2
Views: 74
Reputation: 2608
As other users recommended, you can store a value in the Project Properties, and use it to count how many times the script has been executed. I recommend you to use a condition to compare the new property with 5
.
Remind to convert the property returned result type to Integer, as it is an String by default, otherwise you won't be able to add 1 in each execution.
As an example we can use the setProperty to modify the already saved value, for example:
var userProperties = PropertiesService.getUserProperties();
var newValue = +userProperties.getProperty('Execution_times') + 1;
userProperties.setProperty('Execution_times', newValue); // Updates stored value.
Upvotes: 2