Password support fixes

This commit is contained in:
jhoneill
2018-08-06 14:30:10 +01:00
parent b06a5059c5
commit af77580b5e
3 changed files with 28 additions and 8 deletions

View File

@@ -555,7 +555,7 @@
$pkg = $ExcelPackage $pkg = $ExcelPackage
$Path = $pkg.File $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} $params = @{WorksheetName=$WorksheetName}
if ($NoClobber) {Write-Warning -Message "-NoClobber parameter is no longer used" } if ($NoClobber) {Write-Warning -Message "-NoClobber parameter is no longer used" }
@@ -944,7 +944,8 @@
else { else {
if ($ReturnRange) {$dataRange } if ($ReturnRange) {$dataRange }
$pkg.Save() if ($Password) { $pkg.Save($Password) }
else { $pkg.Save() }
Write-Verbose -Message "Saved workbook $($pkg.File)" Write-Verbose -Message "Saved workbook $($pkg.File)"
if ($ReZip) { if ($ReZip) {
Write-Verbose -Message "Re-Zipping $($pkg.file) using .NET ZIP library" Write-Verbose -Message "Re-Zipping $($pkg.file) using .NET ZIP library"

View File

@@ -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. 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])] [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,
[String]$Password,
#By default open only opens an existing file; -Create instructs it to create a new file if required. #By default open only opens an existing file; -Create instructs it to create a new file if required.
[switch]$Create [switch]$Create
) )
@@ -27,7 +30,7 @@
$Path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path) $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 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. #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)) {
@@ -36,7 +39,10 @@
} }
New-Object -TypeName OfficeOpenXml.ExcelPackage -ArgumentList $Path 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" } else {Write-Warning "Could not find $path" }
} }
@@ -45,6 +51,8 @@ Function Close-ExcelPackage {
.Synopsis .Synopsis
Closes an Excel Package, saving, saving under a new name or abandoning changes and opening the file in Excel as required. 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 ( Param (
#File to close. #File to close.
[parameter(Mandatory=$true, ValueFromPipeline=$true)] [parameter(Mandatory=$true, ValueFromPipeline=$true)]
@@ -54,14 +62,24 @@ Function Close-ExcelPackage {
#Abandon the file without saving. #Abandon the file without saving.
[Switch]$NoSave, [Switch]$NoSave,
#Save file with a new name (ignored if -NoSave Specified). #Save file with a new name (ignored if -NoSave Specified).
$SaveAs $SaveAs,
[ValidateNotNullOrEmpty()]
[String]$Password
) )
if ( $NoSave) {$ExcelPackage.Dispose()} if ( $NoSave) {$ExcelPackage.Dispose()}
else { else {
if ($SaveAs) {$ExcelPackage.SaveAs( $SaveAs ) } if ($SaveAs) {
Else {$ExcelPackage.Save(); $SaveAs = $ExcelPackage.File.FullName } $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() $ExcelPackage.Dispose()
if ($show) {Start-Process -FilePath $SaveAs } if ($Show) {Start-Process -FilePath $SaveAs }
} }
} }

View File

@@ -55,6 +55,7 @@ Install-Module ImportExcel
- Fixed bug where Export-Excel would not recognise it had to set $TitleFillPattern - made the default 'Solid' - 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. - Gave Expand-NumberFormat a better grasp of currency layouts.
- Added DateTime to the list of types which can be exported as single column. - 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 # What's new to 2nd Aug 2018
- Set-Row and Set-Column will now create hyperlinks and insert dates correctly - Set-Row and Set-Column will now create hyperlinks and insert dates correctly