Form QR Code › Unique codes with Google Forms
How to send unique codes through Google Forms
Google Forms can't give each respondent a different code on its own. With a short script in the form's Google Sheet, every person who submits gets their own code by email within a few seconds. It takes about 10 minutes to set up and it's free.
What you need
- A Google Form that collects email addresses (Settings, then Responses, then Collect email addresses), or that has an email question.
- The form linked to a Google Sheet (Responses, then Link to Sheets).
- Your list of codes, one per person: discount codes, ticket numbers, voucher codes or access codes.
1. Add your codes to the Sheet
In the linked Sheet, add a new tab called Codes. Put a header row (Code, Email, Sent) in row 1, then one code per row in column A. Leave columns B and C empty. The script fills them in so you can see who got which code.
2. Paste the script
In the Sheet, open Extensions, then Apps Script. Delete what's there, paste this, and save.
// Sends each Google Form respondent the next unused code from the "Codes" tab.
const CODES_TAB = 'Codes';
const SUBJECT = 'Your code';
function onFormSubmit(e) {
const lock = LockService.getScriptLock();
lock.waitLock(30000); // two people submitting at once never get the same code
try {
const answers = e.namedValues;
const emailKey = Object.keys(answers).find(k => /e-?mail/i.test(k));
const email = emailKey ? String(answers[emailKey][0]).trim() : '';
if (!email) return;
const tab = SpreadsheetApp.getActive().getSheetByName(CODES_TAB);
const rows = tab.getDataRange().getValues();
const i = rows.findIndex((r, n) => n > 0 && r[0] !== '' && r[1] === '');
if (i === -1) {
MailApp.sendEmail(Session.getEffectiveUser().getEmail(), 'Out of codes', 'Someone submitted your form but there are no codes left.');
return;
}
tab.getRange(i + 1, 2, 1, 2).setValues([[email, new Date()]]);
MailApp.sendEmail(email, SUBJECT, 'Thanks for filling out the form. Your code is: ' + rows[i][0]);
} finally {
lock.releaseLock();
}
}3. Turn it on
- In Apps Script, click the clock icon (Triggers), then Add Trigger.
- Choose onFormSubmit, event source From spreadsheet, event type On form submit, and save.
- Approve the permissions Google asks for. The script needs to read the Sheet and send email as you.
- Submit the form yourself to test it. Your code should arrive within a minute, and the Codes tab shows your email next to it.
No list of codes? Generate them
If the codes don't need to come from another system, the script can make one per person. Replace everything inside try after the email lines with this, and you don't need a Codes tab:
const code = Utilities.getUuid().replace(/-/g, '').slice(0, 8).toUpperCase();
const ss = SpreadsheetApp.getActive();
const log = ss.getSheetByName('Sent codes') || ss.insertSheet('Sent codes');
log.appendRow([email, code, new Date()]);
MailApp.sendEmail(email, SUBJECT, 'Thanks for filling out the form. Your code is: ' + code);Each code is saved with the person's email on a Sent codes tab, so you can check it when they redeem it.
Good to know
- Free Google accounts can send about 100 emails a day from a script. Google Workspace accounts can send more.
- Emails come from your Google account, so they show your name. Check your spam folder the first time.
- Want people to reach the form from a poster or flyer? Make a QR code for your Google Form.
Make yours now. Paste your Google Form or any link and style it. Preview free, then $2 USD once. Never expires.
Make a QR codeCommon questions
Can Google Forms give each person a unique code?
Not on its own. Google Forms shows everyone the same confirmation message. To give each respondent a different code, add a short Apps Script to the linked Google Sheet that picks the next unused code and emails it when the form is submitted.
Can I show the unique code on the confirmation page instead of emailing it?
No. The Google Forms confirmation message is the same for everyone and a script can't change it per person. Email is the reliable way to deliver a unique code.
Will two people ever get the same code?
Not with the script above. It locks the Sheet while it assigns a code, so two submissions at the same moment still get different codes, and it marks each code as used.