Source for file Cell.php
Documentation is available at Cell.php
* Copyright (c) 2006 - 2009 PHPExcel
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
* @copyright Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
* @version 1.6.5, 2009-01-05
/** PHPExcel_Cell_DataType */
require_once 'PHPExcel/Cell/DataType.php';
/** PHPExcel_Cell_DataValidation */
require_once 'PHPExcel/Cell/DataValidation.php';
/** PHPExcel_Cell_Hyperlink */
require_once 'PHPExcel/Cell/Hyperlink.php';
/** PHPExcel_Worksheet */
require_once 'PHPExcel/Worksheet.php';
/** PHPExcel_Calculation */
require_once 'PHPExcel/Calculation.php';
* @copyright Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
* @var PHPExcel_Cell_DataValidation
* @var PHPExcel_Cell_Hyperlink
* @var PHPExcel_Worksheet
* @param string $pDataType
* @param PHPExcel_Worksheet $pSheet
public function __construct($pColumn = 'A', $pRow = 1, $pValue = null, $pDataType = null, PHPExcel_Worksheet $pSheet = null)
// Initialise cell coordinate
* Get cell coordinate column
* Get cell coordinate row
* This clears the cell formula.
* @param mixed $pValue Value
* @param bool $pUpdateDataType Update the data type?
public function setValue($pValue = null, $pUpdateDataType = true)
* Set cell value (with explicit data type given)
* @param mixed $pValue Value
* @param string $pDataType Explicit data type
public function setValueExplicit($pValue = null, $pDataType = PHPExcel_Cell_DataType::TYPE_STRING)
* Get caluclated cell value
* @param string $pDataType
public function setDataType($pDataType = PHPExcel_Cell_DataType::TYPE_STRING)
* @return PHPExcel_Cell_DataValidation
* @param PHPExcel_Cell_DataValidation $pDataValidation
public function setDataValidation(PHPExcel_Cell_DataValidation $pDataValidation = null)
* @return PHPExcel_Cell_Hyperlink
* @param PHPExcel_Cell_Hyperlink $pHyperlink
public function setHyperlink(PHPExcel_Cell_Hyperlink $pHyperlink = null)
* @return PHPExcel_Worksheet
* @param PHPExcel_Worksheet $parent
* Is cell in a specific range?
* @param string $pRange Cell range (e.g. A1:A1)
if (strpos($pRange, ':') === false) {
list ($rangeA, $rangeB) = explode(':', $pRange);
// Calculate range outer borders
// Translate column into index
// Verify if cell is in range
($rangeStart[0] <= $myColumn && $rangeEnd[0] >= $myColumn) &&
($rangeStart[1] <= $myRow && $rangeEnd[1] >= $myRow)
* @param string $pCoordinateString
* @return array Array containing column and row (indexes 0 and 1)
if (strpos($pCoordinateString,':') !== false) {
throw new Exception('Cell coordinate string can not be a range of cells.');
} else if ($pCoordinateString == '') {
throw new Exception('Cell coordinate can not be zero-length string.');
// Convert a cell reference
if (preg_match("/([$]?[A-Z]+)([$]?\d+)/", $pCoordinateString, $matches)) {
list (, $column, $row) = $matches;
return array($column, $row);
* Make string coordinate absolute
* @param string $pCoordinateString
* @return string Absolute coordinate
if (strpos($pCoordinateString,':') === false) {
// Create absolute coordinate
$returnValue = '$' . $column . '$' . $row;
throw new Exception("Coordinate string should not be a cell range.");
* Split range into coordinate strings
* @return array Array containg two coordinate strings
public static function splitRange($pRange = 'A1:A1')
* Calculate range dimension
* @param string $pRange Cell range (e.g. A1:A1)
* @return array Range dimension (width, height)
if (strpos($pRange, ':') === false) {
list ($rangeA, $rangeB) = explode(':', $pRange);
// Calculate range outer borders
// Translate column into index
return array( ($rangeEnd[0] - $rangeStart[0] + 1), ($rangeEnd[1] - $rangeStart[1] + 1) );
* Column index from string
* @return int Column index (base 1 !!!)
// Convert column to integer
return (ord($pString{0}) - 64);
} elseif ($strLen == 2) {
return $result = ((1 + (ord($pString{0}) - 65)) * 26) + (ord($pString{1}) - 64);
} elseif ($strLen == 3) {
return ((1 + (ord($pString{0}) - 65)) * 676) + ((1 + (ord($pString{1}) - 65)) * 26) + (ord($pString{2}) - 64);
throw new Exception("Column string index can not be " . ($strLen != 0 ? "longer than 3 characters" : "empty") . ".");
* String from columnindex
* @param int $pColumnIndex Column index (base 0 !!!)
// Determine column string
if ($pColumnIndex < 26) {
return chr(65 + $pColumnIndex);
* Extract all cell references in range
* @param string $pRange Range (e.g. A1 or A1:A10 or A1:A10 A100:A1000)
* @return array Array containing single cell references
foreach ($aExplodeSpaces as $explodedSpaces) {
if (strpos($explodedSpaces, ':') === false) {
$returnValue[] = $explodedSpaces;
$rangeStart = $rangeEnd = '';
$startingCol = $startingRow = $endingCol = $endingRow = 0;
list ($rangeStart, $rangeEnd) = explode(':', $explodedSpaces);
$currentCol = -- $startingCol;
$currentRow = $startingRow;
while ($currentCol < $endingCol) {
while ($currentRow <= $endingRow) {
$returnValue[] = $loopColumn. $currentRow;
$currentRow = $startingRow;
* @param PHPExcel_Cell $a Cell a
* @param PHPExcel_Cell $a Cell b
* @return int Result of comparison (always -1 or 1, never zero!)
public static function compareCells(PHPExcel_Cell $a, PHPExcel_Cell $b)
if ($a->_row < $b->_row) {
} elseif ($a->_row > $b->_row) {
* Implement PHP __clone to create a deep clone, not just a shallow copy.
foreach ($vars as $key => $value) {
$this->$key = clone $value;
|