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

Data Validation flags not read correctly when reading XLSX files #2224

Closed
lucasnetau opened this issue Jul 16, 2021 · 0 comments · Fixed by #2225
Closed

Data Validation flags not read correctly when reading XLSX files #2224

lucasnetau opened this issue Jul 16, 2021 · 0 comments · Fixed by #2225

Comments

@lucasnetau
Copy link
Contributor

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?

Data validation flags like allowBlank, showDropDown, showInputMessage, and showErrorMessage flags are set correctly when reading in a XLSX

What is the current behavior?

Values are always set to false (except showDropDown which inverts the logic incorrectly and is always true)

What are the steps to reproduce?

Read in the XLSX file with data validations set with an input or error message, write file back out to a new XLSX file. Messages will be unset.

The code in PhpOffice\PhpSpreadsheet\Reader\Xlsx\DataValidations looks suspect. In load() the value for $dataValidation record is:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [allowBlank] => true
            [operator] => between
            [prompt] => Enter a valid date
            [showDropDown] => true
            [showErrorMessage] => true
            [showInputMessage] => true
            [sqref] => C505:C1003
            [type] => custom
        )

    [formula1] => OR(NOT(ISERROR(DATEVALUE(C504))), AND(ISNUMBER(C504), LEFT(CELL("format", C504))="D"))
    [formula2] => 0
)

however the resulting PhpOffice\PhpSpreadsheet\Cell\DataValidation Object becomes

PhpOffice\PhpSpreadsheet\Cell\DataValidation Object
(
    [formula1:PhpOffice\PhpSpreadsheet\Cell\DataValidation:private] => OR(NOT(ISERROR(DATEVALUE(C504))), AND(ISNUMBER(C504), LEFT(CELL("format", C504))="D"))
    [formula2:PhpOffice\PhpSpreadsheet\Cell\DataValidation:private] => 0
    [type:PhpOffice\PhpSpreadsheet\Cell\DataValidation:private] => custom
    [errorStyle:PhpOffice\PhpSpreadsheet\Cell\DataValidation:private] => 
    [operator:PhpOffice\PhpSpreadsheet\Cell\DataValidation:private] => between
    [allowBlank:PhpOffice\PhpSpreadsheet\Cell\DataValidation:private] => 
    [showDropDown:PhpOffice\PhpSpreadsheet\Cell\DataValidation:private] => 1
    [showInputMessage:PhpOffice\PhpSpreadsheet\Cell\DataValidation:private] => 
    [showErrorMessage:PhpOffice\PhpSpreadsheet\Cell\DataValidation:private] => 
    [errorTitle:PhpOffice\PhpSpreadsheet\Cell\DataValidation:private] => 
    [error:PhpOffice\PhpSpreadsheet\Cell\DataValidation:private] => 
    [promptTitle:PhpOffice\PhpSpreadsheet\Cell\DataValidation:private] => 
    [prompt:PhpOffice\PhpSpreadsheet\Cell\DataValidation:private] => Enter a valid date
)

$dataValidation is a SimpleXML object

 object(SimpleXMLElement)#15136 (1) {
  [0]=>
  string(4) "true"
}

therefore the loose comparison in PhpOffice\PhpSpreadsheet\Reader\Xlsx\DataValidations::load() will always be false

$docValidation->setAllowBlank($dataValidation['allowBlank'] != 0);

This is the same for other flags like showDropDown, showInputMessage, showErrorMessage

Additionally the logic for showDropDown is inverted (and then inverted again in the Xlsx writer)

Which versions of PhpSpreadsheet and PHP are affected?

Current release and master branch

@lucasnetau lucasnetau changed the title Data Validation flags not read correctly Data Validation flags not read correctly when reading XLSX files Jul 16, 2021
@lucasnetau lucasnetau mentioned this issue Jul 16, 2021
4 tasks
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.

1 participant