3

I'm working with a spreadsheet that has multiple sheets, I would like my custom toolbar menu to change dynamically depending on which sheet is open at the time.

Is there an event that can be called when the active spreadsheet is changed? Or some other method of updating the menus as I change between sheets.

4

1 回答 1

2

使用这个小代码,可以根据活动工作表创建一个动态菜单。

代码

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);
}

例子

我为您创建了一个示例文件:动态菜单

评论

仅当编辑活动工作表中的任何单元格时,菜单才会更改。

于 2013-08-25T18:10:55.697 回答