From 5a235e309ee2e58e5c02e311fee330771bb810b5 Mon Sep 17 00:00:00 2001 From: jhoneill Date: Tue, 7 Aug 2018 15:00:33 +0100 Subject: [PATCH] Tider errors for file open on import; password tests --- ImportExcel.psm1 | 33 +++++++++---------- __tests__/PasswordProtection.ps1 | 24 ++++++++++++++ .../Set-Row_Set-Column-SetFormat.tests.ps1 | 2 +- 3 files changed, 40 insertions(+), 19 deletions(-) create mode 100644 __tests__/PasswordProtection.ps1 diff --git a/ImportExcel.psm1 b/ImportExcel.psm1 index 44d2170..ca38aa0 100644 --- a/ImportExcel.psm1 +++ b/ImportExcel.psm1 @@ -321,28 +321,25 @@ function Import-Excel { } Process { - Try { - #region Open file + #region Open file + try { $Path = (Resolve-Path $Path).ProviderPath Write-Verbose "Import Excel workbook '$Path' with worksheet '$Worksheetname'" - $Stream = New-Object -TypeName System.IO.FileStream -ArgumentList $Path, 'Open', 'Read', 'ReadWrite' + } + Catch {throw "Could not open $Path ; $_ "} - if ($Password) { - $Excel = New-Object -TypeName OfficeOpenXml.ExcelPackage - - Try { - $Excel.Load($Stream,$Password) - } - Catch { - throw "Password '$Password' is not correct." - } - } - else { - $Excel = New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $Stream - } - #endregion - + if ($Password) { + Try {$Excel = New-Object -TypeName OfficeOpenXml.ExcelPackage + $excel.Load( $Stream,$Password)} + Catch { throw "Could not read $Path with the provided password." } + } + else { + try {$Excel = New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $Stream} + Catch {throw "Failed to read $Path"} + } + #endregion + Try { #region Select worksheet if ($WorksheetName) { if (-not ($Worksheet = $Excel.Workbook.Worksheets[$WorkSheetName])) { diff --git a/__tests__/PasswordProtection.ps1 b/__tests__/PasswordProtection.ps1 new file mode 100644 index 0000000..47d5189 --- /dev/null +++ b/__tests__/PasswordProtection.ps1 @@ -0,0 +1,24 @@ +Describe "Password Support" { + Context "Password protected sheet" { + BeforeAll { + $password = "YouMustRememberThis" + $path = "$env:TEMP\Test.xlsx" + Remove-Item $path -ErrorAction SilentlyContinue + Get-Service | Select-Object -First 10 | Export-excel -password $password -Path $Path -DisplayPropertySet + } + it "Threw an error when the password was omitted " { + {Open-ExcelPackage -Path $path } | should throw + } + it "Was able to append when the password was included " { + {Get-Service | Select-Object -First 10 | + Export-excel -password $password -Path $Path -Append } | should not throw + } + it "Kept the password on the file when it was saved " { + {Import-Excel $Path } | should throw + } + it "Could read the file when the password was included " { + (import-excel $path -Password $password).count | should be 20 + } + } +} + \ No newline at end of file diff --git a/__tests__/Set-Row_Set-Column-SetFormat.tests.ps1 b/__tests__/Set-Row_Set-Column-SetFormat.tests.ps1 index e741dd2..62635b4 100644 --- a/__tests__/Set-Row_Set-Column-SetFormat.tests.ps1 +++ b/__tests__/Set-Row_Set-Column-SetFormat.tests.ps1 @@ -55,7 +55,7 @@ Describe "Number format expansion and setting" { Remove-Item -Path $path -ErrorAction SilentlyContinue $n = [datetime]::Now.ToOADate() - $excel = 1..32 | ForEach-Object {$n} | Export-Excel -Path $path -PassThru + $excel = 1..32 | ForEach-Object {$n} | Export-Excel -Path $path -show -WorksheetName s2 -PassThru $ws = $excel.Workbook.Worksheets[1] Set-Format -WorkSheet $ws -Range "A1" -numberFormat 'General' Set-Format -WorkSheet $ws -Range "A2" -numberFormat 'Number'