如何控制从电子表格中读取的日期格式,并在电子邮件中显示为表格

How to control format of date read from spreadsheet, and display as table in email?

本文关键字:格式 电子邮件 表格 显示 日期 读取 何控制 控制 电子表格      更新时间:2023-09-26

这里完全是编码新手,所以请原谅我的无知。

我们有一个google电子表格,在B2中包含一个电子邮件地址,在单元格A4 &DX(取决于数据的大小)和我已经拼凑了一些脚本,以便为每个选项卡发送电子邮件:

function sendEmail() {
var ss = SpreadsheetApp.getActive();
for(var n in ss.getSheets()){// loop over all tabs in the spreadsheet
var sheet = ss.getSheets()[n];// look at every sheet in spreadsheet
var lastRow = sheet.getLastRow();
var to = sheet.getRange('B2').getValue();
var data = sheet.getRange('A4:D' + lastRow).getValues();
var body = '';
for( var row in data ) {
for( var col in data[row] ) {
body += data[row][col] + '' ,';
  }
body += ''n';}
MailApp.sendEmail(to, 'Your Holiday Extras Company Credit Card Statement', body);
}
}

这可以正常工作,但是会发出这样的电子邮件:

Cardholder ,Merchant ,Date Occurred ,Amount ,
A CLOSE ,test 11 ,Thu Jun 25 2015 00:00:00 GMT+0100 (BST) ,8.4 ,
A CLOSE ,test 12 ,Tue Jun 30 2015 00:00:00 GMT+0100 (BST) ,1.5 ,
A CLOSE ,test 13 ,Tue Jun 30 2015 00:00:00 GMT+0100 (BST) ,17 ,
A CLOSE ,test 14 ,Tue Jun 30 2015 00:00:00 GMT+0100 (BST) ,24.55 ,
A CLOSE ,test 15 ,Tue Jun 30 2015 00:00:00 GMT+0100 (BST) ,2.58 ,
A CLOSE ,test 16 ,Thu Jul 02 2015 00:00:00 GMT+0100 (BST) ,133.2 ,

…虽然包含了正确的数据,但看起来相当糟糕。

无论如何,使用上面的脚本作为shell,将电子邮件正文格式化为表以显示上述数据,如果没有,我该如何将数据的格式更改为只是dd/mm/yy,而不是包括时间等,并更改格式以阻止每个字段之后出现的随机空格?

好的,用表格格式化电子邮件的唯一方法是使用HTMLbody选项。至于日期,你有两个选择,要么你把它们变成字符串,删除你想要的,或者像Zig说的那样,使用javascript日期格式。

我修改了你的代码一点有表,并使用字符串格式修改文本..

function sendEmail() {
  var ss = SpreadsheetApp.getActive();
  for(var n in ss.getSheets())  // loop over all tabs in the spreadsheet
  {
    var sheet = ss.getSheets()[n];// look at every sheet in spreadsheet
    var lastRow = sheet.getLastRow();
    var to = sheet.getRange('B2').getValue();
    var headers = sheet.getRange('A4:D4').getValues()[0];
    var data = sheet.getRange('A5:D' + lastRow).getValues();
    var htmlmessage = "<HTML><BODY>"
     +"<P> This is a new paragraphe before the table</P>"
     +"<BR> This is a new line before the table</BR>"
     +"<P><TABLE border='0' table-layout:auto;>"
     +"<TR>";
    for (var header in headers)
    {
      htmlmessage += "<TD><Strong>"+headers[header]+"</strong></TD>";
    }
    htmlmessage += "</TR>";
    for (var row in data)
    {
      htmlmessage += "<TR>";
      for (var col in data[row])
      {
        htmlmessage += "<TD>" + data[row][col].toString().replace("00:00:00 GMT+0100 (BST)","") + "</TD>"
      }
      htmlmessage += "</TR>";
    }
    MailApp.sendEmail({
      to: to,
      subject: 'Your Holiday Extras Company Credit Card Statement',
      htmlBody: htmlmessage});
  }
}

你也可以替换这个

    for (var row in data)
    {
      htmlmessage += "<TR>";
      for (var col in data[row])
      {
        htmlmessage += "<TD>" + data[row][col].toString().replace("00:00:00 GMT+0100 (BST)","") + "</TD>"
      }
      htmlmessage += "</TR>";
    }

,如果你想使用javascript的数据格式。

  for (var col in data[row])
  {
    var temp = data[row][col]
    if (col == 2)
    {
      temp = Utilities.formatDate(temp, "GMT", "MM/dd/YYYY");
    }
    htmlmessage += "<TD>" + temp + "</TD>"
  }
  htmlmessage += "</TR>";
}
结果:

Cardholder  Merchant    Date Occurred   Amount
 A CLOSE    Test 12        6/30/2015    1.5
 A CLOSE    Test 13        6/30/2015    17
 A CLOSE    Test 14        6/30/2015    24.55
 A CLOSE    Test 15         2/7/2015    2.58
 A CLOSE    Test 16         2/7/2015    133.2

我希望这对你有帮助。如果你有任何问题,我将在评论中回答。


  1. 要添加一个初始句子,您需要在

    之前添加一个部分
    +"<TABLE border='1' table-layout:auto;>"
    

,

+"<P> This is a new paragraphe before the table</P>"
+"<BR> This is a new line before the table</BR>"
  • 我不是HTML编写专家,所以要了解更多高级格式,您可以随时搜索HTML格式。

  • 你可以简单地在你想加粗的部分之前和之后加上"strong"。
  • ,

    htmlmessage += "<TD><strong>"+headers[header]+"</strong></TD>";
    

  • 如果你想删除或修改表格的边框设置,你可以增加或减少border=1旁边的数字。
  • 我已按您的要求修改了以上代码

    相关文章: