Dashboard Mode - Creating custom menus
Goal
Create a custom menu in Google Sheets to run a script that highlights all cells with values above a threshold. This helps quickly spot important data.
Jump into concepts and practice - no test required
Create a custom menu in Google Sheets to run a script that highlights all cells with values above a threshold. This helps quickly spot important data.
| Item | Sales |
|---|---|
| Apples | 120 |
| Bananas | 80 |
| Cherries | 150 |
| Dates | 60 |
| Elderberries | 200 |
function onOpen() {
const ui = SpreadsheetApp.getUi();
ui.createMenu('Highlight')
.addItem('Highlight High Sales', 'highlightHighSales')
.addToUi();
}
function highlightHighSales() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const range = sheet.getRange('B2:B6');
const values = range.getValues();
for (let i = 0; i < values.length; i++) {
if (values[i][0] > 100) {
range.getCell(i + 1, 1).setBackground('yellow');
} else {
range.getCell(i + 1, 1).setBackground(null);
}
}
}+---------------------------+
| Google Sheet |
| +-----------------------+ |
| | Item | Sales | |
| |--------|--------------| |
| | Apples | 120 | |
| | Bananas| 80 | |
| | Cherries| 150 | |
| | Dates | 60 | |
| | Elderb.| 200 | |
| +-----------------------+ |
| |
| Custom Menu: "Highlight" |
| - Highlight High Sales |
+---------------------------+When the sheet opens, the onOpen function adds the custom menu. Clicking Highlight High Sales runs the script that colors sales above 100 in yellow. This updates the sheet visually to show important data.
If you change the sales value for "Bananas" to 130 and run Highlight High Sales again, which cells will be highlighted?
onOpen().onOpen() is the correct trigger name recognized by Google Sheets.onOpen():
SpreadsheetApp.getUi()
.createMenu('My Menu')
.addItem('Say Hello', 'sayHello')
.addToUi();
What happens when the Google Sheet is opened?onOpen() function but the custom menu does not appear:
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Tools')
.addItem('Run Task', 'runTask')
}
What is the error preventing the menu from showing?addToUi().startProcess and 'Stop Process' running stopProcess. Which code snippet correctly creates this menu inside onOpen()?addToUi() to add the menu to the UI.