Skip to content

Commit 8f0fc73

Browse files
Add example and automatic column arrangement
Add example and automatic column arrangement
1 parent 62c8d74 commit 8f0fc73

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
try {Import-Module $PSScriptRoot\..\..\ImportExcel.psd1} catch {throw ; return}
2+
3+
# Create example file
4+
$xlFile = "$PSScriptRoot\ImportColumns.xlsx"
5+
Get-Process | Export-Excel -Path $xlFile
6+
# -ImportColumns will also arrange columns
7+
Import-Excel -Path $xlFile -ImportColumns @(1,3,2) -NoHeader -StartRow 1
8+
# Get only pm, npm, cpu, id, processname
9+
Import-Excel -Path $xlFile -ImportColumns @(6,7,12,25,46) | Format-Table -AutoSize

Public/Import-Excel.ps1

+21
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,17 @@
163163
}
164164
else {
165165
if ($ImportColumns) {
166+
# Safe the original array because $ImportColumns needs to be sorted for calculation
167+
$tempArray = $ImportColumns
168+
$ImportColumns = $ImportColumns | Sort-Object
169+
# Check order
170+
if (($tempArray[0] -ne $ImportColumns[0]) -or ($tempArray[$tempArray.Count - 1] -ne $ImportColumns[$ImportColumns.Count - 1])) {
171+
$arrange = $true
172+
} else {
173+
for ($i = 0; $i -lt $ImportColumns.Count; $i++) {
174+
if ($ImportColumns[$i] -ne $tempArray[$i]) { $arrange = $true;break }
175+
}
176+
}
166177
$end = $Worksheet.Dimension.End.Column
167178
if (($StartColumn -ne 1) -or ($EndColumn -ne $end)) { Write-Warning -Message "If ImportColumns is set than individual StartColumn and EndColumn will be ignored." }
168179
# Variable to store all removed columns
@@ -192,6 +203,16 @@
192203
}
193204
$ExcelPackage = Clear-ExcelPackage -Start $start -Count $count
194205
}
206+
# Arrange columns accordingly to $ImportColumns
207+
if ($arrange) {
208+
for ($i = 0; $i -lt $ImportColumns.Count; $i++) {
209+
$srcCol = [array]::IndexOf($ImportColumns,$tempArray[$i]) + 1
210+
$destCol = $ImportColumns.Count + ($i + 1)
211+
$Worksheet.Cells[1,$srcCol,$EndRow,$srcCol].Copy(($Worksheet.Cells[1,$destCol,$EndRow,$destCol]))
212+
}
213+
$ExcelPackage = Clear-ExcelPackage -Start 1 -Count $ImportColumns.Count
214+
Remove-Variable -Name 'tempArray'
215+
}
195216
# Create new array out of the $ImportColumns.Count for the further processing
196217
$columns = 1..$ImportColumns.Count
197218
}

__tests__/ImportExcelHeaderName.tests.ps1

+14
Original file line numberDiff line numberDiff line change
@@ -364,4 +364,18 @@ Describe "Import-Excel on a sheet with no headings" {
364364

365365
$actual[0].E | Should -Be '5'
366366
}
367+
368+
It "Should arrange the columns if -ImportColumns is not in order" {
369+
$actual = @(Import-Excel $xlfileImportColumns -ImportColumns @(5,1,4))
370+
$actualNames = $actual[0].psobject.properties.Name
371+
372+
$actualNames.Count | Should -Be 3
373+
$actualNames[0] | Should -Be 'E'
374+
$actualNames[1] | Should -Be 'A'
375+
$actualNames[2] | Should -Be 'D'
376+
377+
$actual[0].E | Should -Be '5'
378+
$actual[0].A | Should -Be '1'
379+
$actual[0].D | Should -Be '4'
380+
}
367381
}

0 commit comments

Comments
 (0)