値に基づいて列セル値の背景を変更する(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>"
}

スタイルを一箇所に置くことが役に立つ場合

おすすめ記事