首页 > 解决方案 > 使用 createTemplateFromFile 和 getContent() 格式化表 html 上的 GmailApp htmlBody 错误

问题描述

我正在尝试从 google sheet 发送一封电子邮件,我想用数据在 html 中制作一个简单的表格(没有 CSS 或 FancyStuf),但是当我在 GmailApp 中创建草稿时,这个表格带有很多“& nbsp ;" 我不知道这是从哪里来的,让这张桌子对我的目的完全没用。

我使用的代码是

var AS = SpreadsheetApp.getActiveSheet()
var UltimoRegistro = AS.getRange("B15:B").getValues().filter(String).length;
var datos = AS.getRange(14, 1,UltimoRegistro+1,18).getValues()
var t = HtmlService.createTemplateFromFile('Correo');
t.data = datos;
var output = t.evaluate()
var output2 = output.getContent()
Logger.log(output2)
GmailApp.createDraft('testmail@gmail.com', 'prueba','',{htmlBody:output2} )
//for testing purpose
var ui = SpreadsheetApp.getUi();
SpreadsheetApp.getUi().showModalDialog(output, "titulo")

Correo.html

<!DOCTYPE html>
<html>
  <div>
  <body>
    <table>
      <? for (var i = 0; i < data.length; i++) { ?>
        <tr>
          <? for (var j = 0; j < data[i].length; j++) { ?>
            <td><?= data[i][j] ?></td>
          <? } ?>
        </tr>
      <? } ?>
    </table>
  </body>
  </div>
</html>

我在 Logger.log 上得到的结果是:

<!DOCTYPE html>
<html>
  <div>
  <body>
    <table>
              <tr>
                      <td>PM</td>
                      <td>FUNCIONAL </td>
                      <td>TIPO</td>
                      <td>PROYECTO</td>
                      <td>E4E</td>
                      <td>SAP</td>
                      <td>DESCRIPCION</td>
                      <td>UN</td>
                      <td>CANTIDAD</td>
                      <td>OBSERVACIONES</td>
                      <td>WBE</td>
                      <td>SUBESTACIÓN DESTINO</td>
                      <td>ESTATUS</td>
                      <td>PROYECTO WBE</td>
                      <td>CANTIDAD EXISTENTE MRO 43897</td>
                      <td>BARCODE</td>
                      <td>CORREO FUNCIONAL</td>
                      <td>ID PEDIDO</td>
                  </tr>
              <tr>
 ... and so on..

但是在 gmail 槽控制台 o 检查员上草稿的结果是

<table><tbody><tr><td>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td></tr><tr><td>PM</td><td>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td><td>FUNCIONAL </td><td>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td><td>TIPO</td><td>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td><td>PROYECTO</td><td>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td><td>E4E</td><td>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td><td>SAP</td><td>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td><td><span zeum4c16="PR_1_0" data-ddnwab="PR_1_0" aria-invalid="spelling" class="LI ng">DESCRIPCION</span></td><td>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td><td>UN</td><td>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td><td>CANTIDAD</td><td>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td><td>OBSERVACIONES</td><td>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td><td>WBE</td><td>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td><td>SUBESTACIÓN DESTINO</td><td>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td><td>ESTATUS</td><td>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td><td>PROYECTO WBE</td><td>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td><td>CANTIDAD EXISTENTE MRO 43897</td><td>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td><td>BARCODE</td><td>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td><td>CORREO FUNCIONAL</td><td>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td><td>ID PEDIDO</td><td>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td></tr><tr><td>

表格在每列和每行之间都有所有这些空间。

附言。我不想使用 convertRange2html() 因为我想显示 2 个表格并控制显示方式,第二个是从第一个转换,对 gsheets 的格式不感兴趣。

谢谢你能给我的所有建议。

标签: javascripthtmlgoogle-apps-scriptgoogle-sheetsgmail

解决方案


我找到的解决方案是将 Correo.html 修改为

<!DOCTYPE html>
<html>
<div>
<body>
<table>
<? for (var i = 0; i < data.length; i++) { ?>
<tr>
<? for (var j = 0; j < data[i].length; j++) { ?>
<td><?= data[i][j] ?></td>
<? } ?>
</tr>
<? } ?>
</table>
</body>
</div>
</html>

意思是,代码中没有“空格”,所以通过这种方式,表格可以正确构建


推荐阅读