5
* Copyright (c) 2006 - 2010 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_Cell
23
* @copyright Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
24
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
25
* @version 1.7.4, 2010-08-26
33
* @package PHPExcel_Cell
34
* @copyright Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
41
* @var PHPExcel_Cell_IValueBinder
43
private static $_valueBinder = null;
67
* Calculated value of the cell (used for caching)
71
private $_calculatedValue = null;
74
* Type of the cell data
83
* @var PHPExcel_Worksheet
95
* Attributes of the formula
99
private $_formulaAttributes;
103
* Send notification to the cache controller
106
public function notifyCacheController() {
107
$this->_parent->getCellCacheController()->updateCacheData($this);
111
public function detach() {
112
$this->_parent = null;
115
public function attach($parent) {
116
$this->_parent = $parent;
123
* @param string $pColumn
125
* @param mixed $pValue
126
* @param string $pDataType
127
* @param PHPExcel_Worksheet $pSheet
130
public function __construct($pColumn = 'A', $pRow = 1, $pValue = null, $pDataType = null, PHPExcel_Worksheet $pSheet = null)
132
// Initialise cell coordinate
133
$this->_column = strtoupper($pColumn);
136
// Initialise cell value
137
$this->_value = $pValue;
140
$this->_parent = $pSheet;
143
if (!is_null($pDataType)) {
144
$this->_dataType = $pDataType;
146
if (!self::getValueBinder()->bindValue($this, $pValue)) {
147
throw new Exception("Value could not be bound to cell.");
151
// set default index to cellXf
156
* Get cell coordinate column
160
public function getColumn()
162
return $this->_column;
166
* Get cell coordinate row
170
public function getRow()
176
* Get cell coordinate
180
public function getCoordinate()
182
return $this->_column . $this->_row;
190
public function getValue()
192
return $this->_value;
198
* This clears the cell formula.
200
* @param mixed $pValue Value
201
* @return PHPExcel_Cell
203
public function setValue($pValue = null)
205
if (!self::getValueBinder()->bindValue($this, $pValue)) {
206
throw new Exception("Value could not be bound to cell.");
212
* Set cell value (with explicit data type given)
214
* @param mixed $pValue Value
215
* @param string $pDataType Explicit data type
216
* @return PHPExcel_Cell
219
public function setValueExplicit($pValue = null, $pDataType = PHPExcel_Cell_DataType::TYPE_STRING)
221
// set the value according to data type
222
switch ($pDataType) {
223
case PHPExcel_Cell_DataType::TYPE_STRING:
224
case PHPExcel_Cell_DataType::TYPE_NULL:
225
case PHPExcel_Cell_DataType::TYPE_INLINE:
226
$this->_value = PHPExcel_Cell_DataType::checkString($pValue);
229
case PHPExcel_Cell_DataType::TYPE_NUMERIC:
230
$this->_value = (float)$pValue;
233
case PHPExcel_Cell_DataType::TYPE_FORMULA:
234
$this->_value = (string)$pValue;
237
case PHPExcel_Cell_DataType::TYPE_BOOL:
238
$this->_value = (bool)$pValue;
241
case PHPExcel_Cell_DataType::TYPE_ERROR:
242
$this->_value = PHPExcel_Cell_DataType::checkErrorCode($pValue);
246
throw new Exception('Invalid datatype: ' . $pDataType);
251
$this->_dataType = $pDataType;
253
return $this->notifyCacheController();
257
* Get calculated cell value
261
public function getCalculatedValue($resetLog=true)
263
// echo 'Cell '.$this->getCoordinate().' value is a '.$this->_dataType.' with a value of '.$this->getValue().'<br />';
264
if ($this->_dataType == PHPExcel_Cell_DataType::TYPE_FORMULA) {
266
// echo 'Cell value for '.$this->getCoordinate().' is a formula: Calculating value<br />';
267
$result = PHPExcel_Calculation::getInstance()->calculateCellValue($this,$resetLog);
268
// echo $this->getCoordinate().' calculation result is '.$result.'<br />';
269
} catch ( Exception $ex ) {
270
// echo 'Calculation Exception: '.$ex->getMessage().'<br />';
272
throw(new Exception($this->getParent()->getTitle().'!'.$this->getCoordinate().' -> '.$ex->getMessage()));
275
if ($result === '#Not Yet Implemented') {
276
// echo 'Returning fallback value of '.$this->_calculatedValue.' for cell '.$this->getCoordinate().'<br />';
277
return $this->_calculatedValue; // Fallback if calculation engine does not support the formula.
279
// echo 'Returning calculated value of '.$result.' for cell '.$this->getCoordinate().'<br />';
283
if (is_null($this->_value)) {
284
// echo 'Cell '.$this->getCoordinate().' has no value, formula or otherwise<br />';
287
// echo 'Cell value for '.$this->getCoordinate().' is not a formula: Returning data value of '.$this->_value.'<br />';
288
return $this->_value;
292
* Set calculated value (used for caching)
294
* @param mixed $pValue Value
295
* @return PHPExcel_Cell
297
public function setCalculatedValue($pValue = null)
299
if (!is_null($pValue)) {
300
$this->_calculatedValue = $pValue;
303
return $this->notifyCacheController();
307
* Get old calculated value (cached)
311
public function getOldCalculatedValue()
313
return $this->_calculatedValue;
321
public function getDataType()
323
return $this->_dataType;
329
* @param string $pDataType
330
* @return PHPExcel_Cell
332
public function setDataType($pDataType = PHPExcel_Cell_DataType::TYPE_STRING)
334
$this->_dataType = $pDataType;
336
return $this->notifyCacheController();
340
* Has Data validation?
344
public function hasDataValidation()
346
if (!isset($this->_parent)) {
347
throw new Exception('Cannot check for data validation when cell is not bound to a worksheet');
350
return $this->_parent->dataValidationExists($this->getCoordinate());
354
* Get Data validation
356
* @return PHPExcel_Cell_DataValidation
358
public function getDataValidation()
360
if (!isset($this->_parent)) {
361
throw new Exception('Cannot get data validation for cell that is not bound to a worksheet');
364
return $this->_parent->getDataValidation($this->getCoordinate());
368
* Set Data validation
370
* @param PHPExcel_Cell_DataValidation $pDataValidation
372
* @return PHPExcel_Cell
374
public function setDataValidation(PHPExcel_Cell_DataValidation $pDataValidation = null)
376
if (!isset($this->_parent)) {
377
throw new Exception('Cannot set data validation for cell that is not bound to a worksheet');
380
$this->_parent->setDataValidation($this->getCoordinate(), $pDataValidation);
382
return $this->notifyCacheController();
390
public function hasHyperlink()
392
if (!isset($this->_parent)) {
393
throw new Exception('Cannot check for hyperlink when cell is not bound to a worksheet');
396
return $this->_parent->hyperlinkExists($this->getCoordinate());
403
* @return PHPExcel_Cell_Hyperlink
405
public function getHyperlink()
407
if (!isset($this->_parent)) {
408
throw new Exception('Cannot get hyperlink for cell that is not bound to a worksheet');
411
return $this->_parent->getHyperlink($this->getCoordinate());
417
* @param PHPExcel_Cell_Hyperlink $pHyperlink
419
* @return PHPExcel_Cell
421
public function setHyperlink(PHPExcel_Cell_Hyperlink $pHyperlink = null)
423
if (!isset($this->_parent)) {
424
throw new Exception('Cannot set hyperlink for cell that is not bound to a worksheet');
427
$this->_parent->setHyperlink($this->getCoordinate(), $pHyperlink);
429
return $this->notifyCacheController();
435
* @return PHPExcel_Worksheet
437
public function getParent() {
438
return $this->_parent;
444
* @param PHPExcel_Worksheet $parent
445
* @return PHPExcel_Cell
447
public function rebindParent(PHPExcel_Worksheet $parent) {
448
$this->_parent = $parent;
450
return $this->notifyCacheController();
454
* Is cell in a specific range?
456
* @param string $pRange Cell range (e.g. A1:A1)
459
public function isInRange($pRange = 'A1:A1')
461
list($rangeStart,$rangeEnd) = PHPExcel_Cell::rangeBoundaries($pRange);
463
// Translate properties
464
$myColumn = PHPExcel_Cell::columnIndexFromString($this->getColumn()) - 1;
465
$myRow = $this->getRow();
467
// Verify if cell is in range
468
return (($rangeStart[0] <= $myColumn) && ($rangeEnd[0] >= $myColumn) &&
469
($rangeStart[1] <= $myRow) && ($rangeEnd[1] >= $myRow)
474
* Coordinate from string
476
* @param string $pCoordinateString
477
* @return array Array containing column and row (indexes 0 and 1)
480
public static function coordinateFromString($pCoordinateString = 'A1')
482
if (strpos($pCoordinateString,':') !== false) {
483
throw new Exception('Cell coordinate string can not be a range of cells.');
484
} else if ($pCoordinateString == '') {
485
throw new Exception('Cell coordinate can not be zero-length string.');
486
} else if (preg_match("/([$]?[A-Z]+)([$]?\d+)/", $pCoordinateString, $matches)) {
487
list(, $column, $row) = $matches;
488
return array($column, $row);
490
throw new Exception('Invalid cell coordinate.');
495
* Make string coordinate absolute
497
* @param string $pCoordinateString
498
* @return string Absolute coordinate
501
public static function absoluteCoordinate($pCoordinateString = 'A1')
503
if (strpos($pCoordinateString,':') === false && strpos($pCoordinateString,',') === false) {
504
// Create absolute coordinate
505
list($column, $row) = PHPExcel_Cell::coordinateFromString($pCoordinateString);
506
return '$' . $column . '$' . $row;
508
throw new Exception("Coordinate string should not be a cell range.");
513
* Split range into coordinate strings
515
* @param string $pRange
516
* @return array Array containg one or more arrays containing one or two coordinate strings
518
public static function splitRange($pRange = 'A1:A1')
520
$exploded = explode(',', $pRange);
521
for ($i = 0; $i < count($exploded); ++$i) {
522
$exploded[$i] = explode(':', $exploded[$i]);
528
* Build range from coordinate strings
530
* @param array $pRange Array containg one or more arrays containing one or two coordinate strings
531
* @return string String representation of $pRange
534
public static function buildRange($pRange)
537
if (!is_array($pRange) || count($pRange) == 0 || !is_array($pRange[0])) {
538
throw new Exception('Range does not contain any information.');
543
for ($i = 0; $i < count($pRange); ++$i) {
544
$pRange[$i] = implode(':', $pRange[$i]);
546
$imploded = implode(',', $pRange);
552
* Calculate range boundaries
554
* @param string $pRange Cell range (e.g. A1:A1)
555
* @return array Range coordinates (Start Cell, End Cell) where Start Cell and End Cell are arrays (Column Number, Row Number)
557
public static function rangeBoundaries($pRange = 'A1:A1')
559
// Uppercase coordinate
560
$pRange = strtoupper($pRange);
563
if (strpos($pRange, ':') === false) {
564
$rangeA = $rangeB = $pRange;
566
list($rangeA, $rangeB) = explode(':', $pRange);
569
// Calculate range outer borders
570
$rangeStart = PHPExcel_Cell::coordinateFromString($rangeA);
571
$rangeEnd = PHPExcel_Cell::coordinateFromString($rangeB);
573
// Translate column into index
574
$rangeStart[0] = PHPExcel_Cell::columnIndexFromString($rangeStart[0]);
575
$rangeEnd[0] = PHPExcel_Cell::columnIndexFromString($rangeEnd[0]);
577
return array($rangeStart, $rangeEnd);
581
* Calculate range dimension
583
* @param string $pRange Cell range (e.g. A1:A1)
584
* @return array Range dimension (width, height)
586
public static function rangeDimension($pRange = 'A1:A1')
588
// Calculate range outer borders
589
list($rangeStart,$rangeEnd) = PHPExcel_Cell::rangeBoundaries($pRange);
591
return array( ($rangeEnd[0] - $rangeStart[0] + 1), ($rangeEnd[1] - $rangeStart[1] + 1) );
595
* Calculate range boundaries
597
* @param string $pRange Cell range (e.g. A1:A1)
598
* @return array Range boundaries (staring Column, starting Row, Final Column, Final Row)
600
public static function getRangeBoundaries($pRange = 'A1:A1')
602
// Uppercase coordinate
603
$pRange = strtoupper($pRange);
606
if (strpos($pRange, ':') === false) {
610
list($rangeA, $rangeB) = explode(':', $pRange);
613
return array( self::coordinateFromString($rangeA), self::coordinateFromString($rangeB));
617
* Column index from string
619
* @param string $pString
620
* @return int Column index (base 1 !!!)
623
public static function columnIndexFromString($pString = 'A')
625
static $lookup = array(
626
'A' => 1, 'B' => 2, 'C' => 3, 'D' => 4, 'E' => 5, 'F' => 6, 'G' => 7, 'H' => 8, 'I' => 9, 'J' => 10, 'K' => 11, 'L' => 12, 'M' => 13,
627
'N' => 14, 'O' => 15, 'P' => 16, 'Q' => 17, 'R' => 18, 'S' => 19, 'T' => 20, 'U' => 21, 'V' => 22, 'W' => 23, 'X' => 24, 'Y' => 25, 'Z' => 26
630
if (isset($lookup[$pString]))
631
return $lookup[$pString];
633
// Convert to uppercase
634
$pString = strtoupper($pString);
636
$strLen = strlen($pString);
637
// Convert column to integer
639
return (ord($pString{0}) - 64);
640
} elseif ($strLen == 2) {
641
return $result = ((1 + (ord($pString{0}) - 65)) * 26) + (ord($pString{1}) - 64);
642
} elseif ($strLen == 3) {
643
return ((1 + (ord($pString{0}) - 65)) * 676) + ((1 + (ord($pString{1}) - 65)) * 26) + (ord($pString{2}) - 64);
645
throw new Exception("Column string index can not be " . ($strLen != 0 ? "longer than 3 characters" : "empty") . ".");
650
* String from columnindex
652
* @param int $pColumnIndex Column index (base 0 !!!)
655
public static function stringFromColumnIndex($pColumnIndex = 0)
657
// Determine column string
658
if ($pColumnIndex < 26) {
659
return chr(65 + $pColumnIndex);
661
return PHPExcel_Cell::stringFromColumnIndex((int)($pColumnIndex / 26) -1).chr(65 + $pColumnIndex%26) ;
665
* Extract all cell references in range
667
* @param string $pRange Range (e.g. A1 or A1:A10 or A1:A10 A100:A1000)
668
* @return array Array containing single cell references
670
public static function extractAllCellReferencesInRange($pRange = 'A1') {
672
$returnValue = array();
675
$aExplodeSpaces = explode(' ', str_replace('$', '', strtoupper($pRange)));
676
foreach ($aExplodeSpaces as $explodedSpaces) {
678
if (strpos($explodedSpaces,':') === false && strpos($explodedSpaces,',') === false) {
681
list($col, $row) = PHPExcel_Cell::coordinateFromString($explodedSpaces);
683
if (strlen($col) <= 2) {
684
$returnValue[] = $explodedSpaces;
691
$range = PHPExcel_Cell::splitRange($explodedSpaces);
692
for ($i = 0; $i < count($range); ++$i) {
694
if (count($range[$i]) == 1) {
697
list($col, $row) = PHPExcel_Cell::coordinateFromString($range[$i]);
699
if (strlen($col) <= 2) {
700
$returnValue[] = $explodedSpaces;
705
$rangeStart = $rangeEnd = '';
706
$startingCol = $startingRow = $endingCol = $endingRow = 0;
708
list($rangeStart, $rangeEnd) = $range[$i];
709
list($startingCol, $startingRow) = PHPExcel_Cell::coordinateFromString($rangeStart);
710
list($endingCol, $endingRow) = PHPExcel_Cell::coordinateFromString($rangeEnd);
713
$startingCol = PHPExcel_Cell::columnIndexFromString($startingCol);
714
$endingCol = PHPExcel_Cell::columnIndexFromString($endingCol);
717
$currentCol = --$startingCol;
718
$currentRow = $startingRow;
721
while ($currentCol < $endingCol) {
722
$loopColumn = PHPExcel_Cell::stringFromColumnIndex($currentCol);
723
while ($currentRow <= $endingRow) {
724
$returnValue[] = $loopColumn.$currentRow;
728
$currentRow = $startingRow;
740
* @param PHPExcel_Cell $a Cell a
741
* @param PHPExcel_Cell $a Cell b
742
* @return int Result of comparison (always -1 or 1, never zero!)
744
public static function compareCells(PHPExcel_Cell $a, PHPExcel_Cell $b)
746
if ($a->_row < $b->_row) {
748
} elseif ($a->_row > $b->_row) {
750
} elseif (PHPExcel_Cell::columnIndexFromString($a->_column) < PHPExcel_Cell::columnIndexFromString($b->_column)) {
758
* Get value binder to use
760
* @return PHPExcel_Cell_IValueBinder
762
public static function getValueBinder() {
763
if (is_null(self::$_valueBinder)) {
764
self::$_valueBinder = new PHPExcel_Cell_DefaultValueBinder();
767
return self::$_valueBinder;
771
* Set value binder to use
773
* @param PHPExcel_Cell_IValueBinder $binder
776
public static function setValueBinder(PHPExcel_Cell_IValueBinder $binder = null) {
777
if (is_null($binder)) {
778
throw new Exception("A PHPExcel_Cell_IValueBinder is required for PHPExcel to function correctly.");
781
self::$_valueBinder = $binder;
785
* Implement PHP __clone to create a deep clone, not just a shallow copy.
787
public function __clone() {
788
$vars = get_object_vars($this);
789
foreach ($vars as $key => $value) {
790
if ((is_object($value)) && ($key != '_parent')) {
791
$this->$key = clone $value;
793
$this->$key = $value;
799
* Get index to cellXf
803
public function getXfIndex()
805
return $this->_xfIndex;
809
* Set index to cellXf
812
* @return PHPExcel_Cell
814
public function setXfIndex($pValue = 0)
816
$this->_xfIndex = $pValue;
818
return $this->notifyCacheController();
822
public function setFormulaAttributes($pAttributes)
824
$this->_formulaAttributes = $pAttributes;
828
public function getFormulaAttributes()
830
return $this->_formulaAttributes;