ChatGPT for Google Apps Script: test onEdit with a real edit event

Tell ChatGPT whether an Apps Script function ran from a spreadsheet edit or from the editor's Run button. An onEdit handler expects an event argument supplied by the trigger. Running that function directly without an argument does not create an edit event, so reading e.range can fail before the spreadsheet logic is reached.

Start by checking the invocation, not by adding permissions or installing another trigger. Those changes do not manufacture the missing argument in a direct function call.

Keep the event source attached to the error

This original teaching handler is intended for a script bound to a disposable Google Sheets file. It inspects a single-cell edit and logs the sheet and address. It does not write a cell, send a message or call another service:

function onEdit(e) {
  if (!e || !e.range) {
    throw new Error("Edit a cell in the bound test sheet to supply e.range.");
  }
  const range = e.range;
  if (range.getNumRows() !== 1 || range.getNumColumns() !== 1) {
    console.log("Skipped: this example handles one edited cell only.");
    return;
  }
  console.log(range.getSheet().getName() + "!" + range.getA1Notation());
}

The guard makes a missing event explicit. It does not make clicking Run equivalent to editing a cell. Avoid replacing e.range with the current selection merely to silence the exception: the selected cell is not evidence of which range caused an edit.

Google's simple-trigger guide, checked October 24, 2026, describes onEdit(e) as responding when a user changes a spreadsheet value. Its event-object reference describes the edited Range. A write performed by a script or API does not itself cause this simple edit trigger to run.

Compare the invocation cases

Case in the test setup Expected behavior of this handler
Direct call onEdit() with no argument Helpful missing-event error; no address logged
User edits one cell on tab Practice, cell B2 Logs Practice!B2
User pastes a two-cell range Logs the single-cell-only skip message

These are authored expectations for the shown code, not a captured Apps Script execution. Use a disposable bound sheet to check the real trigger path. Ensure the test project has no unrelated handlers that would change data before editing it.

The handler uses the range rather than assuming e.value is present. Google's event reference limits value and oldValue to single-cell edits, and an earlier value may be absent when the cell was empty. A handler that needs cleared-cell or multi-cell behavior must define those cases separately.

Ask ChatGPT to diagnose the invocation before rewriting

Diagnose this Google Apps Script edit handler before changing it.
Bound spreadsheet or standalone project: [ENTER]
Full relevant function and exact error: [PASTE]
How it ran: [EDITOR RUN / USER CELL EDIT / INSTALLED TRIGGER / OTHER]
Edited sheet/range and single-cell or multi-cell action: [ENTER]
Existing triggers and relevant execution evidence: [PASTE NO SECRETS]
Intended behavior and actions that must not occur: [ENTER]

Explain which arguments the invocation supplies and where the error
occurs. Separate event-context failure from permissions and range rules.
Propose a minimal diagnostic that reads the event range and changes no
cells. Do not substitute the active selection for the edited range,
assume e.value always exists, add another trigger, widen authorization,
send messages, or claim a spreadsheet execution. Give expected cases
for a direct call, one-cell edit and multi-cell paste.

The request follows OpenAI's prompting guidance by supplying the execution context and the boundaries that change the answer. You can paste permitted code into a conversation; no connection to the spreadsheet is required for this diagnostic review.

Verify the actual trigger path before adding a write

In the disposable sheet, edit one cell and inspect the corresponding execution and log. Compare the logged sheet and address with the cell you changed. Then test the multi-cell case. A manual helper that constructs a mock event can test your branches, but it cannot establish that the real trigger fired.

For an IMPORTRANGE access error, the Google Sheets formula guide follows a different path. For this handler, keep the observed invocation and range evidence with the accepted fix. Add any intended spreadsheet write only after the real edit event and its scope behave as required.

Keep reading