Excel/VB/HTML 格式表格单元格基于数值。

huangapple go评论64阅读模式
英文:

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

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 on 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 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 "

", it was not appearing. Substituted it for the word "cell".

英文:

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.

&quot;&lt;table style=font-size:11pt;font-family:Arial; width=auto&gt;&quot; &amp; _
            &quot;&lt;tr &gt;&quot; &amp; _
                &quot;&lt;td&gt;Title&lt;/td&gt;&quot; &amp; _
                &quot;&lt;td align=right&gt;&quot; &amp; FormatPercent((&quot;I16&quot;), 0) &amp; &quot;&lt;/td&gt; &quot; &amp; _
                &quot;&lt;td align=right&gt;&quot; &amp; FormatPercent((&quot;J16&quot;), 0) &amp; &quot;&lt;/td&gt; &quot; &amp; _
            &quot;&lt;/tr&gt;&quot; &amp; _
&quot;&lt;/table&gt;&quot;

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 &lt;td&gt; value &gt;= 0.98 then
    color=green
elseif &lt;td&gt; value &gt; 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:

&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-js --&gt;

    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";
    });

&lt;!-- language: lang-html --&gt;

    &lt;table style=font-size:11pt;font-family:Arial; width=auto&gt;
                &lt;tbody&gt;
                &lt;tr id="row"&gt;
                    &lt;td&gt;0.95&lt;/td&gt;
                    &lt;td&gt;0.99&lt;/td&gt;
                    &lt;td&gt;0.67&lt;/td&gt;
                    &lt;td&gt;0.48&lt;/td&gt;
                    &lt;td&gt;0.93&lt;/td&gt;
                &lt;/tr&gt;
                &lt;/tbody&gt;
    &lt;/table&gt;

&lt;!-- end snippet --&gt;
英文:

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(&#39;td&#39;); // Get cells

Array.from(cells).forEach(cell =&gt; { // For each cell
  let value = Number.parseFloat(cell.innerHTML); // Parse the value of the cell
  if (value &gt;= 0.98) { // Check the value
    cell.style.backgroundColor=&quot;green&quot;; // Modify the cell&#39;s CSS
  } else if (value &gt;= 0.9) {
    cell.style.backgroundColor=&quot;blue&quot;;
  } else cell.style.backgroundColor=&quot;red&quot;;
});

<!-- language: lang-html -->

&lt;table style=font-size:11pt;font-family:Arial; width=auto&gt;
            &lt;tbody&gt;
            &lt;tr id=&quot;row&quot;&gt;
                &lt;td&gt;0.95&lt;/td&gt;
                &lt;td&gt;0.99&lt;/td&gt;
                &lt;td&gt;0.67&lt;/td&gt;
                &lt;td&gt;0.48&lt;/td&gt;
                &lt;td&gt;0.93&lt;/td&gt;
            &lt;/tr&gt;
            &lt;/tbody&gt;
&lt;/table&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月4日 06:47:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75924263.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定