Skip to content

Commit

Permalink
Update documentation with details of changes to the StringValueBinder
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkBaker authored and MarkBaker committed Jun 3, 2021
1 parent 504ed9a commit 488701b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).

### Added

- Nothing.
- More flexibility in the StringValueBinder to determine what datatypes should be treated as strings [PR #2138](https://github.com/PHPOffice/PhpSpreadsheet/pull/2138)

### Changed

Expand Down
14 changes: 13 additions & 1 deletion docs/topics/accessing-cells.md
Original file line number Diff line number Diff line change
Expand Up @@ -559,12 +559,24 @@ $spreadsheet->getActiveSheet()->setCellValue('B5', '21 December 1983');
```

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
if you want to preserve all 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 sign such as international phone numbers like `+441615579382`), but that
should be retained as strings (non-international phone numbers with leading zeroes are
already maintained as strings).

By default, the StringValueBinder will cast any datatype passed to it into a string. However, there are a number of settings which allow you to specify that certain datatypes shouldn't be cast to strings, but left "as is":

```php
// Set value binder
$stringValueBinder = new \PhpOffice\PhpSpreadsheet\Cell\StringValueBinder();
$stringValueBinder->setNumericConversion(false)
->setBooleanConversion(false)
->setNullConversion(false)
->setFormulaConversion(false);
\PhpOffice\PhpSpreadsheet\Cell\Cell::setValueBinder( $stringValueBinder );
```

**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
Expand Down

0 comments on commit 488701b

Please sign in to comment.