mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-06 00:23:20 +00:00
Add selective column import
This commit is contained in:
@@ -35,7 +35,8 @@
|
|||||||
[string[]]$AsText,
|
[string[]]$AsText,
|
||||||
[string[]]$AsDate,
|
[string[]]$AsDate,
|
||||||
[ValidateNotNullOrEmpty()]
|
[ValidateNotNullOrEmpty()]
|
||||||
[String]$Password
|
[String]$Password,
|
||||||
|
[Int[]]$ImportColumns
|
||||||
)
|
)
|
||||||
end {
|
end {
|
||||||
$sw = [System.Diagnostics.Stopwatch]::StartNew()
|
$sw = [System.Diagnostics.Stopwatch]::StartNew()
|
||||||
@@ -92,6 +93,26 @@
|
|||||||
throw "Failed creating property names: $_" ; return
|
throw "Failed creating property names: $_" ; return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
function Clear-ExcelPackage {
|
||||||
|
<#
|
||||||
|
.SYNOPSIS
|
||||||
|
Clear given ExcelPackage from specified columns.
|
||||||
|
#>
|
||||||
|
param (
|
||||||
|
# Column from where the cleaning will start.
|
||||||
|
[Parameter(Mandatory)]
|
||||||
|
[Int]
|
||||||
|
$Start,
|
||||||
|
# Count of columns that will be removed.
|
||||||
|
[Parameter(Mandatory)]
|
||||||
|
[Int]
|
||||||
|
$Count
|
||||||
|
)
|
||||||
|
|
||||||
|
$Worksheet.DeleteColumn($Start, $Count)
|
||||||
|
# Return $ExcelPackage to update the variable
|
||||||
|
return $ExcelPackage
|
||||||
|
}
|
||||||
foreach ($Path in $Paths) {
|
foreach ($Path in $Paths) {
|
||||||
if ($path) {
|
if ($path) {
|
||||||
$extension = [System.IO.Path]::GetExtension($Path)
|
$extension = [System.IO.Path]::GetExtension($Path)
|
||||||
@@ -141,7 +162,42 @@
|
|||||||
$columns = ($StartColumn..$EndColumn).Where( { $colHash[$_] })
|
$columns = ($StartColumn..$EndColumn).Where( { $colHash[$_] })
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$Columns = $StartColumn .. $EndColumn ; if ($StartColumn -gt $EndColumn) { Write-Warning -Message "Selecting columns $StartColumn to $EndColumn might give odd results." }
|
if ($ImportColumns) {
|
||||||
|
$end = $Worksheet.Dimension.End.Column
|
||||||
|
if (($StartColumn -ne 1) -or ($EndColumn -ne $end)) { Write-Warning -Message "If ImportColumns is set than individual StartColumn and EndColumn will be ignored." }
|
||||||
|
# Variable to store all removed columns
|
||||||
|
$removedColumns = 0
|
||||||
|
# Preparation run
|
||||||
|
$start = 1
|
||||||
|
$count = $ImportColumns[0] - 1
|
||||||
|
$ExcelPackage = Clear-ExcelPackage -Start $start -Count $count
|
||||||
|
$removedColumns = $removedColumns + $count
|
||||||
|
for ($i = 0; $i -lt $ImportColumns.Count; $i++) {
|
||||||
|
# Check if the current iteration is the last one for cleanup meassures
|
||||||
|
if ($i -eq ($ImportColumns.Count - 1)) { $lastLoop = $true }
|
||||||
|
if ($lastLoop) {
|
||||||
|
# Only clean up if the endcolumn does not match the last entry in the $ImportColumns array
|
||||||
|
if ($ImportColumns[$i] -ne $end) {
|
||||||
|
$start = $ImportColumns.Count + 1
|
||||||
|
$count = $end - ($removedColumns + $ImportColumns.Count)
|
||||||
|
} else {
|
||||||
|
# This means that the endcolumn gets imported
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
# Calculate the StartColumn and ColumnCount for the removal
|
||||||
|
$start = ($i + 1) + 1 # 1 is from the preparation run
|
||||||
|
$count = $ImportColumns[$i + 1] - ($removedColumns + $start)
|
||||||
|
$removedColumns = $removedColumns + $count
|
||||||
|
}
|
||||||
|
$ExcelPackage = Clear-ExcelPackage -Start $start -Count $count
|
||||||
|
}
|
||||||
|
# Create new array out of the $ImportColumns.Count for the further processing
|
||||||
|
$columns = 1..$ImportColumns.Count
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$Columns = $StartColumn .. $EndColumn ; if ($StartColumn -gt $EndColumn) { Write-Warning -Message "Selecting columns $StartColumn to $EndColumn might give odd results." }
|
||||||
|
}
|
||||||
if ($NoHeader) { $rows = $StartRow..$EndRow ; if ($StartRow -gt $EndRow) { Write-Warning -Message "Selecting rows $StartRow to $EndRow might give odd results." } }
|
if ($NoHeader) { $rows = $StartRow..$EndRow ; if ($StartRow -gt $EndRow) { Write-Warning -Message "Selecting rows $StartRow to $EndRow might give odd results." } }
|
||||||
elseif ($HeaderName) { $rows = $StartRow..$EndRow }
|
elseif ($HeaderName) { $rows = $StartRow..$EndRow }
|
||||||
else {
|
else {
|
||||||
|
|||||||
@@ -1,33 +1,66 @@
|
|||||||
|
#Requires -Modules Pester
|
||||||
|
|
||||||
|
if (-not (Get-command Import-Excel -ErrorAction SilentlyContinue)) {
|
||||||
|
Import-Module $PSScriptRoot\..\ImportExcel.psd1
|
||||||
|
}
|
||||||
|
|
||||||
Describe "Import-Excel on a sheet with no headings" {
|
Describe "Import-Excel on a sheet with no headings" {
|
||||||
BeforeAll {
|
BeforeAll {
|
||||||
|
|
||||||
$xlfile = "TestDrive:\testImportExcel.xlsx"
|
$xlfile = "$PSScriptRoot\testImportExcel.xlsx"
|
||||||
$xlfileHeaderOnly = "TestDrive:\testImportExcelHeaderOnly.xlsx"
|
$xlfileHeaderOnly = "$PSScriptRoot\testImportExcelHeaderOnly.xlsx"
|
||||||
$xl = "" | Export-excel $xlfile -PassThru
|
$xlfileImportColumns = "$PSScriptRoot\testImportExcelImportColumns.xlsx"
|
||||||
|
|
||||||
Set-ExcelRange -Worksheet $xl.Sheet1 -Range A1 -Value 'A'
|
# Create $xlfile if it does not exist
|
||||||
Set-ExcelRange -Worksheet $xl.Sheet1 -Range B1 -Value 'B'
|
if (!(Test-Path -Path $xlfile)) {
|
||||||
Set-ExcelRange -Worksheet $xl.Sheet1 -Range C1 -Value 'C'
|
$xl = "" | Export-excel $xlfile -PassThru
|
||||||
|
|
||||||
Set-ExcelRange -Worksheet $xl.Sheet1 -Range A2 -Value 'D'
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range A1 -Value 'A'
|
||||||
Set-ExcelRange -Worksheet $xl.Sheet1 -Range B2 -Value 'E'
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range B1 -Value 'B'
|
||||||
Set-ExcelRange -Worksheet $xl.Sheet1 -Range C2 -Value 'F'
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range C1 -Value 'C'
|
||||||
|
|
||||||
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range A2 -Value 'D'
|
||||||
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range B2 -Value 'E'
|
||||||
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range C2 -Value 'F'
|
||||||
|
|
||||||
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range A3 -Value 'G'
|
||||||
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range B3 -Value 'H'
|
||||||
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range C3 -Value 'I'
|
||||||
|
|
||||||
|
Close-ExcelPackage $xl
|
||||||
|
}
|
||||||
|
|
||||||
Set-ExcelRange -Worksheet $xl.Sheet1 -Range A3 -Value 'G'
|
# Create $xlfileHeaderOnly if it does not exist
|
||||||
Set-ExcelRange -Worksheet $xl.Sheet1 -Range B3 -Value 'H'
|
if (!(Test-Path -Path $xlfileHeaderOnly)) {
|
||||||
Set-ExcelRange -Worksheet $xl.Sheet1 -Range C3 -Value 'I'
|
$xl = "" | Export-excel $xlfileHeaderOnly -PassThru
|
||||||
|
|
||||||
Close-ExcelPackage $xl
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range A1 -Value 'A'
|
||||||
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range B1 -Value 'B'
|
||||||
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range C1 -Value 'C'
|
||||||
|
|
||||||
# crate $xlfileHeaderOnly
|
Close-ExcelPackage $xl
|
||||||
$xl = "" | Export-excel $xlfileHeaderOnly -PassThru
|
}
|
||||||
|
|
||||||
Set-ExcelRange -Worksheet $xl.Sheet1 -Range A1 -Value 'A'
|
# Create $xlfileImportColumns if it does not exist
|
||||||
Set-ExcelRange -Worksheet $xl.Sheet1 -Range B1 -Value 'B'
|
if (!(Test-Path -Path $xlfileImportColumns)) {
|
||||||
Set-ExcelRange -Worksheet $xl.Sheet1 -Range C1 -Value 'C'
|
$xl = "" | Export-Excel $xlfileImportColumns -PassThru
|
||||||
|
|
||||||
Close-ExcelPackage $xl
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range A1 -Value 'A'
|
||||||
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range B1 -Value 'B'
|
||||||
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range C1 -Value 'C'
|
||||||
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range D1 -Value 'D'
|
||||||
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range E1 -Value 'E'
|
||||||
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range F1 -Value 'F'
|
||||||
|
|
||||||
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range A2 -Value '1'
|
||||||
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range B2 -Value '2'
|
||||||
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range C2 -Value '3'
|
||||||
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range D2 -Value '4'
|
||||||
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range E2 -Value '5'
|
||||||
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range F2 -Value '6'
|
||||||
|
|
||||||
|
Close-ExcelPackage $xl
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
It "Import-Excel should have this shape" {
|
It "Import-Excel should have this shape" {
|
||||||
@@ -167,7 +200,7 @@ Describe "Import-Excel on a sheet with no headings" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
It "Should" {
|
It "Should" {
|
||||||
$xlfile = "TestDrive:\testImportExcelSparse.xlsx"
|
$xlfile = "$PSScriptRoot\testImportExcelSparse.xlsx"
|
||||||
$xl = "" | Export-excel $xlfile -PassThru
|
$xl = "" | Export-excel $xlfile -PassThru
|
||||||
|
|
||||||
Set-ExcelRange -Worksheet $xl.Sheet1 -Range A1 -Value 'Chuck'
|
Set-ExcelRange -Worksheet $xl.Sheet1 -Range A1 -Value 'Chuck'
|
||||||
@@ -224,4 +257,111 @@ Describe "Import-Excel on a sheet with no headings" {
|
|||||||
$actual[0].P2 | Should -Be 'B'
|
$actual[0].P2 | Should -Be 'B'
|
||||||
$actual[0].P3 | Should -Be 'C'
|
$actual[0].P3 | Should -Be 'C'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
It "Should import correct data if -ImportColumns is used with the first column" {
|
||||||
|
$actual = @(Import-Excel $xlfileImportColumns -ImportColumns @(1,2,4,5))
|
||||||
|
$actualNames = $actual[0].psobject.properties.Name
|
||||||
|
|
||||||
|
$actualNames.Count | Should -Be 4
|
||||||
|
$actualNames[0] | Should -Be 'A'
|
||||||
|
$actualNames[2] | Should -Be 'D'
|
||||||
|
|
||||||
|
$actual.Count | Should -Be 1
|
||||||
|
$actual[0].A | Should -Be 1
|
||||||
|
$actual[0].B | Should -Be 2
|
||||||
|
$actual[0].D | Should -Be 4
|
||||||
|
$actual[0].E | Should -Be 5
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should import correct data if -ImportColumns is used with the first column" {
|
||||||
|
$actual = @(Import-Excel $xlfileImportColumns -ImportColumns @(1,3,4,5))
|
||||||
|
$actualNames = $actual[0].psobject.properties.Name
|
||||||
|
|
||||||
|
$actualNames.Count | Should -Be 4
|
||||||
|
$actualNames[0] | Should -Be 'A'
|
||||||
|
$actualNames[2] | Should -Be 'D'
|
||||||
|
|
||||||
|
$actual.Count | Should -Be 1
|
||||||
|
$actual[0].A | Should -Be 1
|
||||||
|
$actual[0].C | Should -Be 3
|
||||||
|
$actual[0].D | Should -Be 4
|
||||||
|
$actual[0].E | Should -Be 5
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should import correct data if -ImportColumns is used without the first column" {
|
||||||
|
$actual = @(Import-Excel $xlfileImportColumns -ImportColumns @(2,3,6))
|
||||||
|
$actualNames = $actual[0].psobject.properties.Name
|
||||||
|
|
||||||
|
$actualNames.Count | Should -Be 3
|
||||||
|
$actualNames[0] | Should -Be 'B'
|
||||||
|
$actualNames[2] | Should -Be 'F'
|
||||||
|
|
||||||
|
$actual.Count | Should -Be 1
|
||||||
|
$actual[0].B | Should -Be 2
|
||||||
|
$actual[0].C | Should -Be 3
|
||||||
|
$actual[0].F | Should -Be 6
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should import correct data if -ImportColumns is used without the first column" {
|
||||||
|
$actual = @(Import-Excel $xlfileImportColumns -ImportColumns @(2,5,6))
|
||||||
|
$actualNames = $actual[0].psobject.properties.Name
|
||||||
|
|
||||||
|
$actualNames.Count | Should -Be 3
|
||||||
|
$actualNames[0] | Should -Be 'B'
|
||||||
|
$actualNames[2] | Should -Be 'F'
|
||||||
|
|
||||||
|
$actual.Count | Should -Be 1
|
||||||
|
$actual[0].B | Should -Be 2
|
||||||
|
$actual[0].E | Should -Be 5
|
||||||
|
$actual[0].F | Should -Be 6
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should import correct data if -ImportColumns is used with only 1 column" {
|
||||||
|
$actual = @(Import-Excel $xlfile -ImportColumns @(2))
|
||||||
|
$actualNames = $actual[0].psobject.properties.Name
|
||||||
|
|
||||||
|
$actualNames.Count | Should -Be 1
|
||||||
|
$actualNames[0] | Should -Be 'B'
|
||||||
|
|
||||||
|
$actual.Count | Should -Be 2
|
||||||
|
$actual[0].B | Should -Be 'E'
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should import correct data if -ImportColumns is used with only 1 column which is also the last" {
|
||||||
|
$actual = @(Import-Excel $xlfile -ImportColumns @(3))
|
||||||
|
$actualNames = $actual[0].psobject.properties.Name
|
||||||
|
|
||||||
|
$actualNames.Count | Should -Be 1
|
||||||
|
$actualNames[0] | Should -Be 'C'
|
||||||
|
|
||||||
|
$actual.Count | Should -Be 2
|
||||||
|
$actual[1].C | Should -Be 'I'
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should import correct data if -ImportColumns contains all columns" {
|
||||||
|
$actual = @(Import-Excel $xlfileImportColumns -ImportColumns @(1,2,3,4,5,6))
|
||||||
|
$actualNames = $actual[0].psobject.properties.Name
|
||||||
|
|
||||||
|
$actualNames.Count | Should -Be 6
|
||||||
|
$actualNames[0] | Should -Be 'A'
|
||||||
|
$actualNames[2] | Should -Be 'C'
|
||||||
|
|
||||||
|
$actual.Count | Should -Be 1
|
||||||
|
$actual[0].A | Should -Be 1
|
||||||
|
$actual[0].B | Should -Be 2
|
||||||
|
$actual[0].C | Should -Be 3
|
||||||
|
$actual[0].D | Should -Be 4
|
||||||
|
$actual[0].E | Should -Be 5
|
||||||
|
$actual[0].F | Should -Be 6
|
||||||
|
}
|
||||||
|
|
||||||
|
It "Should ignore -StartColumn and -EndColumn if -ImportColumns is set aswell" {
|
||||||
|
$actual = @(Import-Excel $xlfileImportColumns -ImportColumns @(5) -StartColumn 2 -EndColumn 7)
|
||||||
|
$actualNames = $actual[0].psobject.properties.Name
|
||||||
|
|
||||||
|
$actualNames.Count | Should -Be 1
|
||||||
|
$actualNames[0] | Should -Be 'E'
|
||||||
|
|
||||||
|
$actual[0].E | Should -Be '5'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
BIN
__tests__/testImportExcel.xlsx
Normal file
BIN
__tests__/testImportExcel.xlsx
Normal file
Binary file not shown.
BIN
__tests__/testImportExcelHeaderOnly.xlsx
Normal file
BIN
__tests__/testImportExcelHeaderOnly.xlsx
Normal file
Binary file not shown.
BIN
__tests__/testImportExcelImportColumns.xlsx
Normal file
BIN
__tests__/testImportExcelImportColumns.xlsx
Normal file
Binary file not shown.
BIN
__tests__/testImportExcelSparse.xlsx
Normal file
BIN
__tests__/testImportExcelSparse.xlsx
Normal file
Binary file not shown.
Reference in New Issue
Block a user