Skip to content

Commit

Permalink
Merge pull request #1112 from dfinke/refactor-rezip
Browse files Browse the repository at this point in the history
  • Loading branch information
dfinke authored Nov 30, 2021
2 parents a320cfd + aa1b042 commit 2ca870a
Show file tree
Hide file tree
Showing 5 changed files with 298 additions and 268 deletions.
2 changes: 1 addition & 1 deletion ImportExcel.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
RootModule = 'ImportExcel.psm1'

# Version number of this module.
ModuleVersion = '7.4.0'
ModuleVersion = '7.4.1'

# ID used to uniquely identify this module
GUID = '60dd4136-feff-401a-ba27-a84458c57ede'
Expand Down
25 changes: 25 additions & 0 deletions Private/Invoke-ExcelReZipFile.ps1
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 : $_" }
}
31 changes: 18 additions & 13 deletions Public/Close-ExcelPackage.ps1
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 }
}
}
Loading

0 comments on commit 2ca870a

Please sign in to comment.