【问题标题】:How to send a table in an email in Google Scripts?如何在 Google Scripts 的电子邮件中发送表格?
【发布时间】:2019-11-08 13:51:17
【问题描述】:

我一直在尝试创建一个脚本,将单元格数组作为表格发送(保持格式),但是经过数小时和大约 30 种测试代码的变体后,我一直在碰壁...

到目前为止,我要么: 1. 收到一封空邮件 2. 收到一封包含我所有数据但没有格式的电子邮件(所有数字一个接一个而不是一个表格) 3. 在电子邮件中以 HTML 代码编写的表格,而不是表格本身。 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);
}

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

【问题讨论】:

  • 欢迎来到 StackOverflow!请阅读有关“how to ask a good question”和“how to give a reproducible example”的信息。这将使其他人更容易帮助您。
  • 我找到了一种更好的方法,但是我仍然有格式问题。代码现在如下所示,但我找不到如何制作单元格的背景,并且数字的格式取决于工作表中每个单元格的格式:

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


【解决方案1】:

任何尝试使用 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>"
  }

【讨论】:

    【解决方案2】:

    不要将 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 和 htmlbody here 的更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多