I wrote a simple script trying to programmatically create a script from the onEdit function
function onEdit() {
test();
}
function test() {
triggerLater();
}
function customMsgBox() {
Browser.msgBox("hello world");
}
function triggerLater() {
var date = new Date();
date.setMinutes(date.getMinutes() + 1);
try {
var oneTimeOnly = ScriptApp.newTrigger("customMsgBox")
.timeBased()
.at(date)
.create();
return oneTimeOnly.getUniqueId();
}
catch (e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.toast("error");
}
}
If I try to run onEdit from the script editor the trigger is created but every edit on the spreadsheet get a "error" message in a toast
Can someone help me to understand ?
The
onEdit
simple event handler function has limited permissions (i.e. it can not send emails, open the calendar, etc). Because it runs without the users permissions. Therefore, it can not set up a trigger (which do not have any restrictions and would be a huge security bug).If you expected that the trigger should be created under your account, use the installable on edit trigger. First, rename your onEdit function to something else (so it is not triggered as a simple event handler), then go to the Resources menu and pick your function to run on spreadsheet edit events. Take a look at the docs for more.