How to add menu in google docs, sheet & form

In case we want to extend the capability of existing google docs, google sheet or google form according to our specific need which is not existing in the current implementation.

We can add a menu and on clicking that menu we can execute a script that can do some task for us. For example, we need to encrypt content of document and current google doc implementation doesn’t support this feature then we can write our own function that can encrypt the data and replace it with current plain text on document, but we need one menu option that provides an interface to user for encrypting the data and later on some interface is required that can decrypt it and make it human readable.

Steps to add a menu in google docs, google sheet or google form

  • Open google docs.
  • Open any document
  • From Tools menu select Script editor…
  • add following code in it.
 function onOpen() {  
  var ui = DocumentApp.getUi();  
  ui.createMenu('User Menu')  
    .addItem('Item-1', 'menuItem1')  
    .addSeparator()  
    .addSubMenu(ui.createMenu('Cascaded-menu')  
      .addItem('Item-2', 'menuItem2'))  
    .addToUi();  
 }  
 function menuItem1() {  
  DocumentApp.getUi()  
    .alert('You have clicked the menu item - 1');  
 }  
 function menuItem2() {  
  DocumentApp.getUi()  
    .alert('You have clicked the menu item - 2');  
 }  
  • save it and run it in script editor.
  • once we come back to google doc we can find user menu in menu bar.

 

One Reply to “How to add menu in google docs, sheet & form”

Leave a Reply

Your email address will not be published. Required fields are marked *