电子邮件GAS中的格式表

时间:2014-02-17 11:36:46

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

以下代码发送一封包含所选行数据的电子邮件,我需要帮助格式化输出表。

function sendemail() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Visitors Log");
  var user = Session.getActiveUser();
  var activecell = sheet.getActiveCell().getRow();

  var startdate = Utilities.formatDate((sheet.getRange(activecell, 2).getValue()), 'IST', 'dd/MMM/yyyy');
  var enddate = Utilities.formatDate((sheet.getRange(activecell, 3).getValue()), 'IST', 'dd/MMM/yyyy');
      var visitorsname = sheet.getRange(activecell, 4).getValue();
      var designation  = sheet.getRange(activecell, 5).getValue();

      var body =""


      body += "<tr><tr style='padding:5px'>" + startdate + "</tr><tr style='padding:5px'>" + enddate + "</tr><tr style='padding:5px'>" + visitorsname + "</tr><tr style='padding:5px'>" + designation

      MailApp.sendEmail(user,"Visitor Details",body,{htmlBody: body, cc:'xxx@gmail.com'});

    }

收到的电子邮件

17/Feb/2014
23/Feb/2014
Vasim
Manager

需要电子邮件

Start Date     17/Feb/2014
End Date       23/Feb/2014
Visitor's Name Vasim
Designation    Manager

如果我可以获得一些彩色背景,那么硬编码的标题会非常好而且是的。 (因为我有一些更多的变量定义,因此不需要循环使用工作表)

2 个答案:

答案 0 :(得分:1)

试试这个身体

body += "<table><tr><td>Start Date</td><td>" + startdate + "</td></tr>"+
"<tr><td>End Date</td><td>" + enddate + "</td></tr>"+
"<tr><td>Visitor's Name</td><td>" + visitorsname + "</td></tr>"+
"<tr><td>Destination</td><td>" + designation+ "</td></tr></table>";

当然,您可以使用评估某些文件的模板检查here

答案 1 :(得分:1)

这里您的代码有一些变化:

function sendemail() {
  var ss = SpreadsheetApp.getActiveSheet();
  //  var ss = SpreadsheetApp.getActiveSpreadsheet();
  //  var sheet = ss.getSheetByName("Visitors Log");
  var user = Session.getActiveUser();
  var row = ss.getActiveCell().getRow();
  //  var activecell = sheet.getActiveCell().getRow();
  var vals = ss.getRange(row, 2, 1, 4).getValues();

  var startdate = Utilities.formatDate(new Date(vals[0][0]), 'IST', 'dd/MMM/yyyy');
  var enddate = Utilities.formatDate(new Date(vals[0][1]), 'IST', 'dd/MMM/yyyy');
  var visitorsname = vals[0][2];
  var designation  = vals[0][3];

  var body =""

  var thStyle = "padding:5px;";
  var tdStyle = "font-style:oblique;background-color:#BDBDBD";
  var tableStyle = "border:1px"

      body += "<table style='"+tableStyle+"'><tr>"+
        "<th style='"+thStyle+"'>Start Date </th>"+
          "<td style='"+tdStyle+"'>"+startdate + "</td></tr>"+
            "<tr><th style='"+thStyle+"'>End Date </th>" +
              "<td style='"+tdStyle+"'>"+enddate + "</td></tr>"+
                "<tr><th style='"+thStyle+"'>Visitor's Name</th>"+
                  "<td style='"+tdStyle+"'>"+visitorsname +"</td></tr>"+
                    "<tr><th style='"+thStyle+"'>Designation</th>"+
                      "<td style='"+tdStyle+"'>"+designation+"</td></tr>"+
                        "</table>";
  var cc="";

  MailApp.sendEmail(user,"Visitor Details",body,{htmlBody: body, cc:cc});

}
相关问题