Skip to content
// Apps Script

Assign separate ticket IDs when two Google Forms responses arrive together.

Use the submitted row and a script lock to assign each response a ticket ID in Google Sheets. Replaying a row keeps its original ID and timestamp.

“I need to stop two simultaneous form submissions from running my Apps Script trigger at the same time and corrupting my row counter or sequential invoice ID.”

Look up the handler in Zeal or Dash

Add this feed to your docset manager. In Zeal, open Docset Library, choose Add feed, and paste the address. Search gaslock:getScriptLock or gaslock:e.range to find the lock and the submitted row. Each entry links to the complete handler and a two-row replay you can read offline.

https://bulldo.gs/demos/response-lock-docset/FormResponseLocks.xml

Download the version 1.0.0 docset archive

The script

copy · paste · trigger
assignSequentialId.gs
Apps Script
// Install on the linked spreadsheet: From spreadsheet → On form submit.
function assignSequentialId(e) {
  const eventRow = e.range;
  const sheet = eventRow.getSheet();
  if (sheet.getName() !== 'Form Responses 1') return;

  const lock = LockService.getScriptLock();
  lock.waitLock(10000);
  try {
    const headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
    const index = headers.indexOf('Ticket ID');
    if (index < 0 || index !== headers.lastIndexOf('Ticket ID')) {
      throw new Error('Add one dedicated column named Ticket ID.');
    }

    const row = eventRow.getRow();
    const column = index + 1;
    if (row < 2 || row > sheet.getLastRow()) throw new Error('Invalid response row.');
    const idRange = sheet.getRange(2, column, sheet.getLastRow() - 1, 1);
    const values = idRange.getValues();
    const formulas = idRange.getFormulas();
    const seen = new Set();
    let largest = 0;
    for (let i = 0; i < values.length; i++) {
      const id = values[i][0];
      if (formulas[i][0]) throw new Error('Ticket ID must contain values only.');
      if (id === '') continue;
      if (!Number.isSafeInteger(id) || id < 1 || seen.has(id)) {
        throw new Error('Check existing ticket IDs for duplicates or invalid numbers.');
      }
      seen.add(id);
      largest = Math.max(largest, id);
    }
    if (values[row - 2][0] !== '') return;
    if (!Number.isSafeInteger(largest + 1)) throw new Error('No safe ticket ID remains.');
    sheet.getRange(row, column).setValue(largest + 1);
    SpreadsheetApp.flush();
  } finally {
    lock.releaseLock();
  }
}

Need a variant? Gnaw writes a custom version from one sentence — fields, triggers, edge cases handled.

Walkthrough

Prepare the response sheet

Open a practice Google Form and link its responses to a spreadsheet. In the response tab, add a column named Ticket ID to the right of the last form question. Leave its cells blank. Reserve that heading for the script. If your response tab has another name, replace Form Responses 1 in the snippet with its exact name.

From the spreadsheet, choose Extensions → Apps Script and paste the code. In Triggers, add a trigger for assignSequentialId, choose From spreadsheet and On form submit, and authorize it. Send a response through the form to supply an event. Running the handler directly from the editor leaves e undefined.

Try two responses before checking the sheet

Open the practice form in two tabs. Submit Blue projector from one and Green projector from the other. The linked sheet should contain both original timestamps and answers, with a different positive number in each Ticket ID cell. For a fresh sheet the pair is 1 and 2. Either response may receive 1 because the trigger that acquires the lock first assigns the first available number.

Google Forms appends the response before the spreadsheet trigger handles it. By the time the first handler runs, the second response can already occupy the bottom row. A lock serializes the handlers, but it cannot make the last row belong to the current event. The snippet writes to e.range.getRow() and uses getLastRow() only to read the ID column.

Keep the ticket register intact

Use one script project and an append-only response tab. Keep the ID column free of formulas. Leave issued IDs in place, including cancelled tickets. Make a separate view or copy for sorting and reporting. Sorting or deleting source rows during processing can move an event’s target or allow the highest deleted number to be reused.

The handler checks existing IDs while holding the lock. An existing valid ID on the event row survives a retry. Duplicate numbers, invalid values and formulas stop the run before it writes. SpreadsheetApp.flush() applies the write before another handler can enter the critical section.

Want a custom version?

Describe your sheet and the rule you want. Gnaw writes the Apps Script — fields, triggers, edge cases — in one shot.

FAQ

4 questions
What happens when the lock times out?
The handler throws and leaves the response intact. Check the failed trigger execution and look for the row with a blank Ticket ID. A timeout needs reconciliation. This example has no automatic retry queue.
Can I add this to a populated response sheet?
Yes. Put one dedicated Ticket ID column on the response tab and keep each existing ID as a unique positive integer. The next new response gets the highest retained ID plus one. Installing the trigger does not backfill older blank rows.
Does this work with a trigger installed on the Google Form itself?
Install the trigger on the linked spreadsheet. Its submission event supplies the range that identifies the response row. A Forms-bound submission event supplies a FormResponse instead.
Will a script lock protect the sheet from other editors and projects?
A script lock coordinates executions of this script project that acquire the same lock. Keep other writers away from the ticket column and do reporting on a separate tab.