Suggestion: EnhancedHTML2: Highlighting a single cell in a row

Hi

I use EnhancedHTML2 and needed the ability to highlight only a single cell within a row. To achieve this I edited EnhancedHTML2 as follows:

Replaced:

if ($prop.ContainsKey('css')) { $cell_css = $Object | ForEach $prop['css'] }

With:

if ($prop.ContainsKey('css')) { 
                        # Add property so that css scriptblock can highlight a cell 
                        # within a row.
                        $object | Add-Member -MemberType NoteProperty -Name "__PropertyName" -Value $prop['n'] -Force
                        $cell_css = $Object | ForEach $prop['css'] 
                    }

I then added:

# Remove property that was added to deal with highlighting
# within a row.
$object.PSObject.Properties.Remove( '__Propertyname' )

Just before:

}
     END
{

Example usage:

$css = {
        if( $_.Warnings -ne "0" )
        {
            if( $_.__PropertyName = "Results" ) { "red" }
        }
    }

Just thought this might be helpful to others.

Alan

I guess I’m a bit confused; the CSS property used by EnhancedHTML2 accepts an expression. That expression should output a CSS class name, based on whatever logic you put into the expression. You then assign a style to that CSS class name to accomplish highlighting or whatever. You can absolutely apply a specific style name to a single cell within a row.

Is what you’re doing applying “red” for all of the cells in a single column? You should do that using the existing code without modification… maybe I’m not getting what you’re doing?

Hi Don

Yes you can apply different css to each field and it works fine. However, I didn’t want to create multiple expressions (one for each field). My solution works by allowing a single expression to be applied to multiple fields.

I’ll try giving an example:

    $css = {
        if( $_.Warnings -ne "0" )
        {
            if( $_.__PropertyName -in "Warnings","Results" ) { "red right" }
            if( $_.__PropertyName -in "Database","Source Database Server","Local Instance" ) { "red left" }
        }
        else
        {
            if( $_.__PropertyName -in "Warnings","Results" ) { "right" }
            if( $_.__PropertyName -in "Database","Source Database Server","Local Instance" ) { "left" }
        }
    }

    # Create html table
    $params = @{
        'As'                = 'Table';
        'PostContent'       =  ( "Total Duration:{0}" -f $totalduration.ToString( "hh\:mm\:ss" ) )
        'TableCssID'        = 'dummy';
        'DivCssID'          = 'dummy';
        'EvenRowCssClass'   = 'even';
        'OddRowCssClass'    = 'odd';
        'TableCssClass'     = 'grid';
        'Properties'        = 
            @{ n = 'Database';               e = { $_.Database }; css = $css }, 
            @{ n = 'Source Database Server'; e = { $_.SourceDatabaseServer }; css = $css }, 
            @{ n = 'Local Instance';         e = { $_.LocalInstance }; css = $css }, 
            @{ n = 'Warnings';               e = { $_.Warnings }; css = $css }, 
            @{ n = 'Results';                e = { $_.Results };  css = $css }, 
            @{ n = 'Start';                  e = { $_.Start };    css = $css }, 
            @{ n = 'Finish';                 e = { $_.Finish };   css = $css }, 
            @{ n = 'Duration';               e = { $_.Duration }; css = $css }
    }
    $summary = $results | ConvertTo-EnhancedHTMLFragment @params | Out-String 

The above it part of a script that does test restores of multiple databases. If there are issues I want to highlight them in a html email I create using EnhancedHTML2. The output is eight columns. I wanted to highlight the first three fields if the value for Warning was not “0”. I also wanted to left align the first three fields, and right align the remaining ones.

This is just an alternative method of achieving it. On reflection maybe I could achieve the same with not much extra code using EnhanceHTML2 without my modification.

I see. You’re just not wanting to put an If construct into each property definition. Sure… I think it makes the code a bit harder to follow, but that’s just a personal aesthetic. What would perhaps be easier is to create a variable, $css, that contains ‘red’ if there are warnings and ‘’ if not, and just concatenate that to the individual expressions - which are left or right regardless.

Hi Don

You’ve given me food for thought. I’m going to see if I can achieve what I want without customising EnhancedHTML2.

Hi Don

Here’s the code I ended up with:

    # Prepare html email
    $cssLeft = { if( $_.Warnings -ne "0" ) { "left red" } else { "left" } }
    $cssRight = { if( $_.Warnings -ne "0" ) { "right red" } else { "right" } }

    # Create html table
    $params = @{
        'As'                = 'Table';
        'PostContent'       =  ( "Total Duration:{0}" -f $totalduration.ToString( "hh\:mm\:ss" ) )
        'TableCssID'        = 'dummy';
        'DivCssID'          = 'dummy';
        'EvenRowCssClass'   = 'even';
        'OddRowCssClass'    = 'odd';
        'TableCssClass'     = 'grid';
        'Properties'        = 
            @{ n = 'Database';               e = { $_.Database };             css = $cssLeft }, 
            @{ n = 'Source Database Server'; e = { $_.SourceDatabaseServer }; css = $cssLeft }, 
            @{ n = 'Local Instance';         e = { $_.LocalInstance };        css = $cssLeft }, 
            @{ n = 'Warnings';               e = { $_.Warnings };             css = $cssRight }, 
            @{ n = 'Results';                e = { $_.Results };              css = $cssRight }, 
            @{ n = 'Start';                  e = { $_.Start };                css = "right" }, 
            @{ n = 'Finish';                 e = { $_.Finish };               css = "right" }, 
            @{ n = 'Duration';               e = { $_.Duration };             css = "right" }
    }
    $summary = $results | ConvertTo-EnhancedHTMLFragment @params | Out-String 

This is less lines than I started with and without customising EnhancedHTML2.

Thanks for your feedback and suggestion.