Thanks for the question, in my case, what a try to do is a dynamic menu in a Spreadsheet, which is going to change visually (user interface), showing some menu items, in function to the user need to do, over the same active spreadsheet.
How i made this?
Custom Menus in Google Workspace
A document, spreadsheet, presentation, or form can only contain one menu with a given name. If the same script or another script adds a menu with the same name, the new menu replaces the old. Menus cannot be removed while the file is open, although you can write your onOpen() function to skip the menu in the future if a certain property is set.
Properties Service
The Properties service lets you store simple data in key-value pairs scoped to one script, one user of a script, or one document in which an add-on is used. It is typically used to store developer configuration or user preferences
The sample code
/*1.- Create the menu and define its name*/
const uiMenu = SpreadsheetApp.getUi().createMenu('MyMenu');
/*2.- Define a variable and store in propertyService*/
const properties = PropertiesService.getDocumentProperties();
const isLoad = properties.getProperty('load');
console.log(typeof Boolean(isLoad));
console.log('global', isLoad);
/* 3.- At the moment the spreadsheet is open this function is going to execute */
function onOpen(){
console.log('onOpen', isLoad);
if(Boolean(isLoad)){
uiMenu
.addItem('Send', 'send')
.addItem('End', 'end')
} else {
uiMenu
.addItem('Start', 'start')
.addItem('Load', 'load')
}
uiMenu.addToUi();
}
function start(){
SpreadsheetApp.getUi().alert('You clicked the first menu item!');
SpreadsheetApp.getActiveSpreadsheet().toast('Started');
}
function load(){
SpreadsheetApp.getUi().alert('You clicked the Second menu item!');
//set the variable 'load'
properties.setProperty('load', true);
//set the menu with the new items
uiMenu
.addItem('Send', 'send')
.addItem('End', 'end')
uiMenu.addToUi();
SpreadsheetApp.getActiveSpreadsheet().toast(`Loading data`);
}
function send() {
SpreadsheetApp.getUi().alert('You clicked the Third menu item!');
if(Boolean(isLoad)){
SpreadsheetApp.getActiveSpreadsheet().toast(`Sending data`)
}
}
function end(){
SpreadsheetApp.getActiveSpreadsheet().toast('End the process');
//You can delete or set the variable to start with the process again.
properties.deleteProperty('load');
//set the menu with the new items
uiMenu
.addItem('Start', 'start')
.addItem('Load', 'load')
.addToUi()
}