Apps Script example · 8 min read

Dialog Prompt Input: Copy-Paste Apps Script Pattern

Working dialog prompt input example in Apps Script—copy-paste code, common mistakes, and when to get it built professionally.

UI.promptValidationMenu

ui.prompt is enough when you need one or two values without building HtmlService forms.

This menu item asks for a row number, then a SKU, validates both, and writes Inventory column B plus a lastUpdated stamp in column E.

ButtonSet.OK_CANCEL lets users abort cleanly; always check getSelectedButton before reading text.

Keep regex rules next to the write so invalid SKUs never hit the sheet.

Need this built? Hire a Google Apps Script developer →

Sheet / project setup

SheetColumnPurpose
InventoryA ItemExisting item name
InventoryB SKUUpdated by prompt
InventoryE lastUpdatedTimestamp on change
MenuQuick InputonOpen menu

What this script does

promptSkuForRow sequences two prompts, validates, writes, and alerts success.

Prerequisites

Bound script; Inventory sheet with at least columns B and E.

  • Row 1 headers
  • Data starts row 2
  • Users know target row numbers

Walkthrough

Choose Quick Input → Set SKU on row, enter 2 and ABC-100, confirm B2/E2 update.

Edge cases

Non-integer row strings become NaN and fail the integer check.

  • Cancel exits silently
  • SKU normalized to uppercase

How to test

Try row 1, bad SKU characters, and cancel on the second prompt.

Hardening for production

Look up row by item name instead of number; log changes to Audit.

Variations

ui.alert for confirmations; showModalDialog when you need more fields.

Full code: promptSkuForRow()

Reload the Sheet to load the menu, then run Set SKU on row with a real Inventory row.

/**
 * Prompt dialogs for quick single-value input from a custom menu.
 */
function onOpen() {
  SpreadsheetApp.getUi()
    .createMenu("Quick Input")
    .addItem("Set SKU on row...", "promptSkuForRow")
    .addToUi();
}

function promptSkuForRow() {
  const ui = SpreadsheetApp.getUi();
  const rowResp = ui.prompt(
    "Target row",
    "Enter the row number to update (header is row 1):",
    ui.ButtonSet.OK_CANCEL
  );
  if (rowResp.getSelectedButton() != ui.Button.OK) return;

  const row = Number(rowResp.getResponseText());
  if (!Number.isInteger(row) || row < 2) {
    ui.alert("Row must be an integer >= 2");
    return;
  }

  const skuResp = ui.prompt("SKU", "Enter the SKU code:", ui.ButtonSet.OK_CANCEL);
  if (skuResp.getSelectedButton() != ui.Button.OK) return;
  const sku = skuResp.getResponseText().trim().toUpperCase();
  if (!/^[A-Z0-9-]{3,20}$/.test(sku)) {
    ui.alert("SKU must be 3–20 chars A–Z, 0–9, or hyphen");
    return;
  }

  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Inventory");
  sheet.getRange(row, 2).setValue(sku); // column B
  sheet.getRange(row, 5).setValue(new Date()); // column E lastUpdated
  ui.alert("Updated row " + row + " to SKU " + sku);
}
  1. Line 13: Opens a modal prompt dialog.
  2. Line 18: Ignores Cancel / close without writing.
  3. Line 21: Rejects blank or fractional row numbers.
  4. Line 35: Writes SKU to column B.
  5. Line 22: Confirms the update to the user.

Deploy this example

  1. 01

    Open Apps Script

    In the bound spreadsheet: Extensions → Apps Script. For standalone projects, create one at script.google.com and link your Sheet by ID.

  2. 02

    Paste and save

    Add a .gs file, paste the code below, rename constants at the top (sheet names, column letters, API property keys), then save.

  3. 03

    Authorize once

    Run the main function from the editor. Accept OAuth scopes when prompted — triggers cannot run until authorization succeeds once.

  4. 04

    Add the trigger

    Triggers → Add trigger → choose the handler function and event (time-driven, on edit, or on form submit). Delete test triggers before production.

Before you run: prompt dialogs

  • 1Inventory sheet exists
  • 2Column B is the SKU column
  • 3Reload spreadsheet for onOpen menu
  • 4Authorize on first menu use
  • 5Document SKU format for users
  • 6Test Cancel paths

Frequently asked questions

No — SpreadsheetApp.getUi prompts only work in the Sheets UI container, not HtmlService web apps.

prompt does not take a default text box value; pre-fill via HTML dialogs instead.

Use ui.alert with YES_NO ButtonSet.

Normalization avoids duplicate SKUs that differ only by case.

Accept a string and parse with Utilities.formatDate validation, or use a custom HTML dialog date picker.

Yes — script execution waits until the user dismisses the dialog.

More than two or three fields usually deserves HtmlService.

Related examples

Want this wired into your real workflow?

I adapt these patterns to your Sheet structure, APIs, and triggers — deployed in your Google account. Fixed-scope quotes from $500 · free 30-min consult · quote within 24 hours.