-
Notifications
You must be signed in to change notification settings - Fork 403
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1112 from dfinke/refactor-rezip
- Loading branch information
Showing
5 changed files
with
298 additions
and
268 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
function Invoke-ExcelReZipFile { | ||
<# | ||
#> | ||
param( | ||
[Parameter(Mandatory)] | ||
[OfficeOpenXml.ExcelPackage]$ExcelPackage | ||
) | ||
|
||
Write-Verbose -Message "Re-Zipping $($ExcelPackage.file) using .NET ZIP library" | ||
try { | ||
Add-Type -AssemblyName 'System.IO.Compression.Filesystem' -ErrorAction stop | ||
} | ||
catch { | ||
Write-Error "The -ReZip parameter requires .NET Framework 4.5 or later to be installed. Recommend to install Powershell v4+" | ||
continue | ||
} | ||
try { | ||
$TempZipPath = Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath ([System.IO.Path]::GetRandomFileName()) | ||
$null = [io.compression.zipfile]::ExtractToDirectory($ExcelPackage.File, $TempZipPath) | ||
Remove-Item $ExcelPackage.File -Force | ||
$null = [io.compression.zipfile]::CreateFromDirectory($TempZipPath, $ExcelPackage.File) | ||
Remove-Item $TempZipPath -Recurse -Force | ||
} | ||
catch { throw "Error resizipping $path : $_" } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,38 @@ | ||
function Close-ExcelPackage { | ||
[CmdLetBinding()] | ||
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword","")] | ||
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword", "")] | ||
param ( | ||
[parameter(Mandatory=$true, ValueFromPipeline=$true)] | ||
[parameter(Mandatory = $true, ValueFromPipeline = $true)] | ||
[OfficeOpenXml.ExcelPackage]$ExcelPackage, | ||
[switch]$Show, | ||
[Switch]$Show, | ||
[Switch]$NoSave, | ||
$SaveAs, | ||
[ValidateNotNullOrEmpty()] | ||
[String]$Password, | ||
[switch]$Calculate | ||
[Switch]$Calculate, | ||
[Switch]$ReZip | ||
) | ||
if ( $NoSave) {$ExcelPackage.Dispose()} | ||
|
||
if ( $NoSave) { $ExcelPackage.Dispose() } | ||
else { | ||
if ($Calculate) { | ||
try { [OfficeOpenXml.CalculationExtension]::Calculate($ExcelPackage.Workbook) } | ||
catch { Write-Warning "One or more errors occured while calculating, save will continue, but there may be errors in the workbook."} | ||
try { [OfficeOpenXml.CalculationExtension]::Calculate($ExcelPackage.Workbook) } | ||
catch { Write-Warning "One or more errors occured while calculating, save will continue, but there may be errors in the workbook." } | ||
} | ||
if ($SaveAs) { | ||
$SaveAs = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($SaveAs) | ||
if ($Password) {$ExcelPackage.SaveAs( $SaveAs, $Password ) } | ||
else {$ExcelPackage.SaveAs( $SaveAs)} | ||
if ($Password) { $ExcelPackage.SaveAs( $SaveAs, $Password ) } | ||
else { $ExcelPackage.SaveAs( $SaveAs) } | ||
} | ||
else { | ||
if ($Password) {$ExcelPackage.Save($Password) } | ||
else {$ExcelPackage.Save() } | ||
else { | ||
if ($Password) { $ExcelPackage.Save($Password) } | ||
else { $ExcelPackage.Save() } | ||
$SaveAs = $ExcelPackage.File.FullName | ||
} | ||
if ($ReZip) { | ||
Invoke-ExcelReZipFile -ExcelPackage $ExcelPackage | ||
} | ||
$ExcelPackage.Dispose() | ||
if ($Show) {Start-Process -FilePath $SaveAs } | ||
if ($Show) { Start-Process -FilePath $SaveAs } | ||
} | ||
} |
Oops, something went wrong.