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.7, 2009-04-22
  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. /** PHPExcel_Cell_IValueBinder */
  45. require_once 'PHPExcel/Cell/IValueBinder.php';
  46.  
  47. /** PHPExcel_Cell_DefaultValueBinder */
  48. require_once 'PHPExcel/Cell/DefaultValueBinder.php';
  49.  
  50.  
  51. /**
  52.  * PHPExcel_Cell
  53.  *
  54.  * @category   PHPExcel
  55.  * @package    PHPExcel_Cell
  56.  * @copyright  Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  57.  */
  58. {
  59.     /**
  60.      * Value binder to use
  61.      *
  62.      * @var PHPExcel_Cell_IValueBinder 
  63.      */
  64.     private static $_valueBinder null;
  65.  
  66.     /**
  67.      * Column of the cell
  68.      *
  69.      * @var string 
  70.      */
  71.     private $_column;
  72.  
  73.     /**
  74.      * Row of the cell
  75.      *
  76.      * @var int 
  77.      */
  78.     private $_row;
  79.  
  80.     /**
  81.      * Value of the cell
  82.      *
  83.      * @var mixed 
  84.      */
  85.     private $_value;
  86.  
  87.     /**
  88.      * Calculated value of the cell (used for caching)
  89.      *
  90.      * @var mixed 
  91.      */
  92.     private $_calculatedValue = null;
  93.  
  94.     /**
  95.      * Type of the cell data
  96.      *
  97.      * @var string 
  98.      */
  99.     private $_dataType;
  100.  
  101.     /**
  102.      * Data validation
  103.      *
  104.      * @var PHPExcel_Cell_DataValidation 
  105.      */
  106.     private $_dataValidation;
  107.  
  108.     /**
  109.      * Hyperlink
  110.      *
  111.      * @var PHPExcel_Cell_Hyperlink 
  112.      */
  113.     private $_hyperlink;
  114.  
  115.     /**
  116.      * Parent worksheet
  117.      *
  118.      * @var PHPExcel_Worksheet 
  119.      */
  120.     private $_parent;
  121.  
  122.     /**
  123.      * Create a new Cell
  124.      *
  125.      * @param     string                 $pColumn 
  126.      * @param     int                 $pRow 
  127.      * @param     mixed                 $pValue 
  128.      * @param     string                 $pDataType 
  129.      * @param     PHPExcel_Worksheet    $pSheet 
  130.      * @throws    Exception
  131.      */
  132.     public function __construct($pColumn 'A'$pRow 1$pValue null$pDataType nullPHPExcel_Worksheet $pSheet null)
  133.     {
  134.         // Set value binder?
  135.         if (is_null(self::$_valueBinder)) {
  136.             self::$_valueBinder new PHPExcel_Cell_DefaultValueBinder();
  137.         }
  138.  
  139.         // Initialise cell coordinate
  140.         $this->_column = strtoupper($pColumn);
  141.         $this->_row = $pRow;
  142.  
  143.         // Initialise cell value
  144.         $this->_value = $pValue;
  145.  
  146.         // Set worksheet
  147.         $this->_parent = $pSheet;
  148.  
  149.         // Set datatype?
  150.         if (!is_null($pDataType)) {
  151.             $this->_dataType = $pDataType;
  152.         else {
  153.             if (!self::getValueBinder()->bindValue($this$pValue)) {
  154.                 throw new Exception("Value could not be bound to cell.");
  155.             }
  156.         }
  157.     }
  158.  
  159.     /**
  160.      * Get cell coordinate column
  161.      *
  162.      * @return string 
  163.      */
  164.     public function getColumn()
  165.     {
  166.         return strtoupper($this->_column);
  167.     }
  168.  
  169.     /**
  170.      * Get cell coordinate row
  171.      *
  172.      * @return int 
  173.      */
  174.     public function getRow()
  175.     {
  176.         return $this->_row;
  177.     }
  178.  
  179.     /**
  180.      * Get cell coordinate
  181.      *
  182.      * @return string 
  183.      */
  184.     public function getCoordinate()
  185.     {
  186.         return $this->_column . $this->_row;
  187.     }
  188.  
  189.     /**
  190.      * Get cell value
  191.      *
  192.      * @return mixed 
  193.      */
  194.     public function getValue()
  195.     {
  196.         return $this->_value;
  197.     }
  198.  
  199.     /**
  200.      * Set cell value
  201.      *
  202.      * This clears the cell formula.
  203.      *
  204.      * @param mixed     $pValue                    Value
  205.      * @param bool         $pUpdateDataType        Update the data type?
  206.      */
  207.     public function setValue($pValue null$pUpdateDataType true)
  208.     {
  209.         $this->_value = $pValue;
  210.  
  211.         if ($pUpdateDataType{
  212.             if (!self::getValueBinder()->bindValue($this$pValue)) {
  213.                 throw new Exception("Value could not be bound to cell.");
  214.             }
  215.         }
  216.     }
  217.  
  218.     /**
  219.      * Set cell value (with explicit data type given)
  220.      *
  221.      * @param mixed     $pValue            Value
  222.      * @param string    $pDataType        Explicit data type
  223.      */
  224.     public function setValueExplicit($pValue null$pDataType PHPExcel_Cell_DataType::TYPE_STRING)
  225.     {
  226.         $this->_value         = $pValue;
  227.            $this->_dataType     = $pDataType;
  228.     }
  229.  
  230.     /**
  231.      * Get caluclated cell value
  232.      *
  233.      * @return mixed 
  234.      */
  235.     public function getCalculatedValue()
  236.     {
  237.         if (!is_null($this->_calculatedValue&& $this->_dataType == PHPExcel_Cell_DataType::TYPE_FORMULA{
  238.             try {
  239.                 $result PHPExcel_Calculation::getInstance()->calculate($this);
  240.             catch Exception $ex {
  241.                 $result '#N/A';
  242.             }
  243.  
  244.             if ((is_string($result)) && ($result == '#Not Yet Implemented')) {
  245.                 return $this->_calculatedValue// Fallback if calculation engine does not support the formula.
  246.             else {
  247.                 return $result;
  248.             }
  249.         }
  250.  
  251.         if (is_null($this->_value|| $this->_value === ''{
  252.         else if ($this->_dataType != PHPExcel_Cell_DataType::TYPE_FORMULA{
  253.             return $this->_value;
  254.         else {
  255.             return PHPExcel_Calculation::getInstance()->calculate($this);
  256.         }
  257.     }
  258.  
  259.     /**
  260.      * Set calculated value (used for caching)
  261.      *
  262.      * @param mixed $pValue    Value
  263.      */
  264.     public function setCalculatedValue($pValue null)
  265.     {
  266.         if (!is_null($pValue)) {
  267.             $this->_calculatedValue = $pValue;
  268.         }
  269.     }
  270.  
  271.     public function getOldCalculatedValue()
  272.     {
  273.         return $this->_calculatedValue;
  274.     }
  275.  
  276.     /**
  277.      * Get cell data type
  278.      *
  279.      * @return string 
  280.      */
  281.     public function getDataType()
  282.     {
  283.         return $this->_dataType;
  284.     }
  285.  
  286.     /**
  287.      * Set cell data type
  288.      *
  289.      * @param string $pDataType 
  290.      */
  291.     public function setDataType($pDataType PHPExcel_Cell_DataType::TYPE_STRING)
  292.     {
  293.         $this->_dataType = $pDataType;
  294.     }
  295.  
  296.     /**
  297.      * Has Data validation?
  298.      *
  299.      * @return boolean 
  300.      */
  301.     public function hasDataValidation()
  302.     {
  303.         return !is_null($this->_dataValidation);
  304.     }
  305.  
  306.     /**
  307.      * Get Data validation
  308.      *
  309.      * @return PHPExcel_Cell_DataValidation 
  310.      */
  311.     public function getDataValidation()
  312.     {
  313.         if (is_null($this->_dataValidation)) {
  314.             $this->_dataValidation = new PHPExcel_Cell_DataValidation($this);
  315.         }
  316.  
  317.         return $this->_dataValidation;
  318.     }
  319.  
  320.     /**
  321.      * Set Data validation
  322.      *
  323.      * @param     PHPExcel_Cell_DataValidation    $pDataValidation 
  324.      * @throws     Exception
  325.      */
  326.     public function setDataValidation(PHPExcel_Cell_DataValidation $pDataValidation null)
  327.     {
  328.            $this->_dataValidation = $pDataValidation;
  329.         $this->_dataValidation->setParent($this);
  330.     }
  331.  
  332.     /**
  333.      * Has Hyperlink
  334.      *
  335.      * @return boolean 
  336.      */
  337.     public function hasHyperlink()
  338.     {
  339.         return !is_null($this->_hyperlink);
  340.     }
  341.  
  342.     /**
  343.      * Get Hyperlink
  344.      *
  345.      * @return PHPExcel_Cell_Hyperlink 
  346.      */
  347.     public function getHyperlink()
  348.     {
  349.         if (is_null($this->_hyperlink)) {
  350.             $this->_hyperlink = new PHPExcel_Cell_Hyperlink($this);
  351.         }
  352.  
  353.         return $this->_hyperlink;
  354.     }
  355.  
  356.     /**
  357.      * Set Hyperlink
  358.      *
  359.      * @param     PHPExcel_Cell_Hyperlink    $pHyperlink 
  360.      * @throws     Exception
  361.      */
  362.     public function setHyperlink(PHPExcel_Cell_Hyperlink $pHyperlink null)
  363.     {
  364.            $this->_hyperlink    = $pHyperlink;
  365.         $this->_hyperlink->setParent($this);
  366.     }
  367.  
  368.     /**
  369.      * Get parent
  370.      *
  371.      * @return PHPExcel_Worksheet 
  372.      */
  373.     public function getParent({
  374.         return $this->_parent;
  375.     }
  376.  
  377.     /**
  378.      * Re-bind parent
  379.      *
  380.      * @param PHPExcel_Worksheet $parent 
  381.      */
  382.     public function rebindParent(PHPExcel_Worksheet $parent{
  383.         $this->_parent = $parent;
  384.     }
  385.  
  386.     /**
  387.      * Is cell in a specific range?
  388.      *
  389.      * @param     string     $pRange        Cell range (e.g. A1:A1)
  390.      * @return     boolean 
  391.      */
  392.     public function isInRange($pRange 'A1:A1')
  393.     {
  394.         // Uppercase coordinate
  395.         $pRange strtoupper($pRange);
  396.  
  397.            // Extract range
  398.            $rangeA     '';
  399.            $rangeB     '';
  400.            if (strpos($pRange':'=== false{
  401.                $rangeA $pRange;
  402.                $rangeB $pRange;
  403.            else {
  404.                list($rangeA$rangeBexplode(':'$pRange);
  405.            }
  406.  
  407.            // Calculate range outer borders
  408.            $rangeStart PHPExcel_Cell::coordinateFromString($rangeA);
  409.            $rangeEnd     PHPExcel_Cell::coordinateFromString($rangeB);
  410.  
  411.            // Translate column into index
  412.            $rangeStart[0]    PHPExcel_Cell::columnIndexFromString($rangeStart[0]1;
  413.            $rangeEnd[0]    PHPExcel_Cell::columnIndexFromString($rangeEnd[0]1;
  414.  
  415.            // Translate properties
  416.         $myColumn        PHPExcel_Cell::columnIndexFromString($this->getColumn()) 1;
  417.         $myRow            $this->getRow();
  418.  
  419.         // Verify if cell is in range
  420.         return (
  421.                 ($rangeStart[0<= $myColumn && $rangeEnd[0>= $myColumn&&
  422.                 ($rangeStart[1<= $myRow && $rangeEnd[1>= $myRow)
  423.         );
  424.     }
  425.  
  426.     /**
  427.      * Coordinate from string
  428.      *
  429.      * @param     string     $pCoordinateString 
  430.      * @return     array     Array containing column and row (indexes 0 and 1)
  431.      * @throws    Exception
  432.      */
  433.     public static function coordinateFromString($pCoordinateString 'A1')
  434.     {
  435.         if (strpos($pCoordinateString,':'!== false{
  436.             throw new Exception('Cell coordinate string can not be a range of cells.');
  437.         else if ($pCoordinateString == ''{
  438.             throw new Exception('Cell coordinate can not be zero-length string.');
  439.         else {
  440.             // Column
  441.             $column '';
  442.  
  443.             // Row
  444.             $row '';
  445.  
  446.             // Convert a cell reference
  447.             if (preg_match("/([$]?[A-Z]+)([$]?\d+)/"$pCoordinateString$matches)) {
  448.                 list($column$row$matches;
  449.             }
  450.  
  451.             // Return array
  452.             return array($column$row);
  453.         }
  454.     }
  455.  
  456.     /**
  457.      * Make string coordinate absolute
  458.      *
  459.      * @param     string     $pCoordinateString 
  460.      * @return     string    Absolute coordinate
  461.      * @throws    Exception
  462.      */
  463.     public static function absoluteCoordinate($pCoordinateString 'A1')
  464.     {
  465.         if (strpos($pCoordinateString,':'=== false && strpos($pCoordinateString,','=== false{
  466.             // Return value
  467.             $returnValue '';
  468.  
  469.             // Create absolute coordinate
  470.             list($column$rowPHPExcel_Cell::coordinateFromString($pCoordinateString);
  471.             $returnValue '$' $column '$' $row;
  472.  
  473.             // Return
  474.             return $returnValue;
  475.         else {
  476.             throw new Exception("Coordinate string should not be a cell range.");
  477.         }
  478.     }
  479.  
  480.     /**
  481.      * Split range into coordinate strings
  482.      *
  483.      * @param     string     $pRange 
  484.      * @return     array    Array containg one or more arrays containing one or two coordinate strings
  485.      */
  486.     public static function splitRange($pRange 'A1:A1')
  487.     {
  488.         $exploded explode(','$pRange);
  489.         for ($i 0$i count($exploded)$i++{
  490.             $exploded[$iexplode(':'$exploded[$i]);
  491.         }
  492.         return $exploded;
  493.     }
  494.  
  495.     /**
  496.      * Build range from coordinate strings
  497.      *
  498.      * @param     array    $pRange    Array containg one or more arrays containing one or two coordinate strings
  499.      * @return  string    String representation of $pRange
  500.      * @throws    Exception
  501.      */
  502.     public static function buildRange($pRange)
  503.     {
  504.         // Verify range
  505.         if (!is_array($pRange|| count($pRange== || !is_array($pRange[0])) {
  506.             throw new Exception('Range does not contain any information.');
  507.         }
  508.  
  509.         // Build range
  510.         $imploded array();
  511.         for ($i 0$i count($pRange)$i++{
  512.             $pRange[$iimplode(':'$pRange[$i]);
  513.         }
  514.         $imploded implode(','$pRange);
  515.  
  516.         return $imploded;
  517.     }
  518.  
  519.     /**
  520.      * Calculate range dimension
  521.      *
  522.      * @param     string     $pRange        Cell range (e.g. A1:A1)
  523.      * @return     array    Range dimension (width, height)
  524.      */
  525.     public static function rangeDimension($pRange 'A1:A1')
  526.     {
  527.         // Uppercase coordinate
  528.         $pRange strtoupper($pRange);
  529.  
  530.            // Extract range
  531.            $rangeA     '';
  532.            $rangeB     '';
  533.            if (strpos($pRange':'=== false{
  534.                $rangeA $pRange;
  535.                $rangeB $pRange;
  536.            else {
  537.                list($rangeA$rangeBexplode(':'$pRange);
  538.            }
  539.  
  540.            // Calculate range outer borders
  541.            $rangeStart PHPExcel_Cell::coordinateFromString($rangeA);
  542.            $rangeEnd     PHPExcel_Cell::coordinateFromString($rangeB);
  543.  
  544.            // Translate column into index
  545.            $rangeStart[0]    PHPExcel_Cell::columnIndexFromString($rangeStart[0]);
  546.            $rangeEnd[0]    PHPExcel_Cell::columnIndexFromString($rangeEnd[0]);
  547.  
  548.            return array( ($rangeEnd[0$rangeStart[01)($rangeEnd[1$rangeStart[11) );
  549.     }
  550.  
  551.     /**
  552.      * Column index from string
  553.      *
  554.      * @param     string $pString 
  555.      * @return     int Column index (base 1 !!!)
  556.      * @throws     Exception
  557.      */
  558.     public static function columnIndexFromString($pString 'A')
  559.     {
  560.         // Convert to uppercase
  561.         $pString strtoupper($pString);
  562.  
  563.         $strLen strlen($pString);
  564.         // Convert column to integer
  565.         if ($strLen == 1{
  566.             return (ord($pString{0}64);
  567.         elseif ($strLen == 2{
  568.             return $result (((ord($pString{0}65)) 26(ord($pString{1}64);
  569.         elseif ($strLen == 3{
  570.             return (((ord($pString{0}65)) 676(((ord($pString{1}65)) 26(ord($pString{2}64);
  571.         else {
  572.             throw new Exception("Column string index can not be " ($strLen != "longer than 3 characters" "empty"".");
  573.         }
  574.     }
  575.  
  576.     /**
  577.      * String from columnindex
  578.      *
  579.      * @param int $pColumnIndex Column index (base 0 !!!)
  580.      * @return string 
  581.      */
  582.     public static function stringFromColumnIndex($pColumnIndex 0)
  583.     {
  584.         // Determine column string
  585.         if ($pColumnIndex 26{
  586.             return chr(65 $pColumnIndex);
  587.         }
  588.            return PHPExcel_Cell::stringFromColumnIndex((int)($pColumnIndex 26-1).chr(65 $pColumnIndex%26;
  589.     }
  590.  
  591.     /**
  592.      * Extract all cell references in range
  593.      *
  594.      * @param     string     $pRange        Range (e.g. A1 or A1:A10 or A1:A10 A100:A1000)
  595.      * @return     array    Array containing single cell references
  596.      */
  597.     public static function extractAllCellReferencesInRange($pRange 'A1'{
  598.         // Returnvalue
  599.         $returnValue array();
  600.  
  601.         // Explode spaces
  602.         $aExplodeSpaces explode(' 'str_replace('$'''strtoupper($pRange)));
  603.         foreach ($aExplodeSpaces as $explodedSpaces{
  604.             // Single cell?
  605.             if (strpos($explodedSpaces,':'=== false && strpos($explodedSpaces,','=== false{
  606.                 $col 'A';
  607.                 $row 1;
  608.                 list($col$rowPHPExcel_Cell::coordinateFromString($explodedSpaces);
  609.  
  610.                 if (strlen($col<= 2{
  611.                     $returnValue[$explodedSpaces;
  612.                 }
  613.  
  614.                 continue;
  615.             }
  616.  
  617.             // Range...
  618.             $range PHPExcel_Cell::splitRange($explodedSpaces);
  619.             for ($i 0$i count($range)$i++{
  620.                 // Single cell?
  621.                 if (count($range[$i]== 1{
  622.                     $col 'A';
  623.                     $row 1;
  624.                     list($col$rowPHPExcel_Cell::coordinateFromString($range[$i]);
  625.  
  626.                     if (strlen($col<= 2{
  627.                         $returnValue[$explodedSpaces;
  628.                     }
  629.                 }
  630.  
  631.                 // Range...
  632.                 $rangeStart        $rangeEnd        '';
  633.                 $startingCol    $startingRow    $endingCol    $endingRow    0;
  634.  
  635.                 list($rangeStart$rangeEnd)         $range[$i];
  636.                 list($startingCol$startingRow)    PHPExcel_Cell::coordinateFromString($rangeStart);
  637.                 list($endingCol$endingRow)          PHPExcel_Cell::coordinateFromString($rangeEnd);
  638.  
  639.                 // Conversions...
  640.                 $startingCol     PHPExcel_Cell::columnIndexFromString($startingCol);
  641.                 $endingCol         PHPExcel_Cell::columnIndexFromString($endingCol);
  642.  
  643.                 // Current data
  644.                 $currentCol     = --$startingCol;
  645.                 $currentRow     $startingRow;
  646.  
  647.                 // Loop cells
  648.                 while ($currentCol $endingCol{
  649.                     $loopColumn PHPExcel_Cell::stringFromColumnIndex($currentCol);
  650.                     while ($currentRow <= $endingRow{
  651.                         $returnValue[$loopColumn.$currentRow;
  652.                         ++$currentRow;
  653.                     }
  654.                     ++$currentCol;
  655.                     $currentRow $startingRow;
  656.                 }
  657.             }
  658.         }
  659.  
  660.         // Return value
  661.         return $returnValue;
  662.     }
  663.  
  664.     /**
  665.      * Compare 2 cells
  666.      *
  667.      * @param     PHPExcel_Cell    $a    Cell a
  668.      * @param     PHPExcel_Cell    $a    Cell b
  669.      * @return     int        Result of comparison (always -1 or 1, never zero!)
  670.      */
  671.     public static function compareCells(PHPExcel_Cell $aPHPExcel_Cell $b)
  672.     {
  673.         if ($a->_row $b->_row{
  674.             return -1;
  675.         elseif ($a->_row $b->_row{
  676.             return 1;
  677.         elseif (PHPExcel_Cell::columnIndexFromString($a->_columnPHPExcel_Cell::columnIndexFromString($b->_column)) {
  678.             return -1;
  679.         else {
  680.             return 1;
  681.         }
  682.     }
  683.  
  684.     /**
  685.      * Get value binder to use
  686.      *
  687.      * @return PHPExcel_Cell_IValueBinder 
  688.      */
  689.     public static function getValueBinder({
  690.         return self::$_valueBinder;
  691.     }
  692.  
  693.     /**
  694.      * Set value binder to use
  695.      *
  696.      * @param PHPExcel_Cell_IValueBinder $binder 
  697.      * @throws Exception
  698.      */
  699.     public static function setValueBinder(PHPExcel_Cell_IValueBinder $binder null{
  700.         if (is_null($binder)) {
  701.             throw new Exception("A PHPExcel_Cell_IValueBinder is required for PHPExcel to function correctly.");
  702.         }
  703.  
  704.         self::$_valueBinder $binder;
  705.     }
  706.  
  707.     /**
  708.      * Implement PHP __clone to create a deep clone, not just a shallow copy.
  709.      */
  710.     public function __clone({
  711.         $vars get_object_vars($this);
  712.         foreach ($vars as $key => $value{
  713.             if (is_object($value)) {
  714.                 $this->$key clone $value;
  715.             else {
  716.                 $this->$key $value;
  717.             }
  718.         }
  719.     }
  720. }

Documentation generated on Wed, 22 Apr 2009 08:56:31 +0200 by phpDocumentor 1.4.1