发送电子邮件-保持单元格格式

时间:2019-03-06 22:55:38

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

我有一个Google电子表格,该表格可以发送包含在另一张表格中查询和连接的列中的学生信息。查询的信息由回车分隔。然后将此列通过电子邮件发送给由公式触发的家庭,该公式计算单元格中应该有多少回车。

我已经在工作表本身中处理过这一部分。我需要通过电子邮件发送列和维护单元格格式的帮助。我已经可以使用下面的代码来做到这一点,但是它会在每封电子邮件中发送D列中的所有数据。我只需要发送电子邮件相应行中的内容即可。

给我带来麻烦的代码的主要部分如下:

var OriginalString = SpreadsheetApp.getActive().getRange("D2:D").getValues();
var NewString = OriginalString.toString().replace(/\n/g, '<br>');

任何帮助将不胜感激!

function SendEmail3() {
    var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet3');
    var startrow = 2;
    var lastRow = ss.getLastRow();
    var lastCol = 10;
    var range = ss.getRange(2, 1, lastRow, lastCol);
    var data = range.getValues();
    for (var i = 0; i < data.length; i++) {
        var row = data[i];
        var AccountName = row[0];
        var email = row[1];
        var Parent = row[2];
        var Consent = row[4];
        var StudentData = row[3];
        var CarriageReturns = row[6];
        var Trigger = row[7];
        var emailSent = row[8];
        var subject = "Your Application";
      var OriginalString = SpreadsheetApp.getActive().getRange("D2:D").getValues();
        var NewString = OriginalString.toString().replace(/\n/g, '<br>');

        var message = "<HTML><BODY><font size=4>" +
            "<P>" + "Hello " + Parent + ','
            +"<BR>
            +"<BR>" + NewString + "<BR>"
                        +"</HTML></BODY>";
        var recipientsTo = email;
        if (AccountName.length > 0 && Trigger >= CarriageReturns && emailSent != 'EMAIL_SENT') {
            MailApp.sendEmail({
                subject: subject,
                to: recipientsTo,
                htmlBody: message
            });
            ss.getRange(startrow + i, 9).setValue('EMAIL_SENT');
        }
    }
}

1 个答案:

答案 0 :(得分:1)

这是您要寻找的吗?

我对其进行了设置,以便您可以在无模式的对话框中看到消息,而不是发送电子邮件。

function SendEmail3() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet3');
  var startrow = 2;
  var lastRow = ss.getLastRow();
  var lastCol = 10;
  var range = ss.getRange(2, 1, lastRow, lastCol);
  var data = range.getValues();
  var html='';//Remove
  for (var i = 0; i < data.length; i++) {
    var row = data[i];
    var AccountName = row[0];
    var email = row[1];
    var Parent = row[2];
    var Consent = row[4];
    var StudentData = row[3];
    var CarriageReturns = row[6];
    var Trigger = row[7];
    var emailSent = row[8];
    var subject = "Your Application";
    var NewString = StudentData.split('\n').join('<br />');
    //var message = "<HTML><BODY><font size=4>" + "<P>" + "Hello " + Parent + ',' + "<BR>" +"<BR>" + NewString + "<BR>" +"</HTML></BODY>";
    var message=Utilities.formatString('<HTML><BODY><font size=4><P>Hello %s,<BR><BR>%s<BR></HTML></BODY>', Parent,NewString);
    var recipientsTo = email;
    if (AccountName.length > 0 && Trigger >= CarriageReturns && emailSent != 'EMAIL_SENT') {
      //MailApp.sendEmail({subject: subject,to: recipientsTo,htmlBody: message});
      html+=Utilities.formatString('%s- %s',i+1,message);//Remove
      //ss.getRange(startrow + i, 9).setValue('EMAIL_SENT');
    }
  }
  var userInterface=HtmlService.createHtmlOutput(html);//Remove
  SpreadsheetApp.getUi().showModelessDialog(userInterface, 'Email Message');//Remove
}