Skip to content

Commit

Permalink
Fix for #2149 / Read data validations for drop down list in another s…
Browse files Browse the repository at this point in the history
…heet. (#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]>
  • Loading branch information
otargetj2s and Mark Baker authored Jun 15, 2021
1 parent 1e74282 commit 803737a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/PhpSpreadsheet/Reader/Xlsx.php
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,22 @@ public function load(string $pFilename, int $flags = 0)
$unparsedLoadedData = (new PageSetup($docSheet, $xmlSheet))->load($unparsedLoadedData);
}

if ($xmlSheet !== false && isset($xmlSheet->extLst, $xmlSheet->extLst->ext, $xmlSheet->extLst->ext['uri']) && ($xmlSheet->extLst->ext['uri'] == '{CCE6A557-97BC-4b89-ADB6-D9C93CAAB3DF}')) {
// Create dataValidations node if does not exists, maybe is better inside the foreach ?
if (!$xmlSheet->dataValidations) {
$xmlSheet->addChild('dataValidations');
}

foreach ($xmlSheet->extLst->ext->children('x14', true)->dataValidations->dataValidation as $item) {
$node = $xmlSheet->dataValidations->addChild('dataValidation');
foreach ($item->attributes() as $attr) {
$node->addAttribute($attr->getName(), $attr);
}
$node->addAttribute('sqref', $item->children('xm', true)->sqref);
$node->addChild('formula1', $item->formula1->children('xm', true)->f);
}
}

if ($xmlSheet && $xmlSheet->dataValidations && !$this->readDataOnly) {
(new DataValidations($docSheet, $xmlSheet))->load();
}
Expand Down
26 changes: 26 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/XlsxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PhpOffice\PhpSpreadsheetTests\Reader;

use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Cell\DataValidation;
use PhpOffice\PhpSpreadsheet\Document\Properties;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Shared\File;
Expand Down Expand Up @@ -147,6 +148,31 @@ public function testLoadXlsxDataValidation(): void
self::assertTrue($worksheet->getCell('B3')->hasDataValidation());
}

/*
* Test for load drop down lists of another sheet.
* Pull #2150, issue #2149
*/
public function testLoadXlsxDataValidationOfAnotherSheet(): void
{
$filename = 'tests/data/Reader/XLSX/dataValidation2Test.xlsx';
$reader = new Xlsx();
$spreadsheet = $reader->load($filename);

$worksheet = $spreadsheet->getActiveSheet();

// same sheet
$validationCell = $worksheet->getCell('B5');
self::assertTrue($validationCell->hasDataValidation());
self::assertSame(DataValidation::TYPE_LIST, $validationCell->getDataValidation()->getType());
self::assertSame('$A$5:$A$7', $validationCell->getDataValidation()->getFormula1());

// another sheet
$validationCell = $worksheet->getCell('B14');
self::assertTrue($validationCell->hasDataValidation());
self::assertSame(DataValidation::TYPE_LIST, $validationCell->getDataValidation()->getType());
self::assertSame('Feuil2!$A$3:$A$5', $validationCell->getDataValidation()->getFormula1());
}

/**
* Test load Xlsx file without cell reference.
*
Expand Down
Binary file added tests/data/Reader/XLSX/dataValidation2Test.xlsx
Binary file not shown.

0 comments on commit 803737a

Please sign in to comment.