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

Allow ANCHORARRAY as Valid DataValidation List #4203

Merged
merged 4 commits into from
Oct 30, 2024

Conversation

oleibman
Copy link
Collaborator

Fix #4197. Overlooked in the introduction of Dynamic Arrays, Data Validation can specify a list to be a result of the spill operator, which is implemented via the ANCHORARRAY function.

It appears that function DataValidator::isValid will not work if the list is specified in this manner, nor if it is specified as a defined name. Fixing those situations will be difficult (defined names probably easier than ANCHORARRAY), and there is no reason to delay this change waiting for those to be fixed. I will open a new issue when this PR is merged.

This is:

  • a bugfix
  • a new feature
  • refactoring
  • additional unit tests

Checklist:

  • Changes are covered by unit tests
    • Changes are covered by existing unit tests
    • New unit tests have been added
  • Code style is respected
  • Commit message explains why the change is made (see https://github.com/erlang/otp/wiki/Writing-good-commit-messages)
  • CHANGELOG.md contains a short summary of the change and a link to the pull request if applicable
  • Documentation is updated as necessary

Why this change is needed?

Provide an explanation of why this change is needed, with links to any Issues (if appropriate).
If this is a bugfix or a new feature, and there are no existing Issues, then please also create an issue that will make it easier to track progress with this PR.

Fix PHPOffice#4197. Overlooked in the introduction of Dynamic Arrays, Data Validation can specify a list to be a result of the spill operator, which is implemented via the ANCHORARRAY function.

It appears that function `DataValidator::isValid` will not work if the list is specified in this manner, nor if it is specified as a defined name. Fixing those situations will be difficult (defined names probably easier than ANCHORARRAY), and there is no reason to delay this change waiting for those to be fixed. I will open a new issue when this PR is merged.
@oleibman oleibman added this pull request to the merge queue Oct 30, 2024
Merged via the queue into PHPOffice:master with commit 363705c Oct 30, 2024
12 of 13 checks passed
@oleibman oleibman deleted the issue4197 branch October 30, 2024 22:31
@siarheipashkevich
Copy link

@oleibman it is does not work with russia words

image
local.ERROR: preg_match(): failed executing "/^[^\*\:\/\\\?\[\]\+\-\% \'\^\&\<\>\=\,\;\#\(\)\"\{\}]+(\:[^\*\:\/\\\?\[\]\+\-\% \'\^\&\<\>\=\,\;\#\(\)\"\{\}]+)?\!\$?[A-Ia-i]?[A-Za-z]\$?\d+$/u": Malformed UTF-8 characters, possibly incorrectly encoded {"exception":"[object] (Composer\\Pcre\\PcreException(code: 4): preg_match(): failed executing \"/^[^\\*\\:\\/\\\\\\?\\[\\]\\+\\-\\% \\'\\^\\&\\<\\>\\=\\,\\;\\#\\(\\)\\\"\\{\\}]+(\\:[^\\*\\:\\/\\\\\\?\\[\\]\\+\\-\\% \\'\\^\\&\\<\\>\\=\\,\\;\\#\\(\\)\\\"\\{\\}]+)?\\!\\$?[A-Ia-i]?[A-Za-z]\\$?\\d+$/u\": Malformed UTF-8 characters, possibly incorrectly encoded at /Users/sergey/Herd/esupl-api/vendor/composer/pcre/src/PcreException.php:29)
[stacktrace]
#0 /Users/sergey/Herd/esupl-api/vendor/composer/pcre/src/Preg.php(37): Composer\\Pcre\\PcreException::fromFunction('preg_match', '/^[^\\\\*\\\\:\\\\/\\\\\\\\\\\\?\\\\...')
#1 /Users/sergey/Herd/esupl-api/vendor/composer/pcre/src/Preg.php(303): Composer\\Pcre\\Preg::match('/^[^\\\\*\\\\:\\\\/\\\\\\\\\\\\?\\\\...', '\"\\xD1', Array, 0, 0)
#2 /Users/sergey/Herd/esupl-api/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Xls/Parser.php(1097): Composer\\Pcre\\Preg::isMatch('/^[^\\\\*\\\\:\\\\/\\\\\\\\\\\\?\\\\...', '\"\\xD1')

@oleibman
Copy link
Collaborator Author

oleibman commented Feb 6, 2025

@siarheipashkevich I am unable to duplicate your error. Perhaps it comes farther along in the code than what you have identified. At any rate, here's the code I used (I changed cell B0 to B1 - I'm a bit surprised that your references to B0 did not cause an exception):

    public static function issue(): void
    {
        $spreadsheet = new Spreadsheet();
        $worksheet = $spreadsheet->getActiveSheet();

        $validation = $worksheet->getCell('B1')->getDataValidation();
        $validation->setType(DataValidation::TYPE_LIST);
        $validation->setErrorStyle(DataValidation::STYLE_STOP);
        $validation->setAllowBlank(false);
        $validation->setShowInputMessage(true);
        $validation->setShowErrorMessage(true);
        $validation->setShowDropDown(true);
        $validation->setFormula1('"слово, слово"'); // << russia words here
        $dataValidator = new DataValidator();
        $worksheet->getCell('B1')->setValue('слово');
        // expect true
        var_dump($dataValidator->isValid($worksheet->getCell('B1')));
        $worksheet->getCell('B1')->setValue('слов');
        // expect false
        var_dump($dataValidator->isValid($worksheet->getCell('B1')));

        $worksheet->setDataValidation('B2:B1500', clone $validation);
        echo "Done\n";
    }

And here are my results, exactly as expected:

C:\git\issue.4266.php:28:
bool(true)
C:\git\issue.4266.php:31:
bool(false)
Done

@siarheipashkevich
Copy link

@oleibman thanks for your response. Do you use 1.29.9 version?

@oleibman
Copy link
Collaborator Author

oleibman commented Feb 6, 2025

@siarheipashkevich I just tried with 1.29.9 and received the same expected results without any exceptions.

@siarheipashkevich
Copy link

@oleibman it's very strange. Could you please check this issue SpartnerNL/Laravel-Excel#4266 where I attached more code?

@oleibman
Copy link
Collaborator Author

oleibman commented Feb 6, 2025

Aha, the error does show up later than you reported. It happens when I try to save the spreadsheet as Xls. And, yes, it does not happen with Latin characters. I will research. In the meantime, you can save the spreadsheet as Xlsx instead without problems.

@siarheipashkevich
Copy link

@oleibman thanks, I'll wait your solution.

oleibman added a commit to oleibman/PhpSpreadsheet that referenced this pull request Feb 7, 2025
It currently parsed by byte, which is not a good thing in a UTF-8 system. See the discussion at the bottom of PR PHPOffice#4203. That turns out to not be the change which led to this problem; that would have been PR PHPOffice#4323. That change came about because using Composer/Pcre revealed bugs in several regular expressions used in Writer/Xls/Parser. This ticket comes about because more bugs were revealed in the same module. The problem is that the `advance` method needs to process formulas character by character, but is instead doing it byte by byte. It is changed to advance by characters, and tests for non-ASCII characters are added.
@oleibman
Copy link
Collaborator Author

oleibman commented Feb 7, 2025

@siarheipashkevich If you can test against the parseutf8 branch (or, guessing from our conversation, the release1291 branch), please do so.

@siarheipashkevich
Copy link

@oleibman unfortunately I can't, because I use laravel-excel package. Only if you create a new patch release in that case I'll possible to check.

@oleibman
Copy link
Collaborator Author

oleibman commented Feb 8, 2025

New releases are now available.

@siarheipashkevich
Copy link

@oleibman it's working right now, thank you so much 🚀

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

Successfully merging this pull request may close these issues.

ANCHORARRAY() doen't work in validation
2 participants