Skip to content

Please consider changing DefaultValueBinder to always bind strings as TYPE_STRING #752

@jdufresne

Description

@jdufresne

This is a feature request.

When writing a document, please consider changing the Cell::setValue() behavior to always treat strings as strings. Right now, strings that are formatted as a number are coerced and treated like a number. IMO, this causes confusing behavior. If I want to store a number, I would pass a number. But as I pass a string, I expect PhpSpreadsheet to store a string. There are many use cases where strings that look like numbers should be treated as strings: telephone numbers, employee ID numbers, purchase order numbers, etc. I've run into this enough times, that I now use wrappers around PhpSpreadsheet to workaround this. However, I think this is fundamental enough of a concern that PhpSpreadsheet should consider changing the behavior.

I realize this would be a backwards incompatible change. Some people may be relying on this implicit coercion. I'd argue they should be using numerical types for numerical data. If that is too big of a concern, would it be OK to emit a deprecation warning in DefaultValueBinder::dataTypeForValue() to declare that this behavior will change in a future version?

What is the expected behavior?

When creating and writing a document, $cell->setValue('1234') is stored with type TYPE_STRING.

What is the current behavior?

$cell->setValue('1234') is stored with type TYPE_NUMERIC.

What are the steps to reproduce?

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';

$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();
$cell = $worksheet->getCell('A1');
$cell->setValue('1234');
echo $cell->getDataType() . "\n";

Which versions of PhpSpreadsheet and PHP are affected?

develop branch

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions