How To Create Custom Menus in Google Documents and Spreadsheets

Share This:

Not all end users are programmers right? So it wouldn’t make sense to force them into a programming environment to run functions. That’s why software applications provide user friendly menus where end users can choose from a variety of options. Let’s learn how to get that helpful functionality you just created as an option in a custom menu!

Custom Menus in Google Spreadsheets

Google Apps Script Custom Menus 2

STEP ONE

Navigate to Tools > Script Editor. Google will ask you what type of project you want to create the script for. Choose Blank Project. Give your project a name.

STEP TWO

You’ll see a function was already created for you called myFunction(). Let’s rename it to onOpen().

function onOpen() {
 
}

Why call it onOpen? That’s a special event that Google Apps Script can detect. Since we want our custom menu to be available as soon as an end user opens a spreadsheet, we should write our code to do just that within the onOpen event. Learn more about Google Apps Script Events.

STEP THREE

We’ll need to do three things:

  1. Get the active spreadsheet.
  2. Define the menu options and associate each function they should run.
  3. Add the custom menu to the active spreadsheet.

Here’s the code to do just that:

function onOpen() {
 var ss = SpreadsheetApp.getActiveSpreadsheet(),
     options = [
      {name:"Say Hi", functionName:"sayHello"},
      {name:"Say Goodbye", functionName:"sayGoodbye"}
     ];
 ss.addMenu("Email", options);
}
 
function sayHello() {
 Browser.msgBox("Hello!");
}
 
function sayGoodbye() {
 Browser.msgBox("Goodbye!");
}

Let’s explain what’s going on in that code.

  1. We get the active spreadsheet and store it in a variable called ss.
  2. We define a variable called options, which is an array of objects, each having a name and function name. The name is the text you want to see in the menu option. The function name is the name of the function that should be run when the end user selects the option.
  3. We add the menu to the spreadsheet, giving it the name we want to appear as the menu name and passing in the options we defined.

STEP FOUR

Google Apps Script Custom Menus

Now let’s run the code! Make sure the function onOpen is selected. Then click Run, which looks like a play button.

STEP FIVE

You’ll be asked to authorize the script to run. This happens the first time you run a script. It will also be asked if you include any new calls to Google Apps Script APIs. Click Continue. Then click Accept. Now switch back to your spreadsheet. You should see your new custom menu with options!

Google Apps Script Custom Menus 2

Custom Menus in Google Documents

Google Apps Script Custom Menus 4

Custom menus in Google Documents are a little different compared to Spreadsheets in terms of the code.

STEP ONE

Navigate to Tools > Script Editor. Google will ask you what type of project you want to create the script for. Choose Blank Project. Give your project a name.

STEP TWO

You’ll see a function was already created for you called myFunction(). Let’s rename it to onOpen().

function onOpen() {
 
}

Why call it onOpen? That’s a special event that Google Apps Script can detect. Since we want our custom menu to be available as soon as an end user opens a document, we should write our code to do just that within the onOpen event. Learn more about Google Apps Script Events.

STEP THREE

We’ll need to do three things:

  1. Get the document user interface, create the custom menu and give it the name we want to appear as the menu name.
  2. Add the menu options and associate each function they should run.
  3. Add the custom menu to the document’s user interface.

Here’s the code to do just that:

function onOpen() {
 var menu = DocumentApp.getUi().createMenu('Email');
 menu.addItem('Say Hello', 'sayHello');
 menu.addItem('Say Goodbye', 'sayGoodbye');
 menu.addToUi();
}
 
function sayHello() {
 // Do stuff here... 
}
 
function sayGoodbye() {
 // Do stuff here...
}

Let’s explain what’s going on in that code.

  1. We get the document user interface, create a custom menu and give it a name, and store it in a variable called menu.
  2. We add menu options to the menu, passing in a name and function name. The name is the text you want to see in the menu option. The function name is the name of the function that should be run when the end user selects the option.
  3. We add the menu to the document user interface.

STEP FOUR

Google Apps Script Custom Menus 3

Now let’s run the code! Make sure the function onOpen is selected. Then click Run, which looks like a play button.

STEP FIVE

You’ll be asked to authorize the script to run. This happens the first time you run a script. It will also be asked if you include any new calls to Google Apps Script APIs. Click Continue. Then click Accept. Now switch back to your document. You should see your new custom menu with options!

Google Apps Script Custom Menus 4

 

Share This:

Related Tutorials