Task guide
Give each form response a unique ID
Each new response in the form response sheet gets an ID such as REQ-1, REQ-2, and so on. A script lock makes two submissions that arrive together wait their turn, so they never get the same number.
1 · Prepare
Put these pieces in place
- A Google Form that saves responses to a Google Sheet. In the form, open the Responses tab and choose Link to Sheets if it is not linked yet.
- In the response sheet, type Response ID in row 1 of the first empty column to the right of the form answers.
- Open the script editor from the response sheet with Extensions, then Apps Script, not from the form.
Change these values
- Responses sheet
Form Responses 1Tab the form writes new responses to.
- Id header
Response IDRow 1 header of the column that receives each ID.
- Id prefix
REQ-Text placed before each number.
- Lock wait ms
30000How long one run waits while another is being numbered.
2 · Review before running
What the script can access and change
Permissions you approve
- See, edit, create, and delete all your Google Sheets spreadsheets. The code writes only to the Response ID column of the response row that triggered it.
- LockService and PropertiesService need no extra approval. The script stores the next ID number in its own script properties.
Effects of one run
- Writes one ID, such as REQ-4, into the Response ID column of each new response row. It rewrites that column with the values it already held, so keep plain text there, not formulas.
- Adds 1 to the NEXT_RESPONSE_ID script property for each response it numbers.
- Does not change form answers, rows that already have an ID, or the form itself.
- Running numberMissingResponseIds by hand gives an ID to every response row whose Response ID is blank, in row order.
3 · Copy
Paste this into the Apps Script editor
Create or open a script file, replace the example values marked above, then save the project.
const RESPONSES_SHEET = 'Form Responses 1'; // Tab the form writes new responses to.
const ID_HEADER = 'Response ID'; // Row 1 header of the column that receives each ID.
const ID_PREFIX = 'REQ-'; // Text placed before each number.
const LOCK_WAIT_MS = 30000; // How long one run waits while another is being numbered.
/** @param {Events.SheetsOnFormSubmit} e */
function numberFormResponse(e) {
if (!e || !e.range) {
throw new Error('Submit the form to test this function. To fill in missing IDs, run numberMissingResponseIds.');
}
const sheet = e.range.getSheet();
if (sheet.getName() !== RESPONSES_SHEET) return;
numberRowsUnderLock(sheet, [e.range.getRow()]);
}
function numberMissingResponseIds() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(RESPONSES_SHEET);
if (!sheet) throw new Error('No tab named ' + RESPONSES_SHEET + '.');
const rows = [];
for (let row = 2; row <= sheet.getLastRow(); row += 1) rows.push(row);
numberRowsUnderLock(sheet, rows);
}
/**
* @param {SpreadsheetApp.Sheet} sheet
* @param {number[]} rows
*/
function numberRowsUnderLock(sheet, rows) {
const headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
const idColumn = headers.indexOf(ID_HEADER) + 1;
if (idColumn === 0) {
throw new Error('Add a column headed ' + ID_HEADER + ' to row 1 of ' + RESPONSES_SHEET + '.');
}
const lock = LockService.getScriptLock();
if (!lock.tryLock(LOCK_WAIT_MS)) {
throw new Error('Another run held the lock too long. Run numberMissingResponseIds later to fill in missing IDs.');
}
try {
const properties = PropertiesService.getScriptProperties();
let next = Number(properties.getProperty('NEXT_RESPONSE_ID') ?? '1');
const idRange = sheet.getRange(1, idColumn, sheet.getLastRow(), 1);
const ids = idRange.getValues();
const firstNew = next;
rows.forEach((row) => {
if (ids[row - 1][0] !== '') return;
ids[row - 1][0] = ID_PREFIX + next;
next += 1;
});
if (next === firstNew) return;
properties.setProperty('NEXT_RESPONSE_ID', String(next));
idRange.setValues(ids);
SpreadsheetApp.flush();
} finally {
lock.releaseLock();
}
} 4 · Run
Follow these steps in order
Paste the function and save
In the script project opened from the response sheet, replace the starter code with this script and save. Do not click Run on numberFormResponse, because there is no submitted response there to number. numberMissingResponseIds is the one to run by hand, later, if a row is ever missed.
Install the trigger
Click Triggers, the clock icon in the left sidebar, then click Add Trigger. Choose numberFormResponse as the function, From spreadsheet as the event source, and On form submit as the event type. Click Save and approve access when Google asks.
Submit one test response
Open the form as a respondent and submit it. Within a few seconds, the new row in Form Responses 1 shows REQ-1 in the Response ID column.
Submit two responses at the same time
Open the form in two browser tabs, fill in both, and submit them a second apart. The two new rows get different IDs, such as REQ-2 and REQ-3, because the second run waits for the first to release the lock.
5 · Confirm
How to tell it worked
Every new response row has a different ID in the Response ID column, counting up from REQ-1.
If the result is different
Troubleshooting
- Clicking Run shows Error: Submit the form to test this function.
- Test numberFormResponse by submitting the form. The Run button calls it with no response, so there is no row to number. Run numberMissingResponseIds instead to fill in blank IDs.
- A row is missing an ID, and Executions shows Another run held the lock too long.
- Many responses arrived at once and this run gave up after LOCK_WAIT_MS. Select numberMissingResponseIds in the function dropdown and click Run. It takes the same lock, so responses that arrive meanwhile wait, and it numbers every row whose Response ID is blank. Do not type IDs or edit NEXT_RESPONSE_ID by hand while the form is open.
- The IDs skip a number.
- The script reserves the number before writing it, so a run that fails after reserving it leaves a gap. This keeps two rows from sharing an ID. Check Executions for the failed run and that row.
Technical details
Names used in this script
Open these details when you want to adapt the example or look up an exact service or method name.
Show Apps Script symbols and service pages
Apps Script symbols
Service pages
Keep going
Related tasks, basics, and apps
- Task guide Respond to a Google Form submission
Get an email every time someone submits your Google Form, listing each question and its answer. The email goes only to you, the owner of the script.
- Task guide Store settings with PropertiesService
Keep a setting such as an email address out of your code and change it without editing the script. The project stores it as a script property that every run can read.
- Task guide Send a confirmation to the form respondent
Send each person who submits your Google Form a short thank-you email that repeats their answers. The form must collect verified email addresses.
- Object reference Lock
A LockService.Lock prevents concurrent trigger runs, form submissions, or web-app requests from editing the same Sheet, Doc, or property at once. Acquire it with waitLock(ms) or tryLock(ms), flush pending Sheet writes, and release it in finally.
- Object reference Properties
Properties store text values under names you choose, called keys, and keep them between runs. Use getProperty() and setProperty() to read and save them. PropertiesService.getScriptProperties() is shared across the project, user properties are per-person, and document properties are per-file.
- Object reference Range
A Range represents one cell such as A2 or a rectangular block of rows and columns in a Sheet. Use getValue() for one cell and getValues() or setValues() for a 2D array of rows.
- Basics CacheService, PropertiesService, and LockService
PropertiesService keeps small text settings across runs, CacheService speeds up repeat reads for up to six hours, and LockService keeps two runs from colliding.
- Google app Google Forms
FormApp scripts and form-submit triggers read new Google Form responses via event.response, populate dropdown questions from Google Sheets, and trigger email or Calendar actions.
- Google app Triggers
A trigger is a saved schedule or event rule in ScriptApp that runs your function on time, on edit, or on form submit.
- Google app Script utilities
UrlFetchApp, PropertiesService, and LockService work in any Apps Script project. Use them to call web APIs, save settings between runs, and stop two runs from overlapping.
- Google app Google Sheets
SpreadsheetApp lets a Google Sheets script read a Sheet or Range, add a custom menu or formula, and act on each row, such as sending one email per row.
- Diagnostic Lock timeout: another process was holding the lock for too long
Why waitLock() throws when another run holds the lock, and how tryLock() and a finally block prevent it.
- Book chapter State across runs with PropertiesService
Store state across Apps Script runs with PropertiesService, CacheService, and LockService for durable script properties, per-user keys, and caches.