Selective calculation cache clearance and adjustment for unsetting or renaming worksheets

This commit is contained in:
Mark Baker 2013-02-12 12:44:31 +00:00
parent f05f66fa82
commit b589fab56b
3 changed files with 82 additions and 24 deletions

View File

@ -143,8 +143,7 @@ class PHPExcel
$this->addCellStyleXf(new PHPExcel_Style);
}
/**
/**
* Destroy this workbook
*/
public function __destruct() {
@ -157,7 +156,9 @@ class PHPExcel
* typically so that the PHPExcel object can be unset
*
*/
public function disconnectWorksheets() {
public function disconnectWorksheets()
{
$worksheet = NULL;
foreach($this->_workSheetCollection as $k => &$worksheet) {
$worksheet->disconnectCells();
$this->_workSheetCollection[$k] = null;
@ -171,8 +172,8 @@ class PHPExcel
*
* @return PHPExcel_Calculation
*/
public function getCalculationEngine() {
public function getCalculationEngine()
{
return $this->_calculationEngine;
} // function getCellCacheController()
@ -262,7 +263,9 @@ class PHPExcel
public function addSheet(PHPExcel_Worksheet $pSheet, $iSheetIndex = NULL)
{
if ($this->sheetNameExists($pSheet->getTitle())) {
throw new PHPExcel_Exception("Workbook already contains a worksheet named '{$pSheet->getTitle()}'. Rename this worksheet first.");
throw new PHPExcel_Exception(
"Workbook already contains a worksheet named '{$pSheet->getTitle()}'. Rename this worksheet first."
);
}
if($iSheetIndex === NULL) {
@ -295,8 +298,13 @@ class PHPExcel
*/
public function removeSheetByIndex($pIndex = 0)
{
if ($pIndex > count($this->_workSheetCollection) - 1) {
throw new PHPExcel_Exception("Sheet index is out of bounds.");
$numSheets = count($this->_workSheetCollection);
if ($pIndex > $numSheets - 1) {
throw new PHPExcel_Exception(
"You tried to remove a sheet by the out of bounds index: {$pIndex}. The actual number of sheets is {$numSheets}."
);
} else {
array_splice($this->_workSheetCollection, $pIndex, 1);
}
@ -317,8 +325,13 @@ class PHPExcel
*/
public function getSheet($pIndex = 0)
{
if ($pIndex > count($this->_workSheetCollection) - 1) {
throw new PHPExcel_Exception("Sheet index is out of bounds.");
$numSheets = count($this->_workSheetCollection);
if ($pIndex > $numSheets - 1) {
throw new PHPExcel_Exception(
"Your requested sheet index: {$pIndex} is out of bounds. The actual number of sheets is {$numSheets}."
);
} else {
return $this->_workSheetCollection[$pIndex];
}
@ -424,8 +437,12 @@ class PHPExcel
*/
public function setActiveSheetIndex($pIndex = 0)
{
if ($pIndex > count($this->_workSheetCollection) - 1) {
throw new PHPExcel_Exception("Active sheet index is out of bounds.");
$numSheets = count($this->_workSheetCollection);
if ($pIndex > $numSheets - 1) {
throw new PHPExcel_Exception(
"You tried to set a sheet active by the out of bounds index: {$pIndex}. The actual number of sheets is {$numSheets}."
);
} else {
$this->_activeSheetIndex = $pIndex;
}

View File

@ -1864,6 +1864,25 @@ class PHPExcel_Calculation {
$this->_calculationCache = array();
} // function clearCalculationCache()
/**
* Clear calculation cache for a specified worksheet
*/
public function clearCalculationCacheForWorksheet($worksheetName) {
if (isset($this->_calculationCache[$worksheetName])) {
unset($this->_calculationCache[$worksheetName]);
}
} // function clearCalculationCache()
/**
* Rename calculation cache for a specified worksheet
*/
public function renameCalculationCacheForWorksheet($fromWorksheetName, $toWorksheetName) {
if (isset($this->_calculationCache[$fromWorksheetName])) {
$this->_calculationCache[$toWorksheetName] = &$this->_calculationCache[$fromWorksheetName];
unset($this->_calculationCache[$fromWorksheetName]);
}
} // function clearCalculationCache()
/**
* Get the currently defined locale code

View File

@ -378,13 +378,25 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable
*/
public function disconnectCells() {
$this->_cellCollection->unsetWorksheetCells();
$this->_cellCollection = null;
$this->_cellCollection = NULL;
// detach ourself from the workbook, so that it can then delete this worksheet successfully
$this->_parent = null;
}
/**
* Code to execute when this worksheet is unset()
*
*/
function __destruct() {
if ($this->_cellCollection !== NULL) {
$this->disconnectCells();
}
PHPExcel_Calculation::getInstance($this->_parent)
->clearCalculationCacheForWorksheet($this->_title);
}
/**
* Return the cache controller for the cell collection
*
* @return PHPExcel_CachedObjectStorage_xxx
@ -703,14 +715,20 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable
$cellValue = $cell->getCalculatedValue();
// To formatted string
$cellValue = PHPExcel_Style_NumberFormat::toFormattedString($cellValue, $this->getParent()->getCellXfByIndex($cell->getXfIndex())->getNumberFormat()->getFormatCode());
$cellValue = PHPExcel_Style_NumberFormat::toFormattedString(
$cellValue,
$this->_parent->getCellXfByIndex($cell->getXfIndex())
->getNumberFormat()->getFormatCode()
);
$autoSizes[$cell->getColumn()] = max(
(float)$autoSizes[$cell->getColumn()],
(float)PHPExcel_Shared_Font::calculateColumnWidth(
$this->getParent()->getCellXfByIndex($cell->getXfIndex())->getFont(),
$this->_parent->getCellXfByIndex($cell->getXfIndex())
->getFont(),
$cellValue,
$this->getParent()->getCellXfByIndex($cell->getXfIndex())->getAlignment()->getTextRotation(),
$this->_parent->getCellXfByIndex($cell->getXfIndex())
->getAlignment()->getTextRotation(),
$this->getDefaultStyle()->getFont()
)
);
@ -791,16 +809,16 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable
// Old title
$oldTitle = $this->getTitle();
if ($this->getParent()) {
if ($this->_parent) {
// Is there already such sheet name?
if ($this->getParent()->sheetNameExists($pValue)) {
if ($this->_parent->sheetNameExists($pValue)) {
// Use name, but append with lowest possible integer
if (PHPExcel_Shared_String::CountCharacters($pValue) > 29) {
$pValue = PHPExcel_Shared_String::Substring($pValue,0,29);
}
$i = 1;
while ($this->getParent()->sheetNameExists($pValue . ' ' . $i)) {
while ($this->_parent->sheetNameExists($pValue . ' ' . $i)) {
++$i;
if ($i == 10) {
if (PHPExcel_Shared_String::CountCharacters($pValue) > 28) {
@ -822,11 +840,13 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable
$this->_title = $pValue;
$this->_dirty = true;
if ($this->getParent()) {
if ($this->_parent) {
// New title
$newTitle = $this->getTitle();
PHPExcel_Calculation::getInstance($this->_parent)
->renameCalculationCacheForWorksheet($oldTitle, $newTitle);
if ($updateFormulaCellReferences)
PHPExcel_ReferenceHelper::getInstance()->updateNamedFormulas($this->getParent(), $oldTitle, $newTitle);
PHPExcel_ReferenceHelper::getInstance()->updateNamedFormulas($this->_parent, $oldTitle, $newTitle);
}
return $this;
@ -1092,7 +1112,7 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable
// Worksheet reference?
if (strpos($pCoordinate, '!') !== false) {
$worksheetReference = PHPExcel_Worksheet::extractSheetTitle($pCoordinate, true);
return $this->getParent()->getSheetByName($worksheetReference[0])->getCell($worksheetReference[1]);
return $this->_parent->getSheetByName($worksheetReference[0])->getCell($worksheetReference[1]);
}
// Named range?
@ -1184,7 +1204,7 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable
// Worksheet reference?
if (strpos($pCoordinate, '!') !== false) {
$worksheetReference = PHPExcel_Worksheet::extractSheetTitle($pCoordinate, true);
return $this->getParent()->getSheetByName($worksheetReference[0])->cellExists($worksheetReference[1]);
return $this->_parent->getSheetByName($worksheetReference[0])->cellExists($worksheetReference[1]);
}
// Named range?
@ -2416,7 +2436,9 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable
if ($formatData) {
$style = $this->_parent->getCellXfByIndex($cell->getXfIndex());
$returnValue[$rRef][$cRef] = PHPExcel_Style_NumberFormat::toFormattedString($returnValue[$rRef][$cRef], $style->getNumberFormat()->getFormatCode());
$returnValue[$rRef][$cRef] = PHPExcel_Style_NumberFormat::toFormattedString(
$returnValue[$rRef][$cRef], $style->getNumberFormat()->getFormatCode()
);
}
} else {
// Cell holds a NULL