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

发布于 2025-01-07 03:40:35 字数 2756 浏览 0 评论 0原文

我正在尝试创建一个脚本,该脚本一次查看学生数据的电子表格,如果学生的成绩低于 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;
  }
}

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.

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.

Any idea why it stops running before doing all of the rows?

Here's the 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 < 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;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟燃烟灭 2025-01-14 03:40:35

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

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

Try using the debugger and stepping through the for loop that fails.

You can set a stop at any line in the code by double clicking near the line number in the script editor. The debugger is run by clicking the icon that looks like a spider.

恋你朝朝暮暮 2025-01-14 03:40:35

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

There are many undocumented limits, sometimes Utilities.Sleep(ms) can help

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文