Reputation: 715
I added some menus and enable a custom installable onEdit trigger in onOpen() as the following. All menus are set as expected, but the custom installable onEdit trigger is not set. It seems like it does not execute those lines for deleting and creating triggers. What is happening here? Solutions?
function onOpen() {
var ass = SpreadsheetApp.getActiveSpreadsheet();
ass.addMenu("TriggerOn", [null, {name: "Disable OnEditTrigger", functionName: "disableOnEditTrigger"}]);
ass.addMenu("AlertOn", [null, {name: "Alert Off", functionName: "alertOff"}]);
ass.addMenu("EmailBodyHtml", [null, {name: "TextBody", functionName: "textbody"}]);
ass.addMenu("Setup", [null, {name: "MasterTemplate", functionName: "setMasterTemplate"}]);
//delete all previous triggers
var allTriggers = ScriptApp.getScriptTriggers();
// Loop over all triggers
for(var i=0; i < allTriggers.length; i++)
ScriptApp.deleteTrigger(allTriggers[i]);
// Create onEdit trigger using the Spreadsheet
var onEditTrigger = ScriptApp.newTrigger("myOnEdit")
.forSpreadsheet(ass)
.onEdit()
.create();
ScriptProperties.setProperty("alert", "1");
ScriptProperties.setProperty("emailbody", "htmlbody");
}
Upvotes: 2
Views: 1951
Reputation: 46822
To corroborate the other response, I'd suggest you read the documentation on triggers where it is explained that simple triggers (like onEdit and onOpen) have a limited set of possible actions since they run without the authorization of the user... Adding a trigger is (although I never tried it in such a trigger) beyond its possible action as it needs to be authorized. If you want to be sure, run it manually and see if the script asks for authorization (or may be it has done it already so try with another user account).
You could create an installable onOpen trigger that will prompt the user to authorize the concerned services, in this case you can name it differently to avoid confusion (although the name is non important in this case)
Hoping it is clear enough.
Upvotes: 1
Reputation: 3171
From checking with a try-catch around your code (and dumping to Browser.MsgBox), it looks like you're bumping into security restrictions imposed by Apps Scripts. "You do not have permission to call getScriptTriggers"
- similar problems exist with newTrigger
, or basically anything with ScriptApp
, it seems. This isn't mentioned specifically in the restrictions here, but it seems likely this is the issue. You may need to either hardcode this trigger in your script, or perhaps cause it to be created via another menu item.
Upvotes: 0