Google apps script 为什么当我创建菜单时,它会说;无法从此上下文调用SpreadsheetApp.newMenu()&引用;
我部署了一个web应用程序,它可以在电子表格打开时调用该函数来创建一个新菜单。 下面是我写的部分Google apps script 为什么当我创建菜单时,它会说;无法从此上下文调用SpreadsheetApp.newMenu()&引用;,google-apps-script,google-sheets,google-apps,Google Apps Script,Google Sheets,Google Apps,我部署了一个web应用程序,它可以在电子表格打开时调用该函数来创建一个新菜单。 下面是我写的部分 function onOpen(){ var url = "https://script.google.com/a/slt.org.au/macros/s/AKfycby2pFGHc3qWaxnD4WGTLMEAPMUocohzH_-OsUPxwqi8kmWfRZs8/exec"; var currentSpreadsheet = SpreadsheetApp.getActive().get
function onOpen(){
var url = "https://script.google.com/a/slt.org.au/macros/s/AKfycby2pFGHc3qWaxnD4WGTLMEAPMUocohzH_-OsUPxwqi8kmWfRZs8/exec";
var currentSpreadsheet = SpreadsheetApp.getActive().getId();
var requestAction = "onOpen";
UrlFetchApp.fetch(url+"?requestAction="+requestAction);
}
在我的网络应用程序中,我写
function onOpen(currentSpreadsheet) {
//Adds custom menu
SpreadsheetApp.openById(currentSpreadsheet);
var menu = [{name: "Add row", functionName: "addRow"},
{name: "Generate Invoice", functionName: "generateInvoice"}];
SpreadsheetApp.getActiveSpreadsheet().addMenu("Custom", menu);
}
我有一个测试,我确信电子表格id已被转移到函数中,但它给我一个错误“无法从此上下文调用SpreadsheetApp.newMenu()
我也试过了
function onOpen(currentSpreadsheet) {
//Adds custom menu
var menu = [{name: "Add row", functionName: "addRow"},
{name: "Generate Invoice", functionName: "generateInvoice"}];
SpreadsheetApp.openById(currentSpreadsheet).addMenu("Custom", menu);
}
仍然不起作用。这是否意味着addMenu只能在独立脚本或内置脚本中使用?但不是在谷歌的网络应用中
当我尝试使用addMenu()时,所有错误都会发生。您的脚本正在web应用程序的上下文中运行,但是
onOpen()
触发器仅在绑定到文档/工作表/表单文件的脚本中运行。简而言之,您无法将onOpen()
函数从web应用程序传递或安装到电子表格中
从:
要使用简单触发器,只需创建一个使用
这些保留函数名:
onOpen(e)
在用户打开电子表格、文档或表单时运行
他或她有权编辑
错误信息很清楚,addmenu仅在电子表格绑定脚本中有效,而在独立web应用程序中无效。你为什么要尝试这个设置?