dfinke / ImportExcel

PowerShell module to import/export Excel spreadsheets, without Excel
https://www.powershellgallery.com/packages/ImportExcel/
Apache License 2.0
2.48k stars 400 forks source link

intermittent Save" with "0" argument(s) when importing multiple csvs #1629

Closed twhitesc closed 1 month ago

twhitesc commented 3 months ago

fantastic overall module.

intermittently we are getting "Save with "0" argument(s)" error as it seems the excel file is locked when it tries to update it with additional csv files. we have roughly 20 csvs that must be imported. fails at different places. 1 out of 10 times it fails.

using a start-sleep in between imports reduced the issue but didn't eliminate it. Any suggestions?

example:

import-csv $General_Daily | 
Export-Excel $xlfile -WorkSheetName "Metrics"  -AutoSize -StartRow 10 -StartColumn 13 -TableName General_Daily -TableStyle Light15 -Title "Combined Program totals - Daily" -TitleBold -TitleSize 14

Start-Sleep -Seconds 3

import-csv $General_Daily_Total |
Export-Excel $xlfile -WorkSheetName "Metrics"  -AutoSize -StartRow 20 -StartColumn 13 -TableName General_Daily_Total  -TableStyle Light15 -NoHeader

Start-Sleep -Seconds 3

import-csv $General_Monthly |
Export-Excel $xlfile -WorkSheetName "Metrics"  -AutoSize -StartRow 23 -StartColumn 13 -TableName General_Monthly -TableStyle Light15 -Title "Combined Program totals - Monthly" -TitleBold -TitleSize 14

Start-Sleep -Seconds 3

Thank you

dfinke commented 3 months ago

thanks @twhitesc

Can you show more of the script? what is in $General_Daily? A single file name.

Not sure what the issue might be.

twhitesc commented 3 months ago

Yes $General_Daily points to a csv file. It worked fine for years. And then in 2024 at some point it started popping up. Happens in background and in foreground. The csv files are no more than 3 KB in size. The excel file doesn't yet exist until this script creates it so it wouldn't be opened by another process/user.

#Data $General_Daily = get-childitem C:\Scripts\ClientReports\$filePath\General-Daily.csv $General_Monthly = get-childitem C:\Scripts\ClientReports\$filePath\General-Monthly.csv $General_Yearly = get-childitem C:\Scripts\ClientReports\$filePath\General-Yearly.csv

Totals

$General_Daily_Total = get-childitem C:\Scripts\ClientReports\$filePath\General-Daily-Total.csv $General_Monthly_Total = get-childitem C:\Scripts\ClientReports\$filePath\General-Monthly-Total.csv $General_Yearly_Total = get-childitem C:\Scripts\ClientReports\$filePath\General-Yearly-Total.csv $Callback_Disposition_Total = get-childitem C:\Scripts\ClientReports\$filePath\Callback-Disposition-Total.csv

`try {

import-csv $General_Daily | Export-Excel $xlfile -WorkSheetName "Metrics" -AutoSize -StartRow 10 -StartColumn 13 -TableName General_Daily -TableStyle Light15 -Title "Combined Program totals - Daily" -TitleBold -TitleSize 14 Start-Sleep -Seconds 3 import-csv $General_Daily_Total | Export-Excel $xlfile -WorkSheetName "Metrics" -AutoSize -StartRow 20 -StartColumn 13 -TableName General_Daily_Total -TableStyle Light15 -NoHeader Start-Sleep -Seconds 3 import-csv $General_Monthly | Export-Excel $xlfile -WorkSheetName "Metrics" -AutoSize -StartRow 23 -StartColumn 13 -TableName General_Monthly -TableStyle Light15 -Title "Combined Program totals - Monthly" -TitleBold -TitleSize 14 Start-Sleep -Seconds 3 import-csv $General_Monthly_Total | Export-Excel $xlfile -WorkSheetName "Metrics" -AutoSize -StartRow 33 -StartColumn 13 -TableName General_Monthly_Total -TableStyle Light15 -NoHeader Start-Sleep -Seconds 3` $excel = import-csv $Callback_Disposition_Total | Export-Excel $xlfile -WorkSheetName "Callback Disposition" -AutoSize -StartRow 39 -StartColumn 1 -TableName Callback_DispositionTotal -TableStyle Light15 -NoHeader -PassThru Start-Sleep -Seconds 3 } catch { Write-Host "An error occurred: $($.Exception.Message)" }

Close-ExcelPackage $excel

dfinke commented 3 months ago

did you update the ImportExcel module?

not sure why the -passthru is needed here

$excel = import-csv $Callback_Disposition_Total | Export-Excel $xlfile -WorkSheetName "Callback Disposition" -AutoSize -StartRow 39 -StartColumn 1 -TableName Callback_Disposition_Total -TableStyle Light15 -NoHeader -PassThru

then this is not needed:

Close-ExcelPackage $excel

Doesn't look like issues to my eye. Btw, very cool application of the Excel module.

Maybe the module got updated and it introduced an issue.

twhitesc commented 3 months ago

i didn't update the package until we saw the issue. we've updated it a few times since the issue started.

i believe we do -PassThru as there is a bunch of other excel formatting we do before finally calling Close-ExcelPackage $excel

The error occurs before we get to the final -PassThru line.

dfinke commented 3 months ago

I'd need a small repro of this to see what may be the issue, including data.

If it makes sense, I'd look to go back to a version of ImportExcel that worked.

twhitesc commented 1 month ago

I figured out the issue. Bitdefender was locking the file. Disabling it solved the issue. Thanks for your help.

dfinke commented 1 month ago

Wow - thanks for updating