jmcnamara / excel-writer-xlsx

Perl module to create Excel XLSX files.
https://metacpan.org/pod/distribution/Excel-Writer-XLSX/lib/Excel/Writer/XLSX.pm
Other
100 stars 51 forks source link

num_format '0' doesn't work as documented #220

Closed kevinpacheco closed 5 years ago

kevinpacheco commented 5 years ago

When I do "write_number" in conjunction with a format that includes "num_format => '0'", I should get a cell that is formatted as number with zero decimal places, but I don't. I instead get a cell that is formatted as general.

use Excel::Writer::XLSX;
$workbook = Excel::Writer::XLSX->new('test.xlsx');
$worksheet = $workbook->add_worksheet();
$fmt_int = $workbook->add_format(num_format => '0');
$worksheet->write_number(0,0,2.1,$fmt_int);
$workbook->close();

When I open the resulting spreadsheet in Excel, I see 2.1 in cell A1, when I should see just 2. When I right-click the cell and choose "Format Cells", I see that the format is general.

Here are the first three entries in the table of formats in the "set_num_format()" section of the documentation:

Index   Index   Format String
0       0x00    General
1       0x01    0
2       0x02    0.00

How do I get the format at index 1 instead of the format at index 0?

jmcnamara commented 5 years ago

Since num_format can take either a string or an integer, and since Perl doesn't distinguish between them in term of type '0' is being interpreted as index 0 which gives format General from the table above.

The way to get format string 0 is to use index 1, from the table above:

$fmt_int = $workbook->add_format(num_format => 1);

I know this is counter-intuitive but it remains for backward compatibility reasons. :-(