The google form details has
Name
Age
Gender
Grade
ID
The script should create a unique ID for each row based on the grade.
Grade 1 - 5 Sub Juniors SJ00001
Grade 6 - 9 Juniors J00001
Grade 10 - 12 Seniors
If they have siblings after submitting the google form. The new row should be added with unique id based on the grade system
for example
I have written the code but its not working , can someone help me. The data is not showing the id in the second row
const UID = {
HEADER: "UID", // Column header for UID
//SJ Sub Juniors/J Juniors/S Seniors/SC Seniors Completed/AC Attending College/WA Working Adult
PREFIXES: {
SJ: "SJ-",
J: "J-",
S: "S-",
SC: "SC-",
AC: "AC-",
WA: "WA-"
},
LENGTH: 6 // Fixed length for the ID (e.g., 000001, 000002, etc.)
};
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();
}
// Generate UID based on the grade and a sequential number
createUidByCategoryAndNumber(category, number) {
let x = null;
x = UID.PREFIXES[category] + (10 ** UID.LENGTH + number).toString().slice(-UID.LENGTH);
return x;
}
getLinkedSheet() {
return this.ss.getSheets().find(sheet => sheet.getFormUrl());
}
// Determine the grade category (SJ, J, S) based on the grade
getCategory(grade) {
const gradeNumber = parseInt(grade.replace(/\D/g, ""), 10);
if (gradeNumber >= 1 && gradeNumber <= 5) return "SJ";
if (gradeNumber >= 6 && gradeNumber <= 9) return "J";
if (gradeNumber >= 10 && gradeNumber <= 12) return "S";
if (grade==="12th Completed") return "SC";
if (grade==="Attending College") return "AC";
if (grade==="Working Adult") return "WA";
return "U"; // Default (Unknown)
}
// Get the last used UID from the sheet to create the next one
getLastUsedUid(category) {
const data = this.sheet.getDataRange().getValues();
let lastUsedUid = null;
// Find the last UID for the given category (SJ, J, S, SC, AC, WA, U)
for (let i = data.length - 1; i >= 0; i--) {
const uid = data[i][1]; // Assuming UID is in the second column (index 1)
if (uid && uid.startsWith(UID.PREFIXES[category])) {
lastUsedUid = uid;
break;
}
}
// Extract the numerical part and return the next number
if (lastUsedUid) {
const lastNumber = parseInt(lastUsedUid.replace(UID.PREFIXES[category], ""), 10);
return lastNumber + 1; // Increment the last number
}
// If no UID is found for this category, return the starting number
return 1;
}
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);
}
// Update the confirmation message with both main student and sibling details
updateConfirmationMessage(mainStudent, sibling) {
let message = `Registration Successful. Please Note Your ID Given Below:\n\n`;
message += `UID: ${mainStudent.uid}\n`;
message += `Name: ${mainStudent.name}\n`;
message += `Grade: ${mainStudent.grade}\n`;
message += `Phone: ${mainStudent.phone1}\n`;
// If there is a sibling, include sibling details in the message
if (sibling) {
message += `\nUID: ${sibling.uid}\n`;
message += `Sibling Name : ${sibling.name}\n`;
message += `Sibling Grade : ${sibling.grade}\n`;
message += `Sibling Phone : ${sibling.phone1}\n`;
}
// Set the confirmation message in the form
this.form.setConfirmationMessage(message);
}
run(e) {
const values = e.values;
const timestamp = values[0];
const email = values[2];
const name = values[3];
const phone1= values[4];
const phone2= values[5];
const landline= values[6];
const grade= values[7];
const gender= values[8];
const transportBoolean= values[9];
let areainKuwait = null;
let landmark = null;
let areainAbbasiya = null;
if (transportBoolean === "Yes") {
areainKuwait= values[10];
landmark= values[11];
if (areainKuwait === "Abbasiya") {
areainAbbasiya= values[12];
}
}
//hasSibling==============================================================
//hasSibling is not used in this registration form by default its no
//const hasSibling = values[]; No Sibling for now
const hasSibling = 'no';
//hasSibling==============================================================
const category = this.getCategory(grade);
const lastUserID = this.getLastUsedUid(category);
const nextUid = this.createUidByCategoryAndNumber(category, lastUserID);
const church= values[13];
const parentname= values[14];
const parentcontactno= values[15];
const areainkuwaitFamily= values[16];
const areainkuwaitOther= values[17];
// Insert the main student details into the sheet only
if (nextUid) {
this.sheet.appendRow([timestamp,nextUid,email,name,phone1,phone2,landline,grade,gender,transportBoolean,areainKuwait,landmark,areainAbbasiya,church,parentname,parentcontactno,areainkuwaitFamily,areainkuwaitOther]);
}
// this.saveCurrentUid(nextUid, this.sheet.getLastRow());
//Sibling==============================================================
let siblingNextUid = null;
let siblingDetails = null;
// Only append sibling entry if "YES" is selected for sibling
if (hasSibling.toLowerCase() === "yes") {
const siblingName = values[18]; // Sibling Name from the form
const siblingGrade = values[19]; // Sibling Grade from the form
const siblingCategory = this.getCategory(siblingGrade);
siblingNextUid = this.createUidByCategoryAndNumber(siblingCategory, this.getLastUsedUid(siblingCategory));
// Append sibling entry with the correct columns only if "YES" is selected for sibling
this.sheet.appendRow([timestamp, siblingNextUid, siblingName, siblingGrade, phone1, "Sibling"]);
this.saveCurrentUid(siblingNextUid, this.sheet.getLastRow());
siblingDetails = {
name: siblingName,
uid: siblingNextUid,
grade: siblingGrade,
phone: phone1
};
}
//Sibling==============================================================
// Update the confirmation message with both the main student's and sibling's details
this.updateConfirmationMessage(
{ name, uid: nextUid, grade, phone1 },
siblingDetails
);
// Optional: Log UID for debugging
Logger.log(`UIDs generated: Main - ${nextUid}, Sibling - ${siblingNextUid}`);
}
}
function _onFormSubmit(e) {
new App().run(e);
}