Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setValueExplicit sets the Value wrongly? to "0" instead of "null" #2488

Closed
Wolfy7 opened this issue Jan 7, 2022 · 2 comments · Fixed by #2489
Closed

setValueExplicit sets the Value wrongly? to "0" instead of "null" #2488

Wolfy7 opened this issue Jan 7, 2022 · 2 comments · Fixed by #2489

Comments

@Wolfy7
Copy link

Wolfy7 commented Jan 7, 2022

This is:

- [x] a bug report
- [ ] a feature request
- [ ] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)

What is the expected behavior?

If you want to set the Value explicit to TYPE_Numeric but the value ($pValue) ist "null" the value should be also set to "null".

What is the current behavior?

If a Cell looks like that in the xml-file:
<c r="A1" s="109" t="n" />
the Value for this cell will be set to "0" instead of "null".

What are the steps to reproduce?

If you call setValueExplicit with $pValue = null and $pDataType = DataType::TYPE_NUMERIC the value will be set to "0".

Which versions of PhpSpreadsheet and PHP are affected?

PhpSpeadsheet = 1.18.0 - 2021-05-31
PHP = 7.2.4

Possible solution

Change the line 217 in \src\PhpSpreadsheet\Cell\Cell.php from:

                $this->value = 0 + $pValue;

to

                if(is_null($pValue)){
                    $this->value = null;
                }else{
                    $this->value = 0 + $pValue;
                }

If you want you can assigne this to me and i will provide a PR with this fix.

@MarkBaker
Copy link
Member

It does raise the question of why you want to specify type TYPE_NUMERIC with a value of NULL rather than specifying TYPE_NULL

@Wolfy7
Copy link
Author

Wolfy7 commented Jan 8, 2022

@MarkBaker justified question.
The think is I have (i think) no choice, I have a xlsx-file with some empty cells and in the XML they are defined as mentioned above:

<c r="A1" s="109" t="n" />

So if I read this file in PHPspreadsheet converts this empty cell to "0" cause if the t="n". Maybe the XLSX (XML) file is not "correct" but that's what I don't know.

And also just for my understanding I think if a cell is empty it should stay empty independent of the type. So probably the function should always return empty if the value is null or?

Thanks for your reply.

oleibman added a commit to oleibman/PhpSpreadsheet that referenced this issue Jan 8, 2022
Fix PHPOffice#2488. When Excel sees this situation, it leaves the value of the cell as null rather than casting to the specified DataType. It doesn't really make sense to change setValueExplicit to adopt this convention; it should be sufficient to recognize the situation in the Reader and act there. The same sort of situation might apply to strings, but I don't see any practical difference between null string and null even if so.
oleibman added a commit that referenced this issue Jan 17, 2022
Fix #2488. When Excel sees this situation, it leaves the value of the cell as null rather than casting to the specified DataType. It doesn't really make sense to change setValueExplicit to adopt this convention; it should be sufficient to recognize the situation in the Reader and act there. The same sort of situation might apply to strings, but I don't see any practical difference between null string and null even if so.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants