AhmetKılıç
AhmetKılıç

Reputation: 31

how can change raw color in html table using powershell?

ı 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"

Upvotes: 1

Views: 1337

Answers (2)

AhmetKılı&#231;
AhmetKılı&#231;

Reputation: 31

This output that I get.

I need change color "gun" column or all raw.

enter image description here

Upvotes: 0

Theo
Theo

Reputation: 61178

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 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"

Output:

enter image description here


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 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"

Output:

enter image description here

Upvotes: 1

Related Questions