/**
* @file Pass ars to the menu item
* @url
*/
var MENU = [
{
caption: 'Menu item 1',
functionName: 'itemMenu',
},
{
caption: 'Menu item 2',
functionName: 'itemMenu',
},
{
caption: 'Menu item 3',
functionName: 'itemMenu',
},
{
caption: 'Anoher function',
functionName: 'someAnotherFunction'
}
];
/**
*
*/
function onOpen() {
var ui = SpreadsheetApp.getUi();
var menu = ui.createMenu('Test');
MENU.forEach(function(item, i) {
menu.addItem(item.caption, item.functionName + i);
});
menu.addToUi();
}
/**
*
* @param {{
* item: string,
* order: number
* }} e
*/
function itemMenu(e) {
var caption = e.item.caption;
var order = e.order;
Browser.msgBox(
Utilities.formatString('%sth of the menu was clicked: %s', order + 1, caption)
);
}
/**
*
* @param {{
* item: string,
* order: number
* }} e
*/
function someAnotherFunction(e) {
var caption = e.item.caption;
var order = e.order;
SpreadsheetApp.getActive().toast(
Utilities.formatString('%sth of the menu was clicked: %s', order + 1, caption)
);
}
(function(self) {
MENU.forEach(function(item, i) {
self[item.functionName + i] = function() {
return self[item.functionName]({ item: item, order: i });
};
});
})(this);