如何在 Google 脚本中发送电子邮件中的 table?

How to send a table in an email in Google Scripts?

我一直在尝试创建一个脚本,将元胞数组作为 table(保持格式)发送,但是在经过数小时和大约 30 种代码变体测试后,我继续 运行墙...

到目前为止,我要么: 1. 收到一封空邮件 2. 收到一封包含我所有数据但没有格式的电子邮件(所有数字一个接一个而不是 table) 3. table 在电子邮件中写成 HTML 代码而不是 table 本身。 4. 邮件内容为[Object]的邮件

我使用此代码得到结果 2:

  var conv = SheetConverter.init(ss.getSpreadsheetTimeZone(),
                                 ss.getSpreadsheetLocale());
var html = conv.convertRange2html(dataRange);

最后我的最后一次尝试再次失败但看起来很有希望是下面的。我认为这个问题目前的问题是函数 sendEmail 没有调用它上面的 getHtmlTable:

function getHtmlTable(range){
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  startRow = "6";
  startCol = "B";
  lastRow = "18";
  lastCol = "E";

  // Read table contents
  var dataRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange("B6:E18");
  var data = dataRange.getValues();

  // Get css style attributes from range
  var fontColors = dataRange.getFontColors();
  var backgrounds = dataRange.getBackgrounds();
  var fontFamilies = dataRange.getFontFamilies();
  var fontSizes = dataRange.getFontSizes();
  var fontLines = dataRange.getFontLines();
  var fontWeights = dataRange.getFontWeights();
  var horizontalAlignments = dataRange.getHorizontalAlignments();
  var verticalAlignments = dataRange.getVerticalAlignments();

  // Get column widths in pixels
  var colWidths = ["10"];

  // Get Row heights in pixels
  var rowHeights = ["10"];

  // Future consideration...
  var numberFormats = dataRange.getNumberFormats();

  // Build HTML Table, with inline styling for each cell
  var tableFormat = 'style="border:1px solid black;border-collapse:collapse;text-align:center" border = 1 cellpadding = 5';
  var html = ['<table '+tableFormat+'>'];
  // Column widths appear outside of table rows
  for (col=0;col<colWidths.length;col++) {
    html.push('<col width="'+colWidths[col]+'">')
  }
  // Populate rows
  for (row=0;row<data.length;row++) {
    html.push('<tr height="'+rowHeights[row]+'">');
    for (col=0;col<data[row].length;col++) {
      // Get formatted data
      var cellText = data[row][col];
      if (cellText instanceof Date) {
        cellText = Utilities.formatDate(
                     cellText,
                     ss.getSpreadsheetTimeZone(),
          'EEE, MMM dd YY'); ///          'EEE, MMM dd - h:mm a');
      }
      var style = 'style="'
                + 'color: ' + fontColors[row][col]+'; '
                + 'font-family: ' + fontFamilies[row][col]+'; '
                + 'font-size: ' + fontSizes[row][col]+'; '
                + 'font-weight: ' + fontWeights[row][col]+'; '
                + 'background-color: ' + backgrounds[row][col]+'; '
                + 'text-align: ' + horizontalAlignments[row][col]+'; '
                + 'vertical-align: ' + verticalAlignments[row][col]+'; '
                +'"';
      html.push('<td ' + style + '>'
                +cellText
                +'</td>');
    }
    html.push('</tr>');
  }
  html.push('</table>');

  return html.join('');
}


  function sendEmail() {
  // Fetch email address
var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange("B1");
var emailAddress = emailRange.getValues(); 
  // Send Email.

var message = getHtmlTable;
var subject = 'Test 34';
MailApp.sendEmail(emailAddress, subject, message);
}

如果有人能提供帮助,我们将不胜感激 :) 这让我抓狂

不是将 html 代码推入数组,而是将新字符串加入同一个变量中。另外,使用 htmlbody 作为您的电子邮件正文。注意 for 循环中的变量 table

function createTable(data){ 
  var cells = [];
  //This would be the header of the table
  var table = "<html><body><br><table border=1><tr><th>Column A</th><th>Column B</th><th>Column C</th><th>Column D</th><th>Column E</tr></br>";

  //the body of the table is build in 2D (two foor loops)
  for (var i = 0; i < data.length; i++){
      cells = data[i]; //puts each cell in an array position
      table = table + "<tr>";

      for (var u = 0; u < cells.length; u++){
          table = table + "<td>"+ cells[u] +"</td>";
      }
  table = table + "</tr>"
  }

  table=table+"</table></body></html>";
  


  //Send the email:
 MailApp.sendEmail({
    to: "example@mail.com", 
    subject: "Example",
    htmlBody: table}); 

}

有关 MailApp 和 html正文的更多信息 here

任何试图使用 Jescanellas 答案的人,请注意需要进行一个小的更正:

  //the body of the table is build in 2D (two for loops)
  for (var i = 0; i < data.length; i++){
    cells = data[i]; //puts each cell in an array position
    table = table + "<tr>";

      for (var u = 0; u < cells.length; u++){
          table = table + "<td>"+ cells[u] +"</td>";
      }
    table = table + "</tr>"
  }