Google Script电子邮件发送忽略行中的#N / A值

时间:2018-11-14 16:02:35

标签: javascript google-apps-script

基本上,我还通过公式从单独的工作表中提取电子邮件地址,以便在输入新表单时,B列会填充适当的电子邮件(表单中包含名称)。我面临的问题是,为了使该工具高效运行,我需要无限期地完全运行公式,以便在出现新条目时仅查找名称,而B列会提取正确的电子邮件地址。但是,下面的代码返回错误,因为它无法处理尚不存在电子邮件的#N / A,甚至无法处理公式中带有IFERROR的“”。

理想情况下,如果遇到#N / A,代码将停止,并且仅在CurrentRow [1]中有电子邮件的情况下才继续。如果空白行完全空白,则代码可以正常工作。

这里的当前版本来自我已阅读和研究的内容,包括帖子:Email only new rows in Google sheets。我已经研究过,并且存在许多类似的示例,但是尝试了40多种变体后,我无法正常工作。

function sendEmail() {

 //setup function
 var ActiveSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
 var StartRow = 2;
 var RowRange = ActiveSheet.getLastRow() - StartRow + 1;
 var WholeRange = ActiveSheet.getRange(StartRow,1,RowRange,11);
 var AllValues = WholeRange.getValues();

 var message = "";
 
  //iterate loop
 for (i in AllValues) {

 //set current row
 var CurrentRow = AllValues[i];

 //define column to check if sent
 var KamEmail = CurrentRow[1];

 //if row has been sent, then continue to next iteration
   if (CurrentRow[1] == "#N/A") {break;}
     
  else;
    
     
 //set HTML template for information
  message +=
      "<p>Hi " + CurrentRow[1] + "</p>" +
      "<p>Your constituent has submitted an Agenda blog pitch" + "</p>" +
      "<p><b>Name of constituent: </b>" + CurrentRow[2] + "</p>" +  
      "<p><b>Submission Snapshot: </b>" + CurrentRow[3] + "</p>" +
      "<p><b>Link to Submission: </b>" + CurrentRow[4] + "</p>" +
      "<p><b>Date of Submission: </b>" + CurrentRow[7] + "</p><br><br>";
  
  //set the row to look at
  var setRow = parseInt(i) + StartRow;

  //mark row as "sent"
  ActiveSheet.getRange(setRow, 9).setValue("Sent to KAM");
}

 //define who to send grants to 
 var SendTo = CurrentRow[1];

 //set subject line
  var Subject = "RE: Registrations";


  //send the actual email  
  MailApp.sendEmail({
      to: SendTo,
      cc: "",
      subject: Subject,
      htmlBody: message,
});
}

0 个答案:

没有答案