Google Sheets & Docs Script fails to create add-on menu when add-on is installed from Chrome store

断了今生、忘了曾经 提交于 2019-11-28 12:01:17

问题


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


来源:https://stackoverflow.com/questions/27235886/google-sheets-docs-script-fails-to-create-add-on-menu-when-add-on-is-installed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!