ı want to colored specific raw. if licence days > 90 ,color red. other stiuation yellow or green. how can do that? My table turn report html table.
$sInFile = "C:\Users\akilic\Desktop\tarihlerr.csv"
$cInCsv = Import-Csv -Path $sInFile -Delimiter ";" -Encoding UTF7
$StartDate = Get-Date
$sDateFormat = "%d.%m.%Y"
$header = @"
<style>
table {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;}
th {border-width: 1px; padding: 3px; border-style: solid; border-color: black; background-color: #6495ED;}
td {border-width: 1px; padding: 3px; border-style: solid; border-color: black;}
}
</style>
"@
$result = foreach ($ThisUser in $cInCsv) {
$EndDate = Get-Date ($ThisUser.'bitis')
$licence = (New-TimeSpan –Start $StartDate –End $EndDate).Days
$ThisUser | Select-Object product,bitis, @{Name='gun'; Expression={$licence}}
if($licence -lt 90 -and $licence -gt 0) {
# output the selected object to be collected in variable $result
$ThisUser | Select-Object product,bitis, @{Name='gun'; Expression={$licence}}
}
elseif ($licence -lt 0){
$pos = [Math]::Abs($licence)
$ThisUser | Select-Object product,bitis,@{Name='gun'; Expression={$licence}}
}
}
$result | ConvertTo-Html -Head $header | Out-File "C:\Users\akilic\Desktop\health.html"
CodePudding user response:
Ok, in this case you can manipulate the HTML like below:
Change your $header
variaable into this to add three class definitions:
$header = @"
<style>
table {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;}
th {border-width: 1px; padding: 3px; border-style: solid; border-color: black; background-color: #6495ED;}
td {border-width: 1px; padding: 3px; border-style: solid; border-color: black;}
.redgun {background-color: #ff0000; text-align: right;}
.yellowgun {background-color: #ffcc00; text-align: right;}
.greengun {background-color: #33cc00; text-align: right;}
}
</style>
"@
Then change the final line of the code into:
$html = switch -Regex ($result | ConvertTo-Html -Head $header) {
'<td>(-?\d )</td></tr>$' {
$gunValue = [int][regex]::Match($_,'(?i)<td>(-?\d )</td></tr>$').Groups[1].Value
if ($gunValue -gt 90) { $cell = '<td >' } # red
elseif ($gunValue -lt 0) { $cell = '<td >' } # yellow
else { $cell = '<td >' } # green
# replace that part of the string to insert the class
$_ -replace '<td>-?\d </td></tr>$', ('{0}{1}</td></tr>' -f $cell, $gunValue)
}
default { $_ }
}
$html | Out-File "C:\Users\akilic\Desktop\health.html"
Output:
If you don't want the single cell to have a different background color, but instead color the whole row, do this:
$header = @"
<style>
table {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;}
th {border-width: 1px; padding: 3px; border-style: solid; border-color: black; background-color: #6495ED;}
td {border-width: 1px; padding: 3px; border-style: solid; border-color: black;}
.redgun {background-color: #ff0000;}
.yellowgun {background-color: #ffcc00;}
.greengun {background-color: #33cc00;}
}
</style>
"@
$html = switch -Regex ($result | ConvertTo-Html -Head $header) {
'<td>(-?\d )</td></tr>$' {
$gunValue = [int][regex]::Match($_,'(?i)<td>(-?\d )</td></tr>$').Groups[1].Value
if ($gunValue -gt 90) { $row = '<tr >' } # red
elseif ($gunValue -lt 0) { $row = '<tr >' } # yellow
else { $row = '<tr >' } # green
# replace the string to insert the class
$_ -replace '^<tr>', $row
}
default { $_ }
}
$html | Out-File "C:\Users\akilic\Desktop\health.html"
Output:
CodePudding user response:
this output what ı get
ı need change color "gun" coloumn or all raw.