How to call a library function from the drop-down menu of a spreadsheet generated by the same library

I created a library that has a specific function that creates new spreadsheet menus (using addMenu). The parameters of my menu must call other functions in my library in order to do something.

// Bare Minimum Deployment on a blank spreadsheet with // my library registered (called myLibraryName for this example). function onOpen() { myLibraryName.setMenus(); // creating new drop-down menus } function onEdit(event) { myLibraryName.doEvent(event); // sending the onEdit event to a function in my library. } 

Now the problem is that when I select the menu option, the google script application gives me an error message like

Script myMenuFunction not found

So, I tried to add a prefix to my menu item

 menuEntries.push({name: "About", functionName: "myLibraryName.myMenuFunction"}); 

But it also does not work.

So, I am asking for suggestions on how to create a library that can create menus related to functions within the library.

+6
source share
2 answers

I am not 100% sure that you are calling, but I think this is a known issue.

It looks like you need to call the function directly from the menu, and not call it from the top level.

Here is a good example here .

Looking closer at your question, it looks like you are trying to call the same function in different menus in your spreadsheet. Based on the error I'm connected with, you probably won't be able to do this, since you need to define a local function and use it to interact with the script.

+4
source

Yes, a similar problem.

I have about twenty tables, and it's pretty boring to update all my scripts with stupid code:

  function doSomething(){ myLib.doSomething();} 

every time I add a new entry to the main library.

So, I found a more or less dirty workaround - write the links in the "proxy" in lib and create some similar functions in the client tables in advance (do this once for all client tables)

 //-----------LIB----------- function libMenu() { var mySheet = SpreadsheetApp.getActiveSpreadsheet(); var menuEntries = [ {name: "Increment current cell", functionName: "processMenuEntry0"}, {name: "Do something with row", functionName: "processMenuEntry1"} ]; mySheet.addMenu("Library Functions", menuEntries); } function processMenuEntry0() { incrementCurrentCell();} function processMenuEntry1() { doSomethingWithRow(); } //-----------LIB----------- //-----------CLIENT----------- function onOpen() { Library.libMenu(); } function processMenuEntry0() {gTracking.processMenuEntry0();} function processMenuEntry1() {gTracking.processMenuEntry1();} function processMenuEntry2() {gTracking.processMenuEntry2();} function processMenuEntry3() {gTracking.processMenuEntry3();} // etc. // I have reserved twenty menu entries in a such way //-----------CLIENT----------- 

I am currently using a slightly improved version that allows me to update the menuEntries array. There he is:

  //-----------LIB----------- var menuEntries = [ {name: "Increment current cell", functionName: "incrementCurrentCell"}, {name: "Do something with row", functionName: "doSomethingWithRow"} ]; //returns menu entries with changed 'functionName' parameter (-> "processMenuEntry" + id) function convertMenuEntries() { var newMenuEnties=[]; for (var i=0; i< menuEntries.length ;i++){ if (menuEntries[i] == null) {// for line separators newMenuEnties.push(null); continue; } newMenuEnties.push({name: menuEntries[i]["name"], functionName: "processMenuEntry" + i}); } return newMenuEnties; } function libMenu() { var mySheet = SpreadsheetApp.getActiveSpreadsheet(); mySheet.addMenu("Library Functions", convertMenuEntries()); } // get function name from menuEntries array and call it function processMenuEntry(id){ this[menuEntries[id]["functionName"]](); } function processMenuEntry0() {processMenuEntry(0);} function processMenuEntry1() {processMenuEntry(1);} // etc. //-----------LIB----------- //-----------CLIENT----------- function onOpen() { Library.libMenu(); } function processMenuEntry0() {gTracking.processMenuEntry0();} function processMenuEntry1() {gTracking.processMenuEntry1();} function processMenuEntry2() {gTracking.processMenuEntry2();} function processMenuEntry3() {gTracking.processMenuEntry3();} // etc. //-----------CLIENT----------- 
+3
source

Source: https://habr.com/ru/post/926883/


All Articles