根据值更改列单元格值的背景(由 awk 创建的 HTML 报告)

根据值更改列单元格值的背景(由 awk 创建的 HTML 报告)

我使用 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>"
}

将样式放在一个地方会有所帮助,如果

相关内容