Skip to content

Commit 12cf215

Browse files
authored
Merge pull request #3074 from PHPOffice/Issue-3056_Update-DataValidation-SqRef-On-Insert-Delete-Rows-Columns
Ensure that the sqRef stored for a DataValidation is updated on insert/delete rows/columns
2 parents c465b1c + a6cfd8c commit 12cf215

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
3838

3939
### Fixed
4040

41+
- 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)
4142
- Named ranges not usable as anchors in OFFSET function [Issue #3013](https://github.com/PHPOffice/PhpSpreadsheet/issues/3013)
4243
- Fully flatten an array [Issue #2955](https://github.com/PHPOffice/PhpSpreadsheet/issues/2955) [PR #2956](https://github.com/PHPOffice/PhpSpreadsheet/pull/2956)
4344
- 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)

src/PhpSpreadsheet/ReferenceHelper.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,11 @@ protected function adjustDataValidations(Worksheet $worksheet, $numberOfColumns,
253253
? uksort($aDataValidationCollection, [self::class, 'cellReverseSort'])
254254
: uksort($aDataValidationCollection, [self::class, 'cellSort']);
255255

256-
foreach ($aDataValidationCollection as $cellAddress => $value) {
256+
foreach ($aDataValidationCollection as $cellAddress => $dataValidation) {
257257
$newReference = $this->updateCellReference($cellAddress);
258258
if ($cellAddress !== $newReference) {
259-
$worksheet->setDataValidation($newReference, $value);
259+
$dataValidation->setSqref($newReference);
260+
$worksheet->setDataValidation($newReference, $dataValidation);
260261
$worksheet->setDataValidation($cellAddress, null);
261262
}
262263
}

tests/PhpSpreadsheetTests/ReferenceHelperTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ public function testInsertRowsWithDataValidation(): void
311311

312312
self::assertFalse($sheet->getCell($cellAddress)->hasDataValidation());
313313
self::assertTrue($sheet->getCell('E7')->hasDataValidation());
314+
self::assertSame('E7', $sheet->getDataValidation('E7')->getSqref());
314315
}
315316

316317
public function testDeleteRowsWithDataValidation(): void
@@ -326,6 +327,7 @@ public function testDeleteRowsWithDataValidation(): void
326327

327328
self::assertFalse($sheet->getCell($cellAddress)->hasDataValidation());
328329
self::assertTrue($sheet->getCell('E3')->hasDataValidation());
330+
self::assertSame('E3', $sheet->getDataValidation('E3')->getSqref());
329331
}
330332

331333
public function testDeleteColumnsWithDataValidation(): void
@@ -341,6 +343,7 @@ public function testDeleteColumnsWithDataValidation(): void
341343

342344
self::assertFalse($sheet->getCell($cellAddress)->hasDataValidation());
343345
self::assertTrue($sheet->getCell('C5')->hasDataValidation());
346+
self::assertSame('C5', $sheet->getDataValidation('C5')->getSqref());
344347
}
345348

346349
public function testInsertColumnsWithDataValidation(): void
@@ -356,6 +359,7 @@ public function testInsertColumnsWithDataValidation(): void
356359

357360
self::assertFalse($sheet->getCell($cellAddress)->hasDataValidation());
358361
self::assertTrue($sheet->getCell('G5')->hasDataValidation());
362+
self::assertSame('G5', $sheet->getDataValidation('G5')->getSqref());
359363
}
360364

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

0 commit comments

Comments
 (0)