我有一个 PowerShell 脚本,它运行一些 API 请求,然后将测试信息导出到 Excel 中。
当我创建表时,我为结果添加了几列。但是,当我通过 Excel 导出表格时,有一堆我不想要的额外列。
$ResultsTable = New-Object System.Data.DataTable "ResultsTable"
$RTC1 = New-Object system.Data.DataColumn "Type",([string])
$RTC2 = New-Object system.Data.DataColumn "Endpoint",([string])
$RTC3 = New-Object system.Data.DataColumn "PassRate",([string])
$RTC4 = New-Object system.Data.DataColumn "AvgTime",([string])
$RTC5 = New-Object system.Data.DataColumn "MaxTime",([string])
$RTC6 = New-Object system.Data.DataColumn "AvgSize",([string])
$RTC7 = New-Object system.Data.DataColumn "MaxSize",([string])
$ResultsTable.Columns.Add($RTC1)
$ResultsTable.Columns.Add($RTC2)
$ResultsTable.Columns.Add($RTC3)
$ResultsTable.Columns.Add($RTC4)
$ResultsTable.Columns.Add($RTC5)
$ResultsTable.Columns.Add($RTC6)
$ResultsTable.Columns.Add($RTC7)
$Row = $ResultsTable.NewRow()
$Row.Type = "Direct"
$Row.Endpoint = $Uri
$Row.PassRate = "$PassRate%"
$Row.AvgTime = $AvgTime
$Row.MaxTime = $MaxTime
$Row.AvgSize = $AvgSize
$Row.MaxTime = $MaxSize
$ResultsTable.Rows.Add($Row)
$ResultsTable | Export-Excel -Path ".\Output\Unit\API.Customer.Unit.Tests - $DateTime.xlsx" `
-AutoSize `
-WorksheetName "Results" `
-Title "Results Table" `
-TitleBold `
-BoldTopRow `
-FreezeTopRow
Run Code Online (Sandbox Code Playgroud)
此导出的输出如下所示:
我只需要 A - G 列。如何删除其他列?
选择您想要保留的列:
$ResultsTable |
Select-Object Type, Endpoint, PassRate, AvgTime, MaxTime, AvgSize, MaxSize |
Export-Excel ...
Run Code Online (Sandbox Code Playgroud)
或删除您不想保留的列:
$ResultsTable |
Select-Object -Property * -Exclude RowError, RowState, Table, ItemArray, HasErrors |
Export-Excel ...
Run Code Online (Sandbox Code Playgroud)
如果您知道您总是需要表中定义的列,您也可以直接引用它们:
$ResultsTable |
Select-Object -Property $ResultsTable.Columns.ColumnName |
Export-Excel ...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1684 次 |
| 最近记录: |