PHPExcel_Cell
[ class tree: PHPExcel_Cell ] [ index: PHPExcel_Cell ] [ all elements ]

Source for file Cell.php

Documentation is available at Cell.php

  1. <?php
  2. /**
  3.  * PHPExcel
  4.  *
  5.  * Copyright (c) 2006 - 2009 PHPExcel
  6.  *
  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.
  11.  *
  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.
  16.  *
  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
  20.  *
  21.  * @category   PHPExcel
  22.  * @package    PHPExcel_Cell
  23.  * @copyright  Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.6.5, 2009-01-05
  26.  */
  27.  
  28.  
  29. /** PHPExcel_Cell_DataType */
  30. require_once 'PHPExcel/Cell/DataType.php';
  31.  
  32. /** PHPExcel_Cell_DataValidation */
  33. require_once 'PHPExcel/Cell/DataValidation.php';
  34.  
  35. /** PHPExcel_Cell_Hyperlink */
  36. require_once 'PHPExcel/Cell/Hyperlink.php';
  37.  
  38. /** PHPExcel_Worksheet */
  39. require_once 'PHPExcel/Worksheet.php';
  40.  
  41. /** PHPExcel_Calculation */
  42. require_once 'PHPExcel/Calculation.php';
  43.  
  44.  
  45. /**
  46.  * PHPExcel_Cell
  47.  *
  48.  * @category   PHPExcel
  49.  * @package    PHPExcel_Cell
  50.  * @copyright  Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  51.  */
  52. {
  53.     /**
  54.      * Column of the cell
  55.      *
  56.      * @var string 
  57.      */
  58.     private $_column;
  59.  
  60.     /**
  61.      * Row of the cell
  62.      *
  63.      * @var int 
  64.      */
  65.     private $_row;
  66.  
  67.     /**
  68.      * Value of the cell
  69.      *
  70.      * @var mixed 
  71.      */
  72.     private $_value;
  73.  
  74.     /**
  75.      * Type of the cell data
  76.      *
  77.      * @var string 
  78.      */
  79.     private $_dataType;
  80.  
  81.     /**
  82.      * Data validation
  83.      *
  84.      * @var PHPExcel_Cell_DataValidation 
  85.      */
  86.     private $_dataValidation;
  87.  
  88.     /**
  89.      * Hyperlink
  90.      *
  91.      * @var PHPExcel_Cell_Hyperlink 
  92.      */
  93.     private $_hyperlink;
  94.  
  95.     /**
  96.      * Parent worksheet
  97.      *
  98.      * @var PHPExcel_Worksheet 
  99.      */
  100.     private $_parent;
  101.  
  102.     /**
  103.      * Create a new Cell
  104.      *
  105.      * @param     string                 $pColumn 
  106.      * @param     int                 $pRow 
  107.      * @param     mixed                 $pValue 
  108.      * @param     string                 $pDataType 
  109.      * @param     PHPExcel_Worksheet    $pSheet 
  110.      * @throws    Exception
  111.      */
  112.     public function __construct($pColumn 'A'$pRow 1$pValue null$pDataType nullPHPExcel_Worksheet $pSheet null)
  113.     {
  114.         // Initialise cell coordinate
  115.         $this->_column = strtoupper($pColumn);
  116.         $this->_row = $pRow;
  117.  
  118.         // Initialise cell value
  119.         $this->_value = $pValue;
  120.  
  121.         // Set datatype?
  122.         if (!is_null($pDataType)) {
  123.             $this->_dataType = $pDataType;
  124.         else {
  125.             $this->_dataType = PHPExcel_Cell_DataType::dataTypeForValue($pValue);
  126.         }
  127.  
  128.         // Set worksheet
  129.         $this->_parent = $pSheet;
  130.     }
  131.  
  132.     /**
  133.      * Get cell coordinate column
  134.      *
  135.      * @return string 
  136.      */
  137.     public function getColumn()
  138.     {
  139.         return strtoupper($this->_column);
  140.     }
  141.  
  142.     /**
  143.      * Get cell coordinate row
  144.      *
  145.      * @return int 
  146.      */
  147.     public function getRow()
  148.     {
  149.         return $this->_row;
  150.     }
  151.  
  152.     /**
  153.      * Get cell coordinate
  154.      *
  155.      * @return string 
  156.      */
  157.     public function getCoordinate()
  158.     {
  159.         return $this->_column . $this->_row;
  160.     }
  161.  
  162.     /**
  163.      * Get cell value
  164.      *
  165.      * @return mixed 
  166.      */
  167.     public function getValue()
  168.     {
  169.         return $this->_value;
  170.     }
  171.  
  172.     /**
  173.      * Set cell value
  174.      *
  175.      * This clears the cell formula.
  176.      *
  177.      * @param mixed     $pValue                    Value
  178.      * @param bool         $pUpdateDataType        Update the data type?
  179.      */
  180.     public function setValue($pValue null$pUpdateDataType true)
  181.     {
  182.         $this->_value = $pValue;
  183.  
  184.         if ($pUpdateDataType{
  185.             $this->_dataType = PHPExcel_Cell_DataType::dataTypeForValue($pValue);
  186.         }
  187.     }
  188.  
  189.     /**
  190.      * Set cell value (with explicit data type given)
  191.      *
  192.      * @param mixed     $pValue            Value
  193.      * @param string    $pDataType        Explicit data type
  194.      */
  195.     public function setValueExplicit($pValue null$pDataType PHPExcel_Cell_DataType::TYPE_STRING)
  196.     {
  197.         $this->_value         = $pValue;
  198.            $this->_dataType     = $pDataType;
  199.     }
  200.  
  201.     /**
  202.      * Get caluclated cell value
  203.      *
  204.      * @return mixed 
  205.      */
  206.     public function getCalculatedValue()
  207.     {
  208.         if (is_null($this->_value|| $this->_value === ''{
  209.             return '';
  210.         else if ($this->_dataType != PHPExcel_Cell_DataType::TYPE_FORMULA{
  211.             return $this->_value;
  212.         else {
  213.             return PHPExcel_Calculation::getInstance()->calculate($this);
  214.         }
  215.     }
  216.  
  217.     /**
  218.      * Get cell data type
  219.      *
  220.      * @return string 
  221.      */
  222.     public function getDataType()
  223.     {
  224.         return $this->_dataType;
  225.     }
  226.  
  227.     /**
  228.      * Set cell data type
  229.      *
  230.      * @param string $pDataType 
  231.      */
  232.     public function setDataType($pDataType PHPExcel_Cell_DataType::TYPE_STRING)
  233.     {
  234.         $this->_dataType = $pDataType;
  235.     }
  236.  
  237.     /**
  238.      * Has Data validation?
  239.      *
  240.      * @return boolean 
  241.      */
  242.     public function hasDataValidation()
  243.     {
  244.         return !is_null($this->_dataValidation);
  245.     }
  246.  
  247.     /**
  248.      * Get Data validation
  249.      *
  250.      * @return PHPExcel_Cell_DataValidation 
  251.      */
  252.     public function getDataValidation()
  253.     {
  254.         if (is_null($this->_dataValidation)) {
  255.             $this->_dataValidation = new PHPExcel_Cell_DataValidation($this);
  256.         }
  257.  
  258.         return $this->_dataValidation;
  259.     }
  260.  
  261.     /**
  262.      * Set Data validation
  263.      *
  264.      * @param     PHPExcel_Cell_DataValidation    $pDataValidation 
  265.      * @throws     Exception
  266.      */
  267.     public function setDataValidation(PHPExcel_Cell_DataValidation $pDataValidation null)
  268.     {
  269.            $this->_dataValidation = $pDataValidation;
  270.         $this->_dataValidation->setParent($this);
  271.     }
  272.  
  273.     /**
  274.      * Has Hyperlink
  275.      *
  276.      * @return boolean 
  277.      */
  278.     public function hasHyperlink()
  279.     {
  280.         return !is_null($this->_hyperlink);
  281.     }
  282.  
  283.     /**
  284.      * Get Hyperlink
  285.      *
  286.      * @return PHPExcel_Cell_Hyperlink 
  287.      */
  288.     public function getHyperlink()
  289.     {
  290.         if (is_null($this->_hyperlink)) {
  291.             $this->_hyperlink = new PHPExcel_Cell_Hyperlink($this);
  292.         }
  293.  
  294.         return $this->_hyperlink;
  295.     }
  296.  
  297.     /**
  298.      * Set Hyperlink
  299.      *
  300.      * @param     PHPExcel_Cell_Hyperlink    $pHyperlink 
  301.      * @throws     Exception
  302.      */
  303.     public function setHyperlink(PHPExcel_Cell_Hyperlink $pHyperlink null)
  304.     {
  305.            $this->_hyperlink    = $pHyperlink;
  306.         $this->_hyperlink->setParent($this);
  307.     }
  308.  
  309.     /**
  310.      * Get parent
  311.      *
  312.      * @return PHPExcel_Worksheet 
  313.      */
  314.     public function getParent({
  315.         return $this->_parent;
  316.     }
  317.  
  318.     /**
  319.      * Re-bind parent
  320.      *
  321.      * @param PHPExcel_Worksheet $parent 
  322.      */
  323.     public function rebindParent(PHPExcel_Worksheet $parent{
  324.         $this->_parent = $parent;
  325.     }
  326.  
  327.     /**
  328.      * Is cell in a specific range?
  329.      *
  330.      * @param     string     $pRange        Cell range (e.g. A1:A1)
  331.      * @return     boolean 
  332.      */
  333.     public function isInRange($pRange 'A1:A1')
  334.     {
  335.         // Uppercase coordinate
  336.         $pRange strtoupper($pRange);
  337.  
  338.            // Extract range
  339.            $rangeA     '';
  340.            $rangeB     '';
  341.            if (strpos($pRange':'=== false{
  342.                $rangeA $pRange;
  343.                $rangeB $pRange;
  344.            else {
  345.                list($rangeA$rangeBexplode(':'$pRange);
  346.            }
  347.  
  348.            // Calculate range outer borders
  349.            $rangeStart PHPExcel_Cell::coordinateFromString($rangeA);
  350.            $rangeEnd     PHPExcel_Cell::coordinateFromString($rangeB);
  351.  
  352.            // Translate column into index
  353.            $rangeStart[0]    PHPExcel_Cell::columnIndexFromString($rangeStart[0]1;
  354.            $rangeEnd[0]    PHPExcel_Cell::columnIndexFromString($rangeEnd[0]1;
  355.  
  356.            // Translate properties
  357.         $myColumn        PHPExcel_Cell::columnIndexFromString($this->getColumn()) 1;
  358.         $myRow            $this->getRow();
  359.  
  360.         // Verify if cell is in range
  361.         return (
  362.                 ($rangeStart[0<= $myColumn && $rangeEnd[0>= $myColumn&&
  363.                 ($rangeStart[1<= $myRow && $rangeEnd[1>= $myRow)
  364.         );
  365.     }
  366.  
  367.     /**
  368.      * Coordinate from string
  369.      *
  370.      * @param     string     $pCoordinateString 
  371.      * @return     array     Array containing column and row (indexes 0 and 1)
  372.      * @throws    Exception
  373.      */
  374.     public static function coordinateFromString($pCoordinateString 'A1')
  375.     {
  376.         if (strpos($pCoordinateString,':'!== false{
  377.             throw new Exception('Cell coordinate string can not be a range of cells.');
  378.         else if ($pCoordinateString == ''{
  379.             throw new Exception('Cell coordinate can not be zero-length string.');
  380.         else {
  381.             // Column
  382.             $column '';
  383.  
  384.             // Row
  385.             $row '';
  386.  
  387.             // Convert a cell reference
  388.             if (preg_match("/([$]?[A-Z]+)([$]?\d+)/"$pCoordinateString$matches)) {
  389.                 list($column$row$matches;
  390.             }
  391.  
  392.             // Return array
  393.             return array($column$row);
  394.         }
  395.     }
  396.  
  397.     /**
  398.      * Make string coordinate absolute
  399.      *
  400.      * @param     string     $pCoordinateString 
  401.      * @return     string    Absolute coordinate
  402.      * @throws    Exception
  403.      */
  404.     public static function absoluteCoordinate($pCoordinateString 'A1')
  405.     {
  406.         if (strpos($pCoordinateString,':'=== false{
  407.             // Return value
  408.             $returnValue '';
  409.  
  410.             // Create absolute coordinate
  411.             list($column$rowPHPExcel_Cell::coordinateFromString($pCoordinateString);
  412.             $returnValue '$' $column '$' $row;
  413.  
  414.             // Return
  415.             return $returnValue;
  416.         else {
  417.             throw new Exception("Coordinate string should not be a cell range.");
  418.         }
  419.     }
  420.  
  421.     /**
  422.      * Split range into coordinate strings
  423.      *
  424.      * @param     string     $pRange 
  425.      * @return     array    Array containg two coordinate strings
  426.      */
  427.     public static function splitRange($pRange 'A1:A1')
  428.     {
  429.         return explode(':'$pRange);
  430.     }
  431.  
  432.     /**
  433.      * Calculate range dimension
  434.      *
  435.      * @param     string     $pRange        Cell range (e.g. A1:A1)
  436.      * @return     array    Range dimension (width, height)
  437.      */
  438.     public static function rangeDimension($pRange 'A1:A1')
  439.     {
  440.         // Uppercase coordinate
  441.         $pRange strtoupper($pRange);
  442.  
  443.            // Extract range
  444.            $rangeA     '';
  445.            $rangeB     '';
  446.            if (strpos($pRange':'=== false{
  447.                $rangeA $pRange;
  448.                $rangeB $pRange;
  449.            else {
  450.                list($rangeA$rangeBexplode(':'$pRange);
  451.            }
  452.  
  453.            // Calculate range outer borders
  454.            $rangeStart PHPExcel_Cell::coordinateFromString($rangeA);
  455.            $rangeEnd     PHPExcel_Cell::coordinateFromString($rangeB);
  456.  
  457.            // Translate column into index
  458.            $rangeStart[0]    PHPExcel_Cell::columnIndexFromString($rangeStart[0]);
  459.            $rangeEnd[0]    PHPExcel_Cell::columnIndexFromString($rangeEnd[0]);
  460.  
  461.            return array( ($rangeEnd[0$rangeStart[01)($rangeEnd[1$rangeStart[11) );
  462.     }
  463.  
  464.     /**
  465.      * Column index from string
  466.      *
  467.      * @param     string $pString 
  468.      * @return     int Column index (base 1 !!!)
  469.      * @throws     Exception
  470.      */
  471.     public static function columnIndexFromString($pString 'A')
  472.     {
  473.         // Convert to uppercase
  474.         $pString strtoupper($pString);
  475.  
  476.         $strLen strlen($pString);
  477.         // Convert column to integer
  478.         if ($strLen == 1{
  479.             return (ord($pString{0}64);
  480.         elseif ($strLen == 2{
  481.             return $result (((ord($pString{0}65)) 26(ord($pString{1}64);
  482.         elseif ($strLen == 3{
  483.             return (((ord($pString{0}65)) 676(((ord($pString{1}65)) 26(ord($pString{2}64);
  484.         else {
  485.             throw new Exception("Column string index can not be " ($strLen != "longer than 3 characters" "empty"".");
  486.         }
  487.     }
  488.  
  489.     /**
  490.      * String from columnindex
  491.      *
  492.      * @param int $pColumnIndex Column index (base 0 !!!)
  493.      * @return string 
  494.      */
  495.     public static function stringFromColumnIndex($pColumnIndex 0)
  496.     {
  497.         // Determine column string
  498.         if ($pColumnIndex 26{
  499.             return chr(65 $pColumnIndex);
  500.         }
  501.            return PHPExcel_Cell::stringFromColumnIndex((int)($pColumnIndex 26-1).chr(65 $pColumnIndex%26;
  502.     }
  503.  
  504.     /**
  505.      * Extract all cell references in range
  506.      *
  507.      * @param     string     $pRange        Range (e.g. A1 or A1:A10 or A1:A10 A100:A1000)
  508.      * @return     array    Array containing single cell references
  509.      */
  510.     public static function extractAllCellReferencesInRange($pRange 'A1'{
  511.         // Returnvalue
  512.         $returnValue array();
  513.  
  514.         // Explode spaces
  515.         $aExplodeSpaces explode(' 'str_replace('$'''strtoupper($pRange)));
  516.         foreach ($aExplodeSpaces as $explodedSpaces{
  517.             // Single cell?
  518.             if (strpos($explodedSpaces':'=== false{
  519.                 $col 'A';
  520.                 $row 1;
  521.                 list($col$rowPHPExcel_Cell::coordinateFromString($explodedSpaces);
  522.  
  523.                 if (strlen($col<= 2{
  524.                     $returnValue[$explodedSpaces;
  525.                 }
  526.  
  527.                 continue;
  528.             }
  529.  
  530.             // Range...
  531.             $rangeStart        $rangeEnd        '';
  532.             $startingCol    $startingRow    $endingCol    $endingRow    0;
  533.  
  534.             list($rangeStart$rangeEnd)         explode(':'$explodedSpaces);
  535.             list($startingCol$startingRow)    PHPExcel_Cell::coordinateFromString($rangeStart);
  536.             list($endingCol$endingRow)          PHPExcel_Cell::coordinateFromString($rangeEnd);
  537.  
  538.             // Conversions...
  539.             $startingCol     PHPExcel_Cell::columnIndexFromString($startingCol);
  540.             $endingCol         PHPExcel_Cell::columnIndexFromString($endingCol);
  541.  
  542.             // Current data
  543.             $currentCol     = --$startingCol;
  544.             $currentRow     $startingRow;
  545.  
  546.             // Loop cells
  547.             while ($currentCol $endingCol{
  548.                 $loopColumn PHPExcel_Cell::stringFromColumnIndex($currentCol);
  549.                 while ($currentRow <= $endingRow{
  550.                     $returnValue[$loopColumn.$currentRow;
  551.                     ++$currentRow;
  552.                 }
  553.                 ++$currentCol;
  554.                 $currentRow $startingRow;
  555.             }
  556.         }
  557.  
  558.         // Return value
  559.         return $returnValue;
  560.     }
  561.  
  562.     /**
  563.      * Compare 2 cells
  564.      *
  565.      * @param     PHPExcel_Cell    $a    Cell a
  566.      * @param     PHPExcel_Cell    $a    Cell b
  567.      * @return     int        Result of comparison (always -1 or 1, never zero!)
  568.      */
  569.     public static function compareCells(PHPExcel_Cell $aPHPExcel_Cell $b)
  570.     {
  571.         if ($a->_row $b->_row{
  572.             return -1;
  573.         elseif ($a->_row $b->_row{
  574.             return 1;
  575.         elseif (PHPExcel_Cell::columnIndexFromString($a->_columnPHPExcel_Cell::columnIndexFromString($b->_column)) {
  576.             return -1;
  577.         else {
  578.             return 1;
  579.         }
  580.     }
  581.  
  582.     /**
  583.      * Implement PHP __clone to create a deep clone, not just a shallow copy.
  584.      */
  585.     public function __clone({
  586.         $vars get_object_vars($this);
  587.         foreach ($vars as $key => $value{
  588.             if (is_object($value)) {
  589.                 $this->$key clone $value;
  590.             else {
  591.                 $this->$key $value;
  592.             }
  593.         }
  594.     }
  595. }

Documentation generated on Mon, 05 Jan 2009 20:36:39 +0100 by phpDocumentor 1.4.1