Modify getCell() methods to allow creating a new cell if the selected cell doesn't exist as an option

This commit is contained in:
MarkBaker 2015-12-28 23:59:09 +00:00
parent 0025318d7e
commit 1926c652db
1 changed files with 21 additions and 14 deletions

View File

@ -398,7 +398,7 @@ class Worksheet implements IComparable
public function getCellCacheController() public function getCellCacheController()
{ {
return $this->cellCollection; return $this->cellCollection;
} // function getCellCacheController() }
/** /**
@ -729,8 +729,8 @@ class Worksheet implements IComparable
// loop through all cells in the worksheet // loop through all cells in the worksheet
foreach ($this->getCellCollection(false) as $cellID) { foreach ($this->getCellCollection(false) as $cellID) {
$cell = $this->getCell($cellID); $cell = $this->getCell($cellID, false);
if (isset($autoSizes[$this->cellCollection->getCurrentColumn()])) { if ($cell !== null && isset($autoSizes[$this->cellCollection->getCurrentColumn()])) {
// Determine width if cell does not participate in a merge // Determine width if cell does not participate in a merge
if (!isset($isMergeCell[$this->cellCollection->getCurrentAddress()])) { if (!isset($isMergeCell[$this->cellCollection->getCurrentAddress()])) {
// Calculated value // Calculated value
@ -1139,10 +1139,12 @@ class Worksheet implements IComparable
* Get cell at a specific coordinate * Get cell at a specific coordinate
* *
* @param string $pCoordinate Coordinate of the cell * @param string $pCoordinate Coordinate of the cell
* @param boolean $createIfNotExists Flag indicating whether a new cell should be created if it doesn't
* already exist, or a null should be returned instead
* @throws Exception * @throws Exception
* @return Cell Cell that was found * @return null|Cell Cell that was found/created or null
*/ */
public function getCell($pCoordinate = 'A1') public function getCell($pCoordinate = 'A1', $createIfNotExists = true)
{ {
// Check cell collection // Check cell collection
if ($this->cellCollection->isDataSet(strtoupper($pCoordinate))) { if ($this->cellCollection->isDataSet(strtoupper($pCoordinate))) {
@ -1152,7 +1154,7 @@ class Worksheet implements IComparable
// Worksheet reference? // Worksheet reference?
if (strpos($pCoordinate, '!') !== false) { if (strpos($pCoordinate, '!') !== false) {
$worksheetReference = Worksheet::extractSheetTitle($pCoordinate, true); $worksheetReference = Worksheet::extractSheetTitle($pCoordinate, true);
return $this->parent->getSheetByName($worksheetReference[0])->getCell(strtoupper($worksheetReference[1])); return $this->parent->getSheetByName($worksheetReference[0])->getCell(strtoupper($worksheetReference[1]), $createIfNotExists);
} }
// Named range? // Named range?
@ -1161,7 +1163,7 @@ class Worksheet implements IComparable
$namedRange = NamedRange::resolveRange($pCoordinate, $this); $namedRange = NamedRange::resolveRange($pCoordinate, $this);
if ($namedRange !== null) { if ($namedRange !== null) {
$pCoordinate = $namedRange->getRange(); $pCoordinate = $namedRange->getRange();
return $namedRange->getWorksheet()->getCell($pCoordinate); return $namedRange->getWorksheet()->getCell($pCoordinate, $createIfNotExists);
} }
} }
@ -1174,8 +1176,8 @@ class Worksheet implements IComparable
throw new Exception('Cell coordinate must not be absolute.'); throw new Exception('Cell coordinate must not be absolute.');
} }
// Create new cell object // Create new cell object, if required
return $this->createNewCell($pCoordinate); return $createIfNotExists ? $this->createNewCell($pCoordinate) : null;
} }
/** /**
@ -1183,9 +1185,11 @@ class Worksheet implements IComparable
* *
* @param string $pColumn Numeric column coordinate of the cell * @param string $pColumn Numeric column coordinate of the cell
* @param string $pRow Numeric row coordinate of the cell * @param string $pRow Numeric row coordinate of the cell
* @return Cell Cell that was found * @param boolean $createIfNotExists Flag indicating whether a new cell should be created if it doesn't
* already exist, or a null should be returned instead
* @return null|Cell Cell that was found/created or null
*/ */
public function getCellByColumnAndRow($pColumn = 0, $pRow = 1) public function getCellByColumnAndRow($pColumn = 0, $pRow = 1, $createIfNotExists = true)
{ {
$columnLetter = Cell::stringFromColumnIndex($pColumn); $columnLetter = Cell::stringFromColumnIndex($pColumn);
$coordinate = $columnLetter . $pRow; $coordinate = $columnLetter . $pRow;
@ -1194,7 +1198,8 @@ class Worksheet implements IComparable
return $this->cellCollection->getCacheData($coordinate); return $this->cellCollection->getCacheData($coordinate);
} }
return $this->createNewCell($coordinate); // Create new cell object, if required
return $createIfNotExists ? $this->createNewCell($coordinate) : null;
} }
/** /**
@ -1694,10 +1699,12 @@ class Worksheet implements IComparable
$this->getCell($upperLeft)->setValueExplicit(null, Cell\DataType::TYPE_NULL); $this->getCell($upperLeft)->setValueExplicit(null, Cell\DataType::TYPE_NULL);
} }
// create or blank out the rest of the cells in the range // Blank out the rest of the cells in the range (if they exist)
$count = count($aReferences); $count = count($aReferences);
for ($i = 1; $i < $count; $i++) { for ($i = 1; $i < $count; $i++) {
$this->getCell($aReferences[$i])->setValueExplicit(null, Cell\DataType::TYPE_NULL); if ($this->cellExists($aReferences[$i])) {
$this->getCell($aReferences[$i])->setValueExplicit(null, Cell\DataType::TYPE_NULL);
}
} }
} else { } else {
throw new Exception('Merge must be set on a range of cells.'); throw new Exception('Merge must be set on a range of cells.');