Skip to content

Commit

Permalink
Merge pull request #1161 from dfinke/adding-to-import-excel
Browse files Browse the repository at this point in the history
Update and improve Import-Excel for reading nultiple sheets
  • Loading branch information
dfinke authored Apr 30, 2022
2 parents 9273261 + b2119f0 commit 8d56a35
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 83 deletions.
7 changes: 7 additions & 0 deletions Examples/Import-Excel/ImportMultipleSheetsAsArray.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Import-Module $PSScriptRoot\..\..\ImportExcel.psd1 -Force

$xlfile = "$PSScriptRoot\yearlySales.xlsx"

$result = Import-Excel $xlfile * -NotAsDictionary

$result | Measure-Object
9 changes: 9 additions & 0 deletions Examples/Import-Excel/ImportMultipleSheetsAsHashtable.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Import-Module $PSScriptRoot\..\..\ImportExcel.psd1 -Force

$xlfile = "$PSScriptRoot\yearlySales.xlsx"

$result = Import-Excel $xlfile *

foreach ($sheet in $result.Values) {
$sheet
}
Binary file added Examples/Import-Excel/yearlySales.xlsx
Binary file not shown.
185 changes: 102 additions & 83 deletions Public/Import-Excel.ps1

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions __tests__/ImportExcelHeaderName.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ Describe "Import-Excel on a sheet with no headings" {
}
}

AfterAll {
Remove-Item $PSScriptRoot\testImportExcelSparse.xlsx -ErrorAction SilentlyContinue
}

It "Import-Excel should have this shape" {
$actual = @(Import-Excel $xlfile)

Expand Down
63 changes: 63 additions & 0 deletions __tests__/ImportExcelTests/ImportExcelReadSheets.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#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 {
BeforeAll {
$xlFilename = "$PSScriptRoot\yearlySales.xlsx"
}

Context 'Test reading sheets' {
It 'Should read one sheet' {
$actual = Import-Excel $xlFilename

$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.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.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' {
{ Import-Excel $xlFilename april, june, notthere } | Should -Throw
}

It 'Should return an array not a dictionary' {
$actual = Import-Excel $xlFilename april, june -NotAsDictionary

$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'
}
}
}
Binary file added __tests__/ImportExcelTests/yearlySales.xlsx
Binary file not shown.
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

- Importing multiple files with Import-Excel by pipeline uses only the first file for the row count https://github.com/dfinke/ImportExcel/issues/1172

## New Features

- Import-Excel now supports importing multiple sheets. It can either return a dictionary of all sheets, or as a single array of all sheets combined.
- `Import-Excel $xlfile *` # reads all sheets, returns all data in a dictionary
- `Import-Excel $xlfile * -NotAsDictionary` # reads all sheets, returns all data in a single array

# v7.4.2

- Thank you [James Mueller](https://github.com/jamesmmueller) Updated `ConvertFrom-ExcelToSQLInsert` to handle single quotes in the SQL statement.
Expand Down

0 comments on commit 8d56a35

Please sign in to comment.