Reputation: 358
I deployed a web app, which could call the function to create a new menu when the spreadsheet is open. Below is the part I write
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);
}
and in my web app, I write
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);
}
I have a test and i am sure that the spreadsheet id has be transferred into the function, but it gives me an error of "Cannot call SpreadsheetApp.newMenu() from this context. "
I also tried
function onOpen(currentSpreadsheet) {
//Adds custom menu
var menu = [{name: "Add row", functionName: "addRow"},
{name: "Generate Invoice", functionName: "generateInvoice"}];
SpreadsheetApp.openById(currentSpreadsheet).addMenu("Custom", menu);
}
still not working. So is that means addMenu could only be used in a standalone or build-in script? but not in a google web app?
All the errors occurs when I try to use addMenu().
Upvotes: 1
Views: 3770
Reputation: 953
Your script is running in context of a web app, but the onOpen()
trigger runs only in a script bound to a Docs/Sheet/Form file. In short, you can't pass nor install an onOpen()
function into a spreadsheet from a web app.
From the documentation:
To use a simple trigger, simply create a function that uses one of these reserved function names:
onOpen(e)
runs when a user opens a spreadsheet, document, or form that he or she has permission to edit. ...
Upvotes: 3