Relax validation on merge cells to allow input of a single cell
This commit is contained in:
parent
5608e05eda
commit
00dae1bbda
|
|
@ -1752,31 +1752,35 @@ class Worksheet implements IComparable
|
||||||
{
|
{
|
||||||
$range = Functions::trimSheetFromCellReference(Validations::validateCellRange($range));
|
$range = Functions::trimSheetFromCellReference(Validations::validateCellRange($range));
|
||||||
|
|
||||||
if (preg_match('/^([A-Z]+)(\\d+):([A-Z]+)(\\d+)$/', $range, $matches) === 1) {
|
if (strpos($range, ':') === false) {
|
||||||
$this->mergeCells[$range] = $range;
|
$range .= ":{$range}";
|
||||||
$firstRow = (int) $matches[2];
|
}
|
||||||
$lastRow = (int) $matches[4];
|
|
||||||
$firstColumn = $matches[1];
|
|
||||||
$lastColumn = $matches[3];
|
|
||||||
$firstColumnIndex = Coordinate::columnIndexFromString($firstColumn);
|
|
||||||
$lastColumnIndex = Coordinate::columnIndexFromString($lastColumn);
|
|
||||||
$numberRows = $lastRow - $firstRow;
|
|
||||||
$numberColumns = $lastColumnIndex - $firstColumnIndex;
|
|
||||||
|
|
||||||
// create upper left cell if it does not already exist
|
if (preg_match('/^([A-Z]+)(\\d+):([A-Z]+)(\\d+)$/', $range, $matches) !== 1) {
|
||||||
$upperLeft = "{$firstColumn}{$firstRow}";
|
throw new Exception('Merge must be on a valid range of cells.');
|
||||||
if (!$this->cellExists($upperLeft)) {
|
}
|
||||||
$this->getCell($upperLeft)->setValueExplicit(null, DataType::TYPE_NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Blank out the rest of the cells in the range (if they exist)
|
$this->mergeCells[$range] = $range;
|
||||||
if ($numberRows > $numberColumns) {
|
$firstRow = (int) $matches[2];
|
||||||
$this->clearMergeCellsByColumn($firstColumn, $lastColumn, $firstRow, $lastRow, $upperLeft);
|
$lastRow = (int) $matches[4];
|
||||||
} else {
|
$firstColumn = $matches[1];
|
||||||
$this->clearMergeCellsByRow($firstColumn, $lastColumnIndex, $firstRow, $lastRow, $upperLeft);
|
$lastColumn = $matches[3];
|
||||||
}
|
$firstColumnIndex = Coordinate::columnIndexFromString($firstColumn);
|
||||||
|
$lastColumnIndex = Coordinate::columnIndexFromString($lastColumn);
|
||||||
|
$numberRows = $lastRow - $firstRow;
|
||||||
|
$numberColumns = $lastColumnIndex - $firstColumnIndex;
|
||||||
|
|
||||||
|
// create upper left cell if it does not already exist
|
||||||
|
$upperLeft = "{$firstColumn}{$firstRow}";
|
||||||
|
if (!$this->cellExists($upperLeft)) {
|
||||||
|
$this->getCell($upperLeft)->setValueExplicit(null, DataType::TYPE_NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Blank out the rest of the cells in the range (if they exist)
|
||||||
|
if ($numberRows > $numberColumns) {
|
||||||
|
$this->clearMergeCellsByColumn($firstColumn, $lastColumn, $firstRow, $lastRow, $upperLeft);
|
||||||
} else {
|
} else {
|
||||||
throw new Exception('Merge must be set on a range of cells.');
|
$this->clearMergeCellsByRow($firstColumn, $lastColumnIndex, $firstRow, $lastRow, $upperLeft);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
|
|
|
||||||
|
|
@ -100,7 +100,7 @@ class MergedCellTest extends TestCase
|
||||||
$sheet->mergeCells($range);
|
$sheet->mergeCells($range);
|
||||||
self::fail("Expected invalid merge range $range");
|
self::fail("Expected invalid merge range $range");
|
||||||
} catch (SpreadException $e) {
|
} 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();
|
$spreadSheet = new Spreadsheet();
|
||||||
|
|
||||||
$dataSheet = $spreadSheet->getActiveSheet();
|
$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, 'Invalid');
|
||||||
$this->setBadRange($dataSheet, '1');
|
$this->setBadRange($dataSheet, '1');
|
||||||
$this->setBadRange($dataSheet, 'C');
|
$this->setBadRange($dataSheet, 'C');
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue