Updates to documentation concerning Value Binders

Fix a couple of checks on characters at positions within a string before checking the length of the string when the offset may not exist
This commit is contained in:
MarkBaker 2021-06-02 18:44:28 +02:00 committed by Mark Baker
parent e8ebf11707
commit 85b9022f5b
2 changed files with 19 additions and 7 deletions

View File

@ -110,6 +110,11 @@ values beginning with `=` will be converted to a formula. Strings that
aren't numeric, or that don't begin with a leading `=` will be treated
as genuine string values.
Note that a numeric string that begins with a leading zero (that isn't
immediately followed by a decimal separator) will not be converted to a
numeric, so values like phone numbers (e.g. `01615991375``will remain as
strings).
This "conversion" is handled by a cell "value binder", and you can write
custom "value binders" to change the behaviour of these "conversions".
The standard PhpSpreadsheet package also provides an "advanced value
@ -138,8 +143,10 @@ Formats handled by the advanced value binder include:
- When strings contain a newline character (`\n`), then the cell styling is
set to wrap.
You can read more about value binders later in this section of the
documentation.
Basically, it attempts to mimic the behaviour of the MS Excel GUI.
You can read more about value binders [later in this section of the
documentation](#using-value-binders-to-facilitate-data-entry).
### Setting a formula in a Cell
@ -551,8 +558,13 @@ $spreadsheet->getActiveSheet()->setCellValue('A5', 'Date/time value:');
$spreadsheet->getActiveSheet()->setCellValue('B5', '21 December 1983');
```
**Creating your own value binder is easy.** When advanced value binding
is required, you can implement the
`\PhpOffice\PhpSpreadsheet\Cell\IValueBinder` interface or extend the
Alternatively, a `\PhpOffice\PhpSpreadsheet\Cell\StringValueBinder` class is available
if you want to preserve all string content as strings. This might be appropriate if you
were loading a file containing values that could be interpreted as numbers (e.g. numbers
with leading zeroes such as phone numbers), but that should be retained as strings.
**Creating your own value binder is relatively straightforward.** When more specialised
value binding is required, you can implement the
`\PhpOffice\PhpSpreadsheet\Cell\IValueBinder` interface or extend the existing
`\PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder` or
`\PhpOffice\PhpSpreadsheet\Cell\AdvancedValueBinder` classes.

View File

@ -57,11 +57,11 @@ class DefaultValueBinder implements IValueBinder
return DataType::TYPE_STRING;
} elseif ($value instanceof RichText) {
return DataType::TYPE_INLINE;
} elseif (is_string($value) && $value[0] === '=' && strlen($value) > 1) {
} elseif (is_string($value) && strlen($value) > 1 && $value[0] === '=') {
return DataType::TYPE_FORMULA;
} elseif (preg_match('/^[\+\-]?(\d+\\.?\d*|\d*\\.?\d+)([Ee][\-\+]?[0-2]?\d{1,3})?$/', $value)) {
$tValue = ltrim($value, '+-');
if (is_string($value) && $tValue[0] === '0' && strlen($tValue) > 1 && $tValue[1] !== '.') {
if (is_string($value) && strlen($tValue) > 1 && $tValue[0] === '0' && $tValue[1] !== '.') {
return DataType::TYPE_STRING;
} elseif ((strpos($value, '.') === false) && ($value > PHP_INT_MAX)) {
return DataType::TYPE_STRING;