-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Data Validations Referencing Another Sheet (#2265)
See issues #1432 and #2149. Data validations on an Xlsx worksheet can be specified in two manners - one (henceforth "internal") if a list is specified from the same sheet, and a different one (henceforth "external") if a list is specified from a different sheet. Xlsx worksheet reader formerly processed only the internal format; PR #2150 fixed this so that both would be processed correctly on read. However, Xlsx worksheet writer outputs data validators only in the internal format, and that does not work for external data validations; it appears, however, that internal data validations can be specified in external format. This PR changes Xlsx worksheet writer to use only the external format. Somewhat surprisingly, this must come after most of the other XML tags that constitute a worksheet. It shares this characteristic (and XML tag) with conditional formatting. The new test case DataValidator2Test includes a worksheet which has both internal and external data validation, as well as conditional formatting. There is some additional namespacing work supporting Data Validations that needs to happen on Xlsx reader. Since that is substantially unchanged with this PR, that work will happen in a future namespacing phase, probably phase 2. However, there are some non-namespace-related changes to Xlsx reader in this PR: - Cell DataValidation adds support for a new property sqref, which is initialized through Xlsx reader using a setSqref method. If not initialized at write time, the code will work as it did before the introduction of this property. In particular, before this change, data validation applied to an entire column (as in the sample spreadsheet) would be applied only through the last populated row. In addition, this also allows a user to extend a Data Validation over a range of cells rather than just a single cell; the new method is added to the documentation. - The topLeft property had formerly been used only for worksheets which use "freeze panes". However, as luck would have it, the sample dataset provided to demonstrate the Data Validations problem uses topLeft without freeze panes, slightly affecting the view when the spreadsheet is initially opened; PhpSpreadsheet will now do so as well. It is worth noting issue #2262, which documents a problem with the hasValidValue method involving the calculation engine. That problem existed before this PR, and I do not yet have a handle on how it might be fixed.
- Loading branch information
Showing
9 changed files
with
144 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
namespace PhpOffice\PhpSpreadsheetTests\Cell; | ||
|
||
use PhpOffice\PhpSpreadsheet\Cell\DataValidation; | ||
use PhpOffice\PhpSpreadsheet\Reader\Xlsx; | ||
use PhpOffice\PhpSpreadsheet\Style\Conditional; | ||
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional; | ||
|
||
class DataValidator2Test extends AbstractFunctional | ||
{ | ||
public function testList(): void | ||
{ | ||
$reader = new Xlsx(); | ||
$spreadsheet = $reader->load('tests/data/Reader/XLSX/issue.1432b.xlsx'); | ||
$sheet = $spreadsheet->getActiveSheet(); | ||
self::assertSame('H1', $sheet->getTopLeftCell()); | ||
self::assertSame('K3', $sheet->getSelectedCells()); | ||
|
||
$testCell = $sheet->getCell('K3'); | ||
$validation = $testCell->getDataValidation(); | ||
self::assertSame(DataValidation::TYPE_LIST, $validation->getType()); | ||
|
||
$testCell = $sheet->getCell('R2'); | ||
$validation = $testCell->getDataValidation(); | ||
self::assertSame(DataValidation::TYPE_LIST, $validation->getType()); | ||
|
||
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx'); | ||
$sheet = $reloadedSpreadsheet->getActiveSheet(); | ||
|
||
$cell = 'K3'; | ||
$testCell = $sheet->getCell($cell); | ||
$validation = $testCell->getDataValidation(); | ||
self::assertSame(DataValidation::TYPE_LIST, $validation->getType()); | ||
$testCell->setValue('Y'); | ||
self::assertTrue($testCell->hasValidValue(), 'K3 other sheet has valid value'); | ||
$testCell = $sheet->getCell($cell); | ||
$testCell->setValue('X'); | ||
self::assertFalse($testCell->hasValidValue(), 'K3 other sheet has invalid value'); | ||
|
||
$cell = 'J2'; | ||
$testCell = $sheet->getCell($cell); | ||
$validation = $testCell->getDataValidation(); | ||
self::assertSame(DataValidation::TYPE_LIST, $validation->getType()); | ||
$testCell = $sheet->getCell($cell); | ||
$testCell->setValue('GBP'); | ||
self::assertTrue($testCell->hasValidValue(), 'J2 other sheet has valid value'); | ||
$testCell = $sheet->getCell($cell); | ||
$testCell->setValue('XYZ'); | ||
self::assertFalse($testCell->hasValidValue(), 'J2 other sheet has invalid value'); | ||
|
||
$cell = 'R2'; | ||
$testCell = $sheet->getCell($cell); | ||
$validation = $testCell->getDataValidation(); | ||
self::assertSame(DataValidation::TYPE_LIST, $validation->getType()); | ||
$testCell->setValue('ListItem2'); | ||
self::assertTrue($testCell->hasValidValue(), 'R2 same sheet has valid value'); | ||
$testCell = $sheet->getCell($cell); | ||
$testCell->setValue('ListItem99'); | ||
self::assertFalse($testCell->hasValidValue(), 'R2 same sheet has invalid value'); | ||
|
||
$styles = $sheet->getConditionalStyles('I1:I1048576'); | ||
self::assertCount(1, $styles); | ||
$style = $styles[0]; | ||
self::assertSame(Conditional::CONDITION_CELLIS, $style->getConditionType()); | ||
self::assertSame(Conditional::OPERATOR_BETWEEN, $style->getOperatorType()); | ||
$conditions = $style->getConditions(); | ||
self::assertSame('10', $conditions[0]); | ||
self::assertSame('20', $conditions[1]); | ||
self::assertSame('FF70AD47', $style->getStyle()->getFill()->getEndColor()->getARGB()); | ||
|
||
$spreadsheet->disconnectWorksheets(); | ||
$reloadedSpreadsheet->disconnectWorksheets(); | ||
} | ||
} |
Binary file not shown.