google-apps-script - 设置表格格式并通过电子邮件发送

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

我正在尝试发送一封包含表格的电子邮件,我已设法创建表格,但无法弄清楚如何复制格式(包括背景颜色和数字格式),因为某些单元格包含货币以及百分比和普通数字。

由于数据采用颜色编码,因此将这些数据复制到电子邮件中非常重要。

这是我的代码,如果有人能指出我正确的方向,那将非常有帮助,这是迄今为止我的代码......

function createTable(data){ 
    var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  
  //To be looped in the future, 10 in range to be replaced with k
  var dataRange = sheet.getRange(10,4,1,9);
  var data = dataRange.getValues();

  //Table Header
  var table = "<html><body><br><table border=1><tr><th>Compliance Score</th><th>STSOR Value</th><th>STSOR %</th><th>ZZ lines</th><th>ZZ%</th><th>PI lines Counted</th><th>PI %</th><th>NRDS Value</th><th>NRDS %</tr></br>";

  //table Body
 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>"
    

  //Send the email:
 MailApp.sendEmail({
    to: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d9bca1b8b4a9b5bc99beb4b8b0b5f7bab6b4" rel="noreferrer noopener nofollow">[email protected]</a>", 
    subject: "Example",
    htmlBody: table}); 
     }
 }

最佳答案

我相信您的目标如下。

  • 您想要创建一个包含单元格背景颜色和数字格式的 HTML 表格。

既然如此,下面的修改如何?

修改后的脚本:

function createTable() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  var dataRange = sheet.getRange(10, 4, 1, 9);
  var backgrounds = dataRange.getBackgrounds(); // Added
  var data = dataRange.getDisplayValues(); // Modified
  var table = "<html><body><br><table border=1><tr><th>Compliance Score</th><th>STSOR Value</th><th>STSOR %</th><th>ZZ lines</th><th>ZZ%</th><th>PI lines Counted</th><th>PI %</th><th>NRDS Value</th><th>NRDS %</tr></br>";
  for (var i = 0; i < data.length; i++) {
    cells = data[i];
    table = table + "<tr>";
    for (var u = 0; u < cells.length; u++) {
      table = table + `<td style="background-color:${backgrounds[i][u]}">` + cells[u] + "</td>"; // Modified
    }
    table = table + "</tr>"
    MailApp.sendEmail({
      to: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="87e2ffe6eaf7ebe2c7e0eae6eeeba9e4e8ea" rel="noreferrer noopener nofollow">[email protected]</a>",
      subject: "Example",
      htmlBody: table
    });
  }
}

引用:

关于google-apps-script - 设置表格格式并通过电子邮件发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71171305/

相关文章:

ruby-on-rails - 使用 Rails 3 在 heroku 上使用 Gmail

PHP 到 Google WebApp 返回 "<H1>Moved Temporarily</H1>The document has moved"

xml - eBay API 将 XML 解析为 Google 脚本时出现问题

javascript - 如何在 promise 中包装 google oauth 授权回调?

arrays - Google Sheets Arrayformula 计算速度非常慢

javascript - 自定义函数对于某些值莫名其妙地停止工作

google-apps-script - 自定义函数不适用于 ArrayFormula

selenium - Webdriver 在 Gmail 中打开邮件

javascript - 调用函数结果返回 "undefined"

user-interface - 如何强制 gmail 收件箱重新加载/刷新?