Google Apps Script (V8); why can't I use an object instance inside of onOpen?

Vince

Why can't I use an object instance inside of onOpen?

I'm experimenting with Google Apps Script using the new V8 runtime. I wrote some simple code which makes an instance of a class in the onOpen function and tries to associate a call to a method on that instance when I click on a menu entry.

When I click the associated menu entry, I get a "Script function not found" error. However, when I either create a global instance of the class or create a local instance in another function it works fine.

I tried logging the value of either a local instance or a global instance, but it only shows an empty object: {}.

Is this a bug, or some detail I missed while reading the documentation?

/** Application Class */
class Application {
    /**
     * ShowUi
     */
    showUi() {
        // const html = HtmlService.createHtmlOutputFromFile('Ui');
        const html = HtmlService.createHtmlOutput('<h1>Hello World</h1>');
        const ui = SpreadsheetApp.getUi();
        ui.showModalDialog(html, 'User Interface');
    }
}

const global_app = new Application();

/** onOpen */
function onOpen() {
    const app = new Application();
    const ui = SpreadsheetApp.getUi();
    const menu = ui.createMenu('JSClass Example');

    console.log('app, local scope:');
    console.log(app);

    console.log('app, global scope');
    console.log(global_app);

    menu.addItem('Show UI (local)', 'app.showUi');
    menu.addItem('Show UI (global)', 'global_app.showUi');
    menu.addItem('Show UI (global fn)', 'showUi');

    menu.addToUi();
}

/** showUi */
function showUi() {
    const app3 = new Application();
    app3.showUi();
}

My bound Spreadsheet for this code is here. I think you'll have to make a copy and click through some scary-looking warnings to actually run it, though.

chuckx

In your example, the onOpen() method is executed solely to populate the menu, but not when a menu option is selected.

When you select a menu item, it's performed in a fresh execution context and the state from within the onOpen() function from the previous execution (i.e. to populate the menu) is not carried over.


Another approach is using a static method, which you can call without having to create an instance of the class.

class Application {
    static showUi() {
        const html = HtmlService.createHtmlOutput('<h1>Hello World</h1>');
        const ui = SpreadsheetApp.getUi();
        ui.showModalDialog(html, 'User Interface');
    }
}

function onOpen() {
    const app = new Application();
    const ui = SpreadsheetApp.getUi();
    const menu = ui.createMenu('V8 Menu Test');
    menu.addItem('Show UI', 'Application.showUi');
    menu.addToUi();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

I can't handle object properties in google apps script

Can I use in Google Apps Scripts a defined Class in a library with ES6 (V8)?

Can't use variable inside custom schema (Google Apps Script, Google Workspace)

Why can't I view Stackdriver logs for Web Apps in Google Apps Script?

Google Apps Script inheritance on V8

Why I can't create object instance?

Why can't I do a .split() on a string with a space in it with Google Apps Script?

Why I cannot use any methods in Google Apps Script?

Can I use Class objects in Google Apps Script Libraries?

I can't pass parameters from google excel to apps script

I can't get Google Apps Script run for my Gmail

Declaring private variables in a class Google Apps Script V8

Why can't I use a defined function inside another defined function,when creating an js object?

why i can't use javascript function created inside jquery object and how to declare custom function in jquery?

Why can't i create object attribute inside of an object?

How to add the onOpen trigger to a Google Sheet with Google Apps Script?

Why can't I push an object into an instance array?

I can't use <h1> inside <script>

Why can't I use Promise.resolve with an osmosis instance?

Why can't I use my instance in this trait?

Google Docs Apps Script Replace Dynamic Text Field onOpen

Run Google Apps Script from menu manually and not onOpen

Can I use Google Apps Script to make a Google Form display randomized text from a Google Sheet?

Why I can't manipulate returned object from promise inside then?

Why can't I change the vector inside of an instantiated object?

Why can't I cast an object inside of an if statement?

Why I can't access to object value inside of an array?

Why can't I use an anonymous function inside a thread macro?

Why can't I use `predict` inside a data.table?