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,7 +143,6 @@ class PHPExcel
$this->addCellStyleXf(new PHPExcel_Style); $this->addCellStyleXf(new PHPExcel_Style);
} }
/** /**
* Destroy this workbook * Destroy this workbook
*/ */
@ -157,7 +156,9 @@ class PHPExcel
* typically so that the PHPExcel object can be unset * typically so that the PHPExcel object can be unset
* *
*/ */
public function disconnectWorksheets() { public function disconnectWorksheets()
{
$worksheet = NULL;
foreach($this->_workSheetCollection as $k => &$worksheet) { foreach($this->_workSheetCollection as $k => &$worksheet) {
$worksheet->disconnectCells(); $worksheet->disconnectCells();
$this->_workSheetCollection[$k] = null; $this->_workSheetCollection[$k] = null;
@ -171,8 +172,8 @@ class PHPExcel
* *
* @return PHPExcel_Calculation * @return PHPExcel_Calculation
*/ */
public function getCalculationEngine() { public function getCalculationEngine()
{
return $this->_calculationEngine; return $this->_calculationEngine;
} // function getCellCacheController() } // function getCellCacheController()
@ -262,7 +263,9 @@ class PHPExcel
public function addSheet(PHPExcel_Worksheet $pSheet, $iSheetIndex = NULL) public function addSheet(PHPExcel_Worksheet $pSheet, $iSheetIndex = NULL)
{ {
if ($this->sheetNameExists($pSheet->getTitle())) { 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) { if($iSheetIndex === NULL) {
@ -295,8 +298,13 @@ class PHPExcel
*/ */
public function removeSheetByIndex($pIndex = 0) 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 { } else {
array_splice($this->_workSheetCollection, $pIndex, 1); array_splice($this->_workSheetCollection, $pIndex, 1);
} }
@ -317,8 +325,13 @@ class PHPExcel
*/ */
public function getSheet($pIndex = 0) 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 { } else {
return $this->_workSheetCollection[$pIndex]; return $this->_workSheetCollection[$pIndex];
} }
@ -424,8 +437,12 @@ class PHPExcel
*/ */
public function setActiveSheetIndex($pIndex = 0) public function setActiveSheetIndex($pIndex = 0)
{ {
if ($pIndex > count($this->_workSheetCollection) - 1) { $numSheets = count($this->_workSheetCollection);
throw new PHPExcel_Exception("Active sheet index is out of bounds.");
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 { } else {
$this->_activeSheetIndex = $pIndex; $this->_activeSheetIndex = $pIndex;
} }

View File

@ -1864,6 +1864,25 @@ class PHPExcel_Calculation {
$this->_calculationCache = array(); $this->_calculationCache = array();
} // function clearCalculationCache() } // 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 * Get the currently defined locale code

View File

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