From 483f761016155333bb667a659226458d3716c86b Mon Sep 17 00:00:00 2001 From: dfinke Date: Tue, 12 Apr 2022 18:03:34 -0400 Subject: [PATCH] Add switch to not return data as a dictionary --- Public/Import-Excel.ps1 | 10 ++++++++-- .../ImportExcelReadSheets.tests.ps1 | 13 ++++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/Public/Import-Excel.ps1 b/Public/Import-Excel.ps1 index bd9cb8c..002579a 100644 --- a/Public/Import-Excel.ps1 +++ b/Public/Import-Excel.ps1 @@ -36,7 +36,8 @@ [string[]]$AsDate, [ValidateNotNullOrEmpty()] [String]$Password, - [Int[]]$ImportColumns + [Int[]]$ImportColumns, + [Switch]$NoHashtable ) end { $sw = [System.Diagnostics.Stopwatch]::StartNew() @@ -234,7 +235,12 @@ finally { if ($Path) { $stream.close(); $ExcelPackage.Dispose() } - if ($Worksheet.Count -eq 1) { + if ($NoHashtable) { + foreach ($entry in $xlbook.GetEnumerator()) { + $entry.Value + } + } + elseif ($Worksheet.Count -eq 1) { $xlBook["$targetSheetname"] } else { diff --git a/__tests__/ImportExcelTests/ImportExcelReadSheets.tests.ps1 b/__tests__/ImportExcelTests/ImportExcelReadSheets.tests.ps1 index a2abc40..d1e4e3d 100644 --- a/__tests__/ImportExcelTests/ImportExcelReadSheets.tests.ps1 +++ b/__tests__/ImportExcelTests/ImportExcelReadSheets.tests.ps1 @@ -1,5 +1,5 @@ #Requires -Modules Pester -[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments','',Justification='False Positives')] +[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification = 'False Positives')] param() Import-Module $PSScriptRoot\..\..\ImportExcel.psd1 -Force @@ -48,5 +48,16 @@ Describe 'Different ways to import sheets' -Tag ImportExcelReadSheets { It 'Should throw if it cannot find the sheet' { { Import-Excel $xlFilename april, june, notthere } | Should -Throw } + + It 'Should return an array not a dictionary' { + $actual = Import-Excel $xlFilename april, june -NoHashtable + + $actual.Count | Should -Be 200 + $group = $actual | Group-Object month -NoElement + + $group.Count | Should -Be 2 + $group[0].Name | Should -BeExactly 'April' + $group[1].Name | Should -BeExactly 'June' + } } } \ No newline at end of file