Initial restructuring of cell object

This commit is contained in:
Mark Baker 2013-01-28 01:03:02 +00:00
parent 0baa487442
commit 7b83894b10
5 changed files with 86 additions and 62 deletions

View File

@ -86,6 +86,11 @@ abstract class PHPExcel_CachedObjectStorage_CacheBase {
} // function __construct()
public function getParent()
{
return $this->_parent;
}
/**
* Is a value set in the current PHPExcel_CachedObjectStorage_ICache for an indexed cell?
*
@ -188,6 +193,23 @@ abstract class PHPExcel_CachedObjectStorage_CacheBase {
}
public function getCurrentAddress()
{
return $this->_currentObjectID;
}
public function getCurrentColumn()
{
list($column,$row) = sscanf($this->_currentObjectID, '%[A-Z]%d');
return $column;
}
public function getCurrentRow()
{
list($column,$row) = sscanf($this->_currentObjectID, '%[A-Z]%d');
return $row;
}
/**
* Get highest worksheet column
*

View File

@ -53,6 +53,10 @@ class PHPExcel_CachedObjectStorage_Memory extends PHPExcel_CachedObjectStorage_C
*/
public function addCacheData($pCoord, PHPExcel_Cell $cell) {
$this->_cellCache[$pCoord] = $cell;
// Set current entry to the new/updated entry
$this->_currentObjectID = $pCoord;
return $cell;
} // function addCacheData()
@ -67,10 +71,14 @@ class PHPExcel_CachedObjectStorage_Memory extends PHPExcel_CachedObjectStorage_C
public function getCacheData($pCoord) {
// Check if the entry that has been requested actually exists
if (!isset($this->_cellCache[$pCoord])) {
$this->_currentObjectID = NULL;
// Return null if requested entry doesn't exist in cache
return null;
}
// Set current entry to the requested entry
$this->_currentObjectID = $pCoord;
// Return requested entry
return $this->_cellCache[$pCoord];
} // function getCacheData()

View File

@ -2274,7 +2274,7 @@ class PHPExcel_Calculation {
$wsTitle = "\x00Wrk";
if ($pCell !== NULL) {
$pCellParent = $pCell->getParent();
$pCellParent = $pCell->getWorksheet();
if ($pCellParent !== NULL) {
$wsTitle = $pCellParent->getTitle();
}
@ -2627,7 +2627,7 @@ class PHPExcel_Calculation {
// If we're using cell caching, then $pCell may well be flushed back to the cache (which detaches the parent worksheet),
// so we store the parent worksheet so that we can re-attach it when necessary
$pCellParent = ($pCell !== NULL) ? $pCell->getParent() : NULL;
$pCellParent = ($pCell !== NULL) ? $pCell->getWorksheet() : NULL;
// Binary Operators
// These operators always work on two values
@ -3011,7 +3011,7 @@ class PHPExcel_Calculation {
// If we're using cell caching, then $pCell may well be flushed back to the cache (which detaches the parent worksheet),
// so we store the parent worksheet so that we can re-attach it when necessary
$pCellParent = ($pCell !== NULL) ? $pCell->getParent() : null;
$pCellParent = ($pCell !== NULL) ? $pCell->getWorksheet() : null;
$stack = new PHPExcel_Calculation_Token_Stack;
// Loop through each token in turn
@ -3064,20 +3064,24 @@ class PHPExcel_Calculation {
if ($sheet1 == $sheet2) {
if ($operand1Data['reference'] === NULL) {
if ((trim($operand1Data['value']) != '') && (is_numeric($operand1Data['value']))) {
$operand1Data['reference'] = $pCell->getColumn().$operand1Data['value'];
$operand1Data['reference'] = $pCellParent->getCellCacheController()->getCurrentColumn() .
$operand1Data['value'];
} elseif (trim($operand1Data['reference']) == '') {
$operand1Data['reference'] = $pCell->getCoordinate();
$operand1Data['reference'] = $pCellParent->getCellCacheController()->getCurrentAddress();
} else {
$operand1Data['reference'] = $operand1Data['value'].$pCell->getRow();
$operand1Data['reference'] = $operand1Data['value'] .
$pCellParent->getCellCacheController()->getCurrentRow();
}
}
if ($operand2Data['reference'] === NULL) {
if ((trim($operand2Data['value']) != '') && (is_numeric($operand2Data['value']))) {
$operand2Data['reference'] = $pCell->getColumn().$operand2Data['value'];
$operand2Data['reference'] = $pCellParent->getCellCacheController()->getCurrentColumn() .
$operand2Data['value'];
} elseif (trim($operand2Data['reference']) == '') {
$operand2Data['reference'] = $pCell->getCoordinate();
$operand2Data['reference'] = $pCellParent->getCellCacheController()->getCurrentAddress();
} else {
$operand2Data['reference'] = $operand2Data['value'].$pCell->getRow();
$operand2Data['reference'] = $operand2Data['value'] .
$pCellParent->getCellCacheController()->getCurrentRow();
}
}
@ -3245,7 +3249,7 @@ class PHPExcel_Calculation {
if ($pCellParent !== NULL) {
if ($pCellParent->getParent()->getSheetByName($matches[2])->cellExists($cellRef)) {
$cellValue = $this->extractCellRange($cellRef, $pCellParent->getParent()->getSheetByName($matches[2]), false);
$pCell->attach($pCellParent);
$pCell->attach($pCellParent->getCellCacheController());
} else {
$cellValue = null;
}
@ -3259,7 +3263,7 @@ class PHPExcel_Calculation {
$this->_writeDebug('Evaluating Cell '.$cellRef.' in current worksheet');
if ($pCellParent->cellExists($cellRef)) {
$cellValue = $this->extractCellRange($cellRef, $pCellParent, false);
$pCell->attach($pCellParent);
$pCell->attach($pCellParent->getCellCacheController());
} else {
$cellValue = null;
}
@ -3384,7 +3388,7 @@ class PHPExcel_Calculation {
// echo 'Named Range is '.$namedRange.'<br />';
$this->_writeDebug('Evaluating Named Range '.$namedRange);
$cellValue = $this->extractNamedRange($namedRange, ((null !== $pCell) ? $pCellParent : null), false);
$pCell->attach($pCellParent);
$pCell->attach($pCellParent->getCellCacheController());
$this->_writeDebug('Evaluation Result for named range '.$namedRange.' is '.$this->_showTypeDetails($cellValue));
$stack->push('Named Range',$cellValue,$namedRange);
} else {

View File

@ -50,20 +50,6 @@ class PHPExcel_Cell
*/
private static $_valueBinder = NULL;
/**
* Column of the cell
*
* @var string
*/
private $_column;
/**
* Row of the cell
*
* @var int
*/
private $_row;
/**
* Value of the cell
*
@ -93,7 +79,7 @@ class PHPExcel_Cell
/**
* Parent worksheet
*
* @var PHPExcel_Worksheet
* @var PHPExcel_CachedObjectStorage_CacheBase
*/
private $_parent;
@ -117,7 +103,7 @@ class PHPExcel_Cell
* @return void
**/
public function notifyCacheController() {
$this->_parent->getCellCacheController()->updateCacheData($this);
$this->_parent->updateCacheData($this);
return $this;
}
@ -133,24 +119,18 @@ class PHPExcel_Cell
/**
* Create a new Cell
*
* @param string $pColumn
* @param int $pRow
* @param mixed $pValue
* @param string $pDataType
* @param PHPExcel_Worksheet $pSheet
* @throws PHPExcel_Exception
*/
public function __construct($pColumn = 'A', $pRow = 1, $pValue = NULL, $pDataType = NULL, PHPExcel_Worksheet $pSheet = NULL)
public function __construct($pValue = NULL, $pDataType = NULL, PHPExcel_Worksheet $pSheet = NULL)
{
// Initialise cell coordinate
$this->_column = strtoupper($pColumn);
$this->_row = $pRow;
// Initialise cell value
$this->_value = $pValue;
// Set worksheet
$this->_parent = $pSheet;
// Set worksheet cache
$this->_parent = $pSheet->getCellCacheController();
// Set datatype?
if ($pDataType !== NULL) {
@ -174,7 +154,7 @@ class PHPExcel_Cell
*/
public function getColumn()
{
return $this->_column;
return $this->_parent->getCurrentColumn();
}
/**
@ -184,7 +164,7 @@ class PHPExcel_Cell
*/
public function getRow()
{
return $this->_row;
return $this->_parent->getCurrentRow();
}
/**
@ -194,7 +174,7 @@ class PHPExcel_Cell
*/
public function getCoordinate()
{
return $this->_column . $this->_row;
return $this->_parent->getCurrentAddress();
}
/**
@ -216,7 +196,7 @@ class PHPExcel_Cell
{
return (string) PHPExcel_Style_NumberFormat::toFormattedString(
$this->getCalculatedValue(),
$this->_parent->getParent()->getCellXfByIndex($this->getXfIndex())
$this->_parent->getParent()->getParent()->getCellXfByIndex($this->getXfIndex())
->getNumberFormat()->getFormatCode()
);
}
@ -294,7 +274,7 @@ class PHPExcel_Cell
if ($this->_dataType == PHPExcel_Cell_DataType::TYPE_FORMULA) {
try {
// echo 'Cell value for '.$this->getCoordinate().' is a formula: Calculating value<br />';
$result = PHPExcel_Calculation::getInstance()->calculateCellValue($this,$resetLog);
$result = PHPExcel_Calculation::getInstance()->calculateCellValue($this, $resetLog);
// echo $this->getCoordinate().' calculation result is '.$result.'<br />';
} catch ( PHPExcel_Exception $ex ) {
if (($ex->getMessage() === 'Unable to access External Workbook') && ($this->_calculatedValue !== NULL)) {
@ -394,7 +374,7 @@ class PHPExcel_Cell
throw new PHPExcel_Exception('Cannot check for data validation when cell is not bound to a worksheet');
}
return $this->_parent->dataValidationExists($this->getCoordinate());
return $this->_parent->getParent()->dataValidationExists($this->getCoordinate());
}
/**
@ -409,7 +389,7 @@ class PHPExcel_Cell
throw new PHPExcel_Exception('Cannot get data validation for cell that is not bound to a worksheet');
}
return $this->_parent->getDataValidation($this->getCoordinate());
return $this->_parent->getParent()->getDataValidation($this->getCoordinate());
}
/**
@ -425,7 +405,7 @@ class PHPExcel_Cell
throw new PHPExcel_Exception('Cannot set data validation for cell that is not bound to a worksheet');
}
$this->_parent->setDataValidation($this->getCoordinate(), $pDataValidation);
$this->_parent->getParent()->setDataValidation($this->getCoordinate(), $pDataValidation);
return $this->notifyCacheController();
}
@ -442,7 +422,7 @@ class PHPExcel_Cell
throw new PHPExcel_Exception('Cannot check for hyperlink when cell is not bound to a worksheet');
}
return $this->_parent->hyperlinkExists($this->getCoordinate());
return $this->_parent->getParent()->hyperlinkExists($this->getCoordinate());
}
/**
@ -457,7 +437,7 @@ class PHPExcel_Cell
throw new PHPExcel_Exception('Cannot get hyperlink for cell that is not bound to a worksheet');
}
return $this->_parent->getHyperlink($this->getCoordinate());
return $this->_parent->getParent()->getHyperlink($this->getCoordinate());
}
/**
@ -473,7 +453,7 @@ class PHPExcel_Cell
throw new PHPExcel_Exception('Cannot set hyperlink for cell that is not bound to a worksheet');
}
$this->_parent->setHyperlink($this->getCoordinate(), $pHyperlink);
$this->_parent->getParent()->setHyperlink($this->getCoordinate(), $pHyperlink);
return $this->notifyCacheController();
}
@ -484,7 +464,16 @@ class PHPExcel_Cell
* @return PHPExcel_Worksheet
*/
public function getParent() {
return $this->_parent;
return $this->_parent->getParent();
}
/**
* Get parent worksheet
*
* @return PHPExcel_Worksheet
*/
public function getWorksheet() {
return $this->_parent->getParent();
}
/**
@ -494,7 +483,7 @@ class PHPExcel_Cell
* @return PHPExcel_Cell
*/
public function rebindParent(PHPExcel_Worksheet $parent) {
$this->_parent = $parent;
$this->_parent = $parent->getCellCacheController();
return $this->notifyCacheController();
}
@ -865,11 +854,11 @@ class PHPExcel_Cell
*/
public static function compareCells(PHPExcel_Cell $a, PHPExcel_Cell $b)
{
if ($a->_row < $b->_row) {
if ($a->getRow() < $b->getRow()) {
return -1;
} elseif ($a->_row > $b->_row) {
} elseif ($a->getRow() > $b->getRow()) {
return 1;
} elseif (self::columnIndexFromString($a->_column) < self::columnIndexFromString($b->_column)) {
} elseif (self::columnIndexFromString($a->getColumn()) < self::columnIndexFromString($b->getColumn())) {
return -1;
} else {
return 1;

View File

@ -696,17 +696,18 @@ 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[$cell->getColumn()])) {
if (isset($autoSizes[$this->_cellCollection->getCurrentColumn()])) {
// Determine width if cell does not participate in a merge
if (!isset($isMergeCell[$cell->getCoordinate()])) {
if (!isset($isMergeCell[$this->_cellCollection->getCurrentAddress()])) {
// Calculated value
$cellValue = $cell->getCalculatedValue();
// To formatted string
$cellValue = PHPExcel_Style_NumberFormat::toFormattedString($cellValue, $this->getParent()->getCellXfByIndex($cell->getXfIndex())->getNumberFormat()->getFormatCode());
$cellValue = PHPExcel_Style_NumberFormat::toFormattedString(
$cell->getCalculatedValue(),
$this->getParent()->getCellXfByIndex($cell->getXfIndex())->getNumberFormat()->getFormatCode()
);
$autoSizes[$cell->getColumn()] = max(
(float)$autoSizes[$cell->getColumn()],
$autoSizes[$this->_cellCollection->getCurrentColumn()] = max(
(float)$autoSizes[$this->_cellCollection->getCurrentColumn()],
(float)PHPExcel_Shared_Font::calculateColumnWidth(
$this->getParent()->getCellXfByIndex($cell->getXfIndex())->getFont(),
$cellValue,
@ -1118,7 +1119,7 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable
// Coordinates
$aCoordinates = PHPExcel_Cell::coordinateFromString($pCoordinate);
$cell = $this->_cellCollection->addCacheData($pCoordinate,new PHPExcel_Cell($aCoordinates[0], $aCoordinates[1], null, PHPExcel_Cell_DataType::TYPE_NULL, $this));
$cell = $this->_cellCollection->addCacheData($pCoordinate,new PHPExcel_Cell(NULL, PHPExcel_Cell_DataType::TYPE_NULL, $this));
$this->_cellCollectionIsSorted = false;
if (PHPExcel_Cell::columnIndexFromString($this->_cachedHighestColumn) < PHPExcel_Cell::columnIndexFromString($aCoordinates[0]))
@ -1158,7 +1159,7 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable
$coordinate = $columnLetter . $pRow;
if (!$this->_cellCollection->isDataSet($coordinate)) {
$cell = $this->_cellCollection->addCacheData($coordinate, new PHPExcel_Cell($columnLetter, $pRow, null, PHPExcel_Cell_DataType::TYPE_NULL, $this));
$cell = $this->_cellCollection->addCacheData($coordinate, new PHPExcel_Cell(NULL, PHPExcel_Cell_DataType::TYPE_NULL, $this));
$this->_cellCollectionIsSorted = false;
if (PHPExcel_Cell::columnIndexFromString($this->_cachedHighestColumn) < $pColumn)