title: Google Sheets Script: email with a HTML Table that only has a set of
body: <p>In Google Spreadsheet I want to be able to send a email with a HTML Table within the Email. I have accomplished this below and would like to now extend the functionality more. Now I am trying to understand / find a way on how to filter the getRange based on a variable. </p>
  
  <p>Example:
  If Column C = Todays Date I want to return all rows with Todays date into the HTML Table in the Email. I have been playing with the GetRange but when adjusting it something other then a Range it breaks. How should I go about doing this?</p>
  
  <pre><code>function sendEmail() {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getActiveSheet();
    var range = sheet.getDataRange();
    var recipient = 'email@gmail.com'
    var subject = 'Subject'
    var date = Utilities.formatDate(new Date(), "GMT+1", "dd/MM/yyyy")
    var schedRange = sheet.getRange("A1:L21"); // Trying to understand 
    //var schedRange = sheet.getRange(Col == 3 &amp;&amp; compare == date);
  
    // Put Name &amp; Date into email first.
    // 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;'+ 'Deployment Table Header ' +'&lt;/H1&gt;';
    body += '&lt;H2&gt;'
    body += getHtmlTable(schedRange);
    body += '&lt;/div&gt;';
    debugger;
  
    recipient = 'email@gmail.com';  // For debugging, send only to self
    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();
  
    // Read table contents
    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));
    }
  
    // Build HTML Table, with inline styling for each cell
    var tableFormat = 'style="font-size: 10px; border:1px solid black;border-collapse:collapse;text-align:center" border = 1 cellpadding = 1';
    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(),
                       'M/d');
        }
        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>