I'm trying to create a html table with a CSV as source, which needs a few format including add color based on cell value, and merge the first column if the value is same as the next row.
However, 1. the color part works 2. the merge part doesn't work. there is no error but nothing gets merged.
can someone please help to check?
$file = import-csv I:\SCRIPT\IPCNewScript\BackupCode\ResultFinal.csv
$HTMLBody += "<span style='font-family:Courier New;font-size:12pt'>" # Due to blank space problem
$head = @'
<title>Table</title>
<style>
body { background-color:#E5E4E2;
font-family:Monospace;
font-size:10pt; }
td, th { border:0px solid black;
border-collapse:collapse;
white-space:pre; }
th { color:white;
background-color:black; }
table, tr, td, th { padding: 2px; margin: 0px ;white-space:pre; }
tr:nth-child(odd) {background-color: lightgray}
table { width:95%;margin-left:5px; margin-bottom:20px;}
h2 {
font-family:Tahoma;
color:#6D7B8D;
}
.warning {
color: orange;
}
.issue {
color: red;
}
.mergecol {
rowspan="2";
}
</style>
'@
[xml]$html = $file | convertTo-Html -fragment -As table
for ($j=4; $j -le $html.table.tr[1].td.count-1; $j++){
if(($html.table.tr[1].td[$j] -as [string]) -eq "Sun" ){
for ($i=2; $i -le $html.table.tr.count-1; $i++){
$class = $html.CreateAttribute("class")
$class.value = 'warning'
$html.table.tr[$i].ChildNodes[$j].Attributes.append($class) | out-null
}
}
else {
for ($i=2; $i -le $html.table.tr.count-1; $i++){
if (($html.table.tr[$i].td[$j] -as [int]) -eq 0){
$class = $html.CreateAttribute("class")
$class.value = 'issue'
$html.table.tr[$i].ChildNodes[$j].Attributes.append($class) | out-null
}
}
}}
#merge column
for ($i =1; $i -le $html.table.tr.count-2; $i++){
if (($html.table.tr[$i].td[0] -as [string]) -eq ($html.table.tr[$i+1].td[0] -as [string]))
{
$class = $html.CreateAttribute("class")
$class.value = 'mergecol'
$html.table.tr[$i].ChildNodes[0].Attributes.append($class) | out-null
$i++
}
}
convertto-html -head $head -body $($html.innerxml) | out-file "I:\SCRIPT\IPCNewScript\BackupCode\final28.htm"