0
0
Google Sheetsspreadsheet~20 mins

Creating custom menus in Google Sheets - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom Menu Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📊 Formula Result
intermediate
2:00remaining
What is the output when the custom menu is added?

You add this script to your Google Sheets project:

function onOpen() {
  SpreadsheetApp.getUi()
    .createMenu('My Menu')
    .addItem('Say Hello', 'sayHello')
    .addToUi();
}

function sayHello() {
  SpreadsheetApp.getUi().alert('Hello!');
}

What happens when you open the spreadsheet?

Google Sheets
function onOpen() {
  SpreadsheetApp.getUi()
    .createMenu('My Menu')
    .addItem('Say Hello', 'sayHello')
    .addToUi();
}

function sayHello() {
  SpreadsheetApp.getUi().alert('Hello!');
}
AA new menu named 'My Menu' appears with an item 'Say Hello'. Clicking it shows an alert 'Hello!'.
BThe spreadsheet shows an error because 'onOpen' is not a valid function name.
CNothing happens because custom menus require manual activation.
DThe menu 'My Menu' appears but clicking 'Say Hello' does nothing.
Attempts:
2 left
💡 Hint

Think about what the onOpen function does in Google Sheets scripts.

Function Choice
intermediate
2:00remaining
Which function correctly adds a submenu to a custom menu?

You want to add a submenu called 'Options' with two items under your custom menu. Which function code correctly does this?

A
function onOpen() {
  SpreadsheetApp.getUi()
    .createMenu('Main Menu')
    .addItem('Options', 'funcOptions')
    .addToUi();
}
B
function onOpen() {
  SpreadsheetApp.getUi()
    .createMenu('Main Menu')
    .addSubMenu('Options', ['Option 1', 'Option 2'])
    .addToUi();
}
C
function onOpen() {
  SpreadsheetApp.getUi()
    .createMenu('Main Menu')
    .addSubMenu('Options')
    .addToUi();
}
D
function onOpen() {
  SpreadsheetApp.getUi()
    .createMenu('Main Menu')
    .addSubMenu(SpreadsheetApp.getUi().createMenu('Options')
      .addItem('Option 1', 'func1')
      .addItem('Option 2', 'func2'))
    .addToUi();
}
Attempts:
2 left
💡 Hint

Remember that addSubMenu requires a Menu object, not just a string.

🎯 Scenario
advanced
2:00remaining
You want a custom menu item to run a function that changes cell A1 to 'Done'. Which code snippet achieves this?

Choose the correct function that will be triggered by the custom menu item to set cell A1 to the text 'Done'.

A
function markDone() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  sheet.getRange('A1').setValue('Done');
}
B
function markDone() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
  sheet.getCell(1, 1).setValue('Done');
}
C
function markDone() {
  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.getRange(1, 1).setText('Done');
}
D
function markDone() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  sheet.getRange('A1').setText('Done');
}
Attempts:
2 left
💡 Hint

Check the correct method to set a cell's value and how to get the active sheet.

📊 Formula Result
advanced
2:00remaining
What error occurs if you call a non-existent function from a custom menu?

You add this menu item:

.addItem('Run Task', 'nonExistentFunction')

What happens when you click 'Run Task'?

ANothing happens, the menu item is ignored silently.
BA popup error saying 'Exception: The function nonExistentFunction was not found'.
CThe spreadsheet crashes and reloads automatically.
DThe menu item runs but does nothing.
Attempts:
2 left
💡 Hint

Think about what happens if the script tries to run a function that does not exist.

data_analysis
expert
2:00remaining
How many custom menu items are added by this script?

Consider this script:

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  var menu = ui.createMenu('Tools');
  menu.addItem('Item 1', 'func1');
  menu.addItem('Item 2', 'func2');
  var subMenu = ui.createMenu('More');
  subMenu.addItem('SubItem 1', 'func3');
  subMenu.addItem('SubItem 2', 'func4');
  menu.addSubMenu(subMenu);
  menu.addToUi();
}

How many clickable menu items appear under the 'Tools' menu when the spreadsheet opens?

Google Sheets
function onOpen() {
  var ui = SpreadsheetApp.getUi();
  var menu = ui.createMenu('Tools');
  menu.addItem('Item 1', 'func1');
  menu.addItem('Item 2', 'func2');
  var subMenu = ui.createMenu('More');
  subMenu.addItem('SubItem 1', 'func3');
  subMenu.addItem('SubItem 2', 'func4');
  menu.addSubMenu(subMenu);
  menu.addToUi();
}
A2
B5
C4
D3
Attempts:
2 left
💡 Hint

Count both top-level items and submenu items visible after clicking the submenu.