psobject 属性 集丢失顺序 - 如何保留顺序?
psobject property set is losing order - how to retain order?
我正在使用 Export-Excel,而我使用的 psobject 属性 集是 re-ordering 本身,这使得我的 excel 标题顺序错误。
这是我的代码:
foreach ($errCode in $ErrCodes)
{
$resultHelp = [System.Collections.Generic.List[psobject]]::new()
Write-Host "another:"
$err = $errCode #get each error code to lookup in mdb file
Write-Host $err
$resultHelp = ProcessHelpMDB -mdbLookupError $errCode -mdbFilePath $basePathFull -mdbDeviceSeries $Ver #######
$result = [ordered] @{
"ScreenNumber" = ""
"KKey" = ""
"EnglishUS" = ""
"PictureID" = ""
"ImageFound" =""
"ImageFileName" = ""
} #this didn't do anything
#loop thru results from Help MDB file for $errCode
###this loses order:
$result = foreach($row in $resultHelp.GetEnumerator()) #this is working for each row
{
if($row -ne $True) #not sure why it adds true at the top
{
Write-Host $row.TextID #prints HELP_JAM; this is key for kapptext.mdb
#lookup value from kapptext.mdb
Write-Host $($row.TextID) #prints nothing but looks ok in next function
$longText = ProcessK_MDB -mdbLookupstring $($row.TextID) #gives English-US from db for parameter given, which is long error instruction
#export data found above to excel.
#### minimal example shows how get each row data since it's part of issue
#get the data ready to put in spreadsheet
$result = New-Object psobject -Property @{
ScreenNumber = $($row.ScreenNumber)
KKey = $($row.TextID)
EnglishUS = $($longText)
PictureID = $($row.PictureID)
ImageFound = ""
ImageFileName = ""
}
} #if not true
} #foreach row $resultHelp
#######I think something needs to change in data structure above...not sure what
$date = (Get-Date).tostring("MM-dd-yyyy")
[System.String] $help_spreadsheet_File = "COMPONENT MAP HELP_",$($date),".xlsx"
$out_path = '\company.net\EndToEnd\ComponentMaps'
$outFilePath = Join-Path -Path $out_path -ChildPath $help_spreadsheet_File
#write to dated file at out_path location
#export content to spreadsheet
$xlsx = $result | Export-Excel -Path $outFilePath -WorksheetName $errCode -Autosize -AutoFilter -FreezeTopRow -BoldTopRow -PassThru # -ClearSheet can't ClearSheet every time or it clears previous data ###seems to have issue over-writing each row below header
$ws = $xlsx.Workbook.Worksheets[$errCode]
$ws.Dimension.Columns #number of columns
$ws.Dimension.Rows #number of rows
$ws.Row(1).Height
$ws.Row(1).Height = 25
$ws.Column(5).Width
$ws.Column(5).Width = 50
Close-ExcelPackage $xlsx
Write-Host "Break when done with first errCode $($errCode)" #50-9
这是 $resultHelp 的样子:
[0] $true (I'm not sure why it puts true at the top)
[1] @(HelpCode=9;ScreenNumber=1;TextID=HELP_...JA;PictureID=HELP_...RIB)
[2] @(HelpCode=9;ScreenNumber=2;TextID=HELP_...ROLL;PictureID=HELP_...ROLL)
[3] @(HelpCode=9;ScreenNumber=3;TextID=HELP_...EDGE;PictureID=HELP_...UT)
...
输出看起来像这样(re-ordering 本身):
ImageFound ScreenNumber ImageFileName KKey EnglishUS PictureID
(data here)
我正在查看 ,它没有更改电子表格的订单输出。我也尝试在这里添加 [ordered],
$result = New-Object psobject -Property [ordered]@{
但它有错误消息(我找不到足够接近的示例来修复它)
New-Object : Cannot bind parameter 'Property'. Cannot convert the "[ordered]@" value of type "System.String" to type "System.Collections.IDictionary".
您正在将哈希表传递给 New-Object -Property
,而哈希表不保留输入顺序。
改变这个:
$result = New-Object psobject -Property @{
ScreenNumber = $($row.ScreenNumber)
KKey = $($row.TextID)
EnglishUS = $($longText)
PictureID = $($row.PictureID)
ImageFound = ""
ImageFileName = ""
}
收件人:
$result = New-Object psobject -Property $([ordered]@{
ScreenNumber = $($row.ScreenNumber)
KKey = $($row.TextID)
EnglishUS = $($longText)
PictureID = $($row.PictureID)
ImageFound = ""
ImageFileName = ""
})
除非将 [ordered]@{...}
表达式包装在 $()
中否则它不起作用的原因是 PowerShell 使用 different rules for parsing command arguments
我正在使用 Export-Excel,而我使用的 psobject 属性 集是 re-ordering 本身,这使得我的 excel 标题顺序错误。
这是我的代码:
foreach ($errCode in $ErrCodes)
{
$resultHelp = [System.Collections.Generic.List[psobject]]::new()
Write-Host "another:"
$err = $errCode #get each error code to lookup in mdb file
Write-Host $err
$resultHelp = ProcessHelpMDB -mdbLookupError $errCode -mdbFilePath $basePathFull -mdbDeviceSeries $Ver #######
$result = [ordered] @{
"ScreenNumber" = ""
"KKey" = ""
"EnglishUS" = ""
"PictureID" = ""
"ImageFound" =""
"ImageFileName" = ""
} #this didn't do anything
#loop thru results from Help MDB file for $errCode
###this loses order:
$result = foreach($row in $resultHelp.GetEnumerator()) #this is working for each row
{
if($row -ne $True) #not sure why it adds true at the top
{
Write-Host $row.TextID #prints HELP_JAM; this is key for kapptext.mdb
#lookup value from kapptext.mdb
Write-Host $($row.TextID) #prints nothing but looks ok in next function
$longText = ProcessK_MDB -mdbLookupstring $($row.TextID) #gives English-US from db for parameter given, which is long error instruction
#export data found above to excel.
#### minimal example shows how get each row data since it's part of issue
#get the data ready to put in spreadsheet
$result = New-Object psobject -Property @{
ScreenNumber = $($row.ScreenNumber)
KKey = $($row.TextID)
EnglishUS = $($longText)
PictureID = $($row.PictureID)
ImageFound = ""
ImageFileName = ""
}
} #if not true
} #foreach row $resultHelp
#######I think something needs to change in data structure above...not sure what
$date = (Get-Date).tostring("MM-dd-yyyy")
[System.String] $help_spreadsheet_File = "COMPONENT MAP HELP_",$($date),".xlsx"
$out_path = '\company.net\EndToEnd\ComponentMaps'
$outFilePath = Join-Path -Path $out_path -ChildPath $help_spreadsheet_File
#write to dated file at out_path location
#export content to spreadsheet
$xlsx = $result | Export-Excel -Path $outFilePath -WorksheetName $errCode -Autosize -AutoFilter -FreezeTopRow -BoldTopRow -PassThru # -ClearSheet can't ClearSheet every time or it clears previous data ###seems to have issue over-writing each row below header
$ws = $xlsx.Workbook.Worksheets[$errCode]
$ws.Dimension.Columns #number of columns
$ws.Dimension.Rows #number of rows
$ws.Row(1).Height
$ws.Row(1).Height = 25
$ws.Column(5).Width
$ws.Column(5).Width = 50
Close-ExcelPackage $xlsx
Write-Host "Break when done with first errCode $($errCode)" #50-9
这是 $resultHelp 的样子:
[0] $true (I'm not sure why it puts true at the top)
[1] @(HelpCode=9;ScreenNumber=1;TextID=HELP_...JA;PictureID=HELP_...RIB)
[2] @(HelpCode=9;ScreenNumber=2;TextID=HELP_...ROLL;PictureID=HELP_...ROLL)
[3] @(HelpCode=9;ScreenNumber=3;TextID=HELP_...EDGE;PictureID=HELP_...UT)
...
输出看起来像这样(re-ordering 本身):
ImageFound ScreenNumber ImageFileName KKey EnglishUS PictureID
(data here)
我正在查看
$result = New-Object psobject -Property [ordered]@{
但它有错误消息(我找不到足够接近的示例来修复它)
New-Object : Cannot bind parameter 'Property'. Cannot convert the "[ordered]@" value of type "System.String" to type "System.Collections.IDictionary".
您正在将哈希表传递给 New-Object -Property
,而哈希表不保留输入顺序。
改变这个:
$result = New-Object psobject -Property @{
ScreenNumber = $($row.ScreenNumber)
KKey = $($row.TextID)
EnglishUS = $($longText)
PictureID = $($row.PictureID)
ImageFound = ""
ImageFileName = ""
}
收件人:
$result = New-Object psobject -Property $([ordered]@{
ScreenNumber = $($row.ScreenNumber)
KKey = $($row.TextID)
EnglishUS = $($longText)
PictureID = $($row.PictureID)
ImageFound = ""
ImageFileName = ""
})
除非将 [ordered]@{...}
表达式包装在 $()
中否则它不起作用的原因是 PowerShell 使用 different rules for parsing command arguments