dfinke / ImportExcel

PowerShell module to import/export Excel spreadsheets, without Excel

Home Page:https://www.powershellgallery.com/packages/ImportExcel/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Issue with user-defined number format

TWalijew opened this issue · comments

Hello,
I'm trying to add a number format using
set-excelrange -Range $xl.Workbook.Worksheets["Overview"].cells["B2:C2"] -numberformat '#,##0 \"aaa\"'
taking the format from the Excel macro
selectedSheet.getRange("B2:C2").setNumberFormatLocal("#,##0 \"aaa\"");, which actually works in Excel itself.
The thing is that the script below saves the Excel w/o problems, but Excel says that the spreadsheet is damaged and needs repairing.
set-excelrange -Range $xl.Workbook.Worksheets["Overview"].cells["B2:C2"] -numberformat '#,##0' works fine, though.
What could be the issue? Maybe the guy in front of the screen ?
Best Regards
Thomas

Complete test code below.

$data = ConvertFrom-Csv @"
Name,CIChanges,CIChangeDetails
test_tenant,103456,563676
"@
$xlfile=".\test.xlsx"
remove-item $xlfile -ErrorAction SilentlyContinue
$xl=open-excelpackage $xlfile -create
$xlparams=@{
ExcelPackage=$xl
WorksheetName="Overview"
passthru=$true
TableStyle="light11"
autosize=$true
}
$xl=$data|export-excel @xlparams
set-excelrange -Range $xl.Workbook.Worksheets["Overview"].cells["B2:C2"] -numberformat '#,##0' #OK
##set-excelrange -Range $xl.Workbook.Worksheets["Overview"].cells["B2:C2"] -numberformat '#,##0 "abc"' #not OK
close-excelpackage $xl

Thanks, never tried that sort of format.

The spreadsheet is saved to xlsx, there is no way to determine until runtime if there is an issue. Using the Excel app does more checking and validation.

Two thoughts can you try single quotes 'abc'.

Also, when you create and save the working sheet using Excel. You can unzip the xlsx, this gives you the xml content that defines the sheet. From there you can search the contents to where you did the 'abc' formatting. Please post that, there may be clues how to format this without the sheet having issues.

Thanks,
your answer made cope with this thing a bit; I did s you told me, but no clue.
I tried the script again, and... mirabile dictu... it works!
Don't ask me why.
For the sake of completeness, here the non-culprit:
set-excelrange -Range $xl.Workbook.Worksheets["Overview"].cells["B2:C2"] -numberformat '#,##0 "abc"'
image

Best
Thomas

No issue

Would like to hear more what data you are working with and formating beyond numerics you are doing.

Glad it worked.

Did you delete the xlsx and then re-run?

Exactly, that's what I did. I was flabbergasted.
Well, I run some SQL statements to see how many rows are in certain tables of a number of tenant databases, grouped by year and month, to see if there is a growth trend, or if they grow beyond an acceptable size. In the latter case older entries (e.g. >6 months) will be bulk-deleted.
it's clear, Excel is the tool to make these numbers transparent and comparable to peers and management.
The formatting just was a casual idea, nothing essential.