SUMMARY: Is it possible to ask for authorization onOpen()?
DETAILED VERSION: I have a spreadsheet with buttons that gets distributed to a lot of people. When any button is pressed, some functions that require permissions are called, so Google Apps Script shows this popup:
AFTER this is accepted, everything runs well, since it has authorization now. However, I want to run things that require permissions BEFORE a button is pushed, when the Workbook is opened. However, if you place authorization-requiring code into an onEdit or onOpen function, it runs with no privileges by default and crashes halfway, rather than showing the popup and asking for permissions.
Here is some code that exemplifies this - crashing instead of asking for permissions to create a trigger (also works for CalendarApp, etc.):
function onOpen(e) {
Browser.msgBox("This is shown to the user");
try {
ScriptApp.newTrigger('someFunction').forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet()).onEdit().create();
}
catch (e) {
Browser.msgBox(e);
}
Browser.msgBox("This message is never shown to the user if there is no try catch because it crashes");
}