Skip to content

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

Change these values

Make the code yours before you run it.
Responses sheet
Form Responses 1

Tab the form writes new responses to.

Id header
Response ID

Row 1 header of the column that receives each ID.

Id prefix
REQ-

Text placed before each number.

Lock wait ms
30000

How 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.

Code.gs
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

  1. 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.

  2. 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.

  3. 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.

  4. 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

Keep going