英文:
Excel/VB/HTML Format Table Cells based on value
问题
I am trying to format the background color of a td embedded in my VB code based off the value of the cell from Excel. My code's purpose is to send an email with an HTML table, and I want to be able to highlight cells in the HTML table that are certain values.
This is a basic look at the table I'm creating within VB, minus the bgcolor code.
"<table style=font-size:11pt;font-family:Arial; width=auto>" & _
"<tr >" & _
"<td>Title</td>" & _
"<td align=right>" & FormatPercent("I16", 0) & "</td> " & _
"<td align=right>" & FormatPercent("J16", 0) & "</td> " & _
"</tr>" & _
"</table>"
What I'm trying to do is format a
I've done this manually before, creating a string for every single cell; however, I feel this is extremely inefficient and there is probably a better way to do this.
I am not sure if there is a way I can insert an Excel value into my table and keep the formatting of the cell and use a format condition within Excel to determine colors. I've tried looking this up but could not find anything to define this.
If there is a way to do something like:
If <td> value >= 0.98 then
color = green
elseif <td> value > 0.95 then
color = blue
else
color = red
End if
Any suggestions would be appreciated.
*Edit 1: Didn't realize when I typed "
英文:
I am trying to format the background color of a td imbedded in my VB code based off the value of the cell from Excel. My codes purpose is to send an email with a html table, and I want to be able to highlight cells in the HTML table that are certain values.
This is a basic look at the table I'm creating within VB, minus the bgcolor code.
"<table style=font-size:11pt;font-family:Arial; width=auto>" & _
"<tr >" & _
"<td>Title</td>" & _
"<td align=right>" & FormatPercent(("I16"), 0) & "</td> " & _
"<td align=right>" & FormatPercent(("J16"), 0) & "</td> " & _
"</tr>" & _
"</table>"
What I'm trying to do is format a <td> to give it a bgcolor of either green >=98%, blue >=95% & <98% else red. I have roughly 24 different cells and I want a dynamic color that changes itself based off the content of the cell.
I've done this manually before, creating a string for every single cell; however, I feel this is extremely inefficient and there is probably a better way to do this.
I an not sure if there is a way I can insert an excel value into my table and keep the formatting of the cell and use a format condition within excel to determine colors. I've tried looking this up but could not find anything to define this.
If there is a way to do something like:
If <td> value >= 0.98 then
color=green
elseif <td> value > 0.95 then
color = blue
else
color = red
End if
Any suggestions would be appreciated.
*Edit 1: Didn't realise when I typed "< td>", it was not appearing. Substituted it for the word "cell".
答案1
得分: 1
你无法在HTML中这样做,但是你可以使用JavaScript动态修改你的HTML DOM:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let cells = document.getElementsByTagName('td'); // 获取单元格
Array.from(cells).forEach(cell => { // 对于每个单元格
let value = Number.parseFloat(cell.innerHTML); // 解析单元格的值
if (value >= 0.98) { // 检查值
cell.style.backgroundColor="green"; // 修改单元格的CSS
} else if (value >= 0.9) {
cell.style.backgroundColor="blue";
} else cell.style.backgroundColor="red";
});
<!-- language: lang-html -->
<table style=font-size:11pt;font-family:Arial; width=auto>
<tbody>
<tr id="row">
<td>0.95</td>
<td>0.99</td>
<td>0.67</td>
<td>0.48</td>
<td>0.93</td>
</tr>
</tbody>
</table>
<!-- end snippet -->
英文:
You can't do that in HTML, however you can use JavaScript to dynamically modify your HTML DOM:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let cells = document.getElementsByTagName('td'); // Get cells
Array.from(cells).forEach(cell => { // For each cell
let value = Number.parseFloat(cell.innerHTML); // Parse the value of the cell
if (value >= 0.98) { // Check the value
cell.style.backgroundColor="green"; // Modify the cell's CSS
} else if (value >= 0.9) {
cell.style.backgroundColor="blue";
} else cell.style.backgroundColor="red";
});
<!-- language: lang-html -->
<table style=font-size:11pt;font-family:Arial; width=auto>
<tbody>
<tr id="row">
<td>0.95</td>
<td>0.99</td>
<td>0.67</td>
<td>0.48</td>
<td>0.93</td>
</tr>
</tbody>
</table>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
- css
- excel
- html
- vba
评论