Table HTML AppScript 格式电子邮件中的宽度

Table width in HTML email formatted in AppScript

我有一个 Google AppScript,它生成一个用于格式化电子邮件的 HTML 代码。我会输出一个 table 列,宽度相同,在 PC 和移动设备上 phone table 以不同的方式显示。

其他代码 ..

body += "<table border=2><tbody><tr>";
  //Inserisco l'header del messaggio
  if (report_headline[0].length > 0) body += CreateHTMLTableRow(report_headline, is_header = true);
  //costruisco la tabella con i dati del report dei messaggi
  if (report_attachement[0].length > 0) body += CreateHTMLTableRow(report_attachement, is_header = false);
  //costruisco la tabella con i dati del report dei pagamenti  
  if (report_payments[0].length > 0) body += CreateHTMLTableRow(report_payments, is_header = false);
  // Close the table tag
  body += "</tbody></table>";

.. 其他代码

//Create an HTML table row from an array
function CreateHTMLTableRow(array,is_header){
  var htmlBody = '';
  var n_row = array.length;
  var n_col = 0;
  var tr_width = 0;
  for (var r = 0; r < n_row; r++) {  
    n_col = array[r].length;
    tr_width = Math.round(100/n_col);
    for (var c = 0; c < n_col; c++) {
      //First row has header <th> tag
      if(is_header){
        if(array[r][c] != ""){
          htmlBody += '<th bgcolor="lightgrey" width="'+tr_width+'"%>'+array[r][c]+"</th>"; 
        }
        else htmlBody += "<th>"+"</th>";        
      }
      //Other rows have the normal <td>
      else {   
        if(array[r][c] != ""){
          htmlBody += '<td width="'+tr_width+'"%>'+array[r][c]+"</td>"; 
        }
        else htmlBody += "<td>"+"</td>";
      }
    }
    htmlBody += "</tr>";
  }  
  return htmlBody;
}  

移动设备上的 gmail 客户端以正确的方式显示 table,而在我的笔记本电脑上,第二行都在同一行。

您只定义一次新行的开始:

body += "<table border=2><tbody><tr>";

同时尝试在

内追加

for (var r = 0; r < n_row; r++)

几行。

问题可以通过如下修改代码来解决:

body += "<table border=2><tbody>";

并在 function CreateHTMLTableRow(array,is_header) 内:

for (var r = 0; r < n_row; r++) {  
   // insert a <tr> tag at the start of every new row
    htmlBody +="<tr>";
    n_col = array[r].length;
    ...
    htmlBody += "</tr>";
  }  
  return htmlBody;