I have a script that goes through a spreadsheet of student data one row at a time to email students and their parents if the student's grade is below 60. The spreadsheet columns include: Student ID, Name, Email Address, Parent's Email Address, and Grade. There are 30 rows in the sheet but only up to 25 will be used. Some parent email addresses may be blank, which is handled in the code. All students have email addresses.
The script works fine, except it stops running before completing all iterations. It randomly stops after a different number of runs each time without any error messages. Any thoughts on why this might be happening?
Here is the code I am using:
function onOpen(){
loadMenu();
}
function loadMenu() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.addMenu("F-Watch Emailer", [{"name":"Send Emails", "functionName":"sendEmails"}]);
}
function sendEmails() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = sheet.getRange("B2:E30");
var data = range.getValues();
for (var i = 0; i < 30; i++){
var row = data[i];
var emailAddress = row[1];
var parentEmailAddress = row[2];
var grade = row[3];
if(emailAddress !== ""){
sendStudentEmail(emailAddress, grade);
}
if(parentEmailAddress !== ""){
sendParentEmail(parentEmailAddress, grade);
}
}
}
function sendStudentEmail(emailAddress, grade){
if(grade < 60){
var subject = "Weekly F-Watch Email for Mr. Lipson's Class";
var body = "Attention Students: If your grade is below 60, please check iPass and speak with me. Have a good day, - Mr. L";
//MailApp.sendEmail(emailAddress, subject, body);
return true;
}
else{
return false;
}
}
function sendParentEmail(parentEmailAddress, grade){
if(grade < 60){
var subject = "Weekly F-Watch Email for Mr. Lipson's Class";
var body = "Parent Email Text";
//MailApp.sendEmail(emailAddress, subject, body);
return true;
}
else{
return false;
}
}