Answer the question
In order to leave comments, you need to log in
How to pass arguments to a function called from a menu?
Good afternoon. There is a task - to make the menu which items are loaded from the table.
With a static menu, I do this:
function onOpen() {
//Выполняется при открытии
SpreadsheetApp
.getUi()
.createMenu('Меню')
.addItem('Функция1','function1')
.addItem('Функция2','function2')
.addToUi();
};
Answer the question
In order to leave comments, you need to log in
Let's say you have a menu description for some function
var MENU = [
{
caption: 'Пункт меню 1',
functionName: 'itemMenu',
},
{
caption: 'Пункт меню 2',
functionName: 'itemMenu',
},
{
caption: 'Пункт меню 3',
functionName: 'itemMenu',
},
];
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();
}
itemMenu
works like thisfunction itemMenu(e) {
var caption = e.item.caption;
var order = e.order;
Browser.msgBox(
Utilities.formatString('Был нажат %sй пункт меню: %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);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question