【问题标题】:Google App Script HTML table stylingGoogle 应用脚本 HTML 表格样式
【发布时间】:2018-07-18 08:26:45
【问题描述】:

我在 GAS 中创建了一个函数,它从谷歌表格中获取数据并根据特定的人发送单独的电子邮件;然后该函数循环到下一个人并继续遍历数据。

我现在遇到的问题是格式化我通过循环数据创建的 HTML 表。当我运行它时,数据以表格格式显示,但我无法添加边框、颜色等。我目前在 TABLEFORMAT 变量下有样式,但我也尝试将样式标签放在表格中,th,td,tr 标签,但它似乎忽略了它。但是,我将输出放入一个 HTML 模拟器,它会按照我想要的方式输出。

有谁知道为什么这不适用于 gs 文件?另外,我试图让 cashGoalPosition 采用美元格式。在此先感谢您的帮助;我一直在绞尽脑汁,似乎无法弄清楚。

function Auto_Email(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var shData = ss.getSheetByName("E-mail Data");
  var shEmail = ss.getSheetByName("E-mail List");
  var dataRange = shData.getDataRange(); // Fetch values for each row in the Range.
  var emailRange = shEmail.getDataRange(); //fetch values for email list
  var data = dataRange.getValues();
  var nameData = emailRange.getValues();
  var lastCol = dataRange.getLastColumn();

   for (var i = 1; i < nameData.length; i++) {
      var rows = nameData[i];
      var emailAddress = rows[2];//position of email header -1
      var fullName = rows[1]; //position of name header -1
      var firstName = rows[3]// the first name only
      var cashGoalPosition = rows[0].toString(); //position of billing goal -1
      var subject = firstName+"'s Cash Goal";
      var htmltable = '';
      var htmlmessage = "";
     var TABLEFORMAT = '<!DOCTYPE html><html><style>table {border-collapse:collapse} table,td,th{border:1px solid black}</style>'
      var pre_html = '<p>Dear '+firstName+',</p><p>Your new balance is <b>'+cashGoalPosition+'</b>. Below is a list of the invoices past due or due in the current month.</p><table style="width:100.0%"><tbody>'
      var post_html = '<p>Regards,<br>John</p></html>'

      for (row = 0; row<data.length; row++)
      { 
        for (col = 0 ;col<data[row].length; col++){
          if (row == 0 && col == 0) {htmltable += '<tr><th>' + data[row][col] + '</th>';}
          else
            if (row == 0 && col == lastCol - 1) {htmltable+= '<th>' + data[row][col] + '</th></tr>';}
          else
            if (row == 0) {htmltable+= '<th>' + data[row][col] + '</th>';}
          else
            if (data[row][0] == fullName && col == 0) {htmltable += '<tr><td>' + data[row][col] + '</td>';}
          else
            if (data[row][0] == fullName && col == lastCol -1) {htmltable += '<td>' + data[row][col] + '</td></tr>';}
          else
            if (data[row][0] == fullName) {htmltable += "<td>" + data[row][col] + "</td>";}
}

      }

     htmltable += '</tbody></table>';
     htmlmessage = TABLEFORMAT + pre_html + htmltable + post_html;


     Logger.log(htmlmessage)
MailApp.sendEmail(Session.getActiveUser().getEmail(), subject,'' ,{htmlBody: htmlmessage})
      }(i);
   }

【问题讨论】:

  • “但它似乎忽略了它”是什么意思?您使用什么电子邮件客户端来阅读发送的电子邮件?
  • 我通过 gmail 发送它。我所说的“忽略它”的意思是桌子很简单,不符合我设置的样式。然而,田池的建议奏效了。我一定是做错了什么,因为我尝试了他建议的方式,但无法让它发挥作用。一定有一点点掉了。感谢您输入 Tanaike。关于将数字设置为货币的简单方法有什么想法吗?

标签: javascript html email google-apps-script html-email


【解决方案1】:

&lt;style&gt; 似乎不适用于 Gmail 的 htmlBody。我认为它可能会被删除。所以请使用带有样式的table标签。

在您的情况下,&lt;style&gt;table {border-collapse:collapse} table,td,th{border:1px solid black}&lt;/style&gt;&lt;table style="width:100.0%"&gt; 可以转换为 &lt;table style="width: 100%;border-collapse: collapse;border: 1px solid black"&gt;

修改后的脚本如下。

发件人:

var TABLEFORMAT = '<!DOCTYPE html><html><style>table {border-collapse:collapse} table,td,th{border:1px solid black}</style>'
var pre_html = '<p>Dear '+firstName+',</p><p>Your new balance is <b>'+cashGoalPosition+'</b>. Below is a list of the invoices past due or due in the current month.</p><table style="width:100.0%"><tbody>'

收件人:

var TABLEFORMAT = '<!DOCTYPE html><html>'
var pre_html = '<p>Dear '+firstName+',</p><p>Your new balance is <b>'+cashGoalPosition+'</b>. Below is a list of the invoices past due or due in the current month.</p><table style="width: 100%;border-collapse: collapse;border: 1px solid black"><tbody>'

如果我误解了你的问题,我很抱歉。

【讨论】:

    猜你喜欢
    • 2021-12-03
    • 1970-01-01
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多