Skip to content

Commit 803737a

Browse files
otargetj2sMark Baker
and
Mark Baker
authored
Fix for #2149 / Read data validations for drop down list in another sheet. (#2150)
* Read data validations for drop down list in another sheet. * Add function testLoadXlsxDataValidationOfAnotherSheet() in class tests/PhpSpreadsheetTests/Reader/XlsxTest.php for unit test. * Add sample xlsx for unit tests. * Modifiy call function isset() for warnings. * Additional assertions to ensure that the worksheet has been read correctly for DataValidation that references a list on a different worksheet * This should resolve the phpstan issues Co-authored-by: Mark Baker <[email protected]>
1 parent 1e74282 commit 803737a

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

src/PhpSpreadsheet/Reader/Xlsx.php

+16
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,22 @@ public function load(string $pFilename, int $flags = 0)
797797
$unparsedLoadedData = (new PageSetup($docSheet, $xmlSheet))->load($unparsedLoadedData);
798798
}
799799

800+
if ($xmlSheet !== false && isset($xmlSheet->extLst, $xmlSheet->extLst->ext, $xmlSheet->extLst->ext['uri']) && ($xmlSheet->extLst->ext['uri'] == '{CCE6A557-97BC-4b89-ADB6-D9C93CAAB3DF}')) {
801+
// Create dataValidations node if does not exists, maybe is better inside the foreach ?
802+
if (!$xmlSheet->dataValidations) {
803+
$xmlSheet->addChild('dataValidations');
804+
}
805+
806+
foreach ($xmlSheet->extLst->ext->children('x14', true)->dataValidations->dataValidation as $item) {
807+
$node = $xmlSheet->dataValidations->addChild('dataValidation');
808+
foreach ($item->attributes() as $attr) {
809+
$node->addAttribute($attr->getName(), $attr);
810+
}
811+
$node->addAttribute('sqref', $item->children('xm', true)->sqref);
812+
$node->addChild('formula1', $item->formula1->children('xm', true)->f);
813+
}
814+
}
815+
800816
if ($xmlSheet && $xmlSheet->dataValidations && !$this->readDataOnly) {
801817
(new DataValidations($docSheet, $xmlSheet))->load();
802818
}

tests/PhpSpreadsheetTests/Reader/XlsxTest.php

+26
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PhpOffice\PhpSpreadsheetTests\Reader;
44

55
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
6+
use PhpOffice\PhpSpreadsheet\Cell\DataValidation;
67
use PhpOffice\PhpSpreadsheet\Document\Properties;
78
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
89
use PhpOffice\PhpSpreadsheet\Shared\File;
@@ -147,6 +148,31 @@ public function testLoadXlsxDataValidation(): void
147148
self::assertTrue($worksheet->getCell('B3')->hasDataValidation());
148149
}
149150

151+
/*
152+
* Test for load drop down lists of another sheet.
153+
* Pull #2150, issue #2149
154+
*/
155+
public function testLoadXlsxDataValidationOfAnotherSheet(): void
156+
{
157+
$filename = 'tests/data/Reader/XLSX/dataValidation2Test.xlsx';
158+
$reader = new Xlsx();
159+
$spreadsheet = $reader->load($filename);
160+
161+
$worksheet = $spreadsheet->getActiveSheet();
162+
163+
// same sheet
164+
$validationCell = $worksheet->getCell('B5');
165+
self::assertTrue($validationCell->hasDataValidation());
166+
self::assertSame(DataValidation::TYPE_LIST, $validationCell->getDataValidation()->getType());
167+
self::assertSame('$A$5:$A$7', $validationCell->getDataValidation()->getFormula1());
168+
169+
// another sheet
170+
$validationCell = $worksheet->getCell('B14');
171+
self::assertTrue($validationCell->hasDataValidation());
172+
self::assertSame(DataValidation::TYPE_LIST, $validationCell->getDataValidation()->getType());
173+
self::assertSame('Feuil2!$A$3:$A$5', $validationCell->getDataValidation()->getFormula1());
174+
}
175+
150176
/**
151177
* Test load Xlsx file without cell reference.
152178
*
10.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)