在一封电子邮件中同时结合文本消息和HTML表

时间:2019-05-31 10:48:27

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

我有以下代码,用于发送超出阈值的案例的电子邮件(这些案例作为HTML对象插入到电子邮件中)。现在,代码可以正常工作,但是它并没有做我想做的一件事。

function CheckAAShare() {
  // Fetch the monthly sales
  var Range = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("4. Daily Catergory Share").getRange("I2"); 
  var result = Range.getValue();
var dataSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("4. Email alert");
var lastRow = dataSheet.getLastRow();
var data = dataSheet.getRange(1, 1, lastRow, 6).getValues();


  var TABLEFORMAT = 'cellspacing="2" cellpadding="2" dir="ltr" border="1" style="width:100%;table-layout:fixed;font-size:10pt;font-family:arial,sans,sans-serif;border-collapse:collapse;border:1px solid #ccc;font-weight:normal;color:black;background-color:white;text-align:center;text-decoration:none;font-style:normal;'

  var htmltable = '<table ' + TABLEFORMAT +' ">';

for (row = 0; row<data.length; row++){

htmltable += '<tr>';

for (col = 0 ;col<data[row].length; col++){
  if (data[row][col] === "" || 0) {htmltable += '<td>' + 'None' + '</td>';} 
  else
    if (row === 0)  {
      htmltable += '<th>' + data[row][col] + '</th>';
    }

  else {htmltable += '<td>' + data[row][col] + '</td>';}
}

     htmltable += '</tr>';
}

     htmltable += '</table>';
     Logger.log(data);
     Logger.log(htmltable);
  // Check totals sales
  if (result >0){
    // Fetch the email address
      var emailAddress = "ops@gmail.com";
    // Send Alert Email.
    var message = 'There are ' + result +' deviating metrics: https://docs.google.com/spreadsheets/d/1AQEV4Gt919TIu92Gb9TeZRD3KpSm3L_WCHgTOmw/edit#gid=1525731698'
    + ' See the dashboard here: <Dashboard link>;
    var subject = 'Transaction Monitoring Alert';
    MailApp.sendEmail(emailAddress, subject, message, {htmlBody:htmltable});
    }
}

从代码中可以看到,我还想在电子邮件中的此表之前附加一条文本消息:类似“有4个偏离的度量标准。请在此处查看仪表板: Dashboard_link 。请参见以下摘要:“ + HTML_table

问题是:如何在1封电子邮件中同时组合此文本消息和HTML表?当前输出如下:Dashboard_link

1 个答案:

答案 0 :(得分:0)

You need to write the message in both text and HTML format.

var textMessage = 'There are ' + result +' deviating metrics: ' + ' See the dashboard here: https://docs.google.com/spreadsheets/d/1AQEV4Gt919TIu92Gb9TeZRD3KpSm3L_WCHgTOmw/edit#gid=1525731698';
var htmlMessage = '<p>There are ' + result +' deviating metrics:' + ' See the dashboard <a href="https://docs.google.com/spreadsheets/d/1AQEV4Gt919TIu92Gb9TeZRD3KpSm3L_WCHgTOmw/edit#gid=1525731698">here</a></p>';
var subject = 'Transaction Monitoring Alert';
MailApp.sendEmail(emailAddress, subject, textMessage, {htmlBody:htmlMessage + htmltable});