private/Controls/ConvertTo-UiExportText.ps1
|
function ConvertTo-UiExportText { <# .SYNOPSIS Flattens a cell value for export to CSV or other string formats. #> [CmdletBinding()] param( [AllowNull()] $Value ) # A nested list or dictionary reads the way its own cell would, so a list of hashtables exports as @{a=1}, @{b=2} isntead of a type name per element. $element = { param($item) if ((Get-UiValueKind -Value $item) -in 'Dictionary', 'List', 'Sequence') { ConvertTo-DisplayValue -Value $item } else { [PsUi.ValueKind]::DisplayText($item) } } switch ([PsUi.ValueKind]::Of($Value)) { 'List' { return (@(foreach ($item in $Value) { & $element $item }) -join ', ') } 'Dictionary' { return (@(foreach ($entry in $Value.GetEnumerator()) { "$($entry.Key)=$(& $element $entry.Value)" }) -join '; ') } 'Sequence' { return '[sequence]' } default { # DisplayText ignores the user's culture, so dates and numbers go out raw for Export-Csv to format if ($Value -is [System.IFormattable]) { return $Value } return [PsUi.ValueKind]::DisplayText($Value) } } } |