diff --git a/.gitignore b/.gitignore index 39a724a..82c7631 100644 --- a/.gitignore +++ b/.gitignore @@ -62,3 +62,4 @@ testHide.ps1 ImportExcel.zip .vscode/launch.json +~$* diff --git a/Examples/OutTabulator/demo.txt b/Examples/OutTabulator/demo.txt new file mode 100644 index 0000000..0060900 --- /dev/null +++ b/Examples/OutTabulator/demo.txt @@ -0,0 +1,23 @@ +# ConvertFrom-Excel +'' + +.\test.xlsx + +Import-Excel .\test.xlsx | ft + +ConvertFrom-Excel -ExcelFile .\test.xlsx -outFile .\targetout.html + +# Create a column definition +$columnOptions = @() + +$columnOptions += New-ColumnOption -ColumnName Progress -formatter progress +ConvertFrom-Excel -ExcelFile .\test.xlsx -outFile .\targetout.html -columnOptions $columnOptions + +$columnOptions += New-ColumnOption Activity -formatter lineFormatter +ConvertFrom-Excel -ExcelFile .\test.xlsx -outFile .\targetout.html -columnOptions $columnOptions + +$columnOptions += New-ColumnOption -ColumnName Rating -formatter star +$columnOptions += New-ColumnOption Driver -formatter tickCross +ConvertFrom-Excel -ExcelFile .\test.xlsx -outFile .\targetout.html -columnOptions $columnOptions + +ConvertFrom-Excel -ExcelFile .\test.xlsx -outFile .\targetout.html -columnOptions $columnOptions -groupBy Gender \ No newline at end of file diff --git a/Examples/OutTabulator/start-demo.ps1 b/Examples/OutTabulator/start-demo.ps1 new file mode 100644 index 0000000..e5d4ada --- /dev/null +++ b/Examples/OutTabulator/start-demo.ps1 @@ -0,0 +1,216 @@ +## Start-Demo.ps1 +################################################################################################## +## This is an overhaul of Jeffrey Snover's original Start-Demo script by Joel "Jaykul" Bennett +## +## I've switched it to using ReadKey instead of ReadLine (you don't have to hit Enter each time) +## As a result, I've changed the names and keys for a lot of the operations, so that they make +## sense with only a single letter to tell them apart (sorry if you had them memorized). +## +## I've also been adding features as I come across needs for them, and you'll contribute your +## improvements back to the PowerShell Script repository as well. +################################################################################################## +## Revision History (version 3.3) +## 3.3.3 Fixed: Script no longer says "unrecognized key" when you hit shift or ctrl, etc. +## Fixed: Blank lines in script were showing as errors (now printed like comments) +## 3.3.2 Fixed: Changed the "x" to match the "a" in the help text +## 3.3.1 Fixed: Added a missing bracket in the script +## 3.3 - Added: Added a "Clear Screen" option +## - Added: Added a "Rewind" function (which I'm not using much) +## 3.2 - Fixed: Put back the trap { continue; } +## 3.1 - Fixed: No Output when invoking Get-Member (and other cmdlets like it???) +## 3.0 - Fixed: Commands which set a variable, like: $files = ls +## - Fixed: Default action doesn't continue +## - Changed: Use ReadKey instead of ReadLine +## - Changed: Modified the option prompts (sorry if you had them memorized) +## - Changed: Various time and duration strings have better formatting +## - Enhance: Colors are settable: prompt, command, comment +## - Added: NoPauseAfterExecute switch removes the extra pause +## If you set this, the next command will be displayed immediately +## - Added: Auto Execute mode (FullAuto switch) runs the rest of the script +## at an automatic speed set by the AutoSpeed parameter (or manually) +## - Added: Automatically append an empty line to the end of the demo script +## so you have a chance to "go back" after the last line of you demo +################################################################################################## +## +param( + $file=".\demo.txt", + [int]$command=0, + [System.ConsoleColor]$promptColor="Yellow", + [System.ConsoleColor]$commandColor="White", + [System.ConsoleColor]$commentColor="Green", + [switch]$FullAuto, + [int]$AutoSpeed = 3, + [switch]$NoPauseAfterExecute +) + +$RawUI = $Host.UI.RawUI +$hostWidth = $RawUI.BufferSize.Width + +# A function for reading in a character +function Read-Char() { + $_OldColor = $RawUI.ForeGroundColor + $RawUI.ForeGroundColor = "Red" + $inChar=$RawUI.ReadKey("IncludeKeyUp") + # loop until they press a character, so Shift or Ctrl, etc don't terminate us + while($inChar.Character -eq 0){ + $inChar=$RawUI.ReadKey("IncludeKeyUp") + } + $RawUI.ForeGroundColor = $_OldColor + return $inChar.Character +} + +function Rewind($lines, $index, $steps = 1) { + $started = $index; + $index -= $steps; + while(($index -ge 0) -and ($lines[$index].Trim(" `t").StartsWith("#"))){ + $index-- + } + if( $index -lt 0 ) { $index = $started } + return $index +} + +$file = Resolve-Path $file +while(-not(Test-Path $file)) { + $file = Read-Host "Please enter the path of your demo script (Crtl+C to cancel)" + $file = Resolve-Path $file +} + +Clear-Host + +$_lines = Get-Content $file +# Append an extra (do nothing) line on the end so we can still go back after the last line. +$_lines += "Write-Host 'The End'" +$_starttime = [DateTime]::now +$FullAuto = $false + +Write-Host -nonew -back black -fore $promptColor $(" " * $hostWidth) +Write-Host -nonew -back black -fore $promptColor @" +$(' ' * ($hostWidth -(18 + $(split-path $file -leaf).Length))) +"@ +Write-Host -nonew -back black -fore $promptColor "Press" +Write-Host -nonew -back black -fore Red " ? " +Write-Host -nonew -back black -fore $promptColor "for help.$(' ' * ($hostWidth -17))" +Write-Host -nonew -back black -fore $promptColor $(" " * $hostWidth) + +# We use a FOR and an INDEX ($_i) instead of a FOREACH because +# it is possible to start at a different location and/or jump +# around in the order. +for ($_i = $Command; $_i -lt $_lines.count; $_i++) +{ + # Put the current command in the Window Title along with the demo duration + $Dur = [DateTime]::Now - $_StartTime + $RawUI.WindowTitle = "$(if($dur.Hours -gt 0){'{0}h '})$(if($dur.Minutes -gt 0){'{1}m '}){2}s {3}" -f + $dur.Hours, $dur.Minutes, $dur.Seconds, $($_Lines[$_i]) + + # Echo out the commmand to the console with a prompt as though it were real + Write-Host -nonew -fore $promptColor "[$_i]$([char]0x2265) " + if ($_lines[$_i].Trim(" ").StartsWith("#") -or $_lines[$_i].Trim(" ").Length -le 0) { + Write-Host -fore $commentColor "$($_Lines[$_i]) " + continue + } else { + Write-Host -nonew -fore $commandColor "$($_Lines[$_i]) " + } + + if( $FullAuto ) { Start-Sleep $autoSpeed; $ch = [char]13 } else { $ch = Read-Char } + switch($ch) + { + "?" { + Write-Host -Fore $promptColor @" + +Running demo: $file +(n) Next (p) Previous +(q) Quit (s) Suspend +(t) Timecheck (v) View $(split-path $file -leaf) +(g) Go to line by number +(f) Find lines by string +(a) Auto Execute mode +(c) Clear Screen +"@ + $_i-- # back a line, we're gonna step forward when we loop + } + "n" { # Next (do nothing) + Write-Host -Fore $promptColor "" + } + "p" { # Previous + Write-Host -Fore $promptColor "" + while ($_lines[--$_i].Trim(" ").StartsWith("#")){} + $_i-- # back a line, we're gonna step forward when we loop + } + "a" { # EXECUTE (Go Faster) + $AutoSpeed = [int](Read-Host "Pause (seconds)") + $FullAuto = $true; + Write-Host -Fore $promptColor "" + $_i-- # Repeat this line, and then just blow through the rest + } + "q" { # Quit + Write-Host -Fore $promptColor "" + $_i = $_lines.count; + break; + } + "v" { # View Source + $lines[0..($_i-1)] | Write-Host -Fore Yellow + $lines[$_i] | Write-Host -Fore Green + $lines[($_i+1)..$lines.Count] | Write-Host -Fore Yellow + $_i-- # back a line, we're gonna step forward when we loop + } + "t" { # Time Check + $dur = [DateTime]::Now - $_StartTime + Write-Host -Fore $promptColor $( + "{3} -- $(if($dur.Hours -gt 0){'{0}h '})$(if($dur.Minutes -gt 0){'{1}m '}){2}s" -f + $dur.Hours, $dur.Minutes, $dur.Seconds, ([DateTime]::Now.ToShortTimeString())) + $_i-- # back a line, we're gonna step forward when we loop + } + "s" { # Suspend (Enter Nested Prompt) + Write-Host -Fore $promptColor "" + $Host.EnterNestedPrompt() + $_i-- # back a line, we're gonna step forward when we loop + } + "g" { # GoTo Line Number + $i = [int](Read-Host "line number") + if($i -le $_lines.Count) { + if($i -gt 0) { + # extra line back because we're gonna step forward when we loop + $_i = Rewind $_lines $_i (($_i-$i)+1) + } else { + $_i = -1 # Start negative, because we step forward when we loop + } + } + } + "f" { # Find by pattern + $match = $_lines | Select-String (Read-Host "search string") + if($match -eq $null) { + Write-Host -Fore Red "Can't find a matching line" + } else { + $match | % { Write-Host -Fore $promptColor $("[{0,2}] {1}" -f ($_.LineNumber - 1), $_.Line) } + if($match.Count -lt 1) { + $_i = $match.lineNumber - 2 # back a line, we're gonna step forward when we loop + } else { + $_i-- # back a line, we're gonna step forward when we loop + } + } + } + "c" { + Clear-Host + $_i-- # back a line, we're gonna step forward when we loop + } + "$([char]13)" { # on enter + Write-Host + trap [System.Exception] {Write-Error $_; continue;} + Invoke-Expression ($_lines[$_i]) | out-default + if(-not $NoPauseAfterExecute -and -not $FullAuto) { + $null = $RawUI.ReadKey("NoEcho,IncludeKeyUp") # Pause after output for no apparent reason... ;) + } + } + default + { + Write-Host -Fore Green "`nKey not recognized. Press ? for help, or ENTER to execute the command." + $_i-- # back a line, we're gonna step forward when we loop + } + } +} +$dur = [DateTime]::Now - $_StartTime +Write-Host -Fore $promptColor $( + "" -f + $dur.Hours, $dur.Minutes, $dur.Seconds, [DateTime]::Now.ToLongTimeString()) +Write-Host -Fore $promptColor $([DateTime]::now) +Write-Host \ No newline at end of file diff --git a/Examples/OutTabulator/targetout.html b/Examples/OutTabulator/targetout.html new file mode 100644 index 0000000..7da2bd6 --- /dev/null +++ b/Examples/OutTabulator/targetout.html @@ -0,0 +1,80 @@ + + + + + + + + Out-TabulatorView + + + + + + + + + + + +
+ + + + diff --git a/Examples/OutTabulator/tryConvertFromExcel.ps1 b/Examples/OutTabulator/tryConvertFromExcel.ps1 new file mode 100644 index 0000000..4372ba0 --- /dev/null +++ b/Examples/OutTabulator/tryConvertFromExcel.ps1 @@ -0,0 +1,9 @@ +[CmdletBinding()] +param($outFile = "$PSScriptRoot\targetout.html") + +$columnOptions = @() + +$columnOptions += New-ColumnOption -ColumnName Progress -formatter progress +$columnOptions += New-ColumnOption -ColumnName Activity -formatter lineFormatter + +ConvertFrom-Excel -ExcelFile $PSScriptRoot\test.xlsx -outFile $PSScriptRoot\targetout.html -columnOptions $columnOptions \ No newline at end of file diff --git a/Examples/TestRestAPI/PSExcelPester.psm1 b/Examples/TestRestAPI/PSExcelPester.psm1 new file mode 100644 index 0000000..23e74a9 --- /dev/null +++ b/Examples/TestRestAPI/PSExcelPester.psm1 @@ -0,0 +1,90 @@ +function ConvertTo-PesterTest { + param( + [parameter(Mandatory)] + $XlFilename, + $WorksheetName = 'Sheet1' + ) + + $testFileName = "{0}.tests.ps1" -f (get-date).ToString("yyyyMMddHHmmss") + + $records = Import-Excel $XlFilename + + $params = @{} + + $blocks = $(foreach ($record in $records) { + foreach ($propertyName in $record.psobject.properties.name) { + if ($propertyName -notmatch 'ExpectedResult|QueryString') { + $params.$propertyName = $record.$propertyName + } + } + + if ($record.QueryString) { + $params.Uri += "?{0}" -f $record.QueryString + } + + @" + + it "Should have the expected result '$($record.ExpectedResult)'" { + `$target = '$($params | ConvertTo-Json -compress)' | ConvertFrom-Json + + `$target.psobject.Properties.name | ForEach-Object {`$p=@{}} {`$p.`$_=`$(`$target.`$_)} + + Invoke-RestMethod @p | Should Be '$($record.ExpectedResult)' + } + +"@ + }) + + @" +Describe "Tests from $($XlFilename) in $($WorksheetName)" { +$($blocks) +} +"@ | Set-Content -Encoding Ascii $testFileName + + [PSCustomObject]@{ + TestFileName = (Get-ChildItem $testFileName).FullName + } +} + +function Show-PesterResult { + param( + [Parameter(ValueFromPipelineByPropertyName, Mandatory)] + $TestFileName + ) + + Begin { + $xlfilename = ".\test.xlsx" + Remove-Item $xlfilename -ErrorAction SilentlyContinue + + $ConditionalText = @() + $ConditionalText += New-ConditionalText -Range "Result" -Text failed -BackgroundColor red -ConditionalTextColor black + $ConditionalText += New-ConditionalText -Range "Result" -Text passed -BackgroundColor green -ConditionalTextColor black + $ConditionalText += New-ConditionalText -Range "Result" -Text pending -BackgroundColor gray -ConditionalTextColor black + + $xlParams = @{ + Path = $xlfilename + WorkSheetname = 'PesterTests' + ConditionalText = $ConditionalText + PivotRows = 'Result', 'Name' + PivotData = @{'Result' = 'Count'} + IncludePivotTable = $true + AutoSize = $true + AutoNameRange = $true + AutoFilter = $true + Show = $true + } + } + + End { + + $(foreach ($result in (Invoke-Pester -Script $TestFileName -PassThru -Show None).TestResult) { + [PSCustomObject][Ordered]@{ + Description = $result.Describe + Name = $result.Name + Result = $result.Result + Messge = $result.FailureMessage + StackTrace = $result.StackTrace + } + }) | Export-Excel @xlParams + } +} \ No newline at end of file diff --git a/Examples/TestRestAPI/ShowPesterResults.ps1 b/Examples/TestRestAPI/ShowPesterResults.ps1 index f66558b..aa3b7ba 100644 --- a/Examples/TestRestAPI/ShowPesterResults.ps1 +++ b/Examples/TestRestAPI/ShowPesterResults.ps1 @@ -1,40 +1,32 @@ function Show-PesterResults { - $xlfilename=".\test.xlsx" - rm $xlfilename -ErrorAction Ignore + $xlfilename = ".\test.xlsx" + Remove-Item $xlfilename -ErrorAction Ignore $ConditionalText = @() $ConditionalText += New-ConditionalText -Range "Result" -Text failed -BackgroundColor red -ConditionalTextColor black $ConditionalText += New-ConditionalText -Range "Result" -Text passed -BackgroundColor green -ConditionalTextColor black $ConditionalText += New-ConditionalText -Range "Result" -Text pending -BackgroundColor gray -ConditionalTextColor black - + $xlParams = @{ - Path=$xlfilename - WorkSheetname = 'PesterTests' - ConditionalText=$ConditionalText - PivotRows = 'Description' - PivotColumns = 'Result' - PivotData = @{'Result'='Count'} - IncludePivotTable = $true - #IncludePivotChart = $true - #NoLegend = $true - #ShowPercent = $true - #ShowCategory = $true - AutoSize = $true - AutoNameRange = $true - AutoFilter = $true - Show = $true + Path = $xlfilename + WorkSheetname = 'PesterTests' + ConditionalText = $ConditionalText + PivotRows = 'Result', 'Name' + PivotData = @{'Result' = 'Count'} + IncludePivotTable = $true + AutoSize = $true + AutoNameRange = $true + AutoFilter = $true + Show = $true } - $(foreach($result in (Invoke-Pester -PassThru -Show None).TestResult) { - - [PSCustomObject]@{ - Description = $result.Describe - Name = $result.Name - #Time = $result.Time - Result = $result.Result - Messge = $result.FailureMessage - StackTrace = $result.StackTrace - } - - }) | Sort Description | Export-Excel @xlParams + $(foreach ($result in (Invoke-Pester -PassThru -Show None).TestResult) { + [PSCustomObject]@{ + Description = $result.Describe + Name = $result.Name + Result = $result.Result + Messge = $result.FailureMessage + StackTrace = $result.StackTrace + } + }) | Sort-Object Description | Export-Excel @xlParams } \ No newline at end of file diff --git a/Examples/TestRestAPI/TestAPIReadXls.ps1 b/Examples/TestRestAPI/TestAPIReadXls.ps1 index 954d9e2..ff2e5d3 100644 --- a/Examples/TestRestAPI/TestAPIReadXls.ps1 +++ b/Examples/TestRestAPI/TestAPIReadXls.ps1 @@ -1,5 +1,3 @@ -try {. $PSScriptRoot\..\..\LoadPSD1.ps1} catch {} - function Test-APIReadXls { param( [parameter(Mandatory)] @@ -7,6 +5,8 @@ function Test-APIReadXls { $WorksheetName = 'Sheet1' ) + $testFileName = "{0}.tests.ps1" -f (get-date).ToString("yyyyMMddHHmmss") + $records = Import-Excel $XlFilename $params = @{} @@ -35,15 +35,11 @@ function Test-APIReadXls { "@ }) - $testFileName = "{0}.tests.ps1" -f (get-date).ToString("yyyyMMddHHmmss.fff") - @" Describe "Tests from $($XlFilename) in $($WorksheetName)" { $($blocks) } "@ | Set-Content -Encoding Ascii $testFileName - #Invoke-Pester -Script (Get-ChildItem $testFileName) - Get-ChildItem $testFileName -} - + (Get-ChildItem $testFileName).FullName +} \ No newline at end of file diff --git a/Examples/TestRestAPI/testlist.xlsx b/Examples/TestRestAPI/testlist.xlsx index e030540..b6daf82 100644 Binary files a/Examples/TestRestAPI/testlist.xlsx and b/Examples/TestRestAPI/testlist.xlsx differ diff --git a/Get-HtmlTable.ps1 b/Get-HtmlTable.ps1 index 6a7427f..a64d320 100644 --- a/Get-HtmlTable.ps1 +++ b/Get-HtmlTable.ps1 @@ -1,3 +1,5 @@ +# https://www.leeholmes.com/blog/2015/01/05/extracting-tables-from-powershells-invoke-webrequest/ +# tweaked from the above code function Get-HtmlTable { param( [Parameter(Mandatory=$true)] @@ -39,4 +41,4 @@ function Get-HtmlTable { [PSCustomObject]$result } -} \ No newline at end of file +} diff --git a/ImportExcel.Tests.ps1 b/ImportExcel.Tests.ps1 deleted file mode 100644 index faacee7..0000000 --- a/ImportExcel.Tests.ps1 +++ /dev/null @@ -1,2084 +0,0 @@ -#Requires -Modules Pester -#Requires -Modules Assert - -$here = Split-Path -Parent $MyInvocation.MyCommand.Path -$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.' - -Import-Module $here -Force - -$WarningPreference = 'SilentlyContinue' -# $WarningPreference = 'Continue' -$ProgressPreference = 'SilentlyContinue' - -$Path = 'Test.xlsx' -#<# -Context 'input' { - in $TestDrive { - Describe 'parameters' { - BeforeEach { - Remove-Item ./* -Force - } - Context 'mandatory in sets' { - it 'Path' { - (Get-Command Import-Excel).Parameters['Path'].Attributes.Mandatory | Should be $true - } - it 'HeaderName' { - (Get-Command Import-Excel).Parameters['HeaderName'].Attributes.Mandatory | Should be $true - } - it 'NoHeader' { - (Get-Command Import-Excel).Parameters['NoHeader'].Attributes.Mandatory | Should be $true - } - } - Context 'optional' { - it 'DataOnly' { - (Get-Command Import-Excel).Parameters['DataOnly'].Attributes.Mandatory | Should be $false - } - it 'StartRow' { - (Get-Command Import-Excel).Parameters['StartRow'].Attributes.Mandatory | Should be $false - } - it 'WorksheetName' { - (Get-Command Import-Excel).Parameters['WorksheetName'].Attributes.Mandatory | Should be $false - } - it 'Password' { - (Get-Command Import-Excel).Parameters['Password'].Attributes.Mandatory | Should be $false - } - } - Context 'aliases' { - it 'Path' { - (Get-Command Import-Excel).Parameters['Path'].Attributes.AliasNames | Should be 'FullName' - } - it 'WorksheetName' { - (Get-Command Import-Excel).Parameters['WorksheetName'].Attributes.AliasNames | Should be 'Sheet' - } - it 'StartRow' { - (Get-Command Import-Excel).Parameters['StartRow'].Attributes.AliasNames | Should be @('HeaderRow','TopRow') - } - } - Context 'illegal' { - it 'NoHeader combined with HeaderName' { - 'Kiwi'| Export-Excel -Path $Path -WorkSheetname Fruit - {Import-Excel -Path $Path -WorksheetName Fruit -HeaderName A -NoHeader} | Should Throw 'Parameter set cannot be resolved' - } - it 'HeaderName with blanks' { - 'Kiwi'| Export-Excel -Path $Path -WorkSheetname Fruit - {Import-Excel -Path $Path -WorksheetName Fruit -HeaderName A, $null, C} | Should Throw "Cannot bind argument to parameter 'HeaderName'" - {Import-Excel -Path $Path -WorksheetName Fruit -HeaderName $null, C} | Should Throw "Cannot bind argument to parameter 'HeaderName'" - {Import-Excel -Path $Path -WorksheetName Fruit -HeaderName $null} | Should Throw "Cannot bind argument to parameter 'HeaderName'" - - {Import-Excel -Path $Path -WorksheetName Fruit -HeaderName A, '', C} | Should Throw "Cannot bind argument to parameter 'HeaderName'" - {Import-Excel -Path $Path -WorksheetName Fruit -HeaderName '', C} | Should Throw "Cannot bind argument to parameter 'HeaderName'" - {Import-Excel -Path $Path -WorksheetName Fruit -HeaderName ''} | Should Throw "Cannot bind argument to parameter 'HeaderName'" - } - it 'Path does not exist' { - {Import-Excel -Path D:\DontExist -WorksheetName Fruit} | Should Throw "Cannot validate argument on parameter 'Path'" - } - it 'Path exists but does not have extension .xlsx or .xls' { - 'Kiwi' | Out-File NotAnExcelFile.txt - Test-Path -Path NotAnExcelFile.txt -PathType Leaf | Should be $true - {Import-Excel -Path NotAnExcelFile.txt -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" - } - it 'Password left blank' { - 'Kiwi'| Export-Excel -Path $Path -WorkSheetname Fruit - {Import-Excel -Path $Path -WorksheetName Fruit -Password $null} | Should Throw "Cannot validate argument on parameter 'Password'. The argument is null or empty" - {Import-Excel -Path $Path -WorksheetName Fruit -Password ''} | Should Throw "Cannot validate argument on parameter 'Password'. The argument is null or empty" - } - } - Context 'omit parameter 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 - } - } - } - Describe 'worksheet' { - #region Create test file - $Path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path) - $Excel = New-Object OfficeOpenXml.ExcelPackage $Path - $Excel | Add-WorkSheet -WorkSheetname Test - $Excel.Save() - $Excel.Dispose() - #endregion - - it 'not found' { - {Import-Excel -Path $Path -WorksheetName NotExisting} | Should Throw 'not found' - } - it 'empty' { - Import-Excel -Path $Path -WorksheetName Test -NoHeader | Should BeNullOrEmpty - } - it 'select first worksheet by default' { - Remove-Item ./* -Force - #region Create test file - $Path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path) - $Excel = New-Object OfficeOpenXml.ExcelPackage $Path - - - # ---------------------------------------------- - # | A B C | - # |1 First Name Address | - # |2 Chuck Norris California | - # |3 Jean-Claude Vandamme Brussels | - # ---------------------------------------------- - - # Row, Column - $WorksheetActors = $Excel | Add-WorkSheet -WorkSheetname Actors - $WorksheetActors.Cells[1, 1].Value = 'First Name' - $WorksheetActors.Cells[1, 3].Value = 'Address' - $WorksheetActors.Cells[2, 1].Value = 'Chuck' - $WorksheetActors.Cells[2, 2].Value = 'Norris' - $WorksheetActors.Cells[2, 3].Value = 'California' - $WorksheetActors.Cells[3, 1].Value = 'Jean-Claude' - $WorksheetActors.Cells[3, 2].Value = 'Vandamme' - $WorksheetActors.Cells[3, 3].Value = 'Brussels' - - # --------------------------------------------------------------------- - # | A B C D E | - # |1 Movie name Year Rating Genre | - # |2 The Bodyguard 1992 9 Thriller | - # |3 The Matrix 1999 8 Sci-Fi | - # |4 | - # |5 Skyfall 2012 9 Thriller | - # --------------------------------------------------------------------- - - # Row, Column - $WorksheetMovies = $Excel | Add-WorkSheet -WorkSheetname Movies - $WorksheetMovies.Cells[1, 1].Value = 'Movie name' - $WorksheetMovies.Cells[1, 2].Value = 'Year' - $WorksheetMovies.Cells[1, 3].Value = 'Rating' - $WorksheetMovies.Cells[1, 5].Value = 'Genre' - $WorksheetMovies.Cells[2, 1].Value = 'The Bodyguard' - $WorksheetMovies.Cells[2, 2].Value = '1982' - $WorksheetMovies.Cells[2, 3].Value = '9' - $WorksheetMovies.Cells[2, 5].Value = 'Thriller' - $WorksheetMovies.Cells[3, 1].Value = 'The Matrix' - $WorksheetMovies.Cells[3, 2].Value = '1999' - $WorksheetMovies.Cells[3, 3].Value = '8' - $WorksheetMovies.Cells[3, 5].Value = 'Sci-Fi' - $WorksheetMovies.Cells[5, 1].Value = 'Skyfall' - $WorksheetMovies.Cells[5, 2].Value = '2012' - $WorksheetMovies.Cells[5, 3].Value = '9' - $WorksheetMovies.Cells[5, 5].Value = 'Thriller' - - $Excel.Save() - $Excel.Dispose() - #endregion - - $ExpectedResult = @( - [PSCustomObject]@{ - 'First Name' = 'Chuck' - 'Address' = 'California' - } - [PSCustomObject]@{ - 'First Name' = 'Jean-Claude' - 'Address' = 'Brussels' - } - ) - - $Result = Import-Excel -Path $Path - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - - Remove-Item ./* -Force - - #region Create test file - $Path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path) - $Excel = New-Object OfficeOpenXml.ExcelPackage $Path - - # --------------------------------------------------------------------- - # | A B C D E | - # |1 Movie name Year Rating Genre | - # |2 The Bodyguard 1992 9 Thriller | - # |3 The Matrix 1999 8 Sci-Fi | - # |4 | - # |5 Skyfall 2012 9 Thriller | - # --------------------------------------------------------------------- - - # Row, Column - $WorksheetMovies = $Excel | Add-WorkSheet -WorkSheetname Movies - $WorksheetMovies.Cells[1, 1].Value = 'Movie name' - $WorksheetMovies.Cells[1, 2].Value = 'Year' - $WorksheetMovies.Cells[1, 3].Value = 'Rating' - $WorksheetMovies.Cells[1, 5].Value = 'Genre' - $WorksheetMovies.Cells[2, 1].Value = 'The Bodyguard' - $WorksheetMovies.Cells[2, 2].Value = '1982' - $WorksheetMovies.Cells[2, 3].Value = '9' - $WorksheetMovies.Cells[2, 5].Value = 'Thriller' - $WorksheetMovies.Cells[3, 1].Value = 'The Matrix' - $WorksheetMovies.Cells[3, 2].Value = '1999' - $WorksheetMovies.Cells[3, 3].Value = '8' - $WorksheetMovies.Cells[3, 5].Value = 'Sci-Fi' - $WorksheetMovies.Cells[5, 1].Value = 'Skyfall' - $WorksheetMovies.Cells[5, 2].Value = '2012' - $WorksheetMovies.Cells[5, 3].Value = '9' - $WorksheetMovies.Cells[5, 5].Value = 'Thriller' - - # ---------------------------------------------- - # | A B C | - # |1 First Name Address | - # |2 Chuck Norris California | - # |3 Jean-Claude Vandamme Brussels | - # ---------------------------------------------- - - # Row, Column - $WorksheetActors = $Excel | Add-WorkSheet -WorkSheetname Actors - $WorksheetActors.Cells[1, 1].Value = 'First Name' - $WorksheetActors.Cells[1, 3].Value = 'Address' - $WorksheetActors.Cells[2, 1].Value = 'Chuck' - $WorksheetActors.Cells[2, 2].Value = 'Norris' - $WorksheetActors.Cells[2, 3].Value = 'California' - $WorksheetActors.Cells[3, 1].Value = 'Jean-Claude' - $WorksheetActors.Cells[3, 2].Value = 'Vandamme' - $WorksheetActors.Cells[3, 3].Value = 'Brussels' - - $Excel.Save() - $Excel.Dispose() - #endregion - - $ExpectedResult = @( - [PSCustomObject]@{ - 'Movie name' = 'The Bodyguard' - 'Year' = '1982' - 'Rating' = '9' - 'Genre' = 'Thriller' - } - [PSCustomObject]@{ - 'Movie name' = 'The Matrix' - 'Year' = '1999' - 'Rating' = '8' - 'Genre' = 'Sci-Fi' - } - [PSCustomObject]@{ - 'Movie name' = $null - 'Year' = $null - 'Rating' = $null - 'Genre' = $null - } - [PSCustomObject]@{ - 'Movie name' = 'Skyfall' - 'Year' = '2012' - 'Rating' = '9' - 'Genre' = 'Thriller' - } - ) - - $Result = Import-Excel -Path $Path - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - } - } - } -} - -Context 'output' { - in $TestDrive { - Describe 'missing column header' { - - #region Create test file - - # ---------------------------------------------- - # | A B C | - # |1 First Name Address | - # |2 Chuck Norris California | - # |3 Jean-Claude Vandamme Brussels | - # ---------------------------------------------- - - $Path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path) - $Excel = New-Object OfficeOpenXml.ExcelPackage $Path - $Worksheet = $Excel | Add-WorkSheet -WorkSheetname Test - - # Row, Column - $Worksheet.Cells[1, 1].Value = 'First Name' - $Worksheet.Cells[1, 3].Value = 'Address' - $Worksheet.Cells[2, 1].Value = 'Chuck' - $Worksheet.Cells[2, 2].Value = 'Norris' - $Worksheet.Cells[2, 3].Value = 'California' - $Worksheet.Cells[3, 1].Value = 'Jean-Claude' - $Worksheet.Cells[3, 2].Value = 'Vandamme' - $Worksheet.Cells[3, 3].Value = 'Brussels' - - $Excel.Save() - $Excel.Dispose() - #endregion - - it 'Default' { - $ExpectedResult = @( - [PSCustomObject]@{ - 'First Name' = 'Chuck' - 'Address' = 'California' - } - [PSCustomObject]@{ - 'First Name' = 'Jean-Claude' - 'Address' = 'Brussels' - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - } - it 'Default and StartRow' { - $ExpectedResult = [PSCustomObject]@{ - 'Chuck' = 'Jean-Claude' - 'Norris' = 'Vandamme' - 'California' = 'Brussels' - } - - $Result = Import-Excel -Path $Path -WorksheetName Test -StartRow 2 - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - - Import-Excel -Path $Path -WorksheetName Test -StartRow 4 | Should BeNullOrEmpty - } - it 'Default and DataOnly' { - $ExpectedResult = @( - [PSCustomObject]@{ - 'First Name' = 'Chuck' - 'Address' = 'California' - } - [PSCustomObject]@{ - 'First Name' = 'Jean-Claude' - 'Address' = 'Brussels' - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -DataOnly - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - } - it 'Default, DataOnly and StartRow' { - $ExpectedResult = [PSCustomObject]@{ - 'Chuck' = 'Jean-Claude' - 'Norris' = 'Vandamme' - 'California' = 'Brussels' - } - - $Result = Import-Excel -Path $Path -WorksheetName Test -DataOnly -StartRow 2 - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - - Import-Excel -Path $Path -WorksheetName Test -DataOnly -StartRow 4 | Should BeNullOrEmpty - } - it 'NoHeader' { - $ExpectedResult = @( - [PSCustomObject]@{ - 'P1' = 'First Name' - 'P2' = $null - 'P3' = 'Address' - } - [PSCustomObject]@{ - 'P1' = 'Chuck' - 'P2' = 'Norris' - 'P3' = 'California' - } - [PSCustomObject]@{ - 'P1' = 'Jean-Claude' - 'P2' = 'Vandamme' - 'P3' = 'Brussels' - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -NoHeader - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - } - it 'NoHeader and StartRow' { - $ExpectedResult = @( - [PSCustomObject]@{ - 'P1' = 'Chuck' - 'P2' = 'Norris' - 'P3' = 'California' - } - [PSCustomObject]@{ - 'P1' = 'Jean-Claude' - 'P2' = 'Vandamme' - 'P3' = 'Brussels' - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -NoHeader -StartRow 2 - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - - Import-Excel -Path $Path -WorksheetName Test -NoHeader -StartRow 4 | Should BeNullOrEmpty - } - it 'NoHeader and DataOnly' { - $ExpectedResult = @( - [PSCustomObject]@{ - 'P1' = 'First Name' - 'P2' = $null - 'P3' = 'Address' - } - [PSCustomObject]@{ - 'P1' = 'Chuck' - 'P2' = 'Norris' - 'P3' = 'California' - } - [PSCustomObject]@{ - 'P1' = 'Jean-Claude' - 'P2' = 'Vandamme' - 'P3' = 'Brussels' - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -NoHeader -DataOnly - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - } - it 'NoHeader, DataOnly and StartRow' { - $ExpectedResult = @( - [PSCustomObject]@{ - 'P1' = 'Chuck' - 'P2' = 'Norris' - 'P3' = 'California' - } - [PSCustomObject]@{ - 'P1' = 'Jean-Claude' - 'P2' = 'Vandamme' - 'P3' = 'Brussels' - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -NoHeader -DataOnly -StartRow 2 - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - - Import-Excel -Path $Path -WorksheetName Test -NoHeader -DataOnly -StartRow 4 | Should BeNullOrEmpty - } - it 'HeaderName' { - $ExpectedResult = @( - [PSCustomObject]@{ - 'FirstName' = 'First Name' - 'SecondName' = $null - 'City' = 'Address' - 'Rating' = $null - } - [PSCustomObject]@{ - 'FirstName' = 'Chuck' - 'SecondName' = 'Norris' - 'City' = 'California' - 'Rating' = $null - } - [PSCustomObject]@{ - 'FirstName' = 'Jean-Claude' - 'SecondName' = 'Vandamme' - 'City' = 'Brussels' - 'Rating' = $null - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -HeaderName FirstName, SecondName, City, Rating - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - - $ExpectedResult = @( - [PSCustomObject]@{ - 'FirstName' = 'First Name' - 'SecondName' = $null - 'City' = 'Address' - 'Rating' = $null - 'Country' = $null - } - [PSCustomObject]@{ - 'FirstName' = 'Chuck' - 'SecondName' = 'Norris' - 'City' = 'California' - 'Rating' = $null - 'Country' = $null - } - [PSCustomObject]@{ - 'FirstName' = 'Jean-Claude' - 'SecondName' = 'Vandamme' - 'City' = 'Brussels' - 'Rating' = $null - 'Country' = $null - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -HeaderName FirstName, SecondName, City, Rating, Country - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - } - it 'HeaderName and StartRow' { - $ExpectedResult = @( - [PSCustomObject]@{ - 'FirstName' = 'Chuck' - 'SecondName' = 'Norris' - 'City' = 'California' - 'Rating' = $null - } - [PSCustomObject]@{ - 'FirstName' = 'Jean-Claude' - 'SecondName' = 'Vandamme' - 'City' = 'Brussels' - 'Rating' = $null - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -HeaderName FirstName, SecondName, City, Rating -StartRow 2 - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - - $ExpectedResult = @( - [PSCustomObject]@{ - 'FirstName' = 'Chuck' - 'SecondName' = 'Norris' - 'City' = 'California' - 'Rating' = $null - 'Country' = $null - } - [PSCustomObject]@{ - 'FirstName' = 'Jean-Claude' - 'SecondName' = 'Vandamme' - 'City' = 'Brussels' - 'Rating' = $null - 'Country' = $null - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -HeaderName FirstName, SecondName, City, Rating, Country -StartRow 2 - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - - Import-Excel -Path $Path -WorksheetName Test -HeaderName FirstName, SecondName, City, Rating, Country -StartRow 4 | Should BeNullOrEmpty - } - it 'HeaderName and DataOnly' { - $ExpectedResult = @( - [PSCustomObject]@{ - 'FirstName' = 'First Name' - 'SecondName' = $null - 'City' = 'Address' - 'Rating' = $null - } - [PSCustomObject]@{ - 'FirstName' = 'Chuck' - 'SecondName' = 'Norris' - 'City' = 'California' - 'Rating' = $null - } - [PSCustomObject]@{ - 'FirstName' = 'Jean-Claude' - 'SecondName' = 'Vandamme' - 'City' = 'Brussels' - 'Rating' = $null - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -HeaderName FirstName, SecondName, City, Rating -DataOnly - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - - $ExpectedResult = @( - [PSCustomObject]@{ - 'FirstName' = 'First Name' - 'SecondName' = $null - 'City' = 'Address' - 'Rating' = $null - 'Country' = $null - } - [PSCustomObject]@{ - 'FirstName' = 'Chuck' - 'SecondName' = 'Norris' - 'City' = 'California' - 'Rating' = $null - 'Country' = $null - } - [PSCustomObject]@{ - 'FirstName' = 'Jean-Claude' - 'SecondName' = 'Vandamme' - 'City' = 'Brussels' - 'Rating' = $null - 'Country' = $null - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -HeaderName FirstName, SecondName, City, Rating, Country -DataOnly - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - } - it 'HeaderName, DataOnly and StartRow' { - $ExpectedResult = @( - [PSCustomObject]@{ - 'FirstName' = 'Chuck' - 'SecondName' = 'Norris' - 'City' = 'California' - 'Rating' = $null - } - [PSCustomObject]@{ - 'FirstName' = 'Jean-Claude' - 'SecondName' = 'Vandamme' - 'City' = 'Brussels' - 'Rating' = $null - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -HeaderName FirstName, SecondName, City, Rating -DataOnly -StartRow 2 - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - - $ExpectedResult = @( - [PSCustomObject]@{ - 'FirstName' = 'Chuck' - 'SecondName' = 'Norris' - 'City' = 'California' - 'Rating' = $null - 'Country' = $null - } - [PSCustomObject]@{ - 'FirstName' = 'Jean-Claude' - 'SecondName' = 'Vandamme' - 'City' = 'Brussels' - 'Rating' = $null - 'Country' = $null - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -HeaderName FirstName, SecondName, City, Rating, Country -DataOnly -StartRow 2 - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - - Import-Excel -Path $Path -WorksheetName Test -HeaderName FirstName, SecondName, City, Rating, Country -DataOnly -StartRow 4 | Should BeNullOrEmpty - } - } - Describe 'blank rows and columns' { - - #region Create test file - - # --------------------------------------------------------------------- - # | A B C D E | - # |1 Movie name Year Rating Genre | - # |2 The Bodyguard 1992 9 Thriller | - # |3 The Matrix 1999 8 Sci-Fi | - # |4 | - # |5 Skyfall 2012 9 Thriller | - # --------------------------------------------------------------------- - - $Path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path) - $Excel = New-Object OfficeOpenXml.ExcelPackage $Path - $Worksheet = $Excel | Add-WorkSheet -WorkSheetname Test - - # Row, Column - $Worksheet.Cells[1, 1].Value = 'Movie name' - $Worksheet.Cells[1, 2].Value = 'Year' - $Worksheet.Cells[1, 3].Value = 'Rating' - $Worksheet.Cells[1, 5].Value = 'Genre' - $Worksheet.Cells[2, 1].Value = 'The Bodyguard' - $Worksheet.Cells[2, 2].Value = '1982' - $Worksheet.Cells[2, 3].Value = '9' - $Worksheet.Cells[2, 5].Value = 'Thriller' - $Worksheet.Cells[3, 1].Value = 'The Matrix' - $Worksheet.Cells[3, 2].Value = '1999' - $Worksheet.Cells[3, 3].Value = '8' - $Worksheet.Cells[3, 5].Value = 'Sci-Fi' - $Worksheet.Cells[5, 1].Value = 'Skyfall' - $Worksheet.Cells[5, 2].Value = '2012' - $Worksheet.Cells[5, 3].Value = '9' - $Worksheet.Cells[5, 5].Value = 'Thriller' - - $Excel.Save() - $Excel.Dispose() - #endregion - - it 'Default' { - $ExpectedResult = @( - [PSCustomObject]@{ - 'Movie name' = 'The Bodyguard' - 'Year' = '1982' - 'Rating' = '9' - 'Genre' = 'Thriller' - } - [PSCustomObject]@{ - 'Movie name' = 'The Matrix' - 'Year' = '1999' - 'Rating' = '8' - 'Genre' = 'Sci-Fi' - } - [PSCustomObject]@{ - 'Movie name' = $null - 'Year' = $null - 'Rating' = $null - 'Genre' = $null - } - [PSCustomObject]@{ - 'Movie name' = 'Skyfall' - 'Year' = '2012' - 'Rating' = '9' - 'Genre' = 'Thriller' - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - } - it 'Default and StartRow' { - $ExpectedResult = @( - [PSCustomObject]@{ - 'The Bodyguard' = 'The Matrix' - '1982' = '1999' - '9' = '8' - 'Thriller' = 'Sci-Fi' - } - [PSCustomObject]@{ - 'The Bodyguard' = $null - '1982' = $null - '9' = $null - 'Thriller' = $null - } - [PSCustomObject]@{ - 'The Bodyguard' = 'Skyfall' - '1982' = '2012' - '9' = '9' - 'Thriller' = 'Thriller' - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -StartRow 2 - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - - {Import-Excel -Path $Path -WorksheetName Test -StartRow 4} | Should Throw 'No column headers found' - Import-Excel -Path $Path -WorksheetName Test -StartRow 5 | Should BeNullOrEmpty - } - it 'Default and DataOnly' { - $ExpectedResult = @( - [PSCustomObject]@{ - 'Movie name' = 'The Bodyguard' - 'Year' = '1982' - 'Rating' = '9' - 'Genre' = 'Thriller' - } - [PSCustomObject]@{ - 'Movie name' = 'The Matrix' - 'Year' = '1999' - 'Rating' = '8' - 'Genre' = 'Sci-Fi' - } - [PSCustomObject]@{ - 'Movie name' = 'Skyfall' - 'Year' = '2012' - 'Rating' = '9' - 'Genre' = 'Thriller' - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -DataOnly - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - } - it 'Default, DataOnly and StartRow' { - $ExpectedResult = @( - [PSCustomObject]@{ - 'The Bodyguard' = 'The Matrix' - '1982' = '1999' - '9' = '8' - 'Thriller' = 'Sci-Fi' - } - [PSCustomObject]@{ - 'The Bodyguard' = 'Skyfall' - '1982' = '2012' - '9' = '9' - 'Thriller' = 'Thriller' - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -DataOnly -StartRow 2 - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - - {Import-Excel -Path $Path -WorksheetName Test -DataOnly -StartRow 4} | Should Throw 'No column headers found' - - Import-Excel -Path $Path -WorksheetName Test -DataOnly -StartRow 5 | Should BeNullOrEmpty - } - it 'HeaderName' { - $ExpectedResult = @( - [PSCustomObject]@{ - 'MovieName' = 'Movie name' - 'Year' = 'Year' - 'Rating' = 'Rating' - 'Genre' = $null - } - [PSCustomObject]@{ - 'MovieName' = 'The Bodyguard' - 'Year' = '1982' - 'Rating' = '9' - 'Genre' = $null - } - [PSCustomObject]@{ - 'MovieName' = 'The Matrix' - 'Year' = '1999' - 'Rating' = '8' - 'Genre' = $null - } - [PSCustomObject]@{ - 'MovieName' = $null - 'Year' = $null - 'Rating' = $null - 'Genre' = $null - } - [PSCustomObject]@{ - 'MovieName' = 'Skyfall' - 'Year' = '2012' - 'Rating' = '9' - 'Genre' = $null - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -HeaderName MovieName, Year, Rating, Genre - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - - $ExpectedResult = @( - [PSCustomObject]@{ - 'MovieName' = 'Movie name' - 'Year' = 'Year' - 'Rating' = 'Rating' - 'Genre' = $null - 'Country' = 'Genre' - } - [PSCustomObject]@{ - 'MovieName' = 'The Bodyguard' - 'Year' = '1982' - 'Rating' = '9' - 'Genre' = $null - 'Country' = 'Thriller' - } - [PSCustomObject]@{ - 'MovieName' = 'The Matrix' - 'Year' = '1999' - 'Rating' = '8' - 'Genre' = $null - 'Country' = 'Sci-Fi' - } - [PSCustomObject]@{ - 'MovieName' = $null - 'Year' = $null - 'Rating' = $null - 'Genre' = $null - 'Country' = $null - } - [PSCustomObject]@{ - 'MovieName' = 'Skyfall' - 'Year' = '2012' - 'Rating' = '9' - 'Genre' = $null - 'Country' = 'Thriller' - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -HeaderName MovieName, Year, Rating, Genre, Country - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - } - it 'HeaderName and StartRow' { - $ExpectedResult = @( - [PSCustomObject]@{ - 'MovieName' = 'The Bodyguard' - 'Year' = '1982' - 'Rating' = '9' - 'Genre' = $null - } - [PSCustomObject]@{ - 'MovieName' = 'The Matrix' - 'Year' = '1999' - 'Rating' = '8' - 'Genre' = $null - } - [PSCustomObject]@{ - 'MovieName' = $null - 'Year' = $null - 'Rating' = $null - 'Genre' = $null - } - [PSCustomObject]@{ - 'MovieName' = 'Skyfall' - 'Year' = '2012' - 'Rating' = '9' - 'Genre' = $null - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -HeaderName MovieName, Year, Rating, Genre -StartRow 2 - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - - $ExpectedResult = @( - [PSCustomObject]@{ - 'MovieName' = 'The Bodyguard' - 'Year' = '1982' - 'Rating' = '9' - 'Genre' = $null - 'Country' = 'Thriller' - } - [PSCustomObject]@{ - 'MovieName' = 'The Matrix' - 'Year' = '1999' - 'Rating' = '8' - 'Genre' = $null - 'Country' = 'Sci-Fi' - } - [PSCustomObject]@{ - 'MovieName' = $null - 'Year' = $null - 'Rating' = $null - 'Genre' = $null - 'Country' = $null - } - [PSCustomObject]@{ - 'MovieName' = 'Skyfall' - 'Year' = '2012' - 'Rating' = '9' - 'Genre' = $null - 'Country' = 'Thriller' - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -HeaderName MovieName, Year, Rating, Genre, Country -StartRow 2 - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - - $ExpectedResult = @( - [PSCustomObject]@{ - 'MovieName' = $null - 'Year' = $null - 'Rating' = $null - 'Genre' = $null - 'Country' = $null - } - [PSCustomObject]@{ - 'MovieName' = 'Skyfall' - 'Year' = '2012' - 'Rating' = '9' - 'Genre' = $null - 'Country' = 'Thriller' - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -HeaderName MovieName, Year, Rating, Genre, Country -StartRow 4 - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - - $ExpectedResult = [PSCustomObject]@{ - 'MovieName' = 'Skyfall' - 'Year' = '2012' - 'Rating' = '9' - 'Genre' = $null - 'Country' = 'Thriller' - } - - $Result = Import-Excel -Path $Path -WorksheetName Test -HeaderName MovieName, Year, Rating, Genre, Country -StartRow 5 - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - - Import-Excel -Path $Path -WorksheetName Test -HeaderName MovieName, Year, Rating, Genre, Country -StartRow 6 | Should BeNullOrEmpty - } - it 'HeaderName and DataOnly' { - $ExpectedResult = @( - [PSCustomObject]@{ - 'MovieName' = 'Movie name' - 'Year' = 'Year' - 'Rating' = 'Rating' - 'Genre' = 'Genre' - } - [PSCustomObject]@{ - 'MovieName' = 'The Bodyguard' - 'Year' = '1982' - 'Rating' = '9' - 'Genre' = 'Thriller' - } - [PSCustomObject]@{ - 'MovieName' = 'The Matrix' - 'Year' = '1999' - 'Rating' = '8' - 'Genre' = 'Sci-Fi' - } - - [PSCustomObject]@{ - 'MovieName' = 'Skyfall' - 'Year' = '2012' - 'Rating' = '9' - 'Genre' = 'Thriller' - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -HeaderName MovieName, Year, Rating, Genre -DataOnly - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - - $ExpectedResult = @( - [PSCustomObject]@{ - 'MovieName' = 'Movie name' - 'Year' = 'Year' - 'Rating' = 'Rating' - 'Genre' = 'Genre' - 'Country' = $null - } - [PSCustomObject]@{ - 'MovieName' = 'The Bodyguard' - 'Year' = '1982' - 'Rating' = '9' - 'Genre' = 'Thriller' - 'Country' = $null - } - [PSCustomObject]@{ - 'MovieName' = 'The Matrix' - 'Year' = '1999' - 'Rating' = '8' - 'Genre' = 'Sci-Fi' - 'Country' = $null - } - - [PSCustomObject]@{ - 'MovieName' = 'Skyfall' - 'Year' = '2012' - 'Rating' = '9' - 'Genre' = 'Thriller' - 'Country' = $null - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -HeaderName MovieName, Year, Rating, Genre, Country -DataOnly - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - } - it 'HeaderName, DataOnly and StartRow' { - $ExpectedResult = @( - [PSCustomObject]@{ - 'MovieName' = 'The Bodyguard' - 'Year' = '1982' - 'Rating' = '9' - 'Genre' = 'Thriller' - } - [PSCustomObject]@{ - 'MovieName' = 'The Matrix' - 'Year' = '1999' - 'Rating' = '8' - 'Genre' = 'Sci-Fi' - } - - [PSCustomObject]@{ - 'MovieName' = 'Skyfall' - 'Year' = '2012' - 'Rating' = '9' - 'Genre' = 'Thriller' - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -HeaderName MovieName, Year, Rating, Genre -DataOnly -StartRow 2 - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - - $ExpectedResult = @( - [PSCustomObject]@{ - 'MovieName' = 'The Bodyguard' - 'Year' = '1982' - 'Rating' = '9' - 'Genre' = 'Thriller' - 'Country' = $null - } - [PSCustomObject]@{ - 'MovieName' = 'The Matrix' - 'Year' = '1999' - 'Rating' = '8' - 'Genre' = 'Sci-Fi' - 'Country' = $null - } - - [PSCustomObject]@{ - 'MovieName' = 'Skyfall' - 'Year' = '2012' - 'Rating' = '9' - 'Genre' = 'Thriller' - 'Country' = $null - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -HeaderName MovieName, Year, Rating, Genre, Country -DataOnly -StartRow 2 - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - - $ExpectedResult = [PSCustomObject]@{ - 'MovieName' = 'Skyfall' - 'Year' = '2012' - 'Rating' = '9' - 'Genre' = 'Thriller' - 'Country' = $null - } - - $Result = Import-Excel -Path $Path -WorksheetName Test -HeaderName MovieName, Year, Rating, Genre, Country -DataOnly -StartRow 4 - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - - $Result = Import-Excel -Path $Path -WorksheetName Test -HeaderName MovieName, Year, Rating, Genre, Country -DataOnly -StartRow 5 - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - - Import-Excel -Path $Path -WorksheetName Test -HeaderName MovieName, Year, Rating, Genre, Country -DataOnly -StartRow 6 | Should BeNullOrEmpty - } - it 'NoHeader' { - $ExpectedResult = @( - [PSCustomObject]@{ - 'P1' = 'Movie name' - 'P2' = 'Year' - 'P3' = 'Rating' - 'P4' = $null - 'P5' = 'Genre' - } - [PSCustomObject]@{ - 'P1' = 'The Bodyguard' - 'P2' = '1982' - 'P3' = '9' - 'P4' = $null - 'P5' = 'Thriller' - } - [PSCustomObject]@{ - 'P1' = 'The Matrix' - 'P2' = '1999' - 'P3' = '8' - 'P4' = $null - 'P5' = 'Sci-Fi' - } - [PSCustomObject]@{ - 'P1' = $null - 'P2' = $null - 'P3' = $null - 'P4' = $null - 'P5' = $null - } - [PSCustomObject]@{ - 'P1' = 'Skyfall' - 'P2' = '2012' - 'P3' = '9' - 'P4' = $null - 'P5' = 'Thriller' - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -NoHeader - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - } - it 'NoHeader and StartRow' { - $ExpectedResult = @( - [PSCustomObject]@{ - 'P1' = 'The Bodyguard' - 'P2' = '1982' - 'P3' = '9' - 'P4' = $null - 'P5' = 'Thriller' - } - [PSCustomObject]@{ - 'P1' = 'The Matrix' - 'P2' = '1999' - 'P3' = '8' - 'P4' = $null - 'P5' = 'Sci-Fi' - } - [PSCustomObject]@{ - 'P1' = $null - 'P2' = $null - 'P3' = $null - 'P4' = $null - 'P5' = $null - } - [PSCustomObject]@{ - 'P1' = 'Skyfall' - 'P2' = '2012' - 'P3' = '9' - 'P4' = $null - 'P5' = 'Thriller' - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -NoHeader -StartRow 2 - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - - $ExpectedResult = @( - [PSCustomObject]@{ - 'P1' = $null - 'P2' = $null - 'P3' = $null - 'P4' = $null - 'P5' = $null - } - [PSCustomObject]@{ - 'P1' = 'Skyfall' - 'P2' = '2012' - 'P3' = '9' - 'P4' = $null - 'P5' = 'Thriller' - } - ) - $Result = Import-Excel -Path $Path -WorksheetName Test -NoHeader -StartRow 4 - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - - $ExpectedResult = [PSCustomObject]@{ - 'P1' = 'Skyfall' - 'P2' = '2012' - 'P3' = '9' - 'P4' = $null - 'P5' = 'Thriller' - } - - $Result = Import-Excel -Path $Path -WorksheetName Test -NoHeader -StartRow 5 - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - - Import-Excel -Path $Path -WorksheetName Test -NoHeader -StartRow 6 | Should BeNullOrEmpty - } - it 'NoHeader and DataOnly' { - $ExpectedResult = @( - [PSCustomObject]@{ - 'P1' = 'Movie name' - 'P2' = 'Year' - 'P3' = 'Rating' - 'P4' = 'Genre' - } - [PSCustomObject]@{ - 'P1' = 'The Bodyguard' - 'P2' = '1982' - 'P3' = '9' - 'P4' = 'Thriller' - } - [PSCustomObject]@{ - 'P1' = 'The Matrix' - 'P2' = '1999' - 'P3' = '8' - 'P4' = 'Sci-Fi' - } - [PSCustomObject]@{ - 'P1' = 'Skyfall' - 'P2' = '2012' - 'P3' = '9' - 'P4' = 'Thriller' - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -NoHeader -DataOnly - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - } - it 'NoHeader, DataOnly and StartRow' { - $ExpectedResult = @( - [PSCustomObject]@{ - 'P1' = 'The Bodyguard' - 'P2' = '1982' - 'P3' = '9' - 'P4' = 'Thriller' - } - [PSCustomObject]@{ - 'P1' = 'The Matrix' - 'P2' = '1999' - 'P3' = '8' - 'P4' = 'Sci-Fi' - } - [PSCustomObject]@{ - 'P1' = 'Skyfall' - 'P2' = '2012' - 'P3' = '9' - 'P4' = 'Thriller' - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -NoHeader -DataOnly -StartRow 2 - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - - $ExpectedResult = [PSCustomObject]@{ - 'P1' = 'Skyfall' - 'P2' = '2012' - 'P3' = '9' - 'P4' = 'Thriller' - } - - $Result = Import-Excel -Path $Path -WorksheetName Test -NoHeader -DataOnly -StartRow 4 - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - - $Result = Import-Excel -Path $Path -WorksheetName Test -NoHeader -DataOnly -StartRow 5 - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - - Import-Excel -Path $Path -WorksheetName Test -NoHeader -DataOnly -StartRow 6 | Should BeNullOrEmpty - } - } - Describe 'blank rows and columns with missing headers' { - - #region Create test file - - # --------------------------------------------------------------------------------------------------- - # | A B C D E F G | - # |1 Movie name Rating Director | - # |2 The Bodyguard 9 Thriller Mick Jackson | - # |3 The Matrix 1999 8 Wachowski | - # |4 | - # |5 Skyfall 2012 9 Thriller Sam Mendes | - # |6 10 | - # --------------------------------------------------------------------------------------------------- - - $Path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path) - $Excel = New-Object OfficeOpenXml.ExcelPackage $Path - $Worksheet = $Excel | Add-WorkSheet -WorkSheetname Test - - # Row, Column - $Worksheet.Cells[1, 1].Value = 'Movie name' - $Worksheet.Cells[1, 3].Value = 'Rating' - $Worksheet.Cells[1, 7].Value = 'Director' - $Worksheet.Cells[2, 1].Value = 'The Bodyguard' - $Worksheet.Cells[2, 3].Value = '9' - $Worksheet.Cells[2, 5].Value = 'Thriller' - $Worksheet.Cells[2, 7].Value = 'Mick Jackson' - $Worksheet.Cells[3, 1].Value = 'The Matrix' - $Worksheet.Cells[3, 2].Value = '1999' - $Worksheet.Cells[3, 3].Value = '8' - $Worksheet.Cells[3, 7].Value = 'Wachowski' - $Worksheet.Cells[5, 1].Value = 'Skyfall' - $Worksheet.Cells[5, 2].Value = '2012' - $Worksheet.Cells[5, 3].Value = '9' - $Worksheet.Cells[5, 5].Value = 'Thriller' - $Worksheet.Cells[5, 7].Value = 'Sam Mendes' - $Worksheet.Cells[6, 3].Value = '10' - - $Excel.Save() - $Excel.Dispose() - #endregion - - it 'Default' { - $ExpectedResult = @( - [PSCustomObject]@{ - 'Movie name' = 'The Bodyguard' - 'Rating' = '9' - 'Director' = 'Mick Jackson' - } - [PSCustomObject]@{ - 'Movie name' = 'The Matrix' - 'Rating' = '8' - 'Director' = 'Wachowski' - } - [PSCustomObject]@{ - 'Movie name' = $null - 'Rating' = $null - 'Director' = $null - } - [PSCustomObject]@{ - 'Movie name' = 'Skyfall' - 'Rating' = '9' - 'Director' = 'Sam Mendes' - } - [PSCustomObject]@{ - 'Movie name' = $null - 'Rating' = '10' - 'Director' = $null - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - } - it 'Default and StartRow' { - $ExpectedResult = @( - [PSCustomObject]@{ - 'The Bodyguard' = 'The Matrix' - '9' = '8' - 'Thriller' = $null - 'Mick Jackson' = 'Wachowski' - } - [PSCustomObject]@{ - 'The Bodyguard' = $null - '9' = $null - 'Thriller' = $null - 'Mick Jackson' = $null - } - [PSCustomObject]@{ - 'The Bodyguard' = 'Skyfall' - '9' = '9' - 'Thriller' = 'Thriller' - 'Mick Jackson' = 'Sam Mendes' - } - [PSCustomObject]@{ - 'The Bodyguard' = $null - '9' = '10' - 'Thriller' = $null - 'Mick Jackson' = $null - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -StartRow 2 - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - } - it 'Default and DataOnly' { - $ExpectedResult = @( - [PSCustomObject]@{ - 'Movie name' = 'The Bodyguard' - 'Rating' = '9' - 'Director' = 'Mick Jackson' - } - [PSCustomObject]@{ - 'Movie name' = 'The Matrix' - 'Rating' = '8' - 'Director' = 'Wachowski' - } - [PSCustomObject]@{ - 'Movie name' = 'Skyfall' - 'Rating' = '9' - 'Director' = 'Sam Mendes' - } - [PSCustomObject]@{ - 'Movie name' = $null - 'Rating' = '10' - 'Director' = $null - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -DataOnly - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - } - it 'Default, DataOnly and StartRow' { - $ExpectedResult = @( - [PSCustomObject]@{ - 'The Bodyguard' = 'The Matrix' - '9' = '8' - 'Thriller' = $null - 'Mick Jackson' = 'Wachowski' - } - [PSCustomObject]@{ - 'The Bodyguard' = 'Skyfall' - '9' = '9' - 'Thriller' = 'Thriller' - 'Mick Jackson' = 'Sam Mendes' - } - [PSCustomObject]@{ - 'The Bodyguard' = $null - '9' = '10' - 'Thriller' = $null - 'Mick Jackson' = $null - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -DataOnly -StartRow 2 - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - } - it 'HeaderName' { - $ExpectedResult = @( - [PSCustomObject]@{ - 'MovieName' = 'Movie name' - 'Year' = $null - 'Rating' = 'Rating' - 'Genre' = $null - } - [PSCustomObject]@{ - 'MovieName' = 'The Bodyguard' - 'Year' = $null - 'Rating' = '9' - 'Genre' = $null - } - [PSCustomObject]@{ - 'MovieName' = 'The Matrix' - 'Year' = '1999' - 'Rating' = '8' - 'Genre' = $null - } - [PSCustomObject]@{ - 'MovieName' = $null - 'Year' = $null - 'Rating' = $null - 'Genre' = $null - } - [PSCustomObject]@{ - 'MovieName' = 'Skyfall' - 'Year' = '2012' - 'Rating' = '9' - 'Genre' = $null - } - [PSCustomObject]@{ - 'MovieName' = $null - 'Year' = $null - 'Rating' = '10' - 'Genre' = $null - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -HeaderName MovieName, Year, Rating, Genre - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - - $ExpectedResult = @( - [PSCustomObject]@{ - 'MovieName' = 'Movie name' - 'Year' = $null - 'Rating' = 'Rating' - 'Genre' = $null - 'Country' = $null - } - [PSCustomObject]@{ - 'MovieName' = 'The Bodyguard' - 'Year' = $null - 'Rating' = '9' - 'Genre' = $null - 'Country' = 'Thriller' - } - [PSCustomObject]@{ - 'MovieName' = 'The Matrix' - 'Year' = '1999' - 'Rating' = '8' - 'Genre' = $null - 'Country' = $null - } - [PSCustomObject]@{ - 'MovieName' = $null - 'Year' = $null - 'Rating' = $null - 'Genre' = $null - 'Country' = $null - } - [PSCustomObject]@{ - 'MovieName' = 'Skyfall' - 'Year' = '2012' - 'Rating' = '9' - 'Genre' = $null - 'Country' = 'Thriller' - } - [PSCustomObject]@{ - 'MovieName' = $null - 'Year' = $null - 'Rating' = '10' - 'Genre' = $null - 'Country' = $null - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -HeaderName MovieName, Year, Rating, Genre, Country - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - } - it 'HeaderName and StartRow' { - $ExpectedResult = @( - [PSCustomObject]@{ - 'MovieName' = 'The Bodyguard' - 'Year' = $null - 'Rating' = '9' - 'Genre' = $null - } - [PSCustomObject]@{ - 'MovieName' = 'The Matrix' - 'Year' = '1999' - 'Rating' = '8' - 'Genre' = $null - } - [PSCustomObject]@{ - 'MovieName' = $null - 'Year' = $null - 'Rating' = $null - 'Genre' = $null - } - [PSCustomObject]@{ - 'MovieName' = 'Skyfall' - 'Year' = '2012' - 'Rating' = '9' - 'Genre' = $null - } - [PSCustomObject]@{ - 'MovieName' = $null - 'Year' = $null - 'Rating' = '10' - 'Genre' = $null - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -HeaderName MovieName, Year, Rating, Genre -StartRow 2 - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - - $ExpectedResult = @( - [PSCustomObject]@{ - 'MovieName' = 'The Bodyguard' - 'Year' = $null - 'Rating' = '9' - 'Genre' = $null - 'Country' = 'Thriller' - } - [PSCustomObject]@{ - 'MovieName' = 'The Matrix' - 'Year' = '1999' - 'Rating' = '8' - 'Genre' = $null - 'Country' = $null - } - [PSCustomObject]@{ - 'MovieName' = $null - 'Year' = $null - 'Rating' = $null - 'Genre' = $null - 'Country' = $null - } - [PSCustomObject]@{ - 'MovieName' = 'Skyfall' - 'Year' = '2012' - 'Rating' = '9' - 'Genre' = $null - 'Country' = 'Thriller' - } - [PSCustomObject]@{ - 'MovieName' = $null - 'Year' = $null - 'Rating' = '10' - 'Genre' = $null - 'Country' = $null - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -HeaderName MovieName, Year, Rating, Genre, Country -StartRow 2 - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - } - it 'HeaderName and DataOnly' { - $ExpectedResult = @( - [PSCustomObject]@{ - 'MovieName' = 'Movie name' - 'Year' = $null - 'Rating' = 'Rating' - 'Genre' = $null - } - [PSCustomObject]@{ - 'MovieName' = 'The Bodyguard' - 'Year' = $null - 'Rating' = '9' - 'Genre' = 'Thriller' - } - [PSCustomObject]@{ - 'MovieName' = 'The Matrix' - 'Year' = '1999' - 'Rating' = '8' - 'Genre' = $null - } - [PSCustomObject]@{ - 'MovieName' = 'Skyfall' - 'Year' = '2012' - 'Rating' = '9' - 'Genre' = 'Thriller' - } - [PSCustomObject]@{ - 'MovieName' = $null - 'Year' = $null - 'Rating' = '10' - 'Genre' = $null - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -HeaderName MovieName, Year, Rating, Genre -DataOnly - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - - $ExpectedResult = @( - [PSCustomObject]@{ - 'MovieName' = 'Movie name' - 'Year' = $null - 'Rating' = 'Rating' - 'Genre' = $null - 'Country' = 'Director' - } - [PSCustomObject]@{ - 'MovieName' = 'The Bodyguard' - 'Year' = $null - 'Rating' = '9' - 'Genre' = 'Thriller' - 'Country' = 'Mick Jackson' - } - [PSCustomObject]@{ - 'MovieName' = 'The Matrix' - 'Year' = '1999' - 'Rating' = '8' - 'Genre' = $null - 'Country' = 'Wachowski' - } - [PSCustomObject]@{ - 'MovieName' = 'Skyfall' - 'Year' = '2012' - 'Rating' = '9' - 'Genre' = 'Thriller' - 'Country' = 'Sam Mendes' - } - [PSCustomObject]@{ - 'MovieName' = $null - 'Year' = $null - 'Rating' = '10' - 'Genre' = $null - 'Country' = $null - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -HeaderName MovieName, Year, Rating, Genre, Country -DataOnly - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - } - it 'HeaderName, DataOnly and StartRow' { - $ExpectedResult = @( - [PSCustomObject]@{ - 'MovieName' = 'The Bodyguard' - 'Year' = $null - 'Rating' = '9' - 'Genre' = 'Thriller' - } - [PSCustomObject]@{ - 'MovieName' = 'The Matrix' - 'Year' = '1999' - 'Rating' = '8' - 'Genre' = $null - } - [PSCustomObject]@{ - 'MovieName' = 'Skyfall' - 'Year' = '2012' - 'Rating' = '9' - 'Genre' = 'Thriller' - } - [PSCustomObject]@{ - 'MovieName' = $null - 'Year' = $null - 'Rating' = '10' - 'Genre' = $null - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -HeaderName MovieName, Year, Rating, Genre -DataOnly -StartRow 2 - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - - $ExpectedResult = @( - [PSCustomObject]@{ - 'MovieName' = 'The Bodyguard' - 'Year' = $null - 'Rating' = '9' - 'Genre' = 'Thriller' - 'Country' = 'Mick Jackson' - } - [PSCustomObject]@{ - 'MovieName' = 'The Matrix' - 'Year' = '1999' - 'Rating' = '8' - 'Genre' = $null - 'Country' = 'Wachowski' - } - [PSCustomObject]@{ - 'MovieName' = 'Skyfall' - 'Year' = '2012' - 'Rating' = '9' - 'Genre' = 'Thriller' - 'Country' = 'Sam Mendes' - } - [PSCustomObject]@{ - 'MovieName' = $null - 'Year' = $null - 'Rating' = '10' - 'Genre' = $null - 'Country' = $null - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -HeaderName MovieName, Year, Rating, Genre, Country -DataOnly -StartRow 2 - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - } - it 'NoHeader' { - $ExpectedResult = @( - [PSCustomObject]@{ - 'P1' = 'Movie name' - 'P2' = $null - 'P3' = 'Rating' - 'P4' = $null - 'P5' = $null - 'P6' = $null - 'P7' = 'Director' - } - [PSCustomObject]@{ - 'P1' = 'The Bodyguard' - 'P2' = $null - 'P3' = '9' - 'P4' = $null - 'P5' = 'Thriller' - 'P6' = $null - 'P7' = 'Mick Jackson' - } - [PSCustomObject]@{ - 'P1' = 'The Matrix' - 'P2' = '1999' - 'P3' = '8' - 'P4' = $null - 'P5' = $null - 'P6' = $null - 'P7' = 'Wachowski' - } - [PSCustomObject]@{ - 'P1' = $null - 'P2' = $null - 'P3' = $null - 'P4' = $null - 'P5' = $null - 'P6' = $null - 'P7' = $null - } - [PSCustomObject]@{ - 'P1' = 'Skyfall' - 'P2' = '2012' - 'P3' = '9' - 'P4' = $null - 'P5' = 'Thriller' - 'P6' = $null - 'P7' = 'Sam Mendes' - } - [PSCustomObject]@{ - 'P1' = $null - 'P2' = $null - 'P3' = '10' - 'P4' = $null - 'P5' = $null - 'P6' = $null - 'P7' = $null - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -NoHeader - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - } - it 'NoHeader and StartRow' { - $ExpectedResult = @( - [PSCustomObject]@{ - 'P1' = 'The Bodyguard' - 'P2' = $null - 'P3' = '9' - 'P4' = $null - 'P5' = 'Thriller' - 'P6' = $null - 'P7' = 'Mick Jackson' - } - [PSCustomObject]@{ - 'P1' = 'The Matrix' - 'P2' = '1999' - 'P3' = '8' - 'P4' = $null - 'P5' = $null - 'P6' = $null - 'P7' = 'Wachowski' - } - [PSCustomObject]@{ - 'P1' = $null - 'P2' = $null - 'P3' = $null - 'P4' = $null - 'P5' = $null - 'P6' = $null - 'P7' = $null - } - [PSCustomObject]@{ - 'P1' = 'Skyfall' - 'P2' = '2012' - 'P3' = '9' - 'P4' = $null - 'P5' = 'Thriller' - 'P6' = $null - 'P7' = 'Sam Mendes' - } - [PSCustomObject]@{ - 'P1' = $null - 'P2' = $null - 'P3' = '10' - 'P4' = $null - 'P5' = $null - 'P6' = $null - 'P7' = $null - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -NoHeader -StartRow 2 - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - } - it 'NoHeader and DataOnly' { - $ExpectedResult = @( - [PSCustomObject]@{ - 'P1' = 'Movie name' - 'P2' = $null - 'P3' = 'Rating' - 'P4' = $null - 'P5' = 'Director' - } - [PSCustomObject]@{ - 'P1' = 'The Bodyguard' - 'P2' = $null - 'P3' = '9' - 'P4' = 'Thriller' - 'P5' = 'Mick Jackson' - } - [PSCustomObject]@{ - 'P1' = 'The Matrix' - 'P2' = '1999' - 'P3' = '8' - 'P4' = $null - 'P5' = 'Wachowski' - } - [PSCustomObject]@{ - 'P1' = 'Skyfall' - 'P2' = '2012' - 'P3' = '9' - 'P4' = 'Thriller' - 'P5' = 'Sam Mendes' - } - [PSCustomObject]@{ - 'P1' = $null - 'P2' = $null - 'P3' = '10' - 'P4' = $null - 'P5' = $null - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -NoHeader -DataOnly - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - } - it 'NoHeader, DataOnly and StartRow' { - $ExpectedResult = @( - [PSCustomObject]@{ - 'P1' = 'The Bodyguard' - 'P2' = $null - 'P3' = '9' - 'P4' = 'Thriller' - 'P5' = 'Mick Jackson' - } - [PSCustomObject]@{ - 'P1' = 'The Matrix' - 'P2' = '1999' - 'P3' = '8' - 'P4' = $null - 'P5' = 'Wachowski' - } - [PSCustomObject]@{ - 'P1' = 'Skyfall' - 'P2' = '2012' - 'P3' = '9' - 'P4' = 'Thriller' - 'P5' = 'Sam Mendes' - } - [PSCustomObject]@{ - 'P1' = $null - 'P2' = $null - 'P3' = '10' - 'P4' = $null - 'P5' = $null - } - ) - - $Result = Import-Excel -Path $Path -WorksheetName Test -NoHeader -DataOnly -StartRow 2 - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - } - } - } -} -#> -Context 'special cases' { - in $TestDrive { - #<# - Describe 'duplicate column headers' { - it 'worksheet' { - #region Create test file - - # ---------------------------------------------- - # | A B C | - # |1 First Name first name Address | - # |2 Chuck Norris California | - # |3 Jean-Claude Vandamme Brussels | - # ---------------------------------------------- - - $Path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path) - $Excel = New-Object OfficeOpenXml.ExcelPackage $Path - $Worksheet = $Excel | Add-WorkSheet -WorkSheetname Test - - # Row, Column - $Worksheet.Cells[1, 1].Value = 'First Name' - $Worksheet.Cells[1, 2].Value = 'first name' - $Worksheet.Cells[1, 3].Value = 'Address' - $Worksheet.Cells[2, 1].Value = 'Chuck' - $Worksheet.Cells[2, 2].Value = 'Norris' - $Worksheet.Cells[2, 3].Value = 'California' - $Worksheet.Cells[3, 1].Value = 'Jean-Claude' - $Worksheet.Cells[3, 2].Value = 'Vandamme' - $Worksheet.Cells[3, 3].Value = 'Brussels' - - $Excel.Save() - $Excel.Dispose() - #endregion - - {Import-Excel -Path $Path -WorksheetName Test} | Should Throw 'Duplicate column headers found' - - #region Create test file - Remove-Item .\* -Force - - # ---------------------------------------------- - # | A B C | - # |1 | - # |2 Fruit Fruit Color | - # |3 Kiwi Green | - # ---------------------------------------------- - - $Path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path) - $Excel = New-Object OfficeOpenXml.ExcelPackage $Path - $Worksheet = $Excel | Add-WorkSheet -WorkSheetname Test - - # Row, Column - $Worksheet.Cells[2, 1].Value = 'Fruit' - $Worksheet.Cells[2, 2].Value = 'Fruit' - $Worksheet.Cells[2, 3].Value = 'Color' - $Worksheet.Cells[3, 1].Value = 'Kiwi' - $Worksheet.Cells[3, 3].Value = 'Green' - - $Excel.Save() - $Excel.Dispose() - #endregion - - {Import-Excel -Path $Path -WorksheetName Test -StartRow 2} | Should Throw 'Duplicate column headers found' - } - it 'HeaderName parameter' { - {Import-Excel -Path $Path -WorksheetName Test -HeaderName Apples, Apples, Kiwi} | Should Throw 'Duplicate column headers found' - } - } - #> - Describe 'open password protected files' { - $Password = 'P@ssw0rd' - - #region Create password protected file - - # ---------------- - # | A | - # |1 Type | - # |2 Sensitive | - # ---------------- - - $Path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path) - $Excel = New-Object OfficeOpenXml.ExcelPackage $Path - - # Row, Column - $Worksheet = $Excel | Add-WorkSheet -WorkSheetname Test - $Worksheet.Cells[1, 1].Value = 'Type' - $Worksheet.Cells[2, 1].Value = 'Sensitive' - - $Excel.Save($Password) - $Excel.Dispose() - #endregion - - it 'password correct' { - $Result = Import-Excel -Path $Path -WorksheetName Test -Password $Password - - $ExpectedResult = [PSCustomObject]@{ - Type = 'Sensitive' - } - Assert-Equivalent -Actual $Result -Expected $ExpectedResult - } - it 'password wrong' { - {Import-Excel -Path $Path -WorksheetName Test -Password WrongPassword} | Should Throw 'Password' - } - } - } -} - -Context 'General Tests' { - in $TestDrive { - Describe 'Get Help' { - it 'New-Plot' { - #Get-Help : Unable to find type [PSPlot]. - {Help New-Plot} | Should -Not -Throw - } - } - } -} \ No newline at end of file diff --git a/Old_Export-Excel.Tests.ps1 b/Old_Export-Excel.Tests.ps1 deleted file mode 100644 index 62db210..0000000 --- a/Old_Export-Excel.Tests.ps1 +++ /dev/null @@ -1,94 +0,0 @@ -#Requires -Modules Pester - - -$here = Split-Path -Parent $MyInvocation.MyCommand.Path - - -Import-Module $here -Force - -$WarningPreference = 'SilentlyContinue' -$ProgressPreference = 'SilentlyContinue' - -Function Test-isNumeric { - Param ( - [Parameter(ValueFromPipeline)]$x - ) - - Return $x -is [byte] -or $x -is [int16] -or $x -is [int32] -or $x -is [int64] ` - -or $x -is [sbyte] -or $x -is [uint16] -or $x -is [uint32] -or $x -is [uint64] ` - -or $x -is [float] -or $x -is [double] -or $x -is [decimal] -} - -$fakeData = [PSCustOmobject]@{ - Property_1_Date = (Get-Date).ToString('d') # US '10/16/2017' BE '16/10/2107' - Property_2_Formula = '=SUM(G2:H2)' - Property_3_String = 'My String' - Property_4_String = 'a' - Property_5_IPAddress = '10.10.25.5' - Property_6_Number = '0' - Property_7_Number = '5' - Property_8_Number = '007' - Property_9_Number = (33).ToString('F2') # US '33.00' BE '33,00' - Property_10_Number = (5/3).ToString('F2') # US '1.67' BE '1,67' - Property_11_Number = (15999998/3).ToString('N2') # US '5,333,332.67' BE '5.333.332,67' - Property_12_Number = '1.555,83' - Property_13_PhoneNr = '+32 44' - Property_14_PhoneNr = '+32 4 4444 444' - Property_15_PhoneNr = '+3244444444' -} - -$Path = 'Test.xlsx' - -Describe 'Export-Excel' { - in $TestDrive { - Describe 'Number conversion' { - Context 'numerical values expected' { - #region Create test file - $fakeData | Export-Excel -Path $Path - - $Path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path) - $Excel = New-Object OfficeOpenXml.ExcelPackage $Path - $Worksheet = $Excel.Workbook.WorkSheets[1] - #endregion - - it 'zero' { - $fakeData.Property_6_Number | Should -BeExactly '0' - $Worksheet.Cells[2, 6].Text | Should -BeExactly $fakeData.Property_6_Number - $Worksheet.Cells[2, 6].Value | Test-isNumeric | Should -Be $true - } - - It 'regular number' { - $fakeData.Property_7_Number | Should -BeExactly '5' - $Worksheet.Cells[2, 7].Text | Should -BeExactly $fakeData.Property_7_Number - $Worksheet.Cells[2, 7].Value | Test-isNumeric | Should -Be $true - } - - It 'number starting with zero' { - $fakeData.Property_8_Number | Should -BeExactly '007' - $Worksheet.Cells[2, 8].Text | Should -BeExactly '7' - $Worksheet.Cells[2, 8].Value | Test-isNumeric | Should -Be $true - } - - It 'decimal number' { - # US '33.00' BE '33,00' - $fakeData.Property_9_Number | Should -BeExactly (33).ToString('F2') - $Worksheet.Cells[2, 9].Text | Should -BeExactly '33' - $Worksheet.Cells[2, 9].Value | Test-isNumeric | Should -Be $true - - # US '1.67' BE '1,67' - $fakeData.Property_10_Number | Should -BeExactly (5/3).ToString('F2') - $Worksheet.Cells[2, 10].Text | Should -BeExactly $fakeData.Property_10_Number - $Worksheet.Cells[2, 10].Value | Test-isNumeric | Should -Be $true - } - - It 'thousand seperator and decimal number' { - # US '5,333,332.67' BE '5.333.332,67' - # Excel BE '5333332,67' - $fakeData.Property_11_Number | Should -BeExactly (15999998/3).ToString('N2') - $Worksheet.Cells[2, 11].Text | Should -BeExactly $fakeData.Property_11_Number - $Worksheet.Cells[2, 11].Value | Test-isNumeric | Should -Be $true - } - } - } - } -} \ No newline at end of file