mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-18 05:57:33 +00:00
Separate DataValidations from Other ReferenceHelper Tests
Also add first tests that DV formulas are updated correctly after row/column insert/delete. Many more tests are still needed.
This commit is contained in:
@@ -248,7 +248,7 @@ class ReferenceHelper
|
||||
* @param int $numberOfColumns Number of columns to insert/delete (negative values indicate deletion)
|
||||
* @param int $numberOfRows Number of rows to insert/delete (negative values indicate deletion)
|
||||
*/
|
||||
protected function adjustDataValidations(Worksheet $worksheet, int $numberOfColumns, int $numberOfRows): void
|
||||
protected function adjustDataValidations(Worksheet $worksheet, int $numberOfColumns, int $numberOfRows, string $beforeCellAddress): void
|
||||
{
|
||||
$aDataValidationCollection = $worksheet->getDataValidationCollection();
|
||||
($numberOfColumns > 0 || $numberOfRows > 0)
|
||||
@@ -256,6 +256,32 @@ class ReferenceHelper
|
||||
: uksort($aDataValidationCollection, [self::class, 'cellSort']);
|
||||
|
||||
foreach ($aDataValidationCollection as $cellAddress => $dataValidation) {
|
||||
$formula = $dataValidation->getFormula1();
|
||||
if ($formula !== '') {
|
||||
$dataValidation->setFormula1(
|
||||
$this->updateFormulaReferences(
|
||||
$formula,
|
||||
$beforeCellAddress,
|
||||
$numberOfColumns,
|
||||
$numberOfRows,
|
||||
$worksheet->getTitle(),
|
||||
true
|
||||
)
|
||||
);
|
||||
}
|
||||
$formula = $dataValidation->getFormula2();
|
||||
if ($formula !== '') {
|
||||
$dataValidation->setFormula2(
|
||||
$this->updateFormulaReferences(
|
||||
$formula,
|
||||
$beforeCellAddress,
|
||||
$numberOfColumns,
|
||||
$numberOfRows,
|
||||
$worksheet->getTitle(),
|
||||
true
|
||||
)
|
||||
);
|
||||
}
|
||||
$newReference = $this->updateCellReference($cellAddress);
|
||||
if ($cellAddress !== $newReference) {
|
||||
$dataValidation->setSqref($newReference);
|
||||
@@ -491,7 +517,7 @@ class ReferenceHelper
|
||||
$this->adjustConditionalFormatting($worksheet, $numberOfColumns, $numberOfRows);
|
||||
|
||||
// Update worksheet: data validations
|
||||
$this->adjustDataValidations($worksheet, $numberOfColumns, $numberOfRows);
|
||||
$this->adjustDataValidations($worksheet, $numberOfColumns, $numberOfRows, $beforeCellAddress);
|
||||
|
||||
// Update worksheet: merge cells
|
||||
$this->adjustMergeCells($worksheet);
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheetTests;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Cell\DataValidation;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ReferenceHelperDVTest extends TestCase
|
||||
{
|
||||
|
||||
public function testInsertRowsWithDataValidation(): void
|
||||
{
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$sheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
$sheet->fromArray([['First'], ['Second'], ['Third'], ['Fourth']], null, 'A5', true);
|
||||
$cellAddress = 'E5';
|
||||
$this->setDataValidation($sheet, $cellAddress);
|
||||
|
||||
$sheet->insertNewRowBefore(2, 2);
|
||||
|
||||
self::assertFalse(
|
||||
$sheet->getCell($cellAddress)->hasDataValidation()
|
||||
);
|
||||
self::assertTrue($sheet->getCell('E7')->hasDataValidation());
|
||||
self::assertSame('E7', $sheet->getDataValidation('E7')->getSqref());
|
||||
self::assertSame('$A$7:$A$10', $sheet->getDataValidation('E7')->getFormula1());
|
||||
$spreadsheet->disconnectWorksheets();
|
||||
}
|
||||
|
||||
public function testDeleteRowsWithDataValidation(): void
|
||||
{
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$sheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
$sheet->fromArray([['First'], ['Second'], ['Third'], ['Fourth']], null, 'A5', true);
|
||||
$cellAddress = 'E5';
|
||||
$this->setDataValidation($sheet, $cellAddress);
|
||||
|
||||
$sheet->removeRow(2, 2);
|
||||
|
||||
self::assertFalse(
|
||||
$sheet->getCell($cellAddress)->hasDataValidation()
|
||||
);
|
||||
self::assertTrue($sheet->getCell('E3')->hasDataValidation());
|
||||
self::assertSame('E3', $sheet->getDataValidation('E3')->getSqref());
|
||||
self::assertSame('$A$3:$A$6', $sheet->getDataValidation('E3')->getFormula1());
|
||||
|
||||
$spreadsheet->disconnectWorksheets();
|
||||
}
|
||||
|
||||
public function testDeleteColumnsWithDataValidation(): void
|
||||
{
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$sheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
$sheet->fromArray([['First'], ['Second'], ['Third'], ['Fourth']], null, 'A5', true);
|
||||
$cellAddress = 'E5';
|
||||
$this->setDataValidation($sheet, $cellAddress);
|
||||
|
||||
$sheet->removeColumn('B', 2);
|
||||
|
||||
self::assertFalse(
|
||||
$sheet->getCell($cellAddress)->hasDataValidation()
|
||||
);
|
||||
self::assertTrue($sheet->getCell('C5')->hasDataValidation());
|
||||
self::assertSame('C5', $sheet->getDataValidation('C5')->getSqref());
|
||||
self::assertSame('$A$5:$A$8', $sheet->getDataValidation('C5')->getFormula1());
|
||||
$spreadsheet->disconnectWorksheets();
|
||||
}
|
||||
|
||||
public function testInsertColumnsWithDataValidation(): void
|
||||
{
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$sheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
$sheet->fromArray([['First'], ['Second'], ['Third'], ['Fourth']], null, 'A5', true);
|
||||
$cellAddress = 'E5';
|
||||
$this->setDataValidation($sheet, $cellAddress);
|
||||
|
||||
$sheet->insertNewColumnBefore('C', 2);
|
||||
|
||||
self::assertFalse(
|
||||
$sheet->getCell($cellAddress)->hasDataValidation()
|
||||
);
|
||||
self::assertTrue($sheet->getCell('G5')->hasDataValidation());
|
||||
self::assertSame('G5', $sheet->getDataValidation('G5')->getSqref());
|
||||
self::assertSame('$A$5:$A$8', $sheet->getDataValidation('G5')->getFormula1());
|
||||
$spreadsheet->disconnectWorksheets();
|
||||
}
|
||||
|
||||
public function testInsertColumnsWithDataValidation2(): void
|
||||
{
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$sheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
$sheet->fromArray([['First'], ['Second'], ['Third'], ['Fourth']], null, 'A5', true);
|
||||
$cellAddress = 'E5';
|
||||
$this->setDataValidation($sheet, $cellAddress);
|
||||
|
||||
$sheet->insertNewColumnBefore('A', 2);
|
||||
|
||||
self::assertFalse(
|
||||
$sheet->getCell($cellAddress)->hasDataValidation()
|
||||
);
|
||||
self::assertTrue($sheet->getCell('G5')->hasDataValidation());
|
||||
self::assertSame('G5', $sheet->getDataValidation('G5')->getSqref());
|
||||
self::assertSame('$C$5:$C$8', $sheet->getDataValidation('G5')->getFormula1());
|
||||
$spreadsheet->disconnectWorksheets();
|
||||
}
|
||||
|
||||
private function setDataValidation(Worksheet $sheet, string $cellAddress): void
|
||||
{
|
||||
$validation = $sheet->getCell($cellAddress)
|
||||
->getDataValidation();
|
||||
$validation->setType(DataValidation::TYPE_LIST);
|
||||
$validation->setErrorStyle(
|
||||
DataValidation::STYLE_STOP
|
||||
);
|
||||
$validation->setAllowBlank(false);
|
||||
$validation->setShowInputMessage(true);
|
||||
$validation->setShowErrorMessage(true);
|
||||
$validation->setShowDropDown(true);
|
||||
$validation->setErrorTitle('Input error');
|
||||
$validation->setError('Value is not in list.');
|
||||
$validation->setPromptTitle('Pick from list');
|
||||
$validation->setPrompt('Please pick a value from the drop-down list.');
|
||||
$validation->setFormula1('$A$5:$A$8');
|
||||
}
|
||||
}
|
||||
@@ -102,9 +102,7 @@ class ReferenceHelperTest extends TestCase
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerFormulaUpdates
|
||||
*/
|
||||
#[\PHPUnit\Framework\Attributes\DataProvider('providerFormulaUpdates')]
|
||||
public function testUpdateFormula(string $formula, int $insertRows, int $insertColumns, string $worksheet, string $expectedResult): void
|
||||
{
|
||||
$referenceHelper = ReferenceHelper::getInstance();
|
||||
@@ -119,9 +117,7 @@ class ReferenceHelperTest extends TestCase
|
||||
return require 'tests/data/ReferenceHelperFormulaUpdates.php';
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerMultipleWorksheetFormulaUpdates
|
||||
*/
|
||||
#[\PHPUnit\Framework\Attributes\DataProvider('providerMultipleWorksheetFormulaUpdates')]
|
||||
public function testUpdateFormulaForMultipleWorksheets(string $formula, int $insertRows, int $insertColumns, string $expectedResult): void
|
||||
{
|
||||
$referenceHelper = ReferenceHelper::getInstance();
|
||||
@@ -298,91 +294,6 @@ class ReferenceHelperTest extends TestCase
|
||||
$spreadsheet->disconnectWorksheets();
|
||||
}
|
||||
|
||||
public function testInsertRowsWithDataValidation(): void
|
||||
{
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$sheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
$sheet->fromArray([['First'], ['Second'], ['Third'], ['Fourth']], null, 'A5', true);
|
||||
$cellAddress = 'E5';
|
||||
$this->setDataValidation($sheet, $cellAddress);
|
||||
|
||||
$sheet->insertNewRowBefore(2, 2);
|
||||
|
||||
self::assertFalse($sheet->getCell($cellAddress)->hasDataValidation());
|
||||
self::assertTrue($sheet->getCell('E7')->hasDataValidation());
|
||||
self::assertSame('E7', $sheet->getDataValidation('E7')->getSqref());
|
||||
$spreadsheet->disconnectWorksheets();
|
||||
}
|
||||
|
||||
public function testDeleteRowsWithDataValidation(): void
|
||||
{
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$sheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
$sheet->fromArray([['First'], ['Second'], ['Third'], ['Fourth']], null, 'A5', true);
|
||||
$cellAddress = 'E5';
|
||||
$this->setDataValidation($sheet, $cellAddress);
|
||||
|
||||
$sheet->removeRow(2, 2);
|
||||
|
||||
self::assertFalse($sheet->getCell($cellAddress)->hasDataValidation());
|
||||
self::assertTrue($sheet->getCell('E3')->hasDataValidation());
|
||||
self::assertSame('E3', $sheet->getDataValidation('E3')->getSqref());
|
||||
$spreadsheet->disconnectWorksheets();
|
||||
}
|
||||
|
||||
public function testDeleteColumnsWithDataValidation(): void
|
||||
{
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$sheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
$sheet->fromArray([['First'], ['Second'], ['Third'], ['Fourth']], null, 'A5', true);
|
||||
$cellAddress = 'E5';
|
||||
$this->setDataValidation($sheet, $cellAddress);
|
||||
|
||||
$sheet->removeColumn('B', 2);
|
||||
|
||||
self::assertFalse($sheet->getCell($cellAddress)->hasDataValidation());
|
||||
self::assertTrue($sheet->getCell('C5')->hasDataValidation());
|
||||
self::assertSame('C5', $sheet->getDataValidation('C5')->getSqref());
|
||||
$spreadsheet->disconnectWorksheets();
|
||||
}
|
||||
|
||||
public function testInsertColumnsWithDataValidation(): void
|
||||
{
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$sheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
$sheet->fromArray([['First'], ['Second'], ['Third'], ['Fourth']], null, 'A5', true);
|
||||
$cellAddress = 'E5';
|
||||
$this->setDataValidation($sheet, $cellAddress);
|
||||
|
||||
$sheet->insertNewColumnBefore('C', 2);
|
||||
|
||||
self::assertFalse($sheet->getCell($cellAddress)->hasDataValidation());
|
||||
self::assertTrue($sheet->getCell('G5')->hasDataValidation());
|
||||
self::assertSame('G5', $sheet->getDataValidation('G5')->getSqref());
|
||||
$spreadsheet->disconnectWorksheets();
|
||||
}
|
||||
|
||||
private function setDataValidation(Worksheet $sheet, string $cellAddress): void
|
||||
{
|
||||
$validation = $sheet->getCell($cellAddress)
|
||||
->getDataValidation();
|
||||
$validation->setType(\PhpOffice\PhpSpreadsheet\Cell\DataValidation::TYPE_LIST);
|
||||
$validation->setErrorStyle(\PhpOffice\PhpSpreadsheet\Cell\DataValidation::STYLE_INFORMATION);
|
||||
$validation->setAllowBlank(false);
|
||||
$validation->setShowInputMessage(true);
|
||||
$validation->setShowErrorMessage(true);
|
||||
$validation->setShowDropDown(true);
|
||||
$validation->setErrorTitle('Input error');
|
||||
$validation->setError('Value is not in list.');
|
||||
$validation->setPromptTitle('Pick from list');
|
||||
$validation->setPrompt('Please pick a value from the drop-down list.');
|
||||
$validation->setFormula1('$A5:$A8');
|
||||
}
|
||||
|
||||
public function testInsertRowsWithConditionalFormatting(): void
|
||||
{
|
||||
$spreadsheet = new Spreadsheet();
|
||||
|
||||
Reference in New Issue
Block a user