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');
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…