我正在使用 Apps 脚本尝试从表格创建自动报告。该电子邮件将拉入一个简单的表格,其中包含一周中工作日的标题行,然后包含“x”天缺席的姓名列表的第二行。此列表包含从其他公式浓缩的信息:
=TEXTJOIN(CHAR(10), TRUE, C8:C15)
。
我已尝试遵循本指南并适应我的需求:https://www.youtube.com/watch?v=r-g73gCpS4o
我快到了,可以在电子邮件中获取表格,但是
CHAR(10)
在过程中丢失并破坏了表格的格式。
我有以下文件。我认为问题/解决方案必须位于我的 dataCollector.gs 文件中:
代码.gs
function myFunction() {
// connect to html template
var html = HtmlService.createTemplateFromFile("email");
// make data collection available to html files
html.dataCollect = dataCollector(1800699589);
// connect to the template so we can send it in email
var emailConnect = html.evaluate().getContent();
// send email
GmailApp.sendEmail(
["EXAMPLE EMAIL 1","EXAMPLE EMAIL 2"],
"Planned Absence - Next Week",
"Please see .....",
{htmlBody: emailConnect}
)
}
dataCollector.gs
function dataCollector(sheet_id){
// access the workbook
var wb = SpreadsheetApp.getActiveSpreadsheet();
// access all sheets in workbook
var sheets = wb.getSheets();
// loop through sheets
for(i in sheets){
// conditional to evaluate the sheet ids
if(sheets[i].getSheetId() == sheet_id){
//identify the sheet name
var sheetName = sheets[i].getSheetName();
}
}
// access the sheet
var dataSheet = wb.getSheetByName(sheetName);
// access the data range
var dataRange = dataSheet.getRange(1,1,3,5).getDisplayValues()
// return statement
return dataRange;
}
email.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<?!= HtmlService.createHtmlOutputFromFile('css').getContent(); ?>
</head>
<body>
<p>Hello, this is email</p>
<table>
<thead>
<tr>
<th><?= dataCollect[1][0] ?></th>
<th><?= dataCollect[1][1] ?></th>
<th><?= dataCollect[1][2] ?></th>
<th><?= dataCollect[1][3] ?></th>
<th><?= dataCollect[1][4] ?></th>
</tr>
</thead>
<tbody>
<tr>
<td><?= dataCollect[2][0] ?></td>
<td><?= dataCollect[2][1] ?></td>
<td><?= dataCollect[2][2] ?></td>
<td><?= dataCollect[2][3] ?></td>
<td><?= dataCollect[2][4] ?></td>
</tr>
</tbody>
</table>
</body>
</html>
我尝试在我的变量 dataRange 上使用
.replace(/n, <br>)
。除非转换为字符串,否则替换不起作用。
我尝试将其转换为字符串,然后使用 .replace,但最终丢失了整个表格,只在我的电子邮件中显示了两个字母。
帮助理解的表格页面截图:
邮件结果截图:
使用
<br>
是正确的,我尝试使用这行代码 .replace("\\n", "<br />")
来获取结果,似乎有错误。
而是将
<br>
放在表格数据 <td>
之后。
来自:
<td><?= dataCollect[2][0] ?></td>
<td><?= dataCollect[2][1] ?></td>
<td><?= dataCollect[2][2] ?></td>
<td><?= dataCollect[2][3] ?></td>
<td><?= dataCollect[2][4] ?></td>
出结果前:
致:
<td><?= dataCollect[2][0] ?></td><br>
<td><?= dataCollect[2][1] ?></td><br>
<td><?= dataCollect[2][2] ?></td><br>
<td><?= dataCollect[2][3] ?></td><br>
<td><?= dataCollect[2][4] ?></td><br>
效果后:
参考: