如何在Google Apps Script邮件模板中仅将'DONE'文本设为绿色
问题描述
无开发经验,现有一个搭载Google Apps Script(基于JavaScript)的Google表格,自动发送带表格邮件的功能正常。需要修改HTML模板,仅将表格中内容为'DONE'的文本改为绿色,此前修改全局CSS的color属性导致所有文本变为绿色,寻求正确解决方案。
解决方案
无需修改code.gs代码,仅需在emailTemplate.html的表格单元格渲染逻辑中添加条件判断,为内容是'DONE'的单元格单独设置文本颜色:
修改emailTemplate.html中<tbody>区域的单元格渲染代码:
原代码片段:
<? row.forEach(cell => {?> <td style="text-align:center"><?= cell ?></td> <?})?>
修改为:
<? row.forEach(cell => {?> <td style="text-align:center; <?= cell === 'DONE' ? 'color: green;' : '' ?>"><?= cell ?></td> <?})?>
逻辑说明
这段代码用JavaScript三元运算符做判断:
- 当单元格内容严格等于
'DONE'时,给<td>标签添加color: green;的内联样式,让文本变绿 - 非'DONE'内容则保持原有黑色文本样式,不会被全局修改影响
修改后的完整emailTemplate.html
<!DOCTYPE html> <html> <head> <base target="_top"> <style> * { font-family: Verdana sans-serif; font-size: 12pt; } table { border-collapse: collapse; } table, th, td { border: 1px solid black; font-size:15px; padding: 8px; color: black; } tr:hover {background-color: #ddd;} th, thead { background-color: <?= bgColor ?>; } .table-container, .footer { margin-top: 10px; } </style> </head> <body> <!-- Body --> <div> <pre><?= body ?></pre> </div> <!-- Table --> <div class="table-container"> <table> <thead> <? headers.forEach(cell => {?> <th><?= cell ?></th> <?})?> </thead> <tbody > <? rows.forEach(row => {?> <tr> <? row.forEach(cell => {?> <td style="text-align:center; <?= cell === 'DONE' ? 'color: green;' : '' ?>"><?= cell ?></td> <?})?> </tr> <?})?> </tbody> </table> </div> <!-- Footer --> <div class="footer"> <pre><?= footer ?></pre> </div> </body> </html>
内容的提问来源于stack exchange,提问作者momo li
相关产品推荐
相关产品推荐

