Home > OS >  Unable to extend sub-menu with multiple options in each for Google Sheets
Unable to extend sub-menu with multiple options in each for Google Sheets

Time:07-12

The following apps scripts seems to be working for one sub-menu option (named Region which has further fuctions) created as custom menu for Google Sheets. However, when I try to extend it to one more sub-menu named "Category" I either get an error or the custom menu doesn't load in the UI.

  function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Tracker')
      .addItem('All Regions', 'AllRegions')
      .addItem('Clear Filters', 'clearallfilters')
         
      .addSeparator()
      .addSubMenu(ui.createMenu('Regions')
          .addItem('Regions1', 'Region1')
          .addItem('Regions2', 'Regions2')
          .addItem('Regions3', 'Regions3')
          .addItem('Regions4', 'Regions4')
          .addItem('Regions5', 'Regions5')

.addToUi();

I tried the following to add the new sub-menu option which doesn't seem to work. Any suggestions?

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Tracker')
      .addItem('All Regions', 'AllRegions')
      .addItem('Clear Filters', 'clearallfilters')
         
      .addSeparator()
      .addSubMenu(ui.createMenu('Regions')
          .addItem('Regions1', 'Region1')
          .addItem('Regions2', 'Regions2')
          .addItem('Regions3', 'Regions3')
          .addItem('Regions4', 'Regions4')
          .addItem('Regions5', 'Regions5')

          .addSubMenu(ui.createMenu('Regions')
          .addItem('Category1', 'Category1')
          .addItem('Category2', 'Category2')
          .addItem('Category3', 'Category3')

.addToUi();

CodePudding user response:

Try

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Tracker')
    .addItem('All Regions', 'AllRegions')
    .addItem('Clear Filters', 'clearallfilters')

    .addSeparator()
    .addSubMenu(ui.createMenu('Regions')
      .addItem('Regions1', 'Regions1')
      .addItem('Regions2', 'Regions2')
      .addItem('Regions3', 'Regions3')
      .addItem('Regions4', 'Regions4')
      .addItem('Regions5', 'Regions5')

      .addSubMenu(ui.createMenu('Categories')
        .addItem('Category1', 'Category1')
        .addItem('Category2', 'Category2')
        .addItem('Category3', 'Category3')))

    .addToUi();
}

reference

Custom Menus in Google Workspace

  • Related