Skip to content

Ensure that the sqRef stored for a DataValidation is updated on insert/delete rows/columns #3074

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).

### Fixed

- Fix DataValidation sqRef when inserting/deleting rows/columns [Issue #3056](https://github.com/PHPOffice/PhpSpreadsheet/issues/3056) [PR #3074](https://github.com/PHPOffice/PhpSpreadsheet/pull/3074)
- Named ranges not usable as anchors in OFFSET function [Issue #3013](https://github.com/PHPOffice/PhpSpreadsheet/issues/3013)
- Fully flatten an array [Issue #2955](https://github.com/PHPOffice/PhpSpreadsheet/issues/2955) [PR #2956](https://github.com/PHPOffice/PhpSpreadsheet/pull/2956)
- cellExists() and getCell() methods should support UTF-8 named cells [Issue #2987](https://github.com/PHPOffice/PhpSpreadsheet/issues/2987) [PR #2988](https://github.com/PHPOffice/PhpSpreadsheet/pull/2988)
Expand Down
5 changes: 3 additions & 2 deletions src/PhpSpreadsheet/ReferenceHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,11 @@ protected function adjustDataValidations(Worksheet $worksheet, $numberOfColumns,
? uksort($aDataValidationCollection, [self::class, 'cellReverseSort'])
: uksort($aDataValidationCollection, [self::class, 'cellSort']);

foreach ($aDataValidationCollection as $cellAddress => $value) {
foreach ($aDataValidationCollection as $cellAddress => $dataValidation) {
$newReference = $this->updateCellReference($cellAddress);
if ($cellAddress !== $newReference) {
$worksheet->setDataValidation($newReference, $value);
$dataValidation->setSqref($newReference);
$worksheet->setDataValidation($newReference, $dataValidation);
$worksheet->setDataValidation($cellAddress, null);
}
}
Expand Down
4 changes: 4 additions & 0 deletions tests/PhpSpreadsheetTests/ReferenceHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ public function testInsertRowsWithDataValidation(): void

self::assertFalse($sheet->getCell($cellAddress)->hasDataValidation());
self::assertTrue($sheet->getCell('E7')->hasDataValidation());
self::assertSame('E7', $sheet->getDataValidation('E7')->getSqref());
}

public function testDeleteRowsWithDataValidation(): void
Expand All @@ -326,6 +327,7 @@ public function testDeleteRowsWithDataValidation(): void

self::assertFalse($sheet->getCell($cellAddress)->hasDataValidation());
self::assertTrue($sheet->getCell('E3')->hasDataValidation());
self::assertSame('E3', $sheet->getDataValidation('E3')->getSqref());
}

public function testDeleteColumnsWithDataValidation(): void
Expand All @@ -341,6 +343,7 @@ public function testDeleteColumnsWithDataValidation(): void

self::assertFalse($sheet->getCell($cellAddress)->hasDataValidation());
self::assertTrue($sheet->getCell('C5')->hasDataValidation());
self::assertSame('C5', $sheet->getDataValidation('C5')->getSqref());
}

public function testInsertColumnsWithDataValidation(): void
Expand All @@ -356,6 +359,7 @@ public function testInsertColumnsWithDataValidation(): void

self::assertFalse($sheet->getCell($cellAddress)->hasDataValidation());
self::assertTrue($sheet->getCell('G5')->hasDataValidation());
self::assertSame('G5', $sheet->getDataValidation('G5')->getSqref());
}

private function setDataValidation(Worksheet $sheet, string $cellAddress): void
Expand Down