So I was in IT class and we were making custom menus for Google sheets, I got curious and I wanted to make some sort of loop of menus. Something with this structure: Menu -> Submenu 1 -> Submenu 2 -> Submenu 3 -> … and so on.
I thought the best option was to make a ‘for’ loop, but I have tried so much and it does not work. After trying things the closest thing I have is a code that surprisingly (for me) has a loop but only creates one submenu (Menu -> Submenu 1). The number of repetitions I set in the loop doesnt matter. What is the problem?? Thanks beforehand and sorry for bad english or stupid question (I’m a beginner).
function onOpen(){
var sheet = SpreadsheetApp;
var menu = sheet.getUi().createMenu('Menu'); // create a menu
var parentMenu = menu; // keep a reference to the parent menu
var subMenu;
for(i=1 ; i<=5 ; i++){
subMenu = sheet.getUi().createMenu('Submenu ' + i).addItem('Filling', 'function');
parentMenu.addSubMenu(subMenu); // add the subMenu to the parentMenu
parentMenu = subMenu; // update the parentMenu for the next iteration
}
menu.addToUi(); // apply everything
}
Dasterman800 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.