Google Apps电子表格脚本并不总是完成迭代,否则可以正常工作

时间:2012-02-19 05:29:52

标签: javascript iterator google-apps google-sheets google-apps-script

我正在尝试创建一个脚本,一次查看学生数据的电子表格,并在学生成绩低于60时通过电子邮件发送给学生及其家长。电子表格中的列为:学生ID,学生姓名,学生电子邮件地址,家长电子邮件地址,成绩。电子表格中有30行 - 我预计最多使用25行,其余的则留空。某些父电子邮件地址字段将留空,这将在代码中进行说明。所有学生都有电子邮件地址。

该脚本实际上工作正常,但它并不总是完成所有必要的迭代。它在每次不同的迭代次数后停止 - 有时它会全部完成,但通常不会。当它停止时没有任何错误或任何错误,它只是停止弹出消息框或通过电子邮件发送给学生。

在执行所有行之前知道为什么它会停止运行吗?

以下是代码:

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 < 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 < 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 < 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;
  }
}

2 个答案:

答案 0 :(得分:0)

尝试使用调试器并逐步执行失败的for循环。

您可以在脚本编辑器中双击行号旁边的代码中的任意一行设置停止。通过单击看起来像蜘蛛的图标来运行调试器。

答案 1 :(得分:0)

有许多未记录的限制,有时Utilities.Sleep(ms)可以提供帮助