我想為特定的原始著色。如果許可證天數 > 90,顏色為紅色。其他情況黃色或綠色。怎么能這樣做?我的表轉報告html表。
$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"
uj5u.com熱心網友回復:
好的,在這種情況下,您可以像下面這樣操作 HTML:
將您的變數更改$header為此添加三個類定義:
$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>
"@
然后將代碼的最后一行更改為:
$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 class="redgun">' } # red
elseif ($gunValue -lt 0) { $cell = '<td class="yellowgun">' } # yellow
else { $cell = '<td class="greengun">' } # 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"
輸出:

如果您不希望單個單元格具有不同的背景顏色,而是為整行著色,請執行以下操作:
$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 class="redgun">' } # red
elseif ($gunValue -lt 0) { $row = '<tr class="yellowgun">' } # yellow
else { $row = '<tr class="greengun">' } # green
# replace the string to insert the class
$_ -replace '^<tr>', $row
}
default { $_ }
}
$html | Out-File "C:\Users\akilic\Desktop\health.html"
輸出:

uj5u.com熱心網友回復:
這個輸出我得到了什么
我需要更改顏色“槍”列或全部原始。

轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/487820.html
