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 #4091

Open
bokrma opened this issue Jul 8, 2024 · 3 comments · May be fixed by #4240
Open

Data Validation #4091

bokrma opened this issue Jul 8, 2024 · 3 comments · May be fixed by #4240

Comments

@bokrma
Copy link

bokrma commented Jul 8, 2024

Currently in the functionality there is only way to set validation per cell
public function setDataValidation(string $cellCoordinate, ?DataValidation $dataValidation = null): static
and the get also return the range as parts:
$this->worksheet->getDataValidationCollection();

can we let it return as defined in the excel file:
and also set as range instead of cell by cell
Screenshot 2024-07-08 at 16 12 21

@oleibman
Copy link
Collaborator

It would be helpful to know what you have in mind. Changing the way the Data Validations are stored would be quite difficult. But I'm guessing that you want to emulate Excel's Data Validation "Apply these changes to all other cells with the same settings". If that is the case, a less difficult solution presents itself. You would need two new methods - one which returns a list of all cells with the same Data Validation as a given cell, and another which copies a Data Validation to a list of cells. Then you could do something like:

$cellList = $sheet->getIdenticalDataValidationList($targetCell);
// apply changes to $targetCell->dataValidation
$sheet->copyDataValidation($targetCell, $cellList);

Would that satisfy your requirement?

@bokrma
Copy link
Author

bokrma commented Jul 16, 2024

I will try it out, thank you!

@oleibman
Copy link
Collaborator

Solving this may have a lot in common with a solution, if any, for #797. I'm going to think about it for a while.

oleibman added a commit to oleibman/PhpSpreadsheet that referenced this issue Nov 24, 2024
Fix PHPOffice#797 (marked stale in 2018, but now reopened). Fix PHPOffice#4091. DataValidation is specified in the Xml at the sheet level rather than the cell level. PhpSpreadsheet, however, currently requires a cell and a data validation object for each cell in a range. As a consequence, it may exhaust memory allocating objects that are never really needed. If it reads a spreadsheet which applies DataValidation to an entire column, the reader creates a million cells and a million DataValidation objects. An additional problem is that, when it saves the spreadsheet, it creates a row entry for each of the million rows even if they contain no data.

The code is changed to locate DataValidation for a cell by running the DataValidation list to see if the Sqref for any entry matches the cell in question. This eliminates the need to require a DataValidation entry for each cell; indeed, it eliminates the need to create a cell just because it is subject to DataValidation. Initial tests are very encouraging. The memory-exhausting spreadsheet from 797 required over 1GB of memory. With this change in place, that is reduced to 6MB, and DataValidation works as expected for all cells in the range.

This change is a work in progress. It needs formal tests. I need to see how it works with inserting or removing rows and columns. I need to see how Excel handles conflicting entries. My early tests indicate that it at least doesn't do anything irrational (like claiming corruption) when dealing with conflicts. It appears that, if I have a DataValidation applied to column A, and another applied to cell A5, whichever appears first in the Xml is the ultimate arbiter of what rule applies to A5.

If this change is merged, it will be a breaking range. It will require a new major release (probably PhpSpreadsheet 4.0), and will not happen for at least a couple of months, and probably later.
@oleibman oleibman linked a pull request Nov 24, 2024 that will close this issue
11 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

2 participants