I've written a script that does various things, one part of this script, is installing a trigger:
function setTrigger() {
var ss = SpreadsheetApp.getActive();
var triggers = ScriptApp.getProjectTriggers();
Logger.log('Amount of triggers ' +triggers.length);
var j = 0;
for (var i = 0 ; i < triggers.length;i++){
if(triggers[i].getHandlerFunction() == 'getNotes' ){j++;}
}
Logger.log('Amount of matching triggers ' +j);
if(j == 0 ){ScriptApp.newTrigger("getNotes").forSpreadsheet(ss).onFormSubmit().create();}
}
Here is the problem I'm having.
The above code is called in the onOpen()
trigger.
When I open the sheet, and check logs, my trigger isn't installed and I get the following message.
Execution failed: You do not have permission to call getProjectTriggers
When I run the onOpen()
manually. The trigger is installed.
I currently own the spreadsheet, but ideally, i'd like to share it with people and the trigger installs and works.
Any ideas how to fix this error with getting project triggers called in onOpen
?
Following your comment :
Change the name of your function to anything else and create an installable trigger that runs this function on SS open, as mentioned in the doc, simple triggers can't do anything that requires authorization but installable ones do.
Edit : complete example with your code
After saving this in a spreadsheet and setting manually a trigger on open with the specialonOpen function (and accepting the authorization request), I refreshed the browser and get it working with your menu and the new trigger as well (see illustration below -in french because I used another gmail account with old spreadsheet version, my english one has new version and onFormSubmit doesn't work in new versions)
function specialonOpen() {
var ss = SpreadsheetApp.getActive();
var items = [
{name: 'Refresh TOL Notes', functionName: 'getNotes'},
null, // Results in a line separator.
{name: 'Coming Soon!', functionName: 'menuItem2'}
];
ss.addMenu('TOL Toolkit', items);
var sheet = ss.getSheetByName('New Notes');
if (sheet == null) {var ss = SpreadsheetApp.getActive();
ss.insertSheet('New Notes',0 );
var sheet = ss.getSheetByName('New Notes');
sheet.deleteColumns(3, 18);
sheet.deleteRows(12, 88 );
sheet.getRange('a1').setValue('Agent Name');
sheet.getRange('b1').setValue('Tol Notes');
getNotes();
sheet.setColumnWidth(1, 120);
sheet.setColumnWidth(2, 400);
setTrigger() }
getNotes();
setTrigger()
}
function setTrigger() {
var ss = SpreadsheetApp.getActive();
var triggers = ScriptApp.getProjectTriggers();
Logger.log('Amount of triggers ' + triggers.length);
var j = 0;
for (var i = 0; i < triggers.length; i++) {
if (triggers[i].getHandlerFunction() == 'getNotes') {
j++;
}
}
Logger.log('Amount of matching triggers ' + j);
if (j == 0) {
ScriptApp.newTrigger("getNotes").forSpreadsheet(ss).onFormSubmit().create();
}
}
function getNotes() {
var s = SpreadsheetApp.getActive();
var sheet1 = s.getSheetByName('New Notes');
if (sheet1 == null) {
var s = SpreadsheetApp.getActive();
s.insertSheet('New Notes', 0);
var sheet1 = s.getSheetByName('New Notes');
sheet1.deleteColumns(3, 18);
sheet1.deleteRows(12, 88);
sheet1.getRange('a1').setValue('Agent Name');
sheet1.getRange('b1').setValue('Tol Notes');
sheet1.setColumnWidth(1, 120);
sheet1.setColumnWidth(2, 400);
}
}
EDIT 2 : a solution with a Browser message to suggest install from a menu.
function onOpen() {
var ss = SpreadsheetApp.getActive();
var items = [
{name: 'Install this script', functionName: 'setTriggers'},
];
ss.addMenu('Custom Menu', items);
Browser.msgBox('please run INSTALL from the custom menu');
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With