Skip to content

Commit

Permalink
cache file to temp directory to avoid locking issues
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardmiller-mesirow committed May 21, 2024
1 parent 265bee2 commit 80b821b
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions Public/ConvertTo-ExcelXlsx.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ function ConvertTo-ExcelXlsx {
[parameter(Mandatory = $true, ValueFromPipeline)]
[string]$Path,
[parameter(Mandatory = $false)]
[switch]$Force
[switch]$Force,
[parameter(Mandatory = $false)]
[bool]$CacheToTemp = $false
)
process {
if (-Not ($Path | Test-Path) ) {
Expand Down Expand Up @@ -38,6 +40,15 @@ function ConvertTo-ExcelXlsx {
}
}

if ($CacheToTemp) {
$tempPath = [System.IO.Path]::GetTempFileName()
Copy-Item -Path $xlsFile.FullName -Destination $tempPath -Force
$fileToProcess = $tempPath
}
else {
$fileToProcess = $xlsFile.FullName
}

try {
$Excel = New-Object -ComObject "Excel.Application"
}
Expand All @@ -47,7 +58,7 @@ function ConvertTo-ExcelXlsx {

try {
$Excel.Visible = $false
$workbook = $Excel.Workbooks.Open($xlsFile.FullName, $null, $true)
$workbook = $Excel.Workbooks.Open($fileToProcess, $null, $true)
if ($null -eq $workbook) {
Write-Host "Failed to open workbook"
} else {
Expand All @@ -60,7 +71,10 @@ function ConvertTo-ExcelXlsx {
}

$Excel.Quit()

if ($CacheToTemp) {
Remove-Item -Path $tempPath -Force
}
}
}
}

0 comments on commit 80b821b

Please sign in to comment.