title: How to send a table in an email in Google Scripts?
body: <p>I've been trying to create a script sending a cell array as a table (keeping formatting) however after hours and circa 30 variations of the code tested I keep on running into a wall...</p>
  
  <p>So far I either:
  1. Get an empty email
  2. Get an email with all my data but no formatting (all numbers one after the other instead of a table)
  3. The table written as HTML code in the email instead of the table itself.
  4. An email with [Object] written as the message</p>
  
  <p>I get result 2 by using this code:</p>
  
  <pre><code>  var conv = SheetConverter.init(ss.getSpreadsheetTimeZone(),
                                   ss.getSpreadsheetLocale());
  var html = conv.convertRange2html(dataRange);
  </code></pre>
  
  <p>And finally my last try which was again unsuccessful but seemed promising was the below. My current issue with this one I believe is that the function sendEmail isn't calling getHtmlTable which is above it:</p>
  
  <pre><code>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 = ['&lt;table '+tableFormat+'&gt;'];
    // Column widths appear outside of table rows
    for (col=0;col&lt;colWidths.length;col++) {
      html.push('&lt;col width="'+colWidths[col]+'"&gt;')
    }
    // Populate rows
    for (row=0;row&lt;data.length;row++) {
      html.push('&lt;tr height="'+rowHeights[row]+'"&gt;');
      for (col=0;col&lt;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('&lt;td ' + style + '&gt;'
                  +cellText
                  +'&lt;/td&gt;');
      }
      html.push('&lt;/tr&gt;');
    }
    html.push('&lt;/table&gt;');
  
    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);
  }
  </code></pre>
  
  <p>If anyone can help it would be extremely appreciated :) it's driving me nuts</p>