Custom menus that Clear, Re-import, or Archive should not fire on a misclick. This thin wrapper around SpreadsheetApp.getUi().alert returns true only when the user clicks Yes.
What you'll need
- A container-bound spreadsheet script (Ui alerts need an active spreadsheet)
- A menu item or button handler that can early-return
How to use this snippet
function confirmDangerousAction(title, prompt) {
var ui = SpreadsheetApp.getUi();
var result = ui.alert(
title || 'Confirm',
prompt || 'Are you sure?',
ui.ButtonSet.YES_NO
);
return result === ui.Button.YES;
}
Example
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('Bootstrap')
.addItem('Clear data (keep headers)', 'menuClearData')
.addItem('Re-import now', 'menuReimport')
.addToUi();
}
function menuClearData() {
if (!confirmDangerousAction(
'Clear data',
'Delete all data rows and keep the header row? This cannot be undone.'
)) {
return;
}
var sheet = SpreadsheetApp.getActiveSheet();
clearSheetKeepHeaders(sheet); // your helper
SpreadsheetApp.getActiveSpreadsheet().toast('Cleared', 'Bootstrap', 3);
}
Tips:
- Keep the prompt specific: what will happen, and whether undo exists.
- Alerts block the script until the user answers — fine for menus, wrong for time-driven triggers (those have no UI).
- Pair with a Dry Run Script Property for bulk writes; use this confirm for the irreversible menu actions.
Happy Coding!
