google-apps-scriptgoogle-apps-script-addongoogle-apps-script-editor

How do I call CreateMenu() from within an Apps Script Library?


I'm building a library within Apps Script and would like to make it pretty self sustaining. Essentially, if someone wants to use this Lib in their Google Sheet, they'd need simply to put in the following code in their Sheets Script

function onOpen(){
  myLib.initialize();
}

And this would set up the sheet as needed. One of the things I do here is to actually add a menu item in the Sheet itself. Therefore, in my Library code, I'll have something like;

function initialize(){
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Custom Menu')
      .addItem('First item', 'someCallback')
      .addToUi();
}

The problem with this approach is that when the menu item is clicked, the "someCallback" function cannot be found. This makes sense since it resides in a separate library. It should be called using dot notation like this: myLib.someCallback

Is there some way that I can get the Library Identifier at run time so that I can create the string required to create the menu?

I can do a few things which are not my first choices such as:

Maybe there's some Runtime way that I can get the Library Identifier?


Solution

  • Answer:

    Unfortunately at the moment this isn't possible to do.

    More Information:

    I tested out a few things using both the this keyword and the eval() function in order to either dynamically get the redefined identifier of myLib or calling someCallback directly in the initialize function, however even embedding the callback function inside initialize:

    funciton initialize() {
      var cb = function someCallback() {
        // do stuff
      };
    
      var ui = SpreadsheetApp.getUi();
      ui.createMenu('Custom Menu')
          .addItem('First item', 'cb')
          .addToUi();
    

    or:

    function initialize() {
      function someCallback() {
        //do stuff
      }
      var ui = SpreadsheetApp.getUi();
      ui.createMenu('Custom Menu')
          .addItem('First item', 'someCallback')
          .addToUi();
    }
    

    throw the same error with either cb or someCallback not defined.

    Problems with this:

    To answer your direct question

    Maybe there's some Runtime way that I can get the Library Identifier?

    The answer is yes. Kind of.

    The this keyword is pretty powerful, and at runtime you can use it to get the name of the user-set identifier in the following way:

    function onOpen(){
      myLib.initialize(this);
    }
    

    And in myLib.initialize():

    function initialize(name) {
      console.log(Object.keys(name));
    }
    

    The console showing: ['myLib', 'onOpen']

    This issue is, is that while the names of imported libraries come before the list of functions defined in the user's script, if more than one library has been imported you have no way of telling which of the keys refers to your library, and which refer to the others. Resultantly just calling name[0] won't cut it.

    Doing Some Digging:

    I took to Google's Issue Tracker and discovered that there are some feature requests which have very similar use cases to yours:

    While the use-cases aren't exactly the same, the underlying issue on both is that anonymous functions nor parameters can be passed to libraries that use the UI methods of Apps Script.

    There is also this related feature request which asks to implement a catch-all style method which runs a method if a library method which doesn't exist is called:

    What to do:

    Normally, I would suggest following the Issue Tracker links and clicking the star. In this case, however, as your use case isn't exactly defined in the scope of the aforelinked feature requests, I would urge you to use this link to file an Apps Script feature request and detail all the information you put here in your question.

    As far as workarounds go; the best thing you can do in the meantime is to document that the initialize() function needs to be passed the identifying name of the library. It's not an elegant solution by any standard, but given the complexity of this and the feature requests that are yet to be realised this may be the easiest option.

    It might be possible to loop through the Object.keys(this) array but I feel like this is a very hacky workaround which could introduce more problems than necessary (especially if, for example, another library creator also happens to have a function called someCallback...)

    References: