如何从同一个库生成的电子表格下拉菜单中调用库函数

时间:2012-10-03 20:26:57

标签: google-apps-script google-sheets

我创建了一个具有特定功能的库,可以创建新的电子表格菜单(使用addMenu)。我的菜单选项应该调用我库中的其他函数来做东西。

// 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.
}

现在的问题是,当我选择菜单选项时,谷歌应用脚​​本会给我一个错误消息,如

  

无法找到脚本函数 myMenuFunction

所以我尝试在菜单项

中添加前缀
menuEntries.push({name: "About", functionName: "myLibraryName.myMenuFunction"});

但它也没有用。

所以我要求提供有关如何创建可以创建链接到库中函数的菜单的库的建议。

2 个答案:

答案 0 :(得分:4)

我不是100%肯定你在说什么,但我认为这是一个已知的问题。

看起来你需要直接从菜单调用一个函数,而不是从上一级调用它。

good example here

在仔细查看您的问题时,您似乎试图在电子表格中的不同菜单中调用相同的功能。根据我链接的错误,您可能无法执行此操作,因为您需要定义本地函数并使用它来与脚本进行交互。

答案 1 :(得分:3)

是的,类似的问题。

我有大约20个电子表格,用愚蠢的代码更新所有脚本是非常无聊的:

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

每次我在主lib中添加新的菜单项。

所以我发现或多或少脏的解决方法 - 将菜单项链接到lib中的“代理”功能,并提前在客户端电子表格中创建几个类似的功能(对所有客户端电子表格执行一次)

//-----------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-----------

目前我确实使用了一点点加强版本,只允许我更新menuEntries数组。 这是:

 //-----------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-----------