Ok, I'm a teacher and I have made a google form which asks for a period, assignment, and a due date. After that, I can select students to send an email home to parents (It's for late assignments, the kids will hate it I'm sure).
I'm having a problem with the script only matching the first match in an "if" statement. Here is what's going on: 
The image above shows the responses. An email template uses the period, assignment and due date cells to make an email. (That works) The script uses the emails in the "Students" cell.
Next, the script uses the sheet above to create an array of values. A loop begins with the first email address from the "students" cell in the form response sheet (the first one). A nested loop then iterates through the student emails in the roster sheet and tries to match the emails. When a match is found, the parent emails are gathered and used to send the email.
Here is my code!
function homeworkEmail() {
var lastRow = sheet1.getLastRow();
var emailArray = "";
for (c=4; c<=9; c+=1) { //begin array starting with P1 column and end with P6 column
var students = sheet1.getRange(lastRow,c).getValue();
if (students) {
var sendToStudents = students.split(',');
}
}
var period = sheet1.getRange(lastRow,3).getValue(); //Value of period column (3)
var assignment = sheet1.getRange(lastRow,10).getValue(); //Value of assignment column (10)
var due = sheet1.getRange(lastRow,11).getValue(); //Value of due date column (11)
var allRosterValues = sheet2.getDataRange().getValues();
for (s=0; s < sendToStudent.length; s++) { //start loop through students array to send
var student = sendToStudents[s].replace(/\s+/g, '');
for (r=1; r < sheet2.getLastRow(); r++) { //start loop across entire roster sheet
var studentInRoster = allRosterValues[r][2].replace(/\s+/g, '');
if (String(sendToStudents[s]) == String(allRosterValues[r][2])) { //look for student email in array to match roster list
Logger.log("SEND EMAIL HERE");
break;
}
}
}
}
The problem is, this if statement always catches the first email match and works, but every other email after (so the second email in the form, sendToStudent[s]...) never matches. I started directly declaring everything within the if statement, and now have declared individual var's for each step hoping that might work. I've printed them out and can literally see how the email strings will match 100%, no white space or anything, and it just passes it by. I've checked they match type, and even removed whitespace and printed them out with a "-" before and after to check the length. It even logged "not match" when I checked things using an else statement so it's actually running the if statement and not recognizing a match. I'm totally lost!

Aucun commentaire:
Enregistrer un commentaire