Google Sheets & Docs Script fails to create add-on

2019-01-29 12:14发布

问题:

Possible cause is the following:

Usually this is caused by a problem with the Authorization Lifecycle, specifically the opening stage.

The most common culprit is a global variable in the code that tries to access Google services without authorization, like:

var doc = DocumentApp.getActiveDocument();

See the documentation:

Warning: When your onOpen(e) function runs, the entire script is loaded and any global statements are executed. These statements execute under the same authorization mode as onOpen(e) and will fail if the mode prohibits them. This preventsonOpen(e) from running. If your published add-on fails to add its menu items, look in the browser's JavaScript console to see if an error was thrown, then examine your script to see whether the onOpen(e) function or global variables call services that aren't allowed in AuthMode.NONE.

Here is my script:

    function onOpen(e) {
    SpreadsheetApp.getUi().createAddonMenu()
      .addItem('Browse Templates', 'browseTemplates')
      .addToUi();
}

function onInstall(e) {
  onOpen(e);
}

function browseTemplates(){
    collectBasicData();
   // Display a modal dialog box with custom HtmlService content.
   var htmlOutput = HtmlService
   .createTemplateFromFile("Gallery").evaluate()
   .setWidth(700)
   .setHeight(510);
   SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Spreadsheet123 - Template Vault');
}

function collectAllData(){
  var sheet = SpreadsheetApp.openById(SPREADSHEET_ID).getSheetByName(DATA_SHEET);
  DATA = sheet.getDataRange().getValues();
  return DATA;
}

function collectBasicData(){
  var sheet = SpreadsheetApp.openById(SPREADSHEET_ID).getSheetByName(PIVOT_SHEET);
  var tabSheet = SpreadsheetApp.openById(SPREADSHEET_ID).getSheetByName(TAB_SHEET);
  BASIC_DATA = {
    "tab_about" : getValue(tabSheet,"B1"),
    "tab_help": getValue(tabSheet,"B2"),
    "pivot":sheet.getDataRange().getValues()
  };
  return false;
}

function getValue(sheet,addr){
  return sheet.getRange(addr).getValue().toString().replace(/^\s+|\s+$/g, '');
}

function createACopy(id){
  var docName = DocsList.getFileById(id).getName();
  return DocsList.getFileById(id).makeCopy(docName).getUrl();
}

function insertInCurrent(id){
    var destinationSpreadSheet = SpreadsheetApp.getActiveSpreadsheet();
    var sourceSheets = SpreadsheetApp.openById(id).getSheets();
    for(var i=0;i<sourceSheets.length;i++){
        var sheetName = sourceSheets[i].getName();
        var source = SpreadsheetApp.openById(id).getSheetByName(sheetName);
        source.copyTo(destinationSpreadSheet).setName(sheetName);
    }
}

Can you please help me a little or a lot.

Thanks in advance

回答1:

OK, so my code was actually correct, but my mistake was that I should have saved any changes made to my code under the new version before publishing it to the store, which I did not and therefore all changes that I made were simply ignored.

function onOpen(e) {
    SpreadsheetApp.getUi().createAddonMenu()
      .addItem('Browse Templates', 'browseTemplates')
      .addToUi();
}

function onInstall(e) {
  onOpen(e);
}