假设您想将相关标题和函数名称分别存储在 Sheet1 列 A 和 B 中,您可以简单地调用 .getValues() 并遍历该数据。
/**
* SHEET1 VALUES
* --------------------------------------
* Column A Column B
* Row 1 Class One functionOne
* Row 2 Class Two functionTwo
* Row 3 Class Three functionThree
*/
function onOpen(e) {
// Initialize the menu & submenu
var ui = SpreadsheetApp.getUi();
var menu = ui.createMenu('Menu Name').addItem('First label', 'menuItem1').addSeparator();
var subMenu = ui.createMenu('Submenu Name');
// Get the submenu item data from Sheet1
// Values in column A represent the captions that will appear in the submenu
// Values in column B represent the name of the function to be called
var values = SpreadsheetApp.getActive().getSheetByName('Sheet1').getRange('A:B').getValues();
for (var rowIndex = 0; rowIndex < values.length; ++rowIndex) {
var rowData = values[rowIndex];
var caption = rowData[0]; // Column A value
var functionName = rowData[1]; // Column B value
if (caption != '' && functionName != '') { // Exclude empty strings
subMenu.addItem(caption, functionName);
}
}
// Add the submenu to the menu, and finally to the UI
menu.addSubMenu(subMenu).addToUi();
}
function menuItem1() { Browser.msgBox('menuItem1'); }
function functionOne() { Browser.msgBox('functionOne'); }
function functionTwo() { Browser.msgBox('functionTwo'); }
function functionThree() { Browser.msgBox('functionThree'); }
在原始帖子中,发生错误是因为 .addSubMenu() 被调用值 [9p1] 而不是 Menu object。此外,根据我对问题的理解,我认为菜单 item 旨在在循环中创建,而不是每个类的单独子菜单。所以这是两个需要解决的问题。
我还假设您不想为每个菜单项运行runBatch1,因为这会破坏创建不同菜单项的目的。根据documentation,添加菜单项的方法需要两个字符串:
-
caption – 菜单项的标签。
-
functionName – 用户选择项目时要调用的函数的名称。
因此,您可以在拨打submenu.addItem() 时替换任何字符串。
var caption = "Caption";
var functionName = "functionName";
var subMenu = ui.createMenu('Submenu Name');
subMenu.addItem(caption, functionName);
其他场景
项目对象数组
或者,您可以定义要包含的各种子菜单项的数组(或对象映射),而不是将它们存储在电子表格中。在这里,我使用了一个对象数组来清楚地定义标题和函数名称值。
function onOpen(e) {
// Initialize the menu & submenu
var ui = SpreadsheetApp.getUi();
var menu = ui.createMenu('Menu Name').addItem('First label', 'menuItem1').addSeparator();
var subMenu = ui.createMenu('Submenu Name');
// Array of items to include in the submenu
var items = [
{ caption: 'Class One', functionName: 'functionOne' },
{ caption: 'Class Two', functionName: 'functionTwo' },
{ caption: 'Class Three', functionName: 'functionThree' }
];
for (var i = 0; i < items.length; ++i) {
var item = items[i];
subMenu.addItem(item.caption, item.functionName);
}
// Add the submenu to the menu, and finally to the UI
menu.addSubMenu(subMenu).addToUi();
}
function menuItem1() { Browser.msgBox('menuItem1'); }
function functionOne() { Browser.msgBox('functionOne'); }
function functionTwo() { Browser.msgBox('functionTwo'); }
function functionThree() { Browser.msgBox('functionThree'); }
字幕字符串操作
由于该方法接受任何字符串,因此您可以执行任何字符串操作,从而生成有效的函数名称。在此示例中,我仅将字幕存储在数组中,并根据字幕值动态生成函数名称。 (您也可以从 A 列中提取标题名称并应用相同的操作。)
function onOpen(e) {
// Initialize the menu & submenu
var ui = SpreadsheetApp.getUi();
var menu = ui.createMenu('Menu Name').addItem('First label', 'menuItem1').addSeparator();
var subMenu = ui.createMenu('Submenu Name');
// Array of captions to include in the submenu.
// Will generate function names from this.
var captions = ['Class One', 'Class Two', 'Class Three'];
for (var i = 0; i < captions.length; ++i) {
var caption = captions[i];
subMenu.addItem(caption, 'function' + caption.split(' ')[1]);
}
// Add the submenu to the menu, and finally to the UI
menu.addSubMenu(subMenu).addToUi();
}
function menuItem1() { Browser.msgBox('menuItem1'); }
function functionOne() { Browser.msgBox('functionOne'); }
function functionTwo() { Browser.msgBox('functionTwo'); }
function functionThree() { Browser.msgBox('functionThree'); }