Skip to content

Commit 45ca934

Browse files
author
MarkBaker
committed
Update Change Log and Documentation
1 parent f3d5028 commit 45ca934

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

CHANGELOG.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,22 @@ and this project adheres to [Semantic Versioning](https://semver.org).
1010
### Added
1111

1212
- Implementation of the ISREF() information function.
13-
- Allow Boolean Conversion in Csv Reader to be locale-aware when using the String Value Binder.
13+
- Added support for reading "formatted" numeric values from Csv files; although default behaviour of reading these values as strings is preserved.
1414

15-
(i.e. `"Vrai"` wil be converted to a boolean `true` if the Locale is set to `fr`.)
15+
(i.e a value of "12,345.67" will be read as numeric `1235.67`, not as a string `"12,345.67"`.
16+
17+
This functionality is locale-aware, using the server's locale settings to identify the thousands and decimal separators.
1618

1719
### Changed
1820

1921
- Gnumeric Reader now loads number formatting for cells.
2022
- Gnumeric Reader now correctly identifies selected worksheet.
2123
- Some Refactoring of the Ods Reader, moving all formula and address translation from Ods to Excel into a separate class to eliminate code duplication and ensure consistency.
24+
- Make Boolean Conversion in Csv Reader locale-aware when using the String Value Binder.
25+
26+
This is determined b the Calculation Engine locale setting.
27+
28+
(i.e. `"Vrai"` wil be converted to a boolean `true` if the Locale is set to `fr`.)
2229

2330
### Deprecated
2431

docs/topics/reading-files.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,44 @@ Xlsx | NO | Xls | NO | Xml | NO |
560560
Ods | NO | SYLK | NO | Gnumeric | NO |
561561
CSV | YES | HTML | NO
562562

563+
564+
### Reading formatted Numbers from a CSV File
565+
566+
Unfortunately, numbers in a CSV file may be formatted as strings.
567+
If that number is a simple integer or float (with a decimal `.` separator) without any thousands separator, then it will be treated as a number.
568+
However, if the value has a thousands separator (e.g. `12,345`), or a decimal separator that isn't a `.` (e.g. `123,45` for a European locale), then it will be loaded as a string with that formatting.
569+
If you want the Csv Reader to convert that value to a numeric when it loads the file, the you need to tell it to do so. The `castFormattedNumberToNumeric()` lets you do this.
570+
571+
(Assuming that our server is configured with German locale settings: otherwise it may be necessary to call `setlocale()` before loading the file.)
572+
```php
573+
$inputFileType = 'Csv';
574+
$inputFileName = './sampleData/example1.de.csv';
575+
576+
/** It may be necessary to call setlocale() first if this is not your default locale */
577+
// setlocale(LC_ALL, 'de_DE.UTF-8', 'deu_deu');
578+
579+
/** Create a new Reader of the type defined in $inputFileType **/
580+
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
581+
/** Enable loading numeric values formatted with German , decimal separator and . thousands separator **/
582+
$reader->castFormattedNumberToNumeric(true);
583+
584+
/** Load the file to a Spreadsheet Object **/
585+
$spreadsheet = $reader->load($inputFileName);
586+
```
587+
This will attempt to load those formatted numeric values as numbers, based on the server's locale settings.
588+
589+
If you want to load those values as numbers, but also to retain the formatting as a number format mask, then you can pass a boolean `true` as a second argument to the `castFormattedNumberToNumeric()` method to tell the Reader to identify the format masking to use for that value. This option does have an arbitrary limit of 6 decimal places.
590+
591+
If your Csv file includes other formats for numbers (currencies, scientific format, etc); then you should probably also use the Advanced Value Binder to handle these cases.
592+
593+
Applies to:
594+
595+
Reader | Y/N |Reader | Y/N |Reader | Y/N |
596+
----------|:---:|--------|:---:|--------------|:---:|
597+
Xlsx | NO | Xls | NO | Xml | NO |
598+
Ods | NO | SYLK | NO | Gnumeric | NO |
599+
CSV | YES | HTML | NO
600+
563601
### A Brief Word about the Advanced Value Binder
564602

565603
When loading data from a file that contains no formatting information,

0 commit comments

Comments
 (0)