简体   繁体   English

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

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

complete coding newbie here so excuse my ignorance. 在这里完成新手编码,请原谅我的无知。

We have a google spreadsheet that contains an e-mail address in B2, and a bunch of data between cells A4 & DX (depending on size of data) and I have cobbled together some script to get an e-mail to send out for every tab: 我们有一个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);
}
}

This works OK, but sends out an e-mail like this: 这样可以正常工作,但是会发出如下电子邮件:

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 ,

...which looks quite terrible, though does contain the correct data. ...虽然包含正确的数据,但看起来非常糟糕。

Is there anyway, using the script above as a shell, to format the e-mail body into a table to show the above data, and if not, how would I go about changing the format of the data to be just dd/mm/yy as opposed to including time etc. and also change the formatting to stop random spaces appearing after every field? 无论如何,使用上面的脚本作为外壳,是否可以将电子邮件正文格式化为表格以显示上述数据,如果没有,我将如何将数据格式更改为dd / mm / yy而不是包含时间等,并且还更改了格式以阻止在每个字段之后出现随机空格?

Many thanks in advance! 提前谢谢了!

Okay the only way to format the email with a table is to use the HTMLbody option. 好的,用表格格式化电子邮件的唯一方法是使用HTMLbody选项。 As for the date you have two option either you turn them to string and remove what you want or go as Zig said and use javascript date formats. 至于日期,您有两个选择,要么将它们变成字符串并删除您想要的内容,要么按照Zig的说明使用JavaScript日期格式。

I modified your code a bit to have the table and used the string format to modify the text.. 我对您的代码做了一些修改,使它具有表,并使用字符串格式来修改文本。

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});
  }
}

you can also replace this 你也可以取代这个

    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>";
    }

by this if you want to use the javascript data format. 这样,如果您想使用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>";
}

result : 结果:

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

I hope this helps. 我希望这有帮助。 If you have any question, I will answer in the comments. 如有任何疑问,我会在评论中回答。


  1. To add an initial sentence you'll need to add a section before the 要添加初始句子,您需要在

     +"<TABLE border='1' table-layout:auto;>" 

ex : 例如:

+"<P> This is a new paragraphe before the table</P>"
+"<BR> This is a new line before the table</BR>"
  • I'm not an expert HTML write, so for more advance formating, you can always search for html formating. 我不是HTML专家,因此对于更高级的格式设置,您始终可以搜索html格式设置。

  1. To put somthing in bold you can simply add "strong" before and after the section you want to bold. 要以粗体显示内容,您只需在要加粗的部分之前和之后添加“ strong”即可。

ex: 例如:

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

  1. If you want to remove or modify the border settings of the table you can always increase or lower the number next to "border=1". 如果要删除或修改表格的边框设置,可以随时增加或减少“ border = 1”旁边的数字。

I have modified the above code with the modification you specified. 我已经用您指定的修改方式修改了上面的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM