title: E-mail google spreadsheet range as HTML table preserving values?
body: <p>I have a script I modified and what it does is.</p>
  
  <p>It grabs a spreadsheet range, and then it takes all the values and re-formats them into an HTML table which is sent via e-mail. So far ALL of this works. EXCEPT: The numbers it displays are using a number format with no decimal end. The original spreadsheet, for example, has the formatted value of 10,249.2 which is rounded but the HTML table in the email shows: 10249.243990558198 </p>
  
  <p>This is the code I'm using:</p>
  
  <pre><code>function onOpen() {
    var sheet = SpreadsheetApp.getActiveSpreadsheet();
    var entries = [{
      name : "Send EOD",
      functionName : "sendEmail"
    }];
    sheet.addMenu("Send Email", entries);
  };
  
  function sendEmail() {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getActiveSheet();
   // adding for range
    var lastRow = sheet.getLastRow();
    var lastColumn = sheet.getLastColumn();
   //  var schedRange = sheet.getRange("T10:AC"+lastColumn);
     var schedRange = sheet.getRange("T10:AC28");
   // end of range 
    var range = sheet.getDataRange();
    var formattedDate = Utilities.formatDate(new Date(), "GMT+1", "MM/dd/yyyy")
    var subject = "Daily Volume - " + formattedDate;
  
    // Dynamically get whole sheet based on lastrow and lastcolumn
    // var schedRange = sheet.getRange(1, 1, lastRow, lastColumn);
  
    // We only want the schedule within borders, so
    // these are handled separately.
    var body = '&lt;div style="text-align:center;display: inline-block;font-family: arial,sans,sans-serif"&gt;'
    body += '&lt;H1&gt;'+ "Daily Volume   " + formattedDate  +'&lt;/H1&gt;';
    body += '&lt;H2&gt;'
         + '&lt;/H2&gt;';
    body += getHtmlTable(schedRange);
    body += '&lt;/div&gt;';
    debugger;
    //Logger.log(body);
    recipient = 'my@email.com'+","+"mysecondemail@email.com";
    GmailApp.sendEmail(recipient, subject, "Requires HTML", {htmlBody:body})
  }
  
  /**
   * Return a string containing an HTML table representation
   * of the given range, preserving style settings.
   */
  function getHtmlTable(range){
    var ss = range.getSheet().getParent();
    var sheet = range.getSheet();
    startRow = range.getRow();
    startCol = range.getColumn();
    lastRow = range.getLastRow();
    lastCol = range.getLastColumn();
    // testing
   // var lastRow = sheet.getLastRow();
    // var lastColumn = sheet.getLastColumn();
    // var schedRange = sheet.getRange("T10:AC"+lastColumn);
    //end of testing 
  
    // Read table contents
    var numberFormats = range.getNumberFormats();
    var data = range.getValues();
  
  
    // Get css style attributes from range
    var fontColors = range.getFontColors();
    var backgrounds = range.getBackgrounds();
    var fontFamilies = range.getFontFamilies();
    var fontSizes = range.getFontSizes();
    var fontLines = range.getFontLines();
    var fontWeights = range.getFontWeights();
    var horizontalAlignments = range.getHorizontalAlignments();
    var verticalAlignments = range.getVerticalAlignments();
  
    // Get column widths in pixels
    var colWidths = [];
    for (var col=startCol; col&lt;=lastCol; col++) {
      colWidths.push(sheet.getColumnWidth(col));
    }
    // Get Row heights in pixels
    var rowHeights = [];
    for (var row=startRow; row&lt;=lastRow; row++) {
      rowHeights.push(sheet.getRowHeight(row));
    }
  
    // Future consideration...
    // var numberFormats = schedRange.getNumberFormats();
  
  
  
  
  
    // Build HTML Table, with inline styling for each cell
    var tableFormat = 'style="border:1.5px solid black;border-collapse:collapse;text-align:center" border = 1.5 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(),
                       'MMM/d EEE');
        }
        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('');
  }
  </code></pre>