Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Property of type Float is improperly formatted when locale uses non-dot character to denote decimal place. #3095

Closed
2 of 11 tasks
adjenks opened this issue Sep 30, 2022 · 3 comments · Fixed by #3099
Closed
2 of 11 tasks

Comments

@adjenks
Copy link

adjenks commented Sep 30, 2022

This is:

What is the expected behavior?

For the float property to not contain commas but a dot character instead.

What is the current behavior?

The float is formatted using the current PHP locale.

What are the steps to reproduce?

  1. Set the locale to something that doesn't use . as the decimale separator.
  2. Create a custom property of type float.
  3. Write the document to xlsx.

Please provide a Minimal, Complete, and Verifiable example of code that exhibits the issue without relying on an external Excel file or a web server:

<?php

require __DIR__ . '/vendor/autoload.php';

// Create new Spreadsheet object
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
setlocale(LC_ALL, 'fr_CA');
$spreadsheet->getProperties()->setCustomProperty('my_property_name', 3.01, 'f'); //name,val,type
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$writer->save('test.xlsx');

Opening the file gives you this:
image

What features do you think are causing the issue

  • Reader
  • Writer
  • Styles
  • Data Validations
  • Formula Calculations
  • Charts
  • AutoFilter
  • Form Elements

Does an issue affect all spreadsheet file formats? If not, which formats are affected?

I don't know, it affect xlsx...

Which versions of PhpSpreadsheet and PHP are affected?

I just updated to version 1.25.

Where's the problem?

It seems to be that when you use 'f' as the type, it casts the value to a float here:

return (float) $propertyValue;

Then when the document is written it uses an XML writer to write the float here:

case Properties::PROPERTY_TYPE_FLOAT:

The xml writer in PHP spreadsheet extends PHP's built-in writer, and if you look at the parameters here you can see that the writeElement method accepts a string type as a value:
https://www.php.net/manual/en/xmlwriter.writeelement.php

When you cast a float to a string, PHP uses the locale to format it: https://stackoverflow.com/questions/17587581/php-locale-dependent-float-to-string-cast

Excel doesn't like this. So it needs to be converted to a string and formatted manually to use a . character as the decimal separator before calling writeElement.

@oleibman
Copy link
Collaborator

Are you using Php7.3/7.4? Php8 changed to use "Locale-independent float to string casting". So, if it is feasible for you to use Php8, that might eliminate your problem more quickly than waiting for a change to PhpSpreadsheet. (Which is not to say I won't investigate further.)

@adjenks
Copy link
Author

adjenks commented Oct 1, 2022

Oh... That's news to me. Thanks for letting me know. I'm on 7.4 right now. We plan on switching to 8 soon anyhow.
I can manage for now by switching the property type to string and passing a string though.

oleibman added a commit to oleibman/PhpSpreadsheet that referenced this issue Oct 1, 2022
Fix PHPOffice#3095. Issue is not a problem for Php8, but is for 7.4. Code used to write Xml for float custom property uses locale-dependent (in Php7) cast. Change to use locale-independent (all Php releases) `sprintf('%F'...)` instead.
oleibman added a commit that referenced this issue Oct 3, 2022
…ty (#3099)

Fix #3095. Issue is not a problem for Php8, but is for 7.4. Code used to write Xml for float custom property uses locale-dependent (in Php7) cast. Change to use locale-independent (all Php releases) `sprintf('%F'...)` instead.
@adjenks
Copy link
Author

adjenks commented Oct 4, 2022

Good work! So fast!

MarkBaker added a commit that referenced this issue Dec 21, 2022
### Added

- Extended flag options for the Reader `load()` and Writer `save()` methods
- Apply Row/Column limits (1048576 and XFD) in ReferenceHelper [PR #3213](#3213)
- Allow the creation of In-Memory Drawings from a string of binary image data, or from a stream. [PR #3157](#3157)
- Xlsx Reader support for Pivot Tables [PR #2829](#2829)
- Permit Date/Time Entered on Spreadsheet to be calculated as Float [Issue #1416](#1416) [PR #3121](#3121)

### Changed

- Nothing

### Deprecated

- Direct update of Calculation::suppressFormulaErrors is replaced with setter.
- Font public static variable defaultColumnWidths replaced with constant DEFAULT_COLUMN_WIDTHS.
- ExcelError public static variable errorCodes replaced with constant ERROR_CODES.
- NumberFormat constant FORMAT_DATE_YYYYMMDD2 replaced with existing identical FORMAT_DATE_YYYYMMDD.

### Removed

- Nothing

### Fixed

- Fixed handling for `_xlws` prefixed functions from Office365 [Issue #3245](#3245) [PR #3247](#3247)
- Conditionals formatting rules applied to rows/columns are removed [Issue #3184](#3184) [PR #3213](#3213)
- Treat strings containing currency or accounting values as floats in Calculation Engine operations [Issue #3165](#3165) [PR #3189](#3189)
- Treat strings containing percentage values as floats in Calculation Engine operations [Issue #3155](#3155) [PR #3156](#3156) and [PR #3164](#3164)
- Xlsx Reader Accept Palette of Fewer than 64 Colors [Issue #3093](#3093) [PR #3096](#3096)
- Use Locale-Independent Float Conversion for Xlsx Writer Custom Property [Issue #3095](#3095) [PR #3099](#3099)
- Allow setting AutoFilter range on a single cell or row [Issue #3102](#3102) [PR #3111](#3111)
- Xlsx Reader External Data Validations Flag Missing [Issue #2677](#2677) [PR #3078](#3078)
- Reduces extra memory usage on `__destruct()` calls [PR #3092](#3092)
- Additional properties for Trendlines [Issue #3011](#3011) [PR #3028](#3028)
- Calculation suppressFormulaErrors fix [Issue #1531](#1531) [PR #3092](#3092)
- Permit Date/Time Entered on Spreadsheet to be Calculated as Float [Issue #1416](#1416) [PR #3121](#3121)
- Incorrect Handling of Data Validation Formula Containing Ampersand [Issue #3145](#3145) [PR #3146](#3146)
- Xlsx Namespace Handling of Drawings, RowAndColumnAttributes, MergeCells [Issue #3138](#3138) [PR #3136](#3137)
- Generation3 Copy With Image in Footer [Issue #3126](#3126) [PR #3140](#3140)
- MATCH Function Problems with Int/Float Compare and Wildcards [Issue #3141](#3141) [PR #3142](#3142)
- Fix ODS Read Filter on number-columns-repeated cell [Issue #3148](#3148) [PR #3149](#3149)
- Problems Formatting Very Small and Very Large Numbers [Issue #3128](#3128) [PR #3152](#3152)
- XlsxWrite preserve line styles for y-axis, not just x-axis [PR #3163](#3163)
- Xlsx Namespace Handling of Drawings, RowAndColumnAttributes, MergeCells [Issue #3138](#3138) [PR #3137](#3137)
- More Detail for Cyclic Error Messages [Issue #3169](#3169) [PR #3170](#3170)
- Improved Documentation for Deprecations - many PRs [Issue #3162](#3162)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants