Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
183 views
in Technique[技术] by (71.8m points)

javascript - Execution failed: You do not have permission to call getProjectTriggers

I am very new to Javascript and google apps but slowly finding my feet (with help)

I've written a script that does various things, one part of this script, is installing a trigger. Here it is.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

enter image description here

enter image description here


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);   
  }
}

enter image description here

enter image description here


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');
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...