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:26:15 +00:00
parent 6f43f0429b
commit 7eb10adb3b

View File

@ -396,7 +396,7 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable
public function getCellCacheController()
{
return $this->cellCollection;
} // function getCellCacheController()
}
/**
@ -727,8 +727,8 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable
// loop through all cells in the worksheet
foreach ($this->getCellCollection(false) as $cellID) {
$cell = $this->getCell($cellID);
if (isset($autoSizes[$this->cellCollection->getCurrentColumn()])) {
$cell = $this->getCell($cellID, false);
if ($cell !== null && isset($autoSizes[$this->cellCollection->getCurrentColumn()])) {
// Determine width if cell does not participate in a merge
if (!isset($isMergeCell[$this->cellCollection->getCurrentAddress()])) {
// Calculated value
@ -1137,10 +1137,12 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable
* Get cell at a specific coordinate
*
* @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 PHPExcel_Exception
* @return PHPExcel_Cell Cell that was found
* @return null|PHPExcel_Cell Cell that was found/created or null
*/
public function getCell($pCoordinate = 'A1')
public function getCell($pCoordinate = 'A1', $createIfNotExists = true)
{
// Check cell collection
if ($this->cellCollection->isDataSet(strtoupper($pCoordinate))) {
@ -1150,7 +1152,7 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable
// Worksheet reference?
if (strpos($pCoordinate, '!') !== false) {
$worksheetReference = PHPExcel_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?
@ -1159,7 +1161,7 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable
$namedRange = PHPExcel_NamedRange::resolveRange($pCoordinate, $this);
if ($namedRange !== null) {
$pCoordinate = $namedRange->getRange();
return $namedRange->getWorksheet()->getCell($pCoordinate);
return $namedRange->getWorksheet()->getCell($pCoordinate, $createIfNotExists);
}
}
@ -1172,18 +1174,20 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable
throw new PHPExcel_Exception('Cell coordinate must not be absolute.');
}
// Create new cell object
return $this->createNewCell($pCoordinate);
// Create new cell object, if required
return $createIfNotExists ? $this->createNewCell($pCoordinate) : null;
}
/**
* Get cell at a specific coordinate by using numeric cell coordinates
*
* @param string $pColumn Numeric column coordinate of the cell
* @param string $pColumn Numeric column coordinate of the cell (starting from 0)
* @param string $pRow Numeric row coordinate of the cell
* @return PHPExcel_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|PHPExcel_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 = PHPExcel_Cell::stringFromColumnIndex($pColumn);
$coordinate = $columnLetter . $pRow;
@ -1192,7 +1196,8 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable
return $this->cellCollection->getCacheData($coordinate);
}
return $this->createNewCell($coordinate);
// Create new cell object, if required
return $createIfNotExists ? $this->createNewCell($coordinate) : null;
}
/**
@ -1692,11 +1697,13 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable
$this->getCell($upperLeft)->setValueExplicit(null, PHPExcel_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);
for ($i = 1; $i < $count; $i++) {
if ($this->cellExists($aReferences[$i])) {
$this->getCell($aReferences[$i])->setValueExplicit(null, PHPExcel_Cell_DataType::TYPE_NULL);
}
}
} else {
throw new PHPExcel_Exception('Merge must be set on a range of cells.');
}