Google-sheets – Multiple custom menu order

google sheetsgoogle-apps-script

I have set up multiple custom menus in one of my Google Sheets. While the items in the menu stay in order, the 3 menus that I have created are always switching places. Is there a way for me to order them in the script to stop them changing order.

I have used and onOpen function (see below) for each of them

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  // Or DocumentApp or FormApp.

I am using multiple projects. All 3 menus show up and work on the workbook, but they keep changing order.

Best Answer

There is no way to set the order in which the projects will be loaded. If you want to keep the menus in the same order all the time, they should be handled from the same project and from one on open function.

NOTE: I didn't tried this yet but I'm pretty sure that it should work

Example

function onOpen(e){
  var menu1 = SpreadsheetApp.getUi().createMenu('Menu 1');
  var menu2 = SpreadsheetApp.getUi().createMenu('Menu 2');
  var menu3 = SpreadsheetApp.getUi().createMenu('Menu 3');

  // Populate each menu here

  /**
   * Add the menus to the UI in the order that you want them
   */ 
  menu1.AddToUi();
  menu2.AddToUi();
  menu3.AddToUi();
}

The reason is that the project load order is not deterministic, it depends on several factors including but not limited to Google data centers load and the fact that each on open function has it's own execution instance, they are ran asynchronously , some time one could be the faster but other times the slower.