Suggestion: EnhancedHTML2: Highlighting a single cell in a row

This topic contains 5 replies, has 2 voices, and was last updated by Profile photo of mrveryboring999 . mrveryboring999 . 2 years, 4 months ago.

  • Author
    Posts
  • #17131
    Profile photo of mrveryboring999 .
    mrveryboring999 .
    Participant

    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

  • #17139
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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?

  • #17143
    Profile photo of mrveryboring999 .
    mrveryboring999 .
    Participant

    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.

  • #17145
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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.

  • #17147
    Profile photo of mrveryboring999 .
    mrveryboring999 .
    Participant

    Hi Don

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

  • #17168
    Profile photo of mrveryboring999 .
    mrveryboring999 .
    Participant

    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.

You must be logged in to reply to this topic.