Can you make menu items in Google spreadsheet cust

2019-07-26 20:07发布

When adding menu items to a spreadsheet using spreadsheet.addMenu() is there a way to make them tickable like the item View>Normal?

3条回答
霸刀☆藐视天下
2楼-- · 2019-07-26 20:29

I tried using the answer @KalyanReddy provided, but ran into a problem because the spreadsheet UI compresses spaces and trims menu names. By trial and error, I found a work-around.

Screenshot

Here's a utility function that generates checkbox-aligned menus:

/**
 * Make a checkbox menu item. Returns a string with the original text aligned to
 * leave room for a check mark. (Dependent on font, compatible with default
 * font used in Google Spreadsheets as of May 2013. YMMV.)
 *
 * @param {String} name Original item name
 * @param {boolean} check True if menu item should have a check mark
 *
 * From an idea by Kalyan Reddy, http://stackoverflow.com/a/13452486/1677912
 */
function buildItemName( name, check ) {
  // Prepend with check+space, or EM Space
  return ( check ? "✓ " : "\u2003" ) + name;
}

To make real use of this, you should write a single function that tracks the state of and builds your menu items by calling buildItemName, then call it & updateMenu() whenever state changes.

查看更多
Anthone
3楼-- · 2019-07-26 20:35

If you really wanted to do it, maybe you can do something clunky like the following and call updateMenu to add a check mark whenever necessary.

function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var menuEntries = [ {name: "  item 1", functionName: "one"},
                      {name: "  item 2", functionName: "two"} ];
  ss.addMenu("myMenu", menuEntries);
}

function one() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  ss.updateMenu("myMenu", [ {name: "✓ item 1", functionName: "one"},
                            {name: "  item 2", functionName: "two"} ]);
  // ... item 1 actions
}

function two() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  ss.updateMenu("myMenu", [ {name: "  item 1", functionName: "one"},
                            {name: "✓ item 2", functionName: "two"} ]);
  // ... item 2 actions
}
查看更多
叼着烟拽天下
4楼-- · 2019-07-26 20:43

The short answer is no.

The long(er) answer is that menu items can only call functions so there is no use for it to show a 'state'. Nevertheless you could imagine to use some kind of header in the spreadsheet itself that shows the state you want to show, either by modifying a text value or by changing a color to show that some function has been called.

I'm supposing that the final goal of your request is to show if a function has been called or not... If I'm wrong then please explain was was your intention.

查看更多
登录 后发表回答