WIP - Add sheets and contents to a hashtable

This commit is contained in:
dfinke
2022-04-11 18:48:17 -04:00
parent f83f654c4a
commit d55f0e64d4
2 changed files with 33 additions and 18 deletions

View File

@@ -131,7 +131,10 @@
throw "Worksheet '$WorksheetName' not found, the workbook only contains the worksheets '$($ExcelPackage.Workbook.Worksheets)'. If you only wish to select the first worksheet, please remove the '-WorksheetName' parameter." ; return
}
$xlBook = [Ordered]@{}
foreach ($sheet in $Worksheet) {
$targetSheetname = $sheet.Name
$xlBook["$targetSheetname"] = @()
#region Get rows and columns
#If we are doing dataonly it is quicker to work out which rows to ignore before processing the cells.
if (-not $EndRow ) { $EndRow = $sheet.Dimension.End.Row }
@@ -221,7 +224,7 @@
# Write-Verbose "Import cell '$($Worksheet.Cells[$R, $P.Column].Address)' with property name '$($p.Value)' and value '$($Worksheet.Cells[$R, $P.Column].Value)'."
}
}
[PSCustomObject]$NewRow
$xlBook["$targetSheetname"] += [PSCustomObject]$NewRow
}
#endregion
}
@@ -230,6 +233,13 @@
catch { throw "Failed importing the Excel workbook '$Path' with worksheet '$WorksheetName': $_"; return }
finally {
if ($Path) { $stream.close(); $ExcelPackage.Dispose() }
if ($Worksheet.Count -eq 1) {
$xlBook["$targetSheetname"]
}
else {
$xlBook
}
}
}
}

View File

@@ -1,3 +1,7 @@
#Requires -Modules Pester
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments','',Justification='False Positives')]
param()
Import-Module $PSScriptRoot\..\..\ImportExcel.psd1 -Force
Describe 'Different ways to import sheets' -Tag ImportExcelReadSheets {
@@ -11,33 +15,34 @@ Describe 'Different ways to import sheets' -Tag ImportExcelReadSheets {
$actual.Count | Should -Be 100
$actual[0].Month | Should -BeExactly "April"
$actual[99].Month | Should -BeExactly "April"
}
It 'Should read two sheets' {
$actual = Import-Excel $xlFilename march, june
$actual.Count | Should -Be 200
$actual[0].Month | Should -BeExactly "March"
$actual[100].Month | Should -BeExactly "June"
$actual.keys.Count | Should -Be 2
$actual["March"].Count | Should -Be 100
$actual["June"].Count | Should -Be 100
}
It 'Should read all the sheets' {
$actual = Import-Excel $xlFilename *
$actual.Count | Should -Be 1200
$actual[0].Month | Should -BeExactly "April"
$actual[100].Month | Should -BeExactly "August"
$actual[200].Month | Should -BeExactly "December"
$actual[300].Month | Should -BeExactly "February"
$actual[400].Month | Should -BeExactly "January"
$actual[500].Month | Should -BeExactly "July"
$actual[600].Month | Should -BeExactly "June"
$actual[700].Month | Should -BeExactly "March"
$actual[800].Month | Should -BeExactly "May"
$actual[900].Month | Should -BeExactly "November"
$actual[1000].Month | Should -BeExactly "October"
$actual[1100].Month | Should -BeExactly "September"
$actual.keys.Count | Should -Be 12
$actual["January"].Count | Should -Be 100
$actual["February"].Count | Should -Be 100
$actual["March"].Count | Should -Be 100
$actual["April"].Count | Should -Be 100
$actual["May"].Count | Should -Be 100
$actual["June"].Count | Should -Be 100
$actual["July"].Count | Should -Be 100
$actual["August"].Count | Should -Be 100
$actual["September"].Count | Should -Be 100
$actual["October"].Count | Should -Be 100
$actual["November"].Count | Should -Be 100
$actual["December"].Count | Should -Be 100
}
It 'Should throw if it cannot find the sheet' {