A shared form sends booking requests to an Apps Script routine. It reads the last assigned number, increments it, and records the booking. Two executions can read the same value before either writes. Acquire a script lock before reading the counter, then release it when the write is ready for other executions to see.

const lock = LockService.getScriptLock();
if (!lock.tryLock(5000)) throw new Error('Booking counter is busy');
try {
  assignAndRecordBooking();
  SpreadsheetApp.flush();
} finally {
  lock.releaseLock();
}

In this example, assignAndRecordBooking is your own function that reads and writes the shared sheet. Use the lock in every path in this script that updates the same counter. Keep slow notifications outside the locked block so callers can take their turn.

Submit two test requests close together. Check that the numbers and response IDs identify two different bookings. Then hold the lock long enough in a test execution to force a timeout in another run. Confirm that the second run reports a recognizable failure and leaves no booking behind. If someone can retry, check the original response ID before allocating a second number.

Make a retry return the original number

Start a test with the last recorded number at 417. Submit response IDs A and B close together. The two records should have IDs A and B and the numbers 418 and 419 in whichever order the lock grants access. Submit A again to simulate a replay. Inside the locked routine, look up A before incrementing the counter and return its existing number if already recorded. Confirm that the repeat creates no row and that the next fresh response can take 420.

Sources