ironmansoftware / powershell-universal

Issue tracker for PowerShell Universal
https://powershelluniversal.com
36 stars 4 forks source link

Call Stack error when using ud-grid-layout #1060

Open JLogan3o13 opened 2 years ago

JLogan3o13 commented 2 years ago

Steps to Reproduce

I am creating a demo site that uses info from both a local CSV and Google finance to provide stock prices. The CSV has five columns total: Name, Symbol, OldPrice, CurrentPrice, and Change. The code for the dashboard is below:

`New-UDDashboard -Title "Stock Tracker" -Content { $Layout = '{"lg":[{"w":5,"h":2,"x":4,"y":0,"i":"grid-element-BlankLine1","moved":false,"static":false},{"w":12,"h":1,"x":0,"y":2,"i":"grid-element-BlankLine2","moved":false,"static":false},{"w":12,"h":15,"x":0,"y":3,"i":"grid-element-stocksTable","moved":false,"static":false}]}'

$aCSV = import-csv -Path "C:\Users\BotRunner\Documents\Stock Prices.csv"

New-UDGridLayout -Content {
    New-UDTypography -Id "BlankLine1" -Text "Stock Tracker for $((Get-Date -format 'MM/dd/yyyy'))" -Style @{
        "font-size" = "48px"        }
    New-UDTypography -Id "BlankLine2" -Text ''
    $Data = @()

    foreach($row in $aCSV) {
        $quote = Invoke-WebRequest -Uri "https://www.google.com/finance/quote/$($row.Symbol):NASDAQ" -UseBasicParsing
        $currentPrice = ((($quote.Content) -split ("data-last-price=")) -split ("data-last-normal"))[1] -replace('"', '')
        $lastPrice = $row.CurrentPrice
        $change = ($currentPrice - $lastPrice)         
        if ($change -lt 1) {$change = "0.00"}
        $row.OldPrice = $lastPrice
        $row.CurrentPrice = $currentPrice
        $row.Change = $change

        if (([string]$lastPrice.Length).Trim() -lt 4) {([string]$lastPrice = "$($lastPrice).00")}
        #if (([string]$currentPrice.Length).Trim() -lt 4) {([string]$currentPrice = "$($currentPrice).00")}  

        $Data += @{Company = $row.Name; Symbol = $row.Symbol; OldPrice = $lastPrice; CurrentPrice = $currentPrice; Change = $change}
    }

    $aCSV | Export-Csv -Path "C:\Users\BotRunner\Documents\Stock Prices.csv" -NoTypeInformation

    $Columns = @(
        New-UDTableColumn -Property Company -Title "Company Name"
        New-UDTableColumn -Property Symbol -Title "Symbol" 
        New-UDTableColumn -Property OldPrice -Title "Yesterday's Price" 
        New-UDTableColumn -Property CurrentPrice -Title "Today's Price" 
        New-UDTableColumn -Property Change -Title Change 
    )

    New-UDTable -Id 'stocksTable' -Data $Data -Columns $Columns
} -Layout $Layout
}`

Because the csv truncates a result like 23.00 as 23, I have the two If statements, that if the length is less than 4, add ".00" to the end of the string. As you can see, one of them is commented out. But if I attempt to run them both, I get the following:

image

The log does not show any errors. I have removed the ud-grid-layout component, and the page loads just fine. I have also confirmed this issue from 2.9.2 all the way back to 2.5.1 at least, and confirmed it happens whether using PS 5.1 or 7.2.1.

Expected behavior

If one of the columns has a number truncated by the CSV (23 instead of 23.00) the script should add the missing characters to the string before inserting the data it into the table.

Actual behavior

I get an error if I try to run both lines, but either one of them alone works fine.

Environment data

Windows 10 Professional (Azure VM) 2.5.1 - 2.9.2 of PowerShell Universal PS 5.1 and 7.2.1

Visuals

No response

JLogan3o13 commented 2 years ago

An update, if I move the $Layout var inside the New-UDGridLayout -Content bracket, the error does not occur, even with both If statements. It does not, however, seem to honor the Layout in this case:

image

adamdriscoll commented 1 year ago

I can't how the formatting was accomplished and this doesn't happen to me any more. I can reproduce it with your first example.

New-UDDashboard -Title "Stock Tracker" -Content {
$Layout = '{"lg":[{"w":5,"h":2,"x":4,"y":0,"i":"grid-element-BlankLine1","moved":false,"static":false},{"w":12,"h":1,"x":0,"y":2,"i":"grid-element-BlankLine2","moved":false,"static":false},{"w":12,"h":15,"x":0,"y":3,"i":"grid-element-stocksTable","moved":false,"static":false}]}'

$aCSV = @(
    [PSCustomObject]@{
        Name = "Google"
        Symbol = "GOOG"
        OldPrice = 100
        NewPrice = 200
        Change = 100
    }
)

New-UDGridLayout -Content {
    New-UDTypography -Id "BlankLine1" -Text "Stock Tracker for $((Get-Date -format 'MM/dd/yyyy'))" -Style @{
        "font-size" = "48px"        }
    New-UDTypography -Id "BlankLine2" -Text ''
    $Data = @()

    foreach($row in $aCSV) {
        $lastPrice = $row.OldPrice
        $currentPrice = $row.NewPrice 
        $change = $row.NewPrice - $row.OldPrice

        $Data += @{Company = $row.Name; Symbol = $row.Symbol; OldPrice = $lastPrice.ToString("C"); CurrentPrice = $currentPrice.ToString("C"); Change = $change}
    }

    $Columns = @(
        New-UDTableColumn -Property Company -Title "Company Name"
        New-UDTableColumn -Property Symbol -Title "Symbol" 
        New-UDTableColumn -Property OldPrice -Title "Yesterday's Price" 
        New-UDTableColumn -Property CurrentPrice -Title "Today's Price" 
        New-UDTableColumn -Property Change -Title Change 
    )

    New-UDTable -Id 'stocksTable' -Data $Data -Columns $Columns
} -Layout $Layout
}