title: Google Apps spreadsheet script doesn't always finish iterating, works fine otherwise
body: <p>I'm trying to create a script that looks through a spreadsheet of student data one row at a time and emails a student and their parent if the student's grade is less than 60. The columns in the spreadsheet are: Student ID, Student Name, Student Email Address, Parent Email Address, Grade. There are 30 rows in the spreadsheet - I anticipate using up to 25 of them and leaving the rest blank. Some parent email address fields will be left blank, this is accounted for in the code. All of the students have email addresses.</p>
  
  <p>The script actually works fine, except that it doesn't always finish running through all of the necessary iterations. It stops after a different number of iterations each time - sometimes it does them all, but usually it doesn't. There's no error or anything when it stops, it just stops popping up message boxes or emailing students.</p>
  
  <p>Any idea why it stops running before doing all of the rows?</p>
  
  <p>Here's the code:</p>
  
  <pre><code>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();
    //Browser.msgBox(data);
  
    for (var i = 0; i &lt; 30; i++){
      //set variables
      var row = data[i];
      var emailAddress = row[1];
      var parentEmailAddress = row[2];
      var grade = row[3];
  
      Browser.msgBox("email = "+emailAddress);
  
      //email student if necessary, return true or false
      if(emailAddress !== ""){
        var emailSent = sendStudentEmail(emailAddress, grade);     
      }
      else{
        //Browser.msgBox("Student Email Field " + (i + 1) + " is Empty");
      }
  
      //email parent if necessary, return true or false
      if(parentEmailAddress !== ""){
        var parentEmailSent = sendParentEmail(parentEmailAddress, grade);     
      }
      else{
        //Browser.msgBox("Parent Email Field " + (i + 1) + " is Empty");
      }
    }
  }
  
  function sendStudentEmail(emailAddress, grade){
    if(grade &lt; 60){
      var subject = "Weekly F-Watch Email for Mr. Lipson's Class";
      var body = "Attention Students: If you are receiving this automated email, your term grade is currently below 60. Please check iPass and speak to me to make up any late assignments (please refer to my Late Work Policy). If you have any questions, feel free to email me.Have a good day, - Mr. L Note: This is the first in a weekly series of automated emails, and it's in beta. Please forgive any formatting issues. Also, beginning next week, this email will be sent to parents/guardians as well.";
      //MailApp.sendEmail(emailAddress, subject, body);
      //Browser.msgBox("Email sent to " + emailAddress);
      return true;
    }
    else{
      //Browser.msgBox("Email not sent to " + emailAddress);
      return false;
    }
  }
  
  function sendParentEmail(parentEmailAddress, grade){
    if(grade &lt; 60){
      var subject = "Weekly F-Watch Email for Mr. Lipson's Class";
      var body = "Parent Email Text";
      //MailApp.sendEmail(emailAddress, subject, body);
      //Browser.msgBox("Email sent to " + parentEmailAddress);
      return true;
    }
    else{
      //Browser.msgBox("Email not sent to " + emailAddress);
      return false;
    }
  }
  </code></pre>