Merge pull request #2877 from PHPOffice/Issue-2776_Allow-Merge-Cells-for-single-cell

Relax validation on merge cells to allow input of a single cell
This commit is contained in:
Mark Baker 2022-06-10 01:27:29 +02:00 committed by GitHub
commit 1f1fc360af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 24 deletions

View File

@ -1752,7 +1752,14 @@ class Worksheet implements IComparable
{
$range = Functions::trimSheetFromCellReference(Validations::validateCellRange($range));
if (preg_match('/^([A-Z]+)(\\d+):([A-Z]+)(\\d+)$/', $range, $matches) === 1) {
if (strpos($range, ':') === false) {
$range .= ":{$range}";
}
if (preg_match('/^([A-Z]+)(\\d+):([A-Z]+)(\\d+)$/', $range, $matches) !== 1) {
throw new Exception('Merge must be on a valid range of cells.');
}
$this->mergeCells[$range] = $range;
$firstRow = (int) $matches[2];
$lastRow = (int) $matches[4];
@ -1775,9 +1782,6 @@ class Worksheet implements IComparable
} else {
$this->clearMergeCellsByRow($firstColumn, $lastColumnIndex, $firstRow, $lastRow, $upperLeft);
}
} else {
throw new Exception('Merge must be set on a range of cells.');
}
return $this;
}

View File

@ -100,7 +100,7 @@ class MergedCellTest extends TestCase
$sheet->mergeCells($range);
self::fail("Expected invalid merge range $range");
} catch (SpreadException $e) {
self::assertSame('Merge must be set on a range of cells.', $e->getMessage());
self::assertSame('Merge must be on a valid range of cells.', $e->getMessage());
}
}
@ -109,7 +109,8 @@ class MergedCellTest extends TestCase
$spreadSheet = new Spreadsheet();
$dataSheet = $spreadSheet->getActiveSheet();
$this->setBadRange($dataSheet, 'B1');
// TODO - Reinstate full validation and disallow single cell merging for version 2.0
// $this->setBadRange($dataSheet, 'B1');
$this->setBadRange($dataSheet, 'Invalid');
$this->setBadRange($dataSheet, '1');
$this->setBadRange($dataSheet, 'C');