谷歌脚本发送有条件的电子邮件

时间:2014-12-19 20:43:22

标签: email google-apps-script google-sheets

我正在尝试在Google工作表上运行脚本,以便仅在某个列不等于“y”时才发送电子邮件。如果这是真的,那么我希望它发送电子邮件,然后在列中添加“y”以防止发送重复。这不起作用 - 我不断收到重复的电子邮件。这是代码:

 function sendEmails() {   
   var sheet = SpreadsheetApp.getActiveSheet();
   var startRow = 2;    // First row of data to process   
   var numRows = 600;   // Number of rows to process   
   // Fetch the range of cells A2:B3   
   var dataRange = sheet.getRange(startRow, 1, numRows, 600) 
   //Fetch values for each row in the Range.   
   var data = dataRange.getValues();   
   for (i in data) {
     var row = data[i];
     if (row[19]!= 'y') {
       var emailAddress = "xxx@xxx.com"; // First column
       var message = "A new trip request has been submitted.  Here's what you need to know.  Trip #: " + row[1] + " " + row[4] + " to:" +
 row[10] + " Depart Date: " + row[8];       // Second column
       var subject = "New Trip Request  " + row[1];
       MailApp.sendEmail(emailAddress, subject, message);
       sheet.getRange(i+2,19).setValue('y');
     }
   }
 }

2 个答案:

答案 0 :(得分:0)

条件检查" y" value具有硬编码值:

if (row[19]!= 'y') {

用" i"

替换19
if (row[i]!= 'y') {

答案 1 :(得分:0)

正如KRR已经建议的那样,如果你想检查电子表格的col19,在.getValues()返回的(零索引)2d数组中,你将不得不使用:

if (row[18]!= 'y') {

这一行:

sheet.getRange(i+2,19).setValue('y');

在电子表格的第19列中设置“y”。我认为这是正确的吗?