javascript - 使用 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>
</td></tr><tr><td>PM</td><td>
</td><td>FUNCIONAL </td><td>
</td><td>TIPO</td><td>
</td><td>PROYECTO</td><td>
</td><td>E4E</td><td>
</td><td>SAP</td><td>
</td><td><span zeum4c16="PR_1_0" data-ddnwab="PR_1_0" aria-invalid="spelling" class="LI ng">DESCRIPCION</span></td><td>
</td><td>UN</td><td>
</td><td>CANTIDAD</td><td>
</td><td>OBSERVACIONES</td><td>
</td><td>WBE</td><td>
</td><td>SUBESTACIÓN DESTINO</td><td>
</td><td>ESTATUS</td><td>
</td><td>PROYECTO WBE</td><td>
</td><td>CANTIDAD EXISTENTE MRO 43897</td><td>
</td><td>BARCODE</td><td>
</td><td>CORREO FUNCIONAL</td><td>
</td><td>ID PEDIDO</td><td>
</td></tr><tr><td>
表格在每列和每行之间都有所有这些空间。
附言。我不想使用 convertRange2html() 因为我想显示 2 个表格并控制显示方式,第二个是从第一个转换,对 gsheets 的格式不感兴趣。
谢谢你能给我的所有建议。
解决方案
我找到的解决方案是将 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>
意思是,代码中没有“空格”,所以通过这种方式,表格可以正确构建
推荐阅读
- android - 不能再用 Mockito 模拟最后一堂课了
- python - 使用 Pandas 根据 Condition 将两行数据合二为一
- sql - 如何提高这个 SQL 的速度?
- python - 使用 pandas 将相关列重新定位到表的末尾
- vue.js - 如何将道具传递给vue中的子组件
- r - 是否有用于制作适合报告的描述性表格的 R 包和功能?
- reactjs - 反应原生 | ReferenceError: defaultStyles 未定义
- azure - Ansible 循环使用图形 api 在 Azure AD 组中添加成员
- flutter - 如何在 VS Code 中调试颤振 build_runner 构建?
- python - Python 3 子进程崩溃