gpt4 book ai didi

html - 根据 HTML 表格中的条件更改列颜色

转载 作者:太空宇宙 更新时间:2023-11-04 09:59:06 26 4
gpt4 key购买 nike

我正在尝试使用 bash 脚本以 HTML 格式发送电子邮件。

下面是列名

Portfolio
account_number
critical
distinct_file_name_count
total_file_count
Files_validation

AWK 命令用于填充.HTML 文件

awk -F"#" '
BEGIN {
print "<html><head>\
<style>\
body {\
background-color: #FFF;text-align:left;\
}\
table {\
font-family:Tahoma;\
font-size:11;\
border-collapse:collapse;\
border: 4px solid #dddddd;\
}\
th { padding:10px;\
background-color: #94E1F7;\
}\
th { border: 1px solid black;\
}\
td {\
padding:3px;\
border: 2px solid black;\
text-align:right;\
}\
</style>\
</head>\
<body>\
<table>\
<tr>\
<th>Portfolio</th>
<th>account_number</th>
<th>critical</th>
<th>distinct_file_name_count</th>
<th>total_file_count</th>
<th>Files_validation</th>
</tr>"
}
{
print "<tr>"
printf "<td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td>",$1,$2,$3,$4,$5
for (i = 6; i < 7; i++) {
if ($i == "Files_not_received_today") {
printf "<td bgcolor=#FF0000>%s</td>",$i
} else {
printf "<td bgcolor=#32CD32>%s</td>",$i
}
} print "</tr>"
} END {
print "</table></body></html>"
}' ${run_project_tmp_dir}/QC_VALIDATION_REPORT.txt >> ${run_project_tmp_dir}/QC_VALIDATION_REPORT.html

我要改变的条件是

1) When column when `Files_validation` column is having `Files_not_received_today` then check if `critical` column is `YES`. 

If `YES` then Populate `RED` color in both `Files_validation` and `critical`. Else if when `Files_validation` column is having `Files_not_received_today` then check if `critical` column is `NO` then Populate `AMBER` color in both `Files_validation` and `critical`.

2) when `Files_validation` column is having other than `Files_not_received_today` Populate `GREEN` color in `Files_validation`

使用上面的 awk 命令,我可以使用 REDGREEN 颜色填充 Files_validation我的期望但无法达到我所提到的条件的预期结果

虚拟数据

TESTER#394682876#YES#2#23#Files_received_today
BILLER#6637761#NO#0#0#Files_not_received_today
APPROVER#23478#YES#1#1#Files_received_today
BUYER#12398#YES#0#0#Files_not_received_today

当前输出

TESTER#394682876#YES#2#23#Files_received_today  == No bgcolor in critical and bgcolor of files_validation column is Green
BILLER#6637761#NO#0#0#Files_not_received_today == No bgcolor in critical and bgcolor of files_validation column is Red
APPROVER#23478#YES#1#1#Files_received_today == No bgcolor in critical and bgcolor of files_validation column is Green
BUYER#12398#YES#0#0#Files_not_received_today == No bgcolor in critical and bgcolor of files_validation column is Red

预期输出

TESTER#394682876#YES#2#23#Files_received_today  == No bgcolor in critical and bgcolor of files_validation column is Green
BILLER#6637761#NO#0#0#Files_not_received_today == AMBER bgcolor in critical and bgcolor of files_validation column is AMBER
APPROVER#23478#YES#1#1#Files_received_today == No bgcolor in critical and bgcolor of files_validation column is Green
BUYER#12398#YES#0#0#Files_not_received_today == RED bgcolor in critical and bgcolor of files_validation column is Red

最佳答案

等待@nmr 的额外输入,我将假设问题是如何在 awk 中编写必要的代码以允许分配 3 倍不同的颜色:

RED   = FF0000
AMBER = FFFB00 # per https://www.colorhexa.com/ffbf00
GREEN = 32CD32

有很多方法可以做到这一点……case/switch 语句、嵌套的 if/then/else 等。

我将用 4x 命令替换当前的 for/if/then/else/printf block ;前 3 个命令将确定 bgcolor,而第 4 个命令将是一个新的 printf 命令。

注意:BEGINEND block 将保持不变。

确定 bgcolor 的 3x 命令:

# set the default color to GREEN for each new line of input data

bgcolor="#32CD32"

# if field #6 is the string "Files_not_received_today"
# and field #3 = 'YES' then (re)set color to RED

if ( $6 == "Files_not_received_today" && $3 == "YES" ) { bgcolor="#FF0000" }

# if field #6 is the string "Files_not_received_today"
# and field #3 = 'NO' then (re)set color to AMBER

if ( $6 == "Files_not_received_today" && $3 == "NO" ) { bgcolor="#FFBF00" }

新的打印语句:

printf "<td bgcolor="bgcolor">%s</td>",$6

将所有这些放在一起,awk 代码的中间 block 变为:

{
print "<tr>"
printf "<td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td>",$1,$2,$3,$4,$5

bgcolor="#32CD32" # GREEN
if ( $6 == "Files_not_received_today" && $3 == "YES" ) { bgcolor="#FF0000" } # RED
if ( $6 == "Files_not_received_today" && $3 == "NO" ) { bgcolor="#FFBF00" } # AMBER

printf "<td bgcolor="bgcolor">%s</td>",$6
print "</tr>"
}

当针对样本 4x 行数据运行时,我们得到以下表格单元格的 html 代码:

<tr>
<td>TESTER</td><td>394682876</td><td>YES</td><td>2</td><td>23</td><td bgcolor=#32CD32>Files_received_today</td></tr>
<tr>
<td>BILLER</td><td>6637761</td><td>NO</td><td>0</td><td>0</td><td bgcolor=#FFBF00>Files_not_received_today</td></tr>
<tr>
<td>APPROVER</td><td>23478</td><td>YES</td><td>1</td><td>1</td><td bgcolor=#32CD32>Files_received_today</td></tr>
<tr>
<td>BUYER</td><td>12398</td><td>YES</td><td>0</td><td>0</td><td bgcolor=#FF0000>Files_not_received_today</td></tr>

当完整的 html block 被加载到浏览器中时,我们得到:

enter image description here

关于html - 根据 HTML 表格中的条件更改列颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57728908/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com