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

DataValidation corrupted when written to Xlsx #290

Closed
2 of 3 tasks
TerjeBr opened this issue Dec 13, 2017 · 3 comments
Closed
2 of 3 tasks

DataValidation corrupted when written to Xlsx #290

TerjeBr opened this issue Dec 13, 2017 · 3 comments

Comments

@TerjeBr
Copy link
Contributor

TerjeBr commented Dec 13, 2017

This is:

What is the expected behavior?

Data validation of cells should have the correct type and errorStyle.

What is the current behavior?

All data validation ends up with the type 'none'.
The error style is also garbled.

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

use PhpOffice\PhpSpreadsheet\Cell\DataValidation;

// Create new Spreadsheet object
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();

$workSheet = $spreadsheet->getActiveSheet();
$cell1 = $workSheet->getCell('A1');
$cell1->setValue(77);

$validation1 = $cell1->getDataValidation();
$validation1->setType(DataValidation::TYPE_WHOLE);
$validation1->setErrorStyle(DataValidation::STYLE_STOP);
$validation1->setAllowBlank(false);
$validation1->setShowInputMessage(false);
$validation1->setShowErrorMessage(true);
$validation1->setOperator(DataValidation::OPERATOR_EQUAL);
$validation1->setErrorTitle('Error');
$validation1->setError('This cell value must not be changed');
$validation1->setFormula1(77);

$cell2 = $workSheet->getCell('A2');
$cell2->setValue(88);
$validation2 = clone $validation1;
$validation2->setFormula1(88);
$cell2->setDataValidation($validation2);

$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$writer->save('testFile.xlsx');

Which versions of PhpSpreadsheet and PHP are affected?

Versions after pull request #131

@TerjeBr
Copy link
Contributor Author

TerjeBr commented Dec 13, 2017

This error was introduced in PR #131 because the function PhpOffice\PhpSpreadsheet\Cell\DataValidation::getHashCode() has the side effect of changing the content of the properties type and errorStyle.

If you unzip testFile.xlsx you will see that xl/worksheets/sheet1.xml contains this element:

<dataValidations count="2">
    <dataValidation
            type="nonestopequalffftErrorThis cell value must not be changedPhpOffice\PhpSpreadsheet\Cell\DataValidation"
            errorStyle="stopequalffftErrorThis cell value must not be changedPhpOffice\PhpSpreadsheet\Cell\DataValidation"
            operator="equal" allowBlank="0" showDropDown="1" showInputMessage="0" showErrorMessage="1"
            errorTitle="Error" error="This cell value must not be changed" sqref="A2">
        <formula1>88</formula1>
    </dataValidation>
    <dataValidation
            type="nonestopequalffftErrorThis cell value must not be changedPhpOffice\PhpSpreadsheet\Cell\DataValidation"
            errorStyle="stopequalffftErrorThis cell value must not be changedPhpOffice\PhpSpreadsheet\Cell\DataValidation"
            operator="equal" allowBlank="0" showDropDown="1" showInputMessage="0" showErrorMessage="1"
            errorTitle="Error" error="This cell value must not be changed" sqref="A1">
        <formula1>77</formula1>
    </dataValidation>
</dataValidations>

@PowerKiKi
Copy link
Member

It seems you have a pretty good understanding of the issue. Would you be able to submit a PR to fix it ?

@TerjeBr
Copy link
Contributor Author

TerjeBr commented Dec 14, 2017

I am not sure I have time before Christmas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants