简体   繁体   English

从Chrome商店安装插件后,Google表格和文档脚本无法创建插件菜单

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

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: 最常见的罪魁祸首是代码中的全局变量,该变量试图在未经授权的情况下访问Google服务,例如:

var doc = DocumentApp.getActiveDocument(); 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. 警告:当您的onOpen(e)函数运行时,将加载整个脚本并执行所有全局语句。 These statements execute under the same authorization mode as onOpen(e) and will fail if the mode prohibits them. 这些语句在与onOpen(e)相同的授权模式下执行,并且如果该模式禁止它们,则将失败。 This preventsonOpen(e) from running. 这会阻止open(e)运行。 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. 如果您发布的加载项无法添加其菜单项,请查看浏览器的JavaScript控制台以查看是否引发了错误,然后检查脚本以查看是否onOpen(e)函数或全局变量调用了不允许的服务在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 提前致谢

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM