5 * Copyright (c) 2006 - 2014 PHPExcel
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 * @package PHPExcel_CachedObjectStorage
23 * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
24 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
25 * @version ##VERSION##, ##DATE##
30 * PHPExcel_CachedObjectStorage_CacheBase
33 * @package PHPExcel_CachedObjectStorage
34 * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
36 abstract class PHPExcel_CachedObjectStorage_CacheBase
{
41 * @var PHPExcel_Worksheet
46 * The currently active Cell
50 protected $_currentObject = null;
53 * Coordinate address of the currently active Cell
57 protected $_currentObjectID = null;
61 * Flag indicating whether the currently active Cell requires saving
65 protected $_currentCellIsDirty = true;
68 * An array of cells or cell pointers for the worksheet cells held in this cache,
69 * and indexed by their coordinate address within the worksheet
73 protected $_cellCache = array();
77 * Initialise this new cell collection
79 * @param PHPExcel_Worksheet $parent The worksheet for this cell collection
81 public function __construct(PHPExcel_Worksheet
$parent) {
82 // Set our parent worksheet.
83 // This is maintained within the cache controller to facilitate re-attaching it to PHPExcel_Cell objects when
84 // they are woken from a serialized state
85 $this->_parent
= $parent;
86 } // function __construct()
90 * Return the parent worksheet for this cell collection
92 * @return PHPExcel_Worksheet
94 public function getParent()
96 return $this->_parent
;
100 * Is a value set in the current PHPExcel_CachedObjectStorage_ICache for an indexed cell?
102 * @param string $pCoord Coordinate address of the cell to check
105 public function isDataSet($pCoord) {
106 if ($pCoord === $this->_currentObjectID
) {
109 // Check if the requested entry exists in the cache
110 return isset($this->_cellCache
[$pCoord]);
111 } // function isDataSet()
115 * Move a cell object from one address to another
117 * @param string $fromAddress Current address of the cell to move
118 * @param string $toAddress Destination address of the cell to move
121 public function moveCell($fromAddress, $toAddress) {
122 if ($fromAddress === $this->_currentObjectID
) {
123 $this->_currentObjectID
= $toAddress;
125 $this->_currentCellIsDirty
= true;
126 if (isset($this->_cellCache
[$fromAddress])) {
127 $this->_cellCache
[$toAddress] = &$this->_cellCache
[$fromAddress];
128 unset($this->_cellCache
[$fromAddress]);
132 } // function moveCell()
136 * Add or Update a cell in cache
138 * @param PHPExcel_Cell $cell Cell to update
139 * @return PHPExcel_Cell
140 * @throws PHPExcel_Exception
142 public function updateCacheData(PHPExcel_Cell
$cell) {
143 return $this->addCacheData($cell->getCoordinate(),$cell);
144 } // function updateCacheData()
148 * Delete a cell in cache identified by coordinate address
150 * @param string $pCoord Coordinate address of the cell to delete
151 * @throws PHPExcel_Exception
153 public function deleteCacheData($pCoord) {
154 if ($pCoord === $this->_currentObjectID
&& !is_null($this->_currentObject
)) {
155 $this->_currentObject
->detach();
156 $this->_currentObjectID
= $this->_currentObject
= null;
159 if (is_object($this->_cellCache
[$pCoord])) {
160 $this->_cellCache
[$pCoord]->detach();
161 unset($this->_cellCache
[$pCoord]);
163 $this->_currentCellIsDirty
= false;
164 } // function deleteCacheData()
168 * Get a list of all cell addresses currently held in cache
172 public function getCellList() {
173 return array_keys($this->_cellCache
);
174 } // function getCellList()
178 * Sort the list of all cell addresses currently held in cache by row and column
182 public function getSortedCellList() {
184 foreach ($this->getCellList() as $coord) {
185 sscanf($coord,'%[A-Z]%d', $column, $row);
186 $sortKeys[sprintf('%09d%3s',$row,$column)] = $coord;
190 return array_values($sortKeys);
191 } // function sortCellList()
196 * Get highest worksheet column and highest row that have cell records
198 * @return array Highest column name and highest row number
200 public function getHighestRowAndColumn()
202 // Lookup highest column and highest row
203 $col = array('A' => '1A');
205 foreach ($this->getCellList() as $coord) {
206 sscanf($coord,'%[A-Z]%d', $c, $r);
208 $col[$c] = strlen($c).$c;
211 // Determine highest column and row
212 $highestRow = max($row);
213 $highestColumn = substr(max($col),1);
216 return array( 'row' => $highestRow,
217 'column' => $highestColumn
223 * Return the cell address of the currently active cell object
227 public function getCurrentAddress()
229 return $this->_currentObjectID
;
233 * Return the column address of the currently active cell object
237 public function getCurrentColumn()
239 sscanf($this->_currentObjectID
, '%[A-Z]%d', $column, $row);
244 * Return the row address of the currently active cell object
248 public function getCurrentRow()
250 sscanf($this->_currentObjectID
, '%[A-Z]%d', $column, $row);
251 return (integer) $row;
255 * Get highest worksheet column
257 * @param string $row Return the highest column for the specified row,
258 * or the highest column of any row if no row number is passed
259 * @return string Highest column name
261 public function getHighestColumn($row = null)
264 $colRow = $this->getHighestRowAndColumn();
265 return $colRow['column'];
268 $columnList = array(1);
269 foreach ($this->getCellList() as $coord) {
270 sscanf($coord,'%[A-Z]%d', $c, $r);
274 $columnList[] = PHPExcel_Cell
::columnIndexFromString($c);
276 return PHPExcel_Cell
::stringFromColumnIndex(max($columnList) - 1);
280 * Get highest worksheet row
282 * @param string $column Return the highest row for the specified column,
283 * or the highest row of any column if no column letter is passed
284 * @return int Highest row number
286 public function getHighestRow($column = null)
288 if ($column == null) {
289 $colRow = $this->getHighestRowAndColumn();
290 return $colRow['row'];
294 foreach ($this->getCellList() as $coord) {
295 sscanf($coord,'%[A-Z]%d', $c, $r);
302 return max($rowList);
307 * Generate a unique ID for cache referencing
309 * @return string Unique Reference
311 protected function _getUniqueID() {
312 if (function_exists('posix_getpid')) {
313 $baseUnique = posix_getpid();
315 $baseUnique = mt_rand();
317 return uniqid($baseUnique,true);
321 * Clone the cell collection
323 * @param PHPExcel_Worksheet $parent The new worksheet
326 public function copyCellCollection(PHPExcel_Worksheet
$parent) {
327 $this->_currentCellIsDirty
;
330 $this->_parent
= $parent;
331 if (($this->_currentObject
!== NULL) && (is_object($this->_currentObject
))) {
332 $this->_currentObject
->attach($this);
334 } // function copyCellCollection()
337 * Remove a row, deleting all cells in that row
339 * @param string $row Row number to remove
342 public function removeRow($row) {
343 foreach ($this->getCellList() as $coord) {
344 sscanf($coord,'%[A-Z]%d', $c, $r);
346 $this->deleteCacheData($coord);
352 * Remove a column, deleting all cells in that column
354 * @param string $column Column ID to remove
357 public function removeColumn($column) {
358 foreach ($this->getCellList() as $coord) {
359 sscanf($coord,'%[A-Z]%d', $c, $r);
361 $this->deleteCacheData($coord);
367 * Identify whether the caching method is currently available
368 * Some methods are dependent on the availability of certain extensions being enabled in the PHP build
372 public static function cacheMethodIsAvailable() {