mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-12 06:13:26 +00:00
Improved paramter validation and fixed 'WorksheetName' position. Extra Pester tests added too.
This commit is contained in:
@@ -15,6 +15,9 @@ $Path = 'Test.xlsx'
|
|||||||
Context 'input' {
|
Context 'input' {
|
||||||
in $TestDrive {
|
in $TestDrive {
|
||||||
Describe 'parameters' {
|
Describe 'parameters' {
|
||||||
|
BeforeEach {
|
||||||
|
Remove-Item ./* -Force
|
||||||
|
}
|
||||||
Context 'mandatory in sets' {
|
Context 'mandatory in sets' {
|
||||||
it 'Path' {
|
it 'Path' {
|
||||||
(Get-Command Import-Excel).Parameters['Path'].Attributes.Mandatory | Should be $true
|
(Get-Command Import-Excel).Parameters['Path'].Attributes.Mandatory | Should be $true
|
||||||
@@ -37,10 +40,64 @@ Context 'input' {
|
|||||||
(Get-Command Import-Excel).Parameters['WorksheetName'].Attributes.Mandatory | Should be $false
|
(Get-Command Import-Excel).Parameters['WorksheetName'].Attributes.Mandatory | Should be $false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Context 'illegal combinations' {
|
Context 'illegal' {
|
||||||
it 'NoHeader combined with HeaderName' {
|
it 'NoHeader combined with HeaderName' {
|
||||||
''| Export-Excel -Path $Path -WorkSheetname Kiwi
|
'Kiwi'| Export-Excel -Path $Path -WorkSheetname Fruit
|
||||||
{Import-Excel -Path $Path -WorksheetName Test -HeaderName A -NoHeader} | Should Throw 'Parameter set cannot be resolved'
|
{Import-Excel -Path $Path -WorksheetName Fruit -HeaderName A -NoHeader} | Should Throw 'Parameter set cannot be resolved'
|
||||||
|
}
|
||||||
|
it 'Path does not exist' {
|
||||||
|
{Import-Excel -Path D:\DontExist -WorksheetName Fruit} | Should Throw "Cannot validate argument on parameter 'Path'"
|
||||||
|
}
|
||||||
|
it 'WorksheetName left blank' {
|
||||||
|
'Kiwi'| Export-Excel -Path $Path -WorkSheetname Fruit
|
||||||
|
{Import-Excel -Path $Path -WorksheetName $null} | Should Throw "Cannot validate argument on parameter 'WorksheetName'. The argument is null or empty"
|
||||||
|
{Import-Excel -Path $Path -WorksheetName ''} | Should Throw "Cannot validate argument on parameter 'WorksheetName'. The argument is null or empty"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Context 'omit paramter name' {
|
||||||
|
it 'Path' {
|
||||||
|
[PSCustomObject]@{
|
||||||
|
Number = 1
|
||||||
|
} | Export-Excel -Path $Path -WorkSheetname Test
|
||||||
|
|
||||||
|
$ExpectedResult = [PSCustomObject]@{
|
||||||
|
'Number' = '1'
|
||||||
|
}
|
||||||
|
|
||||||
|
$Result = Import-Excel $Path
|
||||||
|
Assert-Equivalent -Actual $Result -Expected $ExpectedResult
|
||||||
|
}
|
||||||
|
it 'Path and WorksheetName' {
|
||||||
|
[PSCustomObject]@{
|
||||||
|
Number = 1
|
||||||
|
} | Export-Excel -Path $Path -WorkSheetname Test
|
||||||
|
|
||||||
|
$ExpectedResult = [PSCustomObject]@{
|
||||||
|
'Number' = '1'
|
||||||
|
}
|
||||||
|
|
||||||
|
$Result = Import-Excel $Path Test
|
||||||
|
Assert-Equivalent -Actual $Result -Expected $ExpectedResult
|
||||||
|
}
|
||||||
|
it 'Path and WorksheetName with NoHeader' {
|
||||||
|
'Kiwi' | Export-Excel -Path $Path -WorkSheetname Fruit
|
||||||
|
|
||||||
|
$ExpectedResult = [PSCustomObject]@{
|
||||||
|
P1 = 'Kiwi'
|
||||||
|
}
|
||||||
|
|
||||||
|
$Result = Import-Excel $Path Fruit -NoHeader
|
||||||
|
Assert-Equivalent -Actual $Result -Expected $ExpectedResult
|
||||||
|
}
|
||||||
|
it 'Path and WorksheetName with HeaderName' {
|
||||||
|
'Kiwi' | Export-Excel -Path $Path -WorkSheetname Fruit
|
||||||
|
|
||||||
|
$ExpectedResult = [PSCustomObject]@{
|
||||||
|
Fruits = 'Kiwi'
|
||||||
|
}
|
||||||
|
|
||||||
|
$Result = Import-Excel $Path Fruit -HeaderName Fruits
|
||||||
|
Assert-Equivalent -Actual $Result -Expected $ExpectedResult
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -60,7 +117,7 @@ Context 'input' {
|
|||||||
it 'empty' {
|
it 'empty' {
|
||||||
Import-Excel -Path $Path -WorksheetName Test -NoHeader | Should BeNullOrEmpty
|
Import-Excel -Path $Path -WorksheetName Test -NoHeader | Should BeNullOrEmpty
|
||||||
}
|
}
|
||||||
it 'select first worksheet' {
|
it 'select first worksheet by default' {
|
||||||
Remove-Item ./* -Force
|
Remove-Item ./* -Force
|
||||||
#region Create test file
|
#region Create test file
|
||||||
$Path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path)
|
$Path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path)
|
||||||
|
|||||||
@@ -54,7 +54,6 @@ Function Import-Excel {
|
|||||||
|
|
||||||
If the default behavior is not desired and you want to import the complete worksheet <20>as is<69>, the parameter <20>-NoHeader<65> can be used. In case you want to provide your own property names, you can use the parameter <20>-HeaderName<6D>.
|
If the default behavior is not desired and you want to import the complete worksheet <20>as is<69>, the parameter <20>-NoHeader<65> can be used. In case you want to provide your own property names, you can use the parameter <20>-HeaderName<6D>.
|
||||||
|
|
||||||
|
|
||||||
.PARAMETER Path
|
.PARAMETER Path
|
||||||
Specifies the path to the Excel file.
|
Specifies the path to the Excel file.
|
||||||
|
|
||||||
@@ -241,6 +240,8 @@ Function Import-Excel {
|
|||||||
[ValidateScript({Test-Path -Path $_ -PathType Leaf})]
|
[ValidateScript({Test-Path -Path $_ -PathType Leaf})]
|
||||||
[String]$Path,
|
[String]$Path,
|
||||||
[Alias('Sheet')]
|
[Alias('Sheet')]
|
||||||
|
[Parameter(Position=1)]
|
||||||
|
[ValidateNotNullOrEmpty()]
|
||||||
[String]$WorksheetName,
|
[String]$WorksheetName,
|
||||||
[Parameter(ParameterSetName='B', Mandatory)]
|
[Parameter(ParameterSetName='B', Mandatory)]
|
||||||
[String[]]$HeaderName,
|
[String[]]$HeaderName,
|
||||||
|
|||||||
Reference in New Issue
Block a user