mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-11 05:43:17 +00:00
Update how Import-Excel reads sheets, and remain backward compatible
This commit is contained in:
@@ -15,7 +15,7 @@
|
||||
[Alias('Sheet')]
|
||||
[Parameter(Position = 1)]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[String]$WorksheetName,
|
||||
[String[]]$WorksheetName,
|
||||
[Parameter(ParameterSetName = 'PathB' , Mandatory)]
|
||||
[Parameter(ParameterSetName = 'PackageB', Mandatory)]
|
||||
[String[]]$HeaderName ,
|
||||
@@ -64,7 +64,7 @@
|
||||
|
||||
try {
|
||||
if ($ImportColumns) {
|
||||
$end = $Worksheet.Dimension.End.Column
|
||||
$end = $sheet.Dimension.End.Column
|
||||
# Check $ImportColumns
|
||||
if ($ImportColumns[0] -le 0) { throw "The first entry in ImportColumns must be equal or greater 1" ; return }
|
||||
# Check $StartColumn and $EndColumn
|
||||
@@ -95,7 +95,7 @@
|
||||
|
||||
foreach ($C in $Columns) {
|
||||
#allow "False" or "0" to be column headings
|
||||
$Worksheet.Cells[$StartRow, $C] | Where-Object { -not [string]::IsNullOrEmpty($_.Value) } | Select-Object @{N = 'Column'; E = { $C } }, Value
|
||||
$sheet.Cells[$StartRow, $C] | Where-Object { -not [string]::IsNullOrEmpty($_.Value) } | Select-Object @{N = 'Column'; E = { $C } }, Value
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -125,15 +125,17 @@
|
||||
}
|
||||
try {
|
||||
#Select worksheet
|
||||
if (-not $WorksheetName) { $Worksheet = $ExcelPackage.Workbook.Worksheets[1] }
|
||||
if ($WorksheetName -eq '*') { $Worksheet = $ExcelPackage.Workbook.Worksheets }
|
||||
elseif (-not $WorksheetName) { $Worksheet = $ExcelPackage.Workbook.Worksheets[1] }
|
||||
elseif (-not ($Worksheet = $ExcelPackage.Workbook.Worksheets[$WorksheetName])) {
|
||||
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
|
||||
}
|
||||
|
||||
foreach ($sheet in $Worksheet) {
|
||||
#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 (-not $EndRow ) { $EndRow = $Worksheet.Dimension.End.Row }
|
||||
if (-not $EndColumn) { $EndColumn = $Worksheet.Dimension.End.Column }
|
||||
if (-not $EndRow ) { $EndRow = $sheet.Dimension.End.Row }
|
||||
if (-not $EndColumn) { $EndColumn = $sheet.Dimension.End.Column }
|
||||
$endAddress = [OfficeOpenXml.ExcelAddress]::TranslateFromR1C1("R[$EndRow]C[$EndColumn]", 0, 0)
|
||||
if ($DataOnly) {
|
||||
#If we are using headers startrow will be the header-row so examine data from startRow + 1,
|
||||
@@ -145,7 +147,7 @@
|
||||
#using Hash tables: "we've seen it" is all we need, no need to worry about "seen it before" / "Seen it many times".
|
||||
$colHash = @{ }
|
||||
$rowHash = @{ }
|
||||
foreach ($cell in $Worksheet.Cells[$range]) {
|
||||
foreach ($cell in $sheet.Cells[$range]) {
|
||||
if ($null -ne $cell.Value ) { $colHash[$cell.Start.Column] = 1; $rowHash[$cell.Start.row] = 1 }
|
||||
}
|
||||
$rows = ( $StartRow..$EndRow ).Where( { $rowHash[$_] })
|
||||
@@ -205,17 +207,17 @@
|
||||
foreach ($P in $PropertyNames) {
|
||||
$MatchTest = $TextColRegEx.Match($P.value)
|
||||
if ($MatchTest.groups.name -eq "astext") {
|
||||
$NewRow[$P.Value] = $Worksheet.Cells[$R, $P.Column].Text
|
||||
$NewRow[$P.Value] = $sheet.Cells[$R, $P.Column].Text
|
||||
}
|
||||
elseif ($MatchTest.groups.name -eq "asdate" -and $Worksheet.Cells[$R, $P.Column].Value -is [System.ValueType]) {
|
||||
$NewRow[$P.Value] = [datetime]::FromOADate(($Worksheet.Cells[$R, $P.Column].Value))
|
||||
elseif ($MatchTest.groups.name -eq "asdate" -and $sheet.Cells[$R, $P.Column].Value -is [System.ValueType]) {
|
||||
$NewRow[$P.Value] = [datetime]::FromOADate(($sheet.Cells[$R, $P.Column].Value))
|
||||
}
|
||||
else { $NewRow[$P.Value] = $Worksheet.Cells[$R, $P.Column].Value }
|
||||
else { $NewRow[$P.Value] = $sheet.Cells[$R, $P.Column].Value }
|
||||
}
|
||||
}
|
||||
else {
|
||||
foreach ($P in $PropertyNames) {
|
||||
$NewRow[$P.Value] = $Worksheet.Cells[$R, $P.Column].Value
|
||||
$NewRow[$P.Value] = $sheet.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)'."
|
||||
}
|
||||
}
|
||||
@@ -224,6 +226,7 @@
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
catch { throw "Failed importing the Excel workbook '$Path' with worksheet '$WorksheetName': $_"; return }
|
||||
finally {
|
||||
if ($Path) { $stream.close(); $ExcelPackage.Dispose() }
|
||||
|
||||
47
__tests__/ImportExcelTests/ImportExcelReadSheets.tests.ps1
Normal file
47
__tests__/ImportExcelTests/ImportExcelReadSheets.tests.ps1
Normal file
@@ -0,0 +1,47 @@
|
||||
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"
|
||||
}
|
||||
|
||||
It 'Should read two sheets' {
|
||||
$actual = Import-Excel $xlFilename march, june
|
||||
|
||||
$actual.Count | Should -Be 200
|
||||
$actual[0].Month | Should -BeExactly "March"
|
||||
$actual[100].Month | Should -BeExactly "June"
|
||||
}
|
||||
|
||||
It 'Should read all the sheets' {
|
||||
$actual = Import-Excel $xlFilename *
|
||||
|
||||
$actual.Count | Should -Be 1200
|
||||
|
||||
$actual[0].Month | Should -BeExactly "April"
|
||||
$actual[100].Month | Should -BeExactly "August"
|
||||
$actual[200].Month | Should -BeExactly "December"
|
||||
$actual[300].Month | Should -BeExactly "February"
|
||||
$actual[400].Month | Should -BeExactly "January"
|
||||
$actual[500].Month | Should -BeExactly "July"
|
||||
$actual[600].Month | Should -BeExactly "June"
|
||||
$actual[700].Month | Should -BeExactly "March"
|
||||
$actual[800].Month | Should -BeExactly "May"
|
||||
$actual[900].Month | Should -BeExactly "November"
|
||||
$actual[1000].Month | Should -BeExactly "October"
|
||||
$actual[1100].Month | Should -BeExactly "September"
|
||||
}
|
||||
|
||||
It 'Should throw if it cannot find the sheet' {
|
||||
{ Import-Excel $xlFilename april, june, notthere } | Should -Throw
|
||||
}
|
||||
}
|
||||
}
|
||||
BIN
__tests__/ImportExcelTests/yearlySales.xlsx
Normal file
BIN
__tests__/ImportExcelTests/yearlySales.xlsx
Normal file
Binary file not shown.
Reference in New Issue
Block a user