Google Sheets Add-On onOpen() 仅 运行 一次,但 onEdit() 没问题

Google Sheets Add-On onOpen() only running once but onEdit() is fine

我正在为 Sheet 开发一个名为 LastEdit 的 Add-On。我遇到的问题是第一次安装 Add-On 时 onOpen() 仅 运行s:

那么,为什么 onOpen() 运行 不再??

这是代码顺便说一句:

var documentProperties = PropertiesService.getDocumentProperties();
var ui = SpreadsheetApp.getUi();
var sheet = SpreadsheetApp.getActive();
var editCell;

// This function runs whenever cells are edited
function onEdit() {
  updateLastEdit();
}

// This function updates the editCell contents
function updateLastEdit() {
  // Fetch the coordinate of the designated LastEdit cell
  editCell = documentProperties.getProperty('editCell');
  // If our docProp editCell is 0 then we don't have a LastEdit to update
  if (editCell != 0.0) {
    sheet.getRange(editCell).setValue(new Date() + '');
  }
}

// This function will be used to designate the new LastEdit cell
function lastEdit() {
  editCell =  sheet.getActiveCell()
  SpreadsheetApp.getUi().alert("LastEdit cell added at: " + editCell.getA1Notation());
  documentProperties.setProperty('editCell', editCell.getA1Notation());
  // Once we've stashed the location of the LastEdit cell we move to update the LastEdit cell contents
  updateLastEdit();
}

// When the Sheet opens add a new custom menu
function onOpen(e) {

  // This is the alert for testing purposes
  ui.alert("onOpen() has run!");

  // Contingency strategy; set an Installable Trigger to perform the onOpen tasks
  //ScriptApp.newTrigger("openTrigger").forSpreadsheet(sheet).onOpen().create();

  // Creating custom menu for this app
  newMenu = ui.createAddonMenu();
  newMenu.addItem('Insert LastEdit Cell', 'insertLastEdit');
  newMenu.addItem('Disable LastEdit Cell', 'disableLastEdit');
  newMenu.addItem('LastEdit Cell Location', 'locateLastEdit');
  newMenu.addSeparator();
  newMenu.addItem('About', 'aboutLastEdit');
  newMenu.addToUi();
  // I know this can be shortened, but I removed/tested each individual
  // item to see if any of these were derailing onOpen()
}

// Insert LastEdit Cell
function insertLastEdit() {
  lastEdit();
}

// Disable LastEdit Cell
function disableLastEdit() {
  documentProperties.setProperty('editCell', 0);
  SpreadsheetApp.getUi().alert("LastEdit cell disabled");
}

// Fetch and display the LastEdit cell location via popup
function locateLastEdit() {
  editCell = documentProperties.getProperty('editCell');
  if (editCell != 0) {
    SpreadsheetApp.getUi().alert("LastEdit cell is located at: " + editCell);
  } else {
    SpreadsheetApp.getUi().alert("No LastEdit cell is active ");
  }
}

// About! 
function aboutLastEdit() {
  ui.alert("About LastEdit", SpreadsheetApp.getUi().ButtonSet.OK);
}

// After installation just run the onOpen function
function onInstall(e) {
  onOpen(e);
}

// If an Installable Trigger is required...
function openTrigger() {
    // I had a duplicate of onOpen() in here, but have abandoned this strategy
}

我在这里错过了什么? Add-on 授权似乎有很多移动部分(绑定脚本、简单触发器、在文档中启用、authMode.LIMITED 与 FULL 等)我查看了 Add-on 授权生命周期页面,但它似乎表明此过程主要是自动处理的。

在此先感谢您的帮助!

"If an add-on is installed for a user but not enabled in the current document, onOpen(e) runs in AuthMode.NONE; if the add-on is enabled in the current document, onOpen(e) runs in AuthMode.LIMITED. If the add-on is both installed and enabled, the enabled state takes precedence, since LIMITED allows access to more Apps Script services."

当脚本处于 AuthMode.NONE 状态时,它无法访问属性服务。由于您有调用此服务的全局代码,因此如果 onOpen() 执行失败则加载项。

您需要将此全局代码移动到一个函数中。

在这里查看更多内容https://developers.google.com/apps-script/add-ons/lifecycle#authorization_modes