![](/img/trans.png)
[英]How to change cell color in HTML table in a PowerShell script using if condition
[英]how can change raw color in html table using powershell?
我想为特定的原始着色。 如果许可证天数 > 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"
好的,在这种情况下,您可以像下面这样操作 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"
输出:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.