如何调试 Google 脚本不从行中读取信息的原因?

How to debug why a Google Script is not reading information from rows?

我正在尝试编写一个脚本,在员工被录用后通过电子邮件发送他们的帐户详细信息。该脚本最终发送了一封电子邮件。并且不发送重复项。但是,它没有按预期读取每一行的信息。我不知道发生了什么。我也真的迷路了

sheet.getRange(startRow + i-18,1).setValue(EMAIL_SENT);

在底部以及为什么我必须输入 -18 才能将 EMAIL_SENT 写入 sheet.

中的正确行

这是脚本:

var EMAIL_SENT = 'EMAIL_SENT';

function sendEmails() {
  var sheet = SpreadsheetApp.getActiveSheet();

  var startRow = 2; // First row of data to process

  var numRows = 55; // Number of rows to process



  var dataRange = sheet.getRange(startRow, 1, numRows, 9);


  // Fetch values for each row in the Range.

  var data = dataRange.getValues();

  var subject = "MCS Account Info";

    for (i in data) {
 //  clear variables between loops    
      var email1 = "";

      var ccs = "";

      var message = "";

      var row = data[i];

      var email1 = row[4];
      if(row[4] !== "") var ccs = row[4];
      if(row[5] !== "") var ccs = ccs + ", " + row[5];
      if(row[6] !== "") var ccs = ccs + ", " + row[6];
      if(row[7] !== "") var ccs = ccs + ", " + row[7];

      var message = row[8]; 
 // just so I can see what is in each variable will be removed when working    
      Logger.log("email - ",row[3],"subject - ",subject,"message - ",message,"bcc - ",ccs);

      if (row[0] !== EMAIL_SENT) {
        if(row[2] !== "") {
          MailApp.sendEmail(row[3], subject, message, {
            bcc: ccs
          });
          sheet.getRange(startRow + i-18,1).setValue(EMAIL_SENT);
          SpreadsheetApp.flush();
        }
      }
    }
}

和 link 测试 sheet 我正在使用:https://docs.google.com/spreadsheets/d/1CzrMlsgwvkzp_w4IjZcOkSl7rU9T8qnBYBYi2luwWkg/edit?usp=sharing

如果 link 不起作用,这是一个有 9 列的 sheet。

A - EMAIL_SENT message
B - # to look up info from another sheet
C - Username
D - User's Email address
E - H Additional Email Addresses for BCC
G - Message to send

Sheet 随着时间的推移将有数百个条目,现在我正在查看前 56 行,即使我的 sheet 中只有 2 个条目。

试试这个:

function sendEmails() {
  var sheet=SpreadsheetApp.getActiveSheet();
  var startRow=2; 
  var numRows=55; 
  var dataRange=sheet.getRange(startRow,1,numRows,9);
  var data=dataRange.getValues();
  var subject="MCS Account Info";
  for (var i=0;i<data.length;i++) {
    var ccs="";
    var row=data[i];
    var email1=row[4];
    if(row[4]!="") var ccs=row[4];
    if(row[5]!="") var ccs=ccs + ", " + row[5];
    if(row[6]!="") var ccs=ccs + ", " + row[6];
    if(row[7]!="") var ccs=ccs + ", " + row[7];
    var message=row[8]; 
    if (row[0]!='EMAIL_SENT' && row[2]!="") {
      MailApp.sendEmail(row[3], subject, message, {bcc: ccs});
      sheet.getRange(i+startRow,1).setValue('EMAIL_SENT');
    }
  }
}