是的,类似的问题。
我有大约 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-----------