Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Google Sheets Script Custom Menu: Setup and Guide

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To create a custom menu in Google Sheets, you must use Apps Script to write an onOpen() function that utilizes the getUi() and createMenu() methods. This allows you to add a personalized menu to the top toolbar, enabling you to run complex scripts, automate repetitive tasks, or trigger custom functions with a single click rather than navigating the script editor. This is particularly useful for shared spreadsheets where other users need to run scripts without seeing the underlying code.

What You'll Need

  • A Google account with access to Google Sheets.
  • A Google Sheet where you want the custom menu to appear.
  • Basic familiarity with the Google Apps Script editor (though no prior coding experience is required to follow this guide).
  • The necessary permissions to edit the spreadsheet and authorize scripts.

Step-by-Step

  1. Open the Apps Script Editor: Open your Google Sheet. In the top menu, navigate to Extensions > Apps Script. This will open a new tab containing the script editor associated with your specific spreadsheet.
  2. Clear the Default Code: You will see a default function called myFunction(). Delete all existing code in the editor window to start with a clean slate.
  3. Write the onOpen Function: Copy and paste the following code into the editor. The onOpen() function is a reserved trigger in Google Apps Script that runs automatically every time the spreadsheet is loaded.
  4. function onOpen() {
      var ui = SpreadsheetApp.getUi();
      ui.createMenu('My Custom Tools')
          .addItem('Run First Task', 'firstTaskFunction')
          .addItem('Run Second Task', 'secondTaskFunction')
          .addSeparator()
          .addItem('Help/About', 'showHelp')
          .addToUi();
    }
  5. Create the Target Functions: The menu items above are just buttons; they do not do anything until you define the functions they call. You must create functions that match the names used in the addItem method (e.g., firstTaskFunction). Add these below your onOpen function:
  6. function firstTaskFunction() {
      SpreadsheetApp.getUi().alert('You clicked the first task!');
    }
    function secondTaskFunction() {
      var sheet = SpreadsheetApp.getActiveSheet();
      sheet.getRange('A1').setValue('Task 2 Completed');
    }
    function showHelp() {
      SpreadsheetApp.getUi().alert('This menu helps you automate your workflow.');
    }
  7. Save the Project: Click the disk icon (Save) in the toolbar or press Ctrl+S (Cmd+S on Mac). Give your project a name, such as "Custom Menu Project," and click OK.
  8. Execute the onOpen Function Manually: To see the menu without refreshing the entire browser page, select onOpen from the function dropdown menu at the top of the editor and click the "Run" button.
  9. Authorize the Script: Since this is the first time running the script, Google will prompt you for permissions. Click "Review Permissions," select your Google account, click "Advanced," and then click "Go to [Project Name] (unsafe)". Finally, click "Allow." Note that the "unsafe" warning appears because you are the developer of an unverified script for your own use.
  10. Verify the Menu: Go back to your Google Sheets tab. You should now see a new menu item titled "My Custom Tools" appearing to the right of the standard "Help" menu. Click it to see your custom items and test their functionality.

Common Mistakes

One of the most frequent errors is a mismatch between the string name in the addItem method and the actual function name defined in the script. For example, if you write .addItem('Run Task', 'runTask') but name your function function run_task(), the script will throw an error saying the function cannot be found when the menu item is clicked. JavaScript is case-sensitive, so runTask and RunTask are treated as two different functions.

Another common mistake is forgetting to include the .addToUi() method at the end of the menu chain. The createMenu and addItem methods build the menu object in the background, but the menu will not actually render in the spreadsheet interface until addToUi() is called. If your menu doesn't appear after refreshing, check that this final method is present.

Users also frequently forget that onOpen() only runs when the spreadsheet is opened or refreshed. If you make changes to the menu structure in the script editor, those changes will not appear in the sheet until you either refresh the browser page or manually run the onOpen function from the editor.

Tips

To keep your menu organized, use .addSeparator(). This creates a thin horizontal line between menu items, which is visually helpful when you have a long list of tools or when you want to group related functions together (e.g., grouping "Data Import" tools separately from "Report Generation" tools).

If you have a very large number of scripts, consider creating sub-menus. You can achieve this by using createMenuItem() within another menu. This prevents the main toolbar from becoming cluttered and allows for a hierarchical organization of your automation tools.

For a more professional feel, use SpreadsheetApp.getUi().alert() or Browser.msgBox() to provide feedback to the user. When a user clicks a menu item that triggers a long-running script, they may think the sheet has frozen. Adding a simple alert at the beginning or end of the function lets them know the process has started or finished successfully.

You can also use the onInstall() trigger. While onOpen() works for the owner and editors, if you are deploying your script as an Add-on, onInstall() ensures the menu is created immediately after the user installs the add-on, without requiring them to refresh the page.

Troubleshooting

If the menu does not appear after you have saved the script and refreshed the page, first check the Execution Log in the Apps Script editor. Click the "Executions" tab on the left sidebar. Look for the onOpen function; if it shows a "Failed" status, click on it to see the specific error message. This usually points to a syntax error like a missing bracket or a typo in a method name.

If you receive a "Permission Denied" error when clicking a menu item, it is likely that the script requires a scope that was not authorized during the initial setup. This happens if you add new functionality (like sending emails or accessing other files) after the first time you ran the script. To fix this, go back to the script editor and manually run the specific function that is failing. This will trigger the authorization prompt again, allowing you to grant the new necessary permissions.

In cases where the menu exists but clicking an item does nothing, check for "Silent Failures." This occurs when the code runs without crashing but doesn't produce a visible result (e.g., a script that writes data to a hidden sheet). Add console.log("Function started"); to the top of your function and check the Execution Log to confirm the function is actually being triggered.

If the script editor is unresponsive or won't save, try clearing your browser cache or opening the sheet in an Incognito/Private window. Sometimes multiple Google accounts signed into the same browser session can cause authorization conflicts in Apps Script.

Frequently Asked Questions

Can other people see and use my custom menu?

Yes, anyone with edit access to the spreadsheet will see the custom menu. However, the first time they click a menu item, they will be prompted to authorize the script under their own Google account, just as you were during setup.

Will the menu disappear if I close the spreadsheet?

No, the menu is tied to the script project associated with the spreadsheet. It will automatically reappear every time the spreadsheet is opened because of the onOpen() trigger.

Can I add images or icons to the custom menu?

No, the standard Google Sheets UI menu only supports plain text. You cannot add custom icons, images, or change the font and color of the menu items.

Is there a limit to how many items I can add to the menu?

While there is no hard-coded limit, adding too many items makes the menu difficult to navigate. It is recommended to use sub-menus if you have more than 7-10 items.

Does the custom menu work on the Google Sheets mobile app?

No, custom menus created via Apps Script are only visible and functional on the desktop version of Google Sheets. They do not appear in the iOS or Android mobile applications.

Can I make the menu appear only for certain users?

You can wrap the ui.createMenu logic inside an if statement that checks the current user's email using Session.getActiveUser().getEmail(). This allows you to show administrative tools only to specific email addresses.