- you can make it easy for users to run Google Apps script Functions that are associated with your spreadsheet (even non-technical users can use those functions)
- • You can link to functions that require the user's authorization from a menu item and users will be asked to approve the necessary permissions when they select that menu item.
in this part, we will try to build an admin menu and add it to the menu bar
var createCuMenu = ()=> {
var MenuName = "admin pannel";//the menu name
var Menu = SpreadsheetApp.getUi().createMenu(MenuName);// init the menu obj
Menu.addItem("here the menu item name" , "namedFunction");//add item to the menu the forst prm is the name of that item and the other is for the name of the function
Menu.addToUi();//add the the menu to the UI
}in the code above I add a menu name admin pannel using :
const menu = SpreedSheetApp.getUi().CreateMenu(/*menu name*/);add an item to the menu :
menu.addItem(/*name of the item*/, /*name of the fucntion*/);add everything to the spreadsheet page :
me.addToUi()and like that, we were able to add a menu to our sheet
now let's add more items to the admin menu
-
REMEMBER
/**** to create menu ****/ SpreedSheetApp.getUi().CreatMenu(/* menu name */);
//to add menu to the item wee need to use the function addItem() Menu.addItem(/*name*/, /*function*/);
// we can add everything to the spreadsheet by the function .addToUI();
hands-on 🤓
1 - let make a function to make menu creation easy and creep the code clean
function menuInit(name){
return (SpreedSheetApp.getUi()
.createMenu(name))
}2 let add items to our menu :
function createCustomMenu(){
var menu = menuInit("adminPannel");
menu.addItem("item A","itemA");
menu.addItem("item B","itemB");
menu.addToUi()
}3- let's's add a separator :
separator in google apps is the sane as
in HTML!
to use separator :
menu.addSeparator()with Little magic
function createCustomMenu(){
var menu = menuInit("adminPannel");
menu.addItem("item A","itemA");
menu.addSeparator()
menu.addItem("item B","itemB");
menu.addToUi()
}4 - add a sub-menu inside the menu :
to add a sub-menu all we need to do is to create another menu and add it by using the function ( addSubMenu(sub-menu) )
function createCustomMenu(){
var menu = menuInit("adminPannel");
menu.addItem("item A","itemA");
menu.addSeparator()
menu.addItem("item B","itemB");
var subMenu = menuInit("subMenu");
subMenu.addItem("item D","item D");
menu.addSubMenu(subMenu);
menu.addToUi()
}