Add selective column import

This commit is contained in:
muschebubusche
2021-10-26 19:01:25 +02:00
parent 533ed07ac8
commit 62c8d74a59
6 changed files with 218 additions and 22 deletions

View File

@@ -35,7 +35,8 @@
[string[]]$AsText,
[string[]]$AsDate,
[ValidateNotNullOrEmpty()]
[String]$Password
[String]$Password,
[Int[]]$ImportColumns
)
end {
$sw = [System.Diagnostics.Stopwatch]::StartNew()
@@ -92,6 +93,26 @@
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) {
if ($path) {
$extension = [System.IO.Path]::GetExtension($Path)
@@ -141,7 +162,42 @@
$columns = ($StartColumn..$EndColumn).Where( { $colHash[$_] })
}
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." } }
elseif ($HeaderName) { $rows = $StartRow..$EndRow }
else {

View File

@@ -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" {
BeforeAll {
$xlfile = "TestDrive:\testImportExcel.xlsx"
$xlfileHeaderOnly = "TestDrive:\testImportExcelHeaderOnly.xlsx"
$xl = "" | Export-excel $xlfile -PassThru
$xlfile = "$PSScriptRoot\testImportExcel.xlsx"
$xlfileHeaderOnly = "$PSScriptRoot\testImportExcelHeaderOnly.xlsx"
$xlfileImportColumns = "$PSScriptRoot\testImportExcelImportColumns.xlsx"
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'
# Create $xlfile if it does not exist
if (!(Test-Path -Path $xlfile)) {
$xl = "" | Export-excel $xlfile -PassThru
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 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 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'
Set-ExcelRange -Worksheet $xl.Sheet1 -Range B3 -Value 'H'
Set-ExcelRange -Worksheet $xl.Sheet1 -Range C3 -Value 'I'
# Create $xlfileHeaderOnly if it does not exist
if (!(Test-Path -Path $xlfileHeaderOnly)) {
$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
$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'
# Create $xlfileImportColumns if it does not exist
if (!(Test-Path -Path $xlfileImportColumns)) {
$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" {
@@ -167,7 +200,7 @@ Describe "Import-Excel on a sheet with no headings" {
}
It "Should" {
$xlfile = "TestDrive:\testImportExcelSparse.xlsx"
$xlfile = "$PSScriptRoot\testImportExcelSparse.xlsx"
$xl = "" | Export-excel $xlfile -PassThru
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].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'
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.