diff --git a/Public/Import-Excel.ps1 b/Public/Import-Excel.ps1 index f95bc25..b4fe004 100644 --- a/Public/Import-Excel.ps1 +++ b/Public/Import-Excel.ps1 @@ -135,8 +135,13 @@ $xlBook = [Ordered]@{} foreach ($sheet in $Worksheet) { if ($Worksheet.Count -gt 1 -or $Paths.Count -gt 1) { - $EndRow = 0 - $EndColumn = 0 + <# + Needed under these conditions to handle sheets of different number of Row/Col + - When reading more than one xlsx file + - When reading more than one worksheet in the same file + #> + $EndRow = $null + $EndColumn = $null } $targetSheetname = $sheet.Name diff --git a/__tests__/ImportExcelTests/DataInDiffRowColMultipleSheets.xlsx b/__tests__/ImportExcelTests/DataInDiffRowColMultipleSheets.xlsx new file mode 100644 index 0000000..cc3eaff Binary files /dev/null and b/__tests__/ImportExcelTests/DataInDiffRowColMultipleSheets.xlsx differ diff --git a/__tests__/ImportExcelTests/ImportExcelEndRowEndColumn.tests.ps1 b/__tests__/ImportExcelTests/ImportExcelEndRowEndColumn.tests.ps1 index e66a301..f9856f8 100644 --- a/__tests__/ImportExcelTests/ImportExcelEndRowEndColumn.tests.ps1 +++ b/__tests__/ImportExcelTests/ImportExcelEndRowEndColumn.tests.ps1 @@ -5,11 +5,11 @@ Describe 'Test' -Tag ImportExcelEndRowAndCols { $script:xlFilename = "$PSScriptRoot\DataInDiffRowCol.xlsx" } - Context 'Test reading a patial sheet' { + Context 'Test reading a partial sheet' { It 'Should read 2 rows and first 3 columns' { $actual = Import-Excel $xlFilename -StartRow 5 -EndRow 7 -StartColumn 3 -EndColumn 5 - $actual | out-host + # $actual | out-host $actual.Count | Should -Be 2 $colNames = $actual[0].psobject.properties.Name @@ -23,7 +23,7 @@ Describe 'Test' -Tag ImportExcelEndRowAndCols { It 'Should read second 2 rows and last 2 columns' { $actual = Import-Excel $xlFilename -StartRow 8 -EndRow 9 -StartColumn 5 -EndColumn 6 -HeaderName 'Units', 'Price' - $actual | out-host + # $actual | out-host $actual.Count | Should -Be 2 $colNames = $actual[0].psobject.properties.Name @@ -33,4 +33,19 @@ Describe 'Test' -Tag ImportExcelEndRowAndCols { $colNames[1] | Should -Be 'Price' } } + + Context 'Test reading multiple sheets with data in differnt rows and columns' { + It 'Should read 2 sheets same StartRow different dimensions' { + $xlFilename = "$PSScriptRoot\DataInDiffRowColMultipleSheets.xlsx" + + $actual = Import-Excel $xlFilename -StartRow 5 -WorksheetName * + + $actual.Keys.Count | Should -Be 2 + $actual.Contains('Sheet1') | Should -BeTrue + $actual.Contains('Sheet2') | Should -BeTrue + + $actual['Sheet1'].Count | Should -Be 9 + $actual['Sheet2'].Count | Should -Be 12 + } + } } \ No newline at end of file diff --git a/__tests__/ImportExcelTests/ImportExcelReadSheets.tests.ps1 b/__tests__/ImportExcelTests/ImportExcelReadSheets.tests.ps1 index 1b8dfad..362f0aa 100644 --- a/__tests__/ImportExcelTests/ImportExcelReadSheets.tests.ps1 +++ b/__tests__/ImportExcelTests/ImportExcelReadSheets.tests.ps1 @@ -23,7 +23,7 @@ Describe 'Different ways to import sheets' -Tag ImportExcelReadSheets { $actual.keys.Count | Should -Be 2 $actual["March"].Count | Should -Be 100 - $actual["June"].Count | Should -Be 100 + $actual["June"].Count | Should -Be 100x` } It 'Should read all the sheets' {