Add switch to not return data as a dictionary

This commit is contained in:
dfinke
2022-04-12 18:03:34 -04:00
parent d55f0e64d4
commit 483f761016
2 changed files with 20 additions and 3 deletions

View File

@@ -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 {

View File

@@ -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'
}
}
}