cfsimplicity / spreadsheet-cfml

Standalone library for working with spreadsheets and CSV in CFML
MIT License
126 stars 35 forks source link

Lucee/CF : Problem to convert a value to a monetary dataformat #333

Closed sebviatour-iteamlu closed 1 year ago

sebviatour-iteamlu commented 1 year ago

Hello,

I'm trying to convert a value (numeric and/or float) in a monetary format (000.00 €).

I checked in your documentation but nothing works. Each time, I got an error message during the execution.

Could you please help me ?

Thanks in advance.

cfsimplicity commented 1 year ago

Hi. Can you please post some code showing which method(s) you are using and example values that reproduce the problem?

sebviatour-iteamlu commented 1 year ago

Sure.

cellFormula = "AO" & row & "*AP" & row; spreadsheet.setCellFormula(workbook = workbook, formula = cellFormula, row = row, column = column); cellFormat = { dataformat: "$#,##0_);($#,##0)", alignment: "center", verticalalignment: "center", topborder: "THIN", bottomborder: "THIN", leftborder: "THIN", rightborder: "THIN" }; spreadsheet.formatCell(workbook = workbook, format = cellFormat, row = row, column = column);

I tried with dataformat : "0x05" or 5 or "$#,##0.00);($#,##0.00)". It's based with your documentation https://github.com/cfsimplicity/spreadsheet-cfml/wiki/Formatting-options

dataformat (string): One of the POI built-in formats, or your own custom formatting mask.

cfsimplicity commented 1 year ago

dataformat: "$#,##0_);($#,##0)"

Try escaping the pound signs, which have special meaning in CFML, by doubling them up:

dataformat: "$##,####0_);($##,####0)"

sebviatour-iteamlu commented 1 year ago

Hi,

Thanks. It's working.

Do you have an idea to use the symbol € instead of $ ?

cfsimplicity commented 1 year ago

To get the format string you need, I'd recommend opening a new file in Libre Office, right-click any cell then choose Format Cells...

Select the format you want in the Numbers tab then copy the Format Code at the bottom and use that with escaped pound signs.

currency

dataformat: "##,####0.00 [$€-40C];-##,####0.00 [$€-40C]"

You can probably do the same with other software, but LO is what I use.

sebviatour-iteamlu commented 1 year ago

I tried with Microsoft Excel and found the format that you mentionned but the system replaces € to ?

cfsimplicity commented 1 year ago

Here's a full test using the French (France) Euro format from above:

spreadsheet = New spreadsheet.Spreadsheet()
workbook = spreadsheet.newXlsx()
path = ExpandPath( "test.xlsx" )
cellFormat = { dataformat: "##,####0.00 [$€-40C];-##,####0.00 [$€-40C]" }
spreadsheet
    .setCellValue( workbook, "1.23", 1, 1 )
    .formatCell( workbook, cellFormat, 1, 1 )
    .write( workbook, path, true )

I don't have the full Excel, but in MS Excel Viewer this appears as expected:

currency2

sebviatour-iteamlu commented 1 year ago

Hello,

I found what was the problem. Now, it works with your idea.

Thanks for your help.