I'm still fairly new with Google Scripts. I have a student roster Google Sheet with demographic information, including ID numbers. I also have a responses Sheet that automatically collects email addresses on Column B (email addresses are created by the ID number plus "@foo.org"). I'm trying to get my script to parse through auto-collected emails, map the ID portion of the email onto column C. Then I'm using for loops to check the extracted ID number against my main roster, and map the remaining information for the same student on the responses sheet.
As of now, I'm only able to correctly map the first row in the responses sheet; As of the second row I only see the numeric IDs but it doesn't map the other relevant columns. This is what I currently have:
function extractId() {
var responsesSheet = SpreadsheetApp.openById('XXXXXXX').getSheetByName('Sheet1');
var rosterSheet = SpreadsheetApp.openById('XXXXXXX').getSheetByName('Sheet1');
var responsesHighestEntry = responsesSheet.getLastRow();
var email;
var idNumber;
var rosterLastRow = rosterSheet.getLastRow();
var rosterArray = rosterSheet.getRange(2, 1, (rosterLastRow - 1)).getValues();
var studentResponsesRow;
var i;
var x;
var y = 2;
var fullName;
var grade;
var advisoryTeacher;
var advisoryRoom;
for (i = 2; i < responsesHighestEntry; i++) {
email = responsesSheet.getRange(i, 2).getValue();
idNumber = email.replace(/\@(.*)/i,"");
responsesSheet.getRange(i, 3).setValue(idNumber);
for (x = 0; x < rosterLastRow; x++) {
if (rosterArray[x] == idNumber) {
studentResponsesRow = y;
fullName = rosterSheet.getRange(y, 2).getValue();
responsesSheet.getRange(y, 4).setValue(fullName);
grade = rosterSheet.getRange(x + 2, 6).getValue();
responsesSheet.getRange(studentResponsesRow, 5).setValue(grade);
advisoryTeacher = rosterSheet.getRange(x + 2, 7).getValue();
responsesSheet.getRange(studentResponsesRow, 6).setValue(advisoryTeacher);
advisoryRoom = rosterSheet.getRange(x + 2, 8).getValue();
responsesSheet.getRange(studentResponsesRow, 7).setValue(advisoryRoom);
}
y++;
}
}
}
Sample roster sheet with demographic data.
Sample end-result responses sheet. In this case, the email addresses are auto-collected. Columns C-G would be the result of running the script.
I'm only able to correctly map the first row in the responses sheet; As of the second row I only see the numeric IDs but it doesn't map the other relevant columns.? Of course, please remove your personal information. By the way, in your script, it seems that the ID is retrieved from email. But how about directly retrieving it from the column "C"? - TanaikeI'm only able to correctly map the first row in the responses sheet...? Because although I prepared the sample sheet of the images, I cannot replicate your situation using your updated script. So I thought that the sample of images might be the different from the actual format. By the way, in your script, it seems that the ID is retrieved from email. But how about directly retrieving it from the column "C"? If I misunderstood your situation, I apologize. - Tanaike