Google app script how to add parameter to addItem function call
This is a snippet of a google application script for adding a menu to google docs as specified in the submenu - additem method. calls the menuItem2 function, but the snippet does not contain a sample of how to call addItem when you want to add parameters to the function call, or is it not possible?
function onOpen() {
var ui = SpreadsheetApp.getUi();
// Or DocumentApp or FormApp.
ui.createMenu('Custom Menu')
.addItem('First item', 'menuItem1')
.addSeparator()
.addSubMenu(ui.createMenu('Sub-menu')
.addItem('Second item', 'menuItem2'))
.addToUi();
}
function menuItem2() {
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.alert('You clicked the second menu item!');
}
function menuItem2(PARAMETER_HERE) {
// codes
}
+3
source to share
1 answer
You cannot add parameters to menu-called functions.
A simple workaround is to store the parameters somewhere else (like in scripts) and read those parameters if the parameter is undefined.
function menuItem2(PARAMETER) {
// if PARAMETER is undefined then read default parameter in scriptProperties
// codes
}
With this configuration, you can call the menuItem2 function from elsewhere in the script using the "normal" parameter and it will be processed as expected.
+4
source to share