Add Unique ID to Google Forms Confirmation Message: GAS080
Introduction
If you are looking for a solution to add a unique identifier or UID for every single response in your Google Forms’s confirmation message to the users, you may find the right place.
In this Google Apps Script project, I am sharing this small project for you to make it possible in Google Forms.
Demo
Try this demo form I created, so you will see the UID for your submission.
https://forms.gle/SHszxa78HtTdpktYA
YouTube
Check this project introduction video on YouTube if you prefer to watch video instructions.
Instructions (3 Steps)
Follow the steps below to set up your own project.
Step 1
Make a copy of my project here to your Google Drive.
* If the linked form is not copied to your Google Sheets, make sure you create a form on your Google Drive and link it to the copied Google Sheets.
Step 2
Open the Apps Script editor by going to “Extensions > Apps Script”, then add a new trigger to watch the form submit events. It will ask you to authorize the app if it’s your first time to add the trigger, follow the guide below if you are not familiar with the authorization process.
A Complete Guide to Authorize GAS Project
Follow these settings for the trigger, then click the button “Save” to add the trigger.
- Choose which function to run: _onFormSubmit
- Choose which deployment should run: Head
- Select event source: From spreadsheet
- Select event type: On form submit
Step 3
Set up the confirmation message template in the settings of the Google Forms. You can customize your message, but it’s important to remember to add the first UID number for the 1st response. Normally, you can start with UID00001. The number will be incremented by one after the new submission from your form so the next user will see UID00002 by the trigger script we created above, that is the core logic to solve the problem.
If you set up the project correctly, you should get the UID in the confirmation message like this.
Other Settings (Optional)
If you want to change the prefix of the UID of the digit length of the UID, you will need to update the script in the Apps Script editor. For example: if I want to generate an order number for each response with 8 digits numbers.
- HEADER: “Order Number”
- PREFIX: “ON” (for order number)
- LENGTH: 8
const UID = {
HEADER: "UID_HEADER",
PREFIX: "UID",
LENGTH: 5,
}
If we change the HEADER, we’ll need to update the value in the response sheet for saving the UID of each response. In this example, it will be “Order Number” which we updated in the code above.
And the UID in the confirmation message has to be updated accordingly to “OR00000001” to match the new settings in the script.
All Scripts
Alternatively, you can copy the code below to your project.
const UID = {
HEADER: "UID_HEADER",
PREFIX: "UID",
LENGTH: 5,
}
class App{
constructor(){
this.ss = SpreadsheetApp.getActive()
this.sheet = this.getLinkedSheet()
if (!this.sheet) {
throw Error(`There is no linked form in this spreadsheet.`)
}
this.form = FormApp.openByUrl(this.sheet.getFormUrl())
this.message = this.form.getConfirmationMessage()
this.uidRegex = new RegExp(`${UID.PREFIX}[\\d]{${UID.LENGTH}}`, 'gi')
}
createUidByNumber(number){
return UID.PREFIX + (10 ** UID.LENGTH + number).toString().slice(-UID.LENGTH)
}
getLinkedSheet(){
return this.ss.getSheets().find(sheet => sheet.getFormUrl())
}
getUidFromConfirmationMessage(){
const message = this.form.getConfirmationMessage()
const results = message.match(this.uidRegex)
if (!results) throw Error(`No UID found in the current confirmation message with regex ${this.uidRegex}.`)
return results[0]
}
createNextUid(currentUid){
const nextUidNumber = Number(currentUid.replace(UID.PREFIX, "")) + 1
return this.createUidByNumber(nextUidNumber)
}
saveCurrentUid(uid, rowStart){
const [headers] = this.sheet.getDataRange().getDisplayValues()
let uidHeaderIndex = headers.indexOf(UID.HEADER)
if (uidHeaderIndex === -1) {
uidHeaderIndex = headers.length
this.sheet.getRange(1, uidHeaderIndex + 1).setValue(UID.HEADER)
}
this.sheet.getRange(rowStart, uidHeaderIndex + 1).setValue(uid)
}
updateConfirmationMessage(nextUid){
const message = this.message.replace(this.uidRegex, nextUid)
this.form.setConfirmationMessage(message)
}
run(e){
const {rowStart} = e.range
const currentUid = this.getUidFromConfirmationMessage()
this.saveCurrentUid(currentUid, rowStart)
const nextUid = this.createNextUid(currentUid)
this.updateConfirmationMessage(nextUid)
}
}
function _onFormSubmit(e) {
new App().run(e)
}
GoogleAppsScript Playlist (Follow My Channel for New Projects)
Links
Hire me on Upwork (work with me)
Comments
Post a Comment