【问题标题】:Google Sheets Script: email with a HTML Table that only has a set ofGoogle 表格脚本:带有 HTML 表格的电子邮件,该表格只有一组
【发布时间】:2018-10-06 05:55:11
【问题描述】:

在 Google 电子表格中,我希望能够在电子邮件中发送带有 HTML 表格的电子邮件。我已经在下面完成了这项工作,现在想进一步扩展功能。现在我试图了解/找到一种方法来根据变量过滤 getRange。

示例: 如果 C 列 = 今天日期,我想将所有具有今天日期的行返回到电子邮件中的 HTML 表中。我一直在玩 GetRange,但是当调整它时,它会破坏 Range 以外的东西。我该怎么做呢?

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 && compare == date);

  // Put Name & Date into email first.
  // We only want the schedule within borders, so
  // these are handled separately.
  var body = '<div style="text-align:center;display: inline-block;font-family: arial,sans,sans-serif">'
  body += '<H1>'+ 'Deployment Table Header ' +'</H1>';
  body += '<H2>'
  body += getHtmlTable(schedRange);
  body += '</div>';
  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<=lastCol; col++) { 
    colWidths.push(sheet.getColumnWidth(col));
  }

  // Get Row heights in pixels
  var rowHeights = [];
  for (var row=startRow; row<=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 = ['<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(),
                     '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('<td ' + style + '>'
                +cellText
                +'</td>');
    }
    html.push('</tr>');
  }
  html.push('</table>');

  return html.join('');
}

【问题讨论】:

  • 为什么不简单地在函数中添加条件,在添加行的循环中,以便在不满足条件时跳过该行

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


【解决方案1】:

如 cmets 中所述,您可以在构建 HTML 表时过滤掉任何不需要的行:

function getHtmlTable(range){

// ... your code...
  var today = new Date();
  for (row=0;row<data.length;row++) {
    var row_date = data[row][2]; // Assuming date is in 3rd column
    if(sameDay(new Date(row_date), today){ // See note
      html.push('<tr height="'+rowHeights[row]+'">');
      for (col=0;col<data[row].length;col++) {

        // ... your code ...
      }
    }
  }
}

function sameDay(d1, d2) {
  return d1.getFullYear() === d2.getFullYear() &&
    d1.getMonth() === d2.getMonth() &&
    d1.getDate() === d2.getDate();
}

几点说明:

  1. 只有在Date constructor 的日期格式正确时,才能调用new Date(row_date)。如果不是,则需要在创建Date 对象之前parse the date
  2. sameDay 函数借用自 this answer

【讨论】:

  • 格式看起来正确 - 尝试添加您的内容,但我收到错误 t"TypeError: Cannot read property "length" from undefined. (line 49, file "Code")"function getHtmlTable(range){ for (row=0;row
  • data 是您在 getHtmlTable 函数中定义的变量。该错误似乎表明 data 未定义。确保在定义 var data = range.getValues(); 后传入有效范围
猜你喜欢
  • 2015-03-10
  • 1970-01-01
  • 2012-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-26
  • 1970-01-01
相关资源
最近更新 更多