diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e9eec1106..ba74ff7ae4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/docs/topics/accessing-cells.md b/docs/topics/accessing-cells.md index 65538b9bee..346e5858ee 100644 --- a/docs/topics/accessing-cells.md +++ b/docs/topics/accessing-cells.md @@ -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