EvotecIT / PSTeams

PSTeams is a PowerShell Module working on Windows / Linux and Mac. It allows sending notifications to Microsoft Teams via WebHook Notifications. It's pretty flexible and provides a bunch of options. Initially, it only supported one sort of Team Cards but since version 2.X.X it supports Adaptive Cards, Hero Cards, List Cards, and Thumbnail Cards. All those new cards have their own cmdlets and the old version of creating Teams Cards stays as-is for compatibility reasons.
MIT License
409 stars 41 forks source link

Cells not aligned when using wrap and New-AdaptiveTable #50

Closed AaronICT closed 1 year ago

AaronICT commented 1 year ago

When a cell value is wrapped, the cells next to it do not adjust to the same height so the table rows are skewed. Here is a sample of what it looks like: https://i.stack.imgur.com/H02Eo.png Thanks Aaron

AaronICT commented 1 year ago

Based on this thread, https://stackoverflow.com/questions/69552378/is-there-any-way-to-align-the-table-contents-in-the-same-line-in-the-teams-bot-f, I modified the final else statement in the New-AdaptiveTable function from this:

       New-AdaptiveColumnSet {
            for ($i = 0; $i -lt $DataTable[0].PSObject.Properties.Name.Count; $i++) {
                New-AdaptiveColumn {
                    $HeaderText = $DataTable[0].PSObject.Properties.Name[$i]
                    New-AdaptiveTextBlock @HeaderAdaptiveTextBlockSplat -Text $HeaderText

                    for ($j = 0; $j -lt $DataTable.Count; $j++) {
                        $Value = $DataTable[$j].PSObject.Properties.Value[$i]
                        New-AdaptiveTextBlock @ContentAdaptiveTextBlockSplat -Text $Value -Separator
                    }
                } -Width Stretch
            }
        }

to this:

       $AdaptiveColumnSetSplat = @{
            Separator = $false
        }
        for ($Row = 0; $Row -lt $DataTable.Count; $Row++) {
            if ($Row -gt 0) { $AdaptiveColumnSetSplat.Separator = $true}
            New-AdaptiveColumnSet {
                for ($Column = 0; $Column -lt $DataTable[$Row].PSObject.Properties.Name.Count; $Column++) {
                    if ($Row -gt 0){
                        New-AdaptiveColumn {
                            $Value = $DataTable[$Row].PSObject.Properties.Value[$Column]
                            New-AdaptiveTextBlock @ContentAdaptiveTextBlockSplat -Text $Value
                        } -Width Stretch
                    } else {
                        New-AdaptiveColumn {
                            $HeaderText = $DataTable[$Row].PSObject.Properties.Name[$Column]
                            New-AdaptiveTextBlock @HeaderAdaptiveTextBlockSplat -Text $HeaderText
                        } -Width Stretch
                    }
                }
            } @AdaptiveColumnSetSplat
        }

The other section of the if statement should be changed to, but for my immediate need this was enough. Let me know your thoughts... Thanks Aaron

AaronICT commented 1 year ago

I realized there was an issue with my work-a-round. Here is the revised version:

        #Header
        New-AdaptiveColumnSet {
            for ($Column = 0; $Column -lt $DataTable[0].PSObject.Properties.Name.Count; $Column++) {
                New-AdaptiveColumn {
                    $HeaderText = $DataTable[0].PSObject.Properties.Name[$Column]
                    New-AdaptiveTextBlock @HeaderAdaptiveTextBlockSplat -Text $HeaderText
                } -Width Stretch
            }
        } -Separator

        #data
        for ($Row = 0; $Row -lt $DataTable.Count; $Row++) {
            New-AdaptiveColumnSet {
                for ($Column = 0; $Column -lt $DataTable[$Row].PSObject.Properties.Name.Count; $Column++) {
                    New-AdaptiveColumn {
                        $Value = $DataTable[$Row].PSObject.Properties.Value[$Column]
                        New-AdaptiveTextBlock @ContentAdaptiveTextBlockSplat -Text $Value
                    } -Width Stretch
                }
            }
        }
chaoscreater commented 1 year ago

@PrzemyslawKlys

Could you look at updating the module to include the above?

PrzemyslawKlys commented 1 year ago

The way I see it in newest version:

image

# Lets prepare dummmy object array with few elements
$Objects = @(
    [PSCustomObject] @{
        "VeryLongTItleAlal dsadsadsdasd aslsqdasdsadasdasdasd"  = 123
        Test2 = "Tes1t"
        Test3 = "Tes1t"
        Test4 = "Tes1t"
    }
    [PSCustomObject] @{
        "VeryLongTItleAlal dsadsadsdasd aslsqdasdsadasdasdasd"  = 456
        Test2 = "Test2"
    }
    [PSCustomObject] @{
        "VeryLongTItleAlal dsadsadsdasd aslsqdasdsadasdasdasd"  = 789
        Test2 = "Test3"
    }
)

# Lets create a new adaptive card
$Card = New-AdaptiveCard {
    # lets add some text, table and line breaks
    New-AdaptiveTextBlock -Size 'Medium' -Weight Bolder -Text 'Table usage with PSCustomObject 🔥' -Wrap

    New-AdaptiveTable -DataTable $Objects -Width Auto

    New-AdaptiveLineBreak

    New-AdaptiveTable -DataTable $Objects -Width Stretch

} -Uri $Env:TEAMSPESTERID -FullWidth -ReturnJson

I am not sure what you think is wrong with this one?

chaoscreater commented 1 year ago

Try testing it with lots of data. Prior to using @AaronICT's workaround, my output looksl ike this:

image