使用这个小代码,可以根据活动工作表创建一个动态菜单。
代码
var ss = SpreadsheetApp.getActiveSpreadsheet();
function onOpen() {
ss.addMenu("Extra",
[{name: "Sheet A", functionName: "A"},
{name: "Sheet B", functionName: "B"},
{name: "Sheet C", functionName: "C"}
]);
}
function onEdit() {
var activeSheet = ss.getActiveSheet().getName(), nMenu;
ss.removeMenu("Extra");
switch (activeSheet) {
case "A":
nMenu = [{name: "Sheet B", functionName: "B"},
{name: "Sheet C", functionName: "C"}];
break;
case "B":
nMenu = [{name: "Sheet A", functionName: "A"},
{name: "Sheet C", functionName: "C"}];
break;
case "C":
nMenu = [{name: "Sheet A", functionName: "A"},
{name: "Sheet B", functionName: "B"}];
break;
}
ss.addMenu("Extra", nMenu);
}
例子
我为您创建了一个示例文件:动态菜单
评论
仅当编辑活动工作表中的任何单元格时,菜单才会更改。