I have 30+ users each with their own spreadsheet. Each user's bound script references a custom library script that I use to keep my code centralized and easy to update.
I'm trying to use apps script to create triggers that run a specific function at a specific time. However, I can't figure out how to specify which script document the clock trigger is installed on.
Here's what I tried first:
createTimeTrigger("sendTimesheetsToHeads",1440);
function createTimeTrigger(functionToRun,minutesToWait) {
ScriptApp.newTrigger(functionToRun)
.timeBased()
.at(new Date(new Date().getTime()+minutesToWait*60*1000))
.create();
}
The above code is in the library file, but since the user is running the code, it installs the trigger on the user's container-bound script file, and not the library file. When the trigger runs, it fails because the trigger uses a function in the library that the user doesn't have direct reference to.
I need to be able to install the trigger programmatically on the independent library script file just like I can do manually. So after reviewing the apps script documentation (https://developers.google.com/apps-script/reference/script/trigger-builder#forDocument(String)), I found the forDocument() function for the trigger builder and tried this:
createTimeTrigger("sendTimesheetsToHeads",1440);
function createTimeTrigger(functionToRun,minutesToWait) {
ScriptApp.newTrigger(functionToRun)
.forDocument(<library script id here>)
.timeBased()
.at(new Date(new Date().getTime()+minutesToWait*60*1000))
.create();
}
But the above code produces the following error: "TypeError: Cannot find function timeBased in object DocumentTriggerBuilder."
So then I tried switching the order around:
function createTimeTrigger(functionToRun,minutesToWait) {
ScriptApp.newTrigger(functionToRun)
.timeBased()
.at(new Date(new Date().getTime()+minutesToWait*60*1000))
.forDocument(<library script id here>)
.create();
}
And received this error: "TypeError: Cannot find function forDocument in object ClockTriggerBuilder." It looks like the forDocument() and timeBased() functions are incompatible in the trigger builder?
Is it possible to do what I'm asking with apps script?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…