Extend Google Spreadsheets UI with a Google Web Ap

2019-02-23 13:49发布

问题:

I created a script for Google Spreadsheets, this script just adds a new menu item as described here Custom Menu Items in a Spreadsheet. Then I deploy this script as a Web App and I want all users who install the app to be able to see the new menu item. And I'm stuck at this point.

As I understand, when you deploy a script as a Web App, onOpen functions looses it's meaning. So, inside doGet I create custom trigger for onOpen event, attach myOnOpen handler to it and inside myOnOpen I add a menu item, but the item doesn't show up.

Here's my code:

function doGet() {
    var newSheet = SpreadsheetApp.create("new sheet");
    var newId = newSheet.getId();
    ScriptProperties.setProperty('newId', newId); 

    ScriptApp.newTrigger("myOnOpen")
        .forSpreadsheet(newId)
        .onOpen()
        .create();
};

function myOnOpen() {
    var newId = ScriptProperties.getProperty('newId');
    var sheet = SpreadsheetApp.openById(newId);

    var entries = [ { name : "Show bingo", functionName : "Bingo" } ];
    sheet.addMenu("My Menu", entries);
};

function Bingo() {
    Browser.msgBox("Bingo!");
};

So, when a user who installed the app opens "new sheet" spreadsheet, he doesn't see the "My Menu". What am I doing wrong here? Why the menu item doesn't show up? At the end of the day I want to create a Web App which extends Google Spreadsheets UI with additional menus and dialogs.

Any advice is welcome. Thanks!

回答1:

When you deploy the script as a web app, users of the web app will only see content you "return" from the doGet function. You can create content using UiApp or HtmlService and return this content to be rendered in a browser. Your new menu items are attached to a spreadsheet, so they can only be displayed when the user goes to the spreadsheet itself (activating the onOpen trigger, etc).