Google脚本帮助-如果任何数据为空,请停止尝试

时间:2019-01-08 17:33:26

标签: google-apps-script google-sheets

我有一个google脚本,工作人员可在该脚本中将数据(例如学生电子邮件地址和家庭作业信息)输入到电子表格中。从该数据中,如果学生每天需要进一步的帮助,他们会在每天10:00通过电子邮件发送给他们。它工作得很好,但是当行没有完全填写时,有时会出现错误。它只会简单地出错并停止该过程,从而使所有学生在该不完整条目下的表格上全天没有电子邮件。我试图放入一个错误捕获,将发现一个单元格为空白,并将该行传递到下一个而不是简单地停止。

这是我现在掌握的内容。有人可以指出我正确的方向吗?无论如何我都不是一个伟大的程序员。

var mainSheet = ivList[i];  
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(mainSheet);


//empty row checker. Ends with lastFullRow
var range = sheet.getDataRange();
var values = range.getValues();
var row = 0;
for (var row=0; row<values.length; row++) {
  if (!values[row].join("")) break;
}
var lastFullRow = row;
//Stop empty row Check

var cell = sheet.getRange(3, 1, lastFullRow, 7);
Logger.log(lastFullRow);

var UserData = cell.getValues();
  try{
    for (var j in UserData) {
      var row = UserData[j];
      var grade = row[0];
      var lastName = row[1];
      var firstName = row[2];
      var studentEmail = row[3];
      var staffEmail= row[4];
      var iCanStatement = row[5];
      var date = row[6];


      // if IV sheet send IV email
     if (i == 0 || i == 2) {
       var emailSubject = "INTERVENTION NOTICE for "+date;
       var studentBody = "NOTICE: This is an automated message confirming that you have been assigned to the INTERVENTION LIST."+"\n"+"\n"+"DATE: "+date+"\n"+"\n"+"FIRST NAME: "+firstName+"\n"+"\n"+"LAST NAME: "+lastName+"\n"+"\n"+"EMAIL ADDRESS: "+studentEmail+"\n"+"\n"+"STAFF MEMBER: "+staffEmail+"\n"+"\n"+"I CAN STATEMENT: "+iCanStatement;
       var staffBody = "NOTICE: This is an automated message confirming that you have assigned a student to the INTERVENTION LIST"+"\n"+"\n"+"DATE: "+date+"\n"+"\n"+"FIRST NAME: "+firstName+"\n"+"\n"+"LAST NAME: "+lastName+"\n"+"\n"+"EMAIL ADDRESS: "+studentEmail+"\n"+"\n"+"STAFF MEMBER: "+staffEmail+"\n"+"\n"+"I CAN STATEMENT: "+iCanStatement;
       var fromName = "**"
     } 

     // if homework sheet send homework Email
     if (i == 1 || i ==3) {
        var emailSubject = "HOMEWORK ROOM NOTICE for "+date;
        var studentBody = "NOTICE: This is an automated message confirming that you have been assigned to the HOMEWORK LIST."+"\n"+"\n"+"DATE: "+date+"\n"+"\n"+"FIRST NAME: "+firstName+"\n"+"\n"+"LAST NAME: "+lastName+"\n"+"\n"+"EMAIL ADDRESS: "+studentEmail+"\n"+"\n"+"STAFF MEMBER: "+staffEmail+"\n"+"\n"+"ASSIGNMENT: "+iCanStatement;
        var staffBody = "NOTICE: This is an automated message confirming that you have assigned a student to the HOMEWORK LIST"+"\n"+"\n"+"DATE: "+date+"\n"+"\n"+"FIRST NAME: "+firstName+"\n"+"\n"+"LAST NAME: "+lastName+"\n"+"\n"+"EMAIL ADDRESS: "+studentEmail+"\n"+"\n"+"STAFF MEMBER: "+staffEmail+"\n"+"\n"+"ASSIGNMENT: "+iCanStatement;
        var fromName = "**"
     }

     // Send Emails
        MailApp.sendEmail(studentEmail, emailSubject, studentBody, {bcc:"**@**.org", name:fromName, replyTo:staffEmail});
        MailApp.sendEmail(staffEmail, emailSubject, staffBody, {name:fromName, replyTo:staffEmail});
      }
  }

  catch(e) {
   MailApp.sendEmail("**@**.org", "Error on Intervention Spreadsheet 3.0", e.message);
  }

1 个答案:

答案 0 :(得分:0)

问题出在您要计算要处理的行数的行中:

for (var row=0; row<values.length; row++) {
  if (!values[row].join("")) break;
}
var cell = sheet.getRange(3, 1, lastFullRow, 7);

这样,脚本将仅处理前填充的行。考虑改用getLastRow并将填充行的检查移到后面的for循环中:

var mainSheet = ivList[i];  
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(mainSheet);

var range = sheet.getDataRange();
var values = range.getValues();


var UserData = values;
  try{
    for (var j in UserData) {
      var row = UserData[j];
      // do nothign if the row is empty
      // Updates
      if (!row.join("")) continue;

      // proceed with non empty rows
      var grade = row[0];
      var lastName = row[1];
      var firstName = row[2];
      var studentEmail = row[3];
      var staffEmail= row[4];
      var iCanStatement = row[5];
      var date = row[6];


      // if IV sheet send IV email
     if (i == 0 || i == 2) {
       var emailSubject = "INTERVENTION NOTICE for "+date;
       var studentBody = "NOTICE: This is an automated message confirming that you have been assigned to the INTERVENTION LIST."+"\n"+"\n"+"DATE: "+date+"\n"+"\n"+"FIRST NAME: "+firstName+"\n"+"\n"+"LAST NAME: "+lastName+"\n"+"\n"+"EMAIL ADDRESS: "+studentEmail+"\n"+"\n"+"STAFF MEMBER: "+staffEmail+"\n"+"\n"+"I CAN STATEMENT: "+iCanStatement;
       var staffBody = "NOTICE: This is an automated message confirming that you have assigned a student to the INTERVENTION LIST"+"\n"+"\n"+"DATE: "+date+"\n"+"\n"+"FIRST NAME: "+firstName+"\n"+"\n"+"LAST NAME: "+lastName+"\n"+"\n"+"EMAIL ADDRESS: "+studentEmail+"\n"+"\n"+"STAFF MEMBER: "+staffEmail+"\n"+"\n"+"I CAN STATEMENT: "+iCanStatement;
       var fromName = "**"
     } 

     // if homework sheet send homework Email
     if (i == 1 || i ==3) {
        var emailSubject = "HOMEWORK ROOM NOTICE for "+date;
        var studentBody = "NOTICE: This is an automated message confirming that you have been assigned to the HOMEWORK LIST."+"\n"+"\n"+"DATE: "+date+"\n"+"\n"+"FIRST NAME: "+firstName+"\n"+"\n"+"LAST NAME: "+lastName+"\n"+"\n"+"EMAIL ADDRESS: "+studentEmail+"\n"+"\n"+"STAFF MEMBER: "+staffEmail+"\n"+"\n"+"ASSIGNMENT: "+iCanStatement;
        var staffBody = "NOTICE: This is an automated message confirming that you have assigned a student to the HOMEWORK LIST"+"\n"+"\n"+"DATE: "+date+"\n"+"\n"+"FIRST NAME: "+firstName+"\n"+"\n"+"LAST NAME: "+lastName+"\n"+"\n"+"EMAIL ADDRESS: "+studentEmail+"\n"+"\n"+"STAFF MEMBER: "+staffEmail+"\n"+"\n"+"ASSIGNMENT: "+iCanStatement;
        var fromName = "**"
     }

     // Send Emails
        MailApp.sendEmail(studentEmail, emailSubject, studentBody, {bcc:"**@**.org", name:fromName, replyTo:staffEmail});
        MailApp.sendEmail(staffEmail, emailSubject, staffBody, {name:fromName, replyTo:staffEmail});
      }
  }

  catch(e) {
   MailApp.sendEmail("**@**.org", "Error on Intervention Spreadsheet 3.0", e.message);
  }