You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Description:
When adding data validation (drop-down list) using PhpSpreadsheet, the validation either:
Does not appear in Excel after saving the file.
Is duplicated in the sheet1.xml file inside the .xlsx structure, causing unexpected behavior.
After debugging, it seems that PhpSpreadsheet does not properly handle removing old data validations, which may lead to duplication when re-adding validation to the same cell.
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Cell\DataValidation;
// Create new spreadsheet
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
// Set dropdown values in column B
$sheet->setCellValue('B1', 'Option 1');
$sheet->setCellValue('B2', 'Option 2');
$sheet->setCellValue('B3', 'Option 3');
// Create validation
$validation = $sheet->getCell('A1')->getDataValidation();
$validation->setType(DataValidation::TYPE_LIST);
$validation->setErrorStyle(DataValidation::STYLE_STOP);
$validation->setAllowBlank(false);
$validation->setShowDropDown(true);
$validation->setFormula1('$B$1:$B$3'); // 🔹 Reference for dropdown list
// Save file
$writer = new Xlsx($spreadsheet);
$writer->save('test.xlsx');`
Expected Behavior:
The drop-down should appear in A1 referencing B1:B3.
No duplicate or entries in sheet1.xml.
Actual Behavior:
The generated sheet1.xml contains duplicate elements, which may cause conflicts in Excel.
In some cases, validation does not appear in Excel when opening the file.
If setFormula1() is not explicitly set, the validation does not work properly.
Temporary Workaround (Using ZipArchive):
Since PhpSpreadsheet does not expose a removeDataValidation() method, a workaround is to manually edit sheet1.xml inside the .xlsx file using ZipArchive.
This issue may be related to Excel's extLst schema handling.
The getDataValidations() or removeDataValidation() methods are missing in PhpSpreadsheet, which may be causing this issue.
Forking the repo and modifying Worksheet.php might be required to properly remove or replace existing validation instead of adding duplicate entries.
Would appreciate any insights from the maintainers! 🙌
I am sorry, I am not able to help with any unsupported versions. You will have to upgrade to at least PhpSpreadsheet 1.29.10, and Php 8.1 (in a pinch Php 7.4, but not for much longer).
Description:
When adding data validation (drop-down list) using PhpSpreadsheet, the validation either:
After debugging, it seems that PhpSpreadsheet does not properly handle removing old data validations, which may lead to duplication when re-adding validation to the same cell.
Expected Behavior:
Actual Behavior:
The generated sheet1.xml contains duplicate elements, which may cause conflicts in Excel.
Temporary Workaround (Using ZipArchive):
Since PhpSpreadsheet does not expose a removeDataValidation() method, a workaround is to manually edit sheet1.xml inside the .xlsx file using ZipArchive.
Additional Notes:
Would appreciate any insights from the maintainers! 🙌
Link to Example File:
simple_example_debug.xlsx
The text was updated successfully, but these errors were encountered: