表格应用程序脚本 - 循环行以电子邮件形式发送

时间:2017-11-29 03:48:31

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

我使用以下代码从工作表发送电子邮件,但我有一个问题......

我无法进入下一次迭代,其中发送的列="是" ie:只想包含发送列=""

的行

提前感谢您的协助。



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
 const EmailSent = CurrentRow[11];
   
  //if row has been sent, then continue to next iteration
   if (EmailSent == "Yes") 
     continue;
   
  //set HTML template for information
  message +=
      "<p><b>Date of Works: </b>" + CurrentRow[0] + "</p>" +
      "<p><b>Customer: </b>" + CurrentRow[1] + "</p>" +
      "<p><b>Project No: </b>" + CurrentRow[2] + "</p>" +
      "<p><b>Project: </b>" + CurrentRow[3] + "</p>" +
      "<p><b>Location: </b>" + CurrentRow[4] + "</p>" +
      "<p><b>Site Contact: </b>" + CurrentRow[5] + "</p>" +
      "<p><b>Starting Point: </b>" + CurrentRow[6] + "</p>" +
      "<p><b>Start Time: </b>" + CurrentRow[7] + "</p>" +
      "<p><b>Truck Size: </b>" + CurrentRow[8] + "</p><br><br>";
   
   //set the row to look at
   var setRow = parseInt(i) + StartRow;

   
  //mark row once Sent
  ActiveSheet.getRange(setRow, 11).setValue("Yes");
}

  //define who to send email to 
  var SendTo = "scheduling@baqprojects.com"
 
  //set subject line
  var Subject = "#6 Test - 2 - Selected Rows";


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

2 个答案:

答案 0 :(得分:0)

试试这个:

function sendEmail(){
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet();
  var startRow=2;
  var rg=ss.getDataRange();
  var vA=rg.getValues();
  var message="";
  for(var i=1;i<vA.length;i++){
    message +="<p><b>Date of Works: </b>" + vA[i][0] + "</p>" + "<p><b>Customer: </b>" + vA[i][1] + "</p>" + "<p><b>Project No: </b>" + vA[i][2] + "</p>" + "<p><b>Project: </b>" + vA[i][3] + "</p>" + "<p><b>Location: </b>" + vA[i][4] + "</p>" + "<p><b>Site Contact: </b>" + vA[i][5] + "</p>" + "<p><b>Starting Point: </b>" + vA[i][6] + "</p>" + "<p><b>Start Time: </b>" + vA[i][7] + "</p>" + "<p><b>Truck Size: </b>" + vA[i][8] + "</p><br><br>";
    var SendTo="scheduling@baqprojects.com"
    var Subject="#6 Test - 2 - Selected Rows";
    if(vA[i][11]!='Yes'){
      MailApp.sendEmail({to: SendTo,cc: "",subject: Subject,htmlBody: message,});
      sh.getRange(i+1,12).setValue('Yes');
    }
  }
}

答案 1 :(得分:0)

我最终采用了不同的方法......

在电子表格中,我有一个&#39;控制单元&#39;它使用数据验证列出我想要发送电子邮件的公司。

在此之下,我运行一个查询,仅根据控制单元格将该公司的数据绘制到一个表中。

以下脚本遍历公司列表,更新控制单元格并更改表格,然后使用其他功能将相关表格单独发送给每个公司。

希望这有助于你们中的一些人!

function bulkbookingsdraft() {

  // loop through table booking all listed companies

  var app = SpreadsheetApp;
  var sheet = app.getActiveSpreadsheet().getActiveSheet();
  var companies = sheet.getDataRange();
  var lastRow = companies.getLastRow();
  var searchRange = sheet.getRange(3,9, lastRow-1,9);
  var cmdbox = sheet.getRange(1, 5)

  // Get array of values in the searchRange - the list of companies
  var rangeValues = searchRange.getValues();

  // Loop through array and update control cell
    for (r = 1 ; r < lastRow - 1; r++){
     var company = sheet.getRange(r+2,9).getValue()        

      cmdbox.setValue(company);
      sendEmail_draft();     

   }
}