如何在脚本再次运行时将“已发送的电子邮件”添加到状态列并排除那些行?

时间:2019-03-14 15:32:12

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

我对脚本编写非常陌生,并且正在努力为青年联盟计划的服装筹款活动编写脚本。单击图像触发脚本后,该脚本将向列表中的所有收件人发送电子邮件,在电子邮件发送至的所有行中将“电子邮件发送”添加到状态列“ I” ...脚本再次运行,仅将电子邮件发送到尚未通过电子邮件发送的新数据行。

我拥有的脚本会插入发送的电子邮件,但是如果再次运行该脚本,它不会关注状态列“ I”,并且会收到重复的电子邮件。我已经使用了教程视频来带我深入,但现在我陷入了困境。我试图从类似的问题中寻求帮助,但是我经验不足,无法对其进行修改以满足我的需求。这是Google工作表的副本,“发送电子邮件”工作表包含正在运行脚本的信息。更新:

https://docs.google.com/spreadsheets/d/1MBVhLj1A7Z_cpYxs_s5ae11ykJvTmS16E0jW4tGsNU4/edit?usp=sharing

function sendOrderEmails() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  ss.setActiveSheet(ss.getSheetByName("Send Emails"));
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;
  var dataRange = sheet.getRange("A2:I10");
  var data = dataRange.getValues();
  for (var i = 0; i < data.length; ++i) {
    var rowData = data[i];
    var emailAddress = rowData[0];
    var recipient = rowData[1];
    var message1 = rowData[2];
    var parameter1 = rowData[3];
    var message2 = rowData[4];
    var message3 = rowData[5];
    var parameter2 = rowData[6];
    var message4 = rowData[7];
    var emailSent = rowData[9];
    var message = 'Hi ' + recipient + ',\n\n' + message1 + parameter1 + ' ' + message2 + ' ' + message3 + parameter2 + '. ' + message4 + '\n\n' + 'Tri-Valley Youth League Softball';
    var subject = 'Order Reference Number ' + parameter2;
    if (emailSent != "EMAIL_SENT" ) { // Prevents sending duplicates
      MailApp.sendEmail(emailAddress, subject, message);
      sheet.getRange(startRow + i, 9).setValue("EMAIL_SENT");
      // Make sure the cell is updated right away in case the script is interrupted
      SpreadsheetApp.flush();
    }
  }
}

1 个答案:

答案 0 :(得分:0)

尝试一下:

function sendOrderEmails() {
  var ss=SpreadsheetApp.getActive();
  var sheet=ss.getSheetByName("Send Emails");
  var startRow=2;
  var dataRange=sheet.getRange("A2:J10");//Include column 10 in the range
  var data=dataRange.getValues();
  for (var i = 0; i < data.length; ++i) {
    var rowData = data[i];
    var emailAddress = rowData[0];
    var recipient = rowData[1];
    var message1 = rowData[2];
    var parameter1 = rowData[3];
    var message2 = rowData[4];
    var message3 = rowData[5];
    var parameter2 = rowData[6];
    var message4 = rowData[7];
    var emailSent = rowData[8];//This is column 9
    var message = 'Hi ' + recipient + ',\n\n' + message1 + parameter1 + ' ' + message2 + ' ' + message3 + parameter2 + '. ' + message4 + '\n\n' + 'Tri-Valley Youth League Softball';
    var subject = 'Order Reference Number ' + parameter2;
    if (emailSent != "EMAIL_SENT" ) { 
      MailApp.sendEmail(emailAddress, subject, message);
      sheet.getRange(startRow + i, 9).setValue("EMAIL_SENT");
    }
  }
}

我这样做是为了让您可以看到行,列和数据索引之间的差异。

function rowsColumnsAndIndices() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet();
  var rg=sh.getRange(1,1,26,26);
  var vA=rg.getValues();
  for(var i=0;i<vA.length;i++) {
    for(var j=0;j<vA[i].length;j++) {
      vA[i][j]=Utilities.formatString('r: %s, c: %s\ni: %s,j: %s', i+1,j+1,i,j);
    }
  }
  rg.setValues(vA);
}