From af77580b5e0b85ec282665d13566a5a669c77bc5 Mon Sep 17 00:00:00 2001 From: jhoneill Date: Mon, 6 Aug 2018 14:30:10 +0100 Subject: [PATCH] Password support fixes --- Export-Excel.ps1 | 5 +++-- Open-ExcelPackage.ps1 | 30 ++++++++++++++++++++++++------ README.md | 1 + 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/Export-Excel.ps1 b/Export-Excel.ps1 index 4771b09..c3d6b29 100644 --- a/Export-Excel.ps1 +++ b/Export-Excel.ps1 @@ -555,7 +555,7 @@ $pkg = $ExcelPackage $Path = $pkg.File } - Else { $pkg = Open-ExcelPackage -Path $Path -Create -KillExcel:$KillExcel} + Else { $pkg = Open-ExcelPackage -Path $Path -Create -KillExcel:$KillExcel -Password:$Password} $params = @{WorksheetName=$WorksheetName} if ($NoClobber) {Write-Warning -Message "-NoClobber parameter is no longer used" } @@ -944,7 +944,8 @@ else { if ($ReturnRange) {$dataRange } - $pkg.Save() + if ($Password) { $pkg.Save($Password) } + else { $pkg.Save() } Write-Verbose -Message "Saved workbook $($pkg.File)" if ($ReZip) { Write-Verbose -Message "Re-Zipping $($pkg.file) using .NET ZIP library" diff --git a/Open-ExcelPackage.ps1 b/Open-ExcelPackage.ps1 index da81907..265f25d 100644 --- a/Open-ExcelPackage.ps1 +++ b/Open-ExcelPackage.ps1 @@ -10,12 +10,15 @@ This will open the file at $xlPath, select sheet1 apply formatting to two blocks of the sheet and save the package, and launch it in Excel. #> + [CmdLetBinding()] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword","")] [OutputType([OfficeOpenXml.ExcelPackage])] Param ( #The Path to the file to open [Parameter(Mandatory=$true)]$Path, #If specified, any running instances of Excel will be terminated before opening the file. [switch]$KillExcel, + [String]$Password, #By default open only opens an existing file; -Create instructs it to create a new file if required. [switch]$Create ) @@ -27,7 +30,7 @@ $Path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path) #If -Create was not specified only open the file if it exists already (send a warning if it doesn't exist). - if ($Create) { + if ($Create -and -not (Test-Path -Path $path)) { #Create the directory if required. $targetPath = Split-Path -Parent -Path $Path if (!(Test-Path -Path $targetPath)) { @@ -36,7 +39,10 @@ } New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $Path } - elseif (Test-Path -Path $path) {New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $Path } + elseif (Test-Path -Path $path) { + if ($Password) {New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $Path , $Password } + else {New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $Path } + } else {Write-Warning "Could not find $path" } } @@ -45,6 +51,8 @@ Function Close-ExcelPackage { .Synopsis Closes an Excel Package, saving, saving under a new name or abandoning changes and opening the file in Excel as required. #> + [CmdLetBinding()] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword","")] Param ( #File to close. [parameter(Mandatory=$true, ValueFromPipeline=$true)] @@ -54,14 +62,24 @@ Function Close-ExcelPackage { #Abandon the file without saving. [Switch]$NoSave, #Save file with a new name (ignored if -NoSave Specified). - $SaveAs + $SaveAs, + [ValidateNotNullOrEmpty()] + [String]$Password ) if ( $NoSave) {$ExcelPackage.Dispose()} else { - if ($SaveAs) {$ExcelPackage.SaveAs( $SaveAs ) } - Else {$ExcelPackage.Save(); $SaveAs = $ExcelPackage.File.FullName } + if ($SaveAs) { + $SaveAs = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($SaveAs) + if ($Password) {$ExcelPackage.SaveAs( $SaveAs, $Password ) } + else {$ExcelPackage.SaveAs( $SaveAs)} + } + Else { + if ($Password) {$ExcelPackage.Save($Password) } + else {$ExcelPackage.Save() } + $SaveAs = $ExcelPackage.File.FullName + } $ExcelPackage.Dispose() - if ($show) {Start-Process -FilePath $SaveAs } + if ($Show) {Start-Process -FilePath $SaveAs } } } diff --git a/README.md b/README.md index a33e8f9..aba5534 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,7 @@ Install-Module ImportExcel - Fixed bug where Export-Excel would not recognise it had to set $TitleFillPattern - made the default 'Solid' - Gave Expand-NumberFormat a better grasp of currency layouts. - Added DateTime to the list of types which can be exported as single column. +- Addded Password support to Open- and Close-ExcelPackage (password was not doing anything in Export-Excel) # What's new to 2nd Aug 2018 - Set-Row and Set-Column will now create hyperlinks and insert dates correctly