7

我创建了一个库,它具有创建新电子表格菜单(使用 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"});

但它也不起作用。

因此,我正在寻求有关如何创建一个库的建议,该库可以创建链接到库中函数的菜单。

4

2 回答 2

4

我不是 100% 确定你在打电话,但我认为这是一个已知问题。

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

这里有一个很好的例子

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

于 2012-10-03T20:42:50.647 回答
4

是的,类似的问题。

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

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

每次我在主库中添加新菜单项时。

所以我发现了或多或少的肮脏解决方法 - 将菜单条目链接到 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-----------
于 2012-10-12T11:51:54.907 回答