I would like to get a trigger to run a specified amount of time after an edit occurs.
I'm thinking that I could someone combine an edit trigger and a time based trigger for this functionality, but am not clear how.
To provide some additional detail.
I have a Google Sheet where I track my insulin usage. I am supposed to check my blood sugar 2 hours after I administer my insulin.
When I administer the insulin I make an entry into Google Sheets, time/amount/etc... When I make this entry I would like to create a trigger that will run in 2 hours to send me an email notification as a reminder to check my blood sugar again and make a new entry.
This is how I use time triggers in Google App Script.
Function to create the time trigger.
function createTriggger(name,action,time){
// Time is in minutes
var trigger = ScriptApp.newTrigger(action)
.timeBased()
.after(time*60*1000)
.create();
var triggerID = trigger.getUniqueId();
// Store trigger id to delete the trigger once it has executed
PropertiesService.getScriptProperties().setProperty(name, triggerID);
};
This name
is the name of the property to store the trigger id, so you can delete the trigger after it executes. action
is the name of the function you want the trigger to execute, and time
is in minutes.
On the last line of your function that you want the trigger to execute, use this line to grab the trigger id and delete it from your trigger list. Put the name of your trigger id property in name
.
var triggerID = PropertiesService.getScriptProperties().getProperty(name)
deleteThisOne(triggerID)
Here is the delete funciton:
function deleteThisOne(id){
var triggers = ScriptApp.getProjectTriggers();
for(var i=0;i<triggers.length;i++){
if(triggers[i].getUniqueId() == id){
ScriptApp.deleteTrigger(triggers[i]);
break;
};
};
};
Look at google scripts Installable Triggers for Limitations
When your edit trigger fires, you could use a ClockTrigger
- see the ClockTriggerBuilder documentation and example, which allows you to either set a time relative to the current execution time, or at an absolute time.
You can use a work around like this:
Set '0' value in some corner cell in spreadsheet which you won't modify ever. Hide that column.
Set onEdit trigger on spreadsheet and write its function, which sets the value '0'.
Set another trigger which is triggered every 10 minutes to increment that cell value by 10 every time. Also add this to that function:
if(cellValue%120 == 0)
{
setReminder(); //function to send you reminder
cellValue = 0;
}