如何使用工作表中的邮件?

时间:2019-07-05 10:49:19

标签: google-apps-script

我正在使用Send single email with values from all new rows in a spreadsheet (Google Script / GAS)将邮件从新行的工作表发送到给定的地址。现在,我喜欢使用当前行中的邮件地址。我也想在邮件消息中包含指向文档的链接...

这是我正在使用的脚本:

function sendEmail() {

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

 //iterate loop
 for (i in AllValues) {

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

 //set subject line
   var Subject = "Neu für " + CurrentRow[1]  + ": "+ CurrentRow[2] + " für " + CurrentRow[3];

 //set HTML template for information
 var message = 
      "<p><b>Ressort: </b>" + CurrentRow[1] + "</p>" +
      "<p><b>Textart: </b>" + CurrentRow[2] + "</p>" +
      "<p><b>Domain: </b>" + CurrentRow[3] + "</p>" +
      "<p><b>Thema: </b>" + CurrentRow[4] + "</p>" +
      "<p><b>fertig bis: </b>" + CurrentRow[15] + "</p>";

 //define column to check if sent
 var EmailSent = CurrentRow[0];

 //define who to send grants to 
 var SendTo = "xxx@yyy.com" + "," + "xxx@zzz.yyy.com";

 //if row has not been sent, then...  
 if (EmailSent != "gesendet") {

  //set the row to look at
  var setRow = parseInt(i) + StartRow;

  //mark row as "sent"
  ActiveSheet.getRange(setRow, 1).setValue("gesendet");

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

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

我个人不会使用活动表。我更喜欢使用getSheetByName(),但是我想所有这些都对您有用,所以我没有更改。我已经评论了我更改或添加的项目。

function sendEmail() {
  var ActiveSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var StartRow = 3;
  var RowRange = ActiveSheet.getLastRow() - StartRow + 1;
  var WholeRange = ActiveSheet.getRange(StartRow,1,RowRange,23);//Added a new column for recipient email address
  var AllValues = WholeRange.getValues();
  for (var i=0;i<AllValues.length;i++) {
    var CurrentRow = AllValues[i];
    var Subject = "Neu für " + CurrentRow[1]  + ": "+ CurrentRow[2] + " für " + CurrentRow[3];
    var message = "<p><b>Ressort: </b>" + CurrentRow[1] + "</p>" + "<p><b>Textart: </b>" + CurrentRow[2] + "</p>" + "<p><b>Domain: </b>" + CurrentRow[3] + "</p>" + "<p><b>Thema: </b>" + CurrentRow[4] + "</p>" + "<p><b>fertig bis: </b>" + CurrentRow[15] + "</p>";
    var EmailSent = CurrentRow[0];
    var SendTo = (CurrentRow[22])?CurrentRow[22]:"xxx@yyy.com" + "," + "xxx@zzz.yyy.com";//conditional (ternary) operator.  If CurrentRow[22] is there then use it otherwise use your default.
    if (EmailSent != "gesendet") {
      var setRow = parseInt(i) + StartRow;
      ActiveSheet.getRange(setRow, 1).setValue("gesendet");
      MailApp.sendEmail({to: SendTo,cc: "",subject: Subject,htmlBody: message});
    }
  }
}

conditional (ternary) operator