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 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) { foreach ($sheet in $Worksheet) {
$targetSheetname = $sheet.Name
$xlBook["$targetSheetname"] = @()
#region Get rows and columns #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 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 } 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)'." # 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 #endregion
} }
@@ -230,6 +233,13 @@
catch { throw "Failed importing the Excel workbook '$Path' with worksheet '$WorksheetName': $_"; return } catch { throw "Failed importing the Excel workbook '$Path' with worksheet '$WorksheetName': $_"; return }
finally { finally {
if ($Path) { $stream.close(); $ExcelPackage.Dispose() } 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 Import-Module $PSScriptRoot\..\..\ImportExcel.psd1 -Force
Describe 'Different ways to import sheets' -Tag ImportExcelReadSheets { 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.Count | Should -Be 100
$actual[0].Month | Should -BeExactly "April" $actual[0].Month | Should -BeExactly "April"
$actual[99].Month | Should -BeExactly "April"
} }
It 'Should read two sheets' { It 'Should read two sheets' {
$actual = Import-Excel $xlFilename march, june $actual = Import-Excel $xlFilename march, june
$actual.Count | Should -Be 200 $actual.keys.Count | Should -Be 2
$actual[0].Month | Should -BeExactly "March" $actual["March"].Count | Should -Be 100
$actual[100].Month | Should -BeExactly "June" $actual["June"].Count | Should -Be 100
} }
It 'Should read all the sheets' { It 'Should read all the sheets' {
$actual = Import-Excel $xlFilename * $actual = Import-Excel $xlFilename *
$actual.Count | Should -Be 1200 $actual.keys.Count | Should -Be 12
$actual[0].Month | Should -BeExactly "April" $actual["January"].Count | Should -Be 100
$actual[100].Month | Should -BeExactly "August" $actual["February"].Count | Should -Be 100
$actual[200].Month | Should -BeExactly "December" $actual["March"].Count | Should -Be 100
$actual[300].Month | Should -BeExactly "February" $actual["April"].Count | Should -Be 100
$actual[400].Month | Should -BeExactly "January" $actual["May"].Count | Should -Be 100
$actual[500].Month | Should -BeExactly "July" $actual["June"].Count | Should -Be 100
$actual[600].Month | Should -BeExactly "June" $actual["July"].Count | Should -Be 100
$actual[700].Month | Should -BeExactly "March" $actual["August"].Count | Should -Be 100
$actual[800].Month | Should -BeExactly "May" $actual["September"].Count | Should -Be 100
$actual[900].Month | Should -BeExactly "November" $actual["October"].Count | Should -Be 100
$actual[1000].Month | Should -BeExactly "October" $actual["November"].Count | Should -Be 100
$actual[1100].Month | Should -BeExactly "September" $actual["December"].Count | Should -Be 100
} }
It 'Should throw if it cannot find the sheet' { It 'Should throw if it cannot find the sheet' {