![根据值更改列单元格值的背景(由 awk 创建的 HTML 报告)](https://linux22.com/image/11913/%E6%A0%B9%E6%8D%AE%E5%80%BC%E6%9B%B4%E6%94%B9%E5%88%97%E5%8D%95%E5%85%83%E6%A0%BC%E5%80%BC%E7%9A%84%E8%83%8C%E6%99%AF%EF%BC%88%E7%94%B1%20awk%20%E5%88%9B%E5%BB%BA%E7%9A%84%20HTML%20%E6%8A%A5%E5%91%8A%EF%BC%89.png)
我使用 awk 创建了一个 HTML 表(来自数据库的数据)。我坚持根据单元格值创建列的背景。对css不太了解。我可以根据条件更改 ont b,但不能根据背景颜色更改。这是我的代码。如果状态小于 100 背景应该是红色..否则背景与表的其余部分相同..
BEGIN {
print "<html><body></br></br>The report provides overall Percentage Secured in the given subjects.</br></br></br>"
print "<table border=1 bgcolor= \'LemonChiffon\" cellspacing=1 cellpadding=1>"
}
NR==1 {
# Header row
print "<tr>"
for ( i = 1; i <= NF; i++ ) {
print "<td><b>"$i"</b></td>"
}
print "</tr>"
}
NR>1 {
# Data rows
print "<tr>"
if( $i < 100 ) {
color="RED" \\ Background should be Red if the status is less tha 100\\
}
if( $i == 100 ) {
color="BLACK" \\Same background as rest o the table\\
}
print "<td><b><FONT COLOR=\""color"\" FACE=\"verdana\" SIZE=2>"$1"</b></FONT></td><td>"$2"</td><td>"$3"</td><td>"$4"</td><td>"$5"</td>"
print "</tr>"
}
END {
print "</table></body></html>"
}
答案1
这是一个古老但很好的基本 css 属性参考:http://www.htmlhelp.com/reference/css/properties.html
我会做
BEGIN {
print "<html><head>"
print "<title> Set the page title here </title>"
print "<style type=\"text/css\">"
print ".error {
print " color: red,"
print " font-size: larger,"
print " // other properties... take care, no trailing comma allowed"
print "}"
print "</style></head>"
print "<body>"
# use P tags, not BR line breaks -- easier to apply styling.
print "<p>The report provides overall Percentage Secured in the given subjects.</p>"
print "<table border=1 bgcolor=\"LemonChiffon\" cellspacing=1 cellpadding=1>"
}
NR == 1 {...} # I would use TH tags for the headers
NR > 1 {
# Data rows
print "<tr>"
for ( i = 1; i <= NF; i++ ) {
class = $i < 100 ? "class=\"error\"" : ""
printf "<td %s>%s</td>\n", class, $i
}
print "</tr>"
}
将样式放在一个地方会有所帮助,如果