发送电子邮件并管理空行google脚本

时间:2017-09-05 09:19:56

标签: google-apps-script google-sheets gmail-api

我有一个将电子邮件发送到列表的脚本有点问题,我发现并编辑了一下这个,但我需要它来处理空行。

我如何编码"如果行[1]已填写,则发送电子邮件,否则跳过并转到下一行" ?

谢谢^^



function sendEmails() {
  var sheet = SpreadsheetApp.getActive().getSheetByName("Cache");
  var startRow = 5; 
  var numRows = 100; 
  var dataRange = sheet.getRange(startRow, 8, numRows, 2)
 
  var data = dataRange.getValues();
  var message = sheet.getRange('J5').getValues();
  for (i in data) {
  var row = data[i];
  var emailAddress = row[1]; 
  var subject = "Title";
  var body2 = "Hello " + row[0] + "\n\n" + message
    
  MailApp.sendEmail(emailAddress, subject, body2);
 }
}




1 个答案:

答案 0 :(得分:0)

如果前两列不为空,则不会发送电子邮件

  function sendEmails() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('Cache')
  var startRow=5; 
  var numRows=100; 
  var rg=sh.getRange(startRow, 8, numRows, 2);
  var vA=rg.getValues();
  for (var i=0;i<vA.length;i++) 
  {
    var emailAddress=vA[i][1];
    var body2 = "Hello " + vA[i][0] + "\n\n" + sh.getRange('J5').getValue();
    if(vA[i][1] && vA[i][0])//only sends emails when first two columns have something in them
    {
      MailApp.sendEmail(vA[i][1], 'Title', body2);
    } 
 }
}