mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-10 21:33:16 +00:00
Add try/catch, warning, and tests
This commit is contained in:
@@ -1,10 +1,10 @@
|
|||||||
function Open-ExcelPackage {
|
function Open-ExcelPackage {
|
||||||
[CmdLetBinding()]
|
[CmdLetBinding()]
|
||||||
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword","")]
|
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword", "")]
|
||||||
[OutputType([OfficeOpenXml.ExcelPackage])]
|
[OutputType([OfficeOpenXml.ExcelPackage])]
|
||||||
param(
|
param(
|
||||||
#The path to the file to open.
|
#The path to the file to open.
|
||||||
[Parameter(Mandatory=$true)]$Path,
|
[Parameter(Mandatory = $true)]$Path,
|
||||||
#If specified, any running instances of Excel will be terminated before opening the file.
|
#If specified, any running instances of Excel will be terminated before opening the file.
|
||||||
[switch]$KillExcel,
|
[switch]$KillExcel,
|
||||||
#The password for a protected worksheet, as a [normal] string (not a secure string).
|
#The password for a protected worksheet, as a [normal] string (not a secure string).
|
||||||
@@ -13,7 +13,7 @@
|
|||||||
[switch]$Create
|
[switch]$Create
|
||||||
)
|
)
|
||||||
|
|
||||||
if($KillExcel) {
|
if ($KillExcel) {
|
||||||
Get-Process -Name "excel" -ErrorAction Ignore | Stop-Process
|
Get-Process -Name "excel" -ErrorAction Ignore | Stop-Process
|
||||||
while (Get-Process -Name "excel" -ErrorAction Ignore) {}
|
while (Get-Process -Name "excel" -ErrorAction Ignore) {}
|
||||||
}
|
}
|
||||||
@@ -24,21 +24,26 @@
|
|||||||
#Create the directory if required.
|
#Create the directory if required.
|
||||||
$targetPath = Split-Path -Parent -Path $Path
|
$targetPath = Split-Path -Parent -Path $Path
|
||||||
if (!(Test-Path -Path $targetPath)) {
|
if (!(Test-Path -Path $targetPath)) {
|
||||||
Write-Debug "Base path $($targetPath) does not exist, creating"
|
Write-Debug "Base path $($targetPath) does not exist, creating"
|
||||||
$null = New-item -ItemType Directory -Path $targetPath -ErrorAction Ignore
|
$null = New-item -ItemType Directory -Path $targetPath -ErrorAction Ignore
|
||||||
}
|
}
|
||||||
New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $Path
|
New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $Path
|
||||||
}
|
}
|
||||||
elseif (Test-Path -Path $path) {
|
elseif (Test-Path -Path $path) {
|
||||||
if ($Password) {$pkgobj = New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $Path , $Password }
|
if ($Password) { $pkgobj = New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $Path , $Password }
|
||||||
else {$pkgobj = New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $Path }
|
else { $pkgobj = New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $Path }
|
||||||
if ($pkgobj) {
|
if ($pkgobj) {
|
||||||
foreach ($w in $pkgobj.Workbook.Worksheets) {
|
foreach ($w in $pkgobj.Workbook.Worksheets) {
|
||||||
$sb = [scriptblock]::Create(('$this.workbook.Worksheets["{0}"]' -f $w.name))
|
$sb = [scriptblock]::Create(('$this.workbook.Worksheets["{0}"]' -f $w.name))
|
||||||
Add-Member -InputObject $pkgobj -MemberType ScriptProperty -Name $w.name -Value $sb
|
try {
|
||||||
|
Add-Member -InputObject $pkgobj -MemberType ScriptProperty -Name $w.name -Value $sb -ErrorAction Stop
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
Write-Warning "Could not add sheet $($w.name) as 'short cut', you need to access it via `$wb.Worksheets['$($w.name)'] "
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return $pkgobj
|
return $pkgobj
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {Write-Warning "Could not find $path" }
|
else { Write-Warning "Could not find $path" }
|
||||||
}
|
}
|
||||||
|
|||||||
39
__tests__/Open-ExcelPackage.tests.ps1
Normal file
39
__tests__/Open-ExcelPackage.tests.ps1
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
#Requires -Modules Pester
|
||||||
|
|
||||||
|
if (-not (Get-command Import-Excel -ErrorAction SilentlyContinue)) {
|
||||||
|
Import-Module $PSScriptRoot\..\ImportExcel.psd1
|
||||||
|
}
|
||||||
|
|
||||||
|
<#
|
||||||
|
Methods
|
||||||
|
-------
|
||||||
|
Dispose
|
||||||
|
Equals
|
||||||
|
GetAsByteArray
|
||||||
|
GetHashCode
|
||||||
|
GetType
|
||||||
|
Load
|
||||||
|
Save
|
||||||
|
SaveAs
|
||||||
|
ToString
|
||||||
|
|
||||||
|
Properties
|
||||||
|
----------
|
||||||
|
|
||||||
|
Compatibility
|
||||||
|
Compression
|
||||||
|
DoAdjustDrawings
|
||||||
|
Encryption
|
||||||
|
File
|
||||||
|
Package
|
||||||
|
Stream
|
||||||
|
Workbook
|
||||||
|
#>
|
||||||
|
|
||||||
|
Describe "Test Open Excel Package" -Tag Open-ExcelPackage {
|
||||||
|
It "Should handle opening a workbook with Worksheet Names that will cause errors" {
|
||||||
|
$xlFilename = "$PSScriptRoot\UnsupportedWorkSheetNames.xlsx"
|
||||||
|
|
||||||
|
{ Open-ExcelPackage -Path $xlFilename -ErrorAction Stop } | Should -Not -Throw
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
__tests__/UnsupportedWorkSheetNames.xlsx
Normal file
BIN
__tests__/UnsupportedWorkSheetNames.xlsx
Normal file
Binary file not shown.
Reference in New Issue
Block a user