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

Source for file Cell.php

Documentation is available at Cell.php

  1. <?php
  2. /**
  3.  * PHPExcel
  4.  *
  5.  * Copyright (c) 2006 - 2008 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
  23.  * @copyright  Copyright (c) 2006 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.6.3, 2008-08-25
  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
  50.  * @copyright  Copyright (c) 2006 - 2008 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 = null, PHPExcel_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, $rangeB) = explode(':', $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 (eregi(':', $pCoordinateString)) {
  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 (!eregi(':', $pCoordinateString)) {
  407.             // Return value
  408.             $returnValue = '';
  409.             
  410.             // Create absolute coordinate
  411.             list($column, $row) = PHPExcel_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, $rangeB) = explode(':', $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[0] + 1), ($rangeEnd[1] - $rangeStart[1] + 1) );
  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.         // Convert column to integer
  477.         if (strlen($pString) == 1) {
  478.             $result = 0;
  479.             $result += (ord(substr($pString, 0, 1)) - 65);
  480.             $result += 1;
  481.             
  482.             return $result;
  483.         } else if (strlen($pString) == 2) {
  484.             $result = 0;
  485.             $result += ( (1 + (ord(substr($pString, 0, 1)) - 65) ) * 26);
  486.             $result += (ord(substr($pString, 1, 2)) - 65);
  487.             $result += 1;
  488.             
  489.             return $result;
  490.         } else if (strlen($pString) == 3) {
  491.             $result = 0;
  492.             $result += ( (1 + (ord(substr($pString, 0, 1)) - 65) ) * 26 * 26);
  493.             $result += ( (1 + (ord(substr($pString, 1, 1)) - 65) ) * 26);
  494.             $result += (ord(substr($pString, 2, 2)) - 65);
  495.             $result += 1;
  496.             
  497.             return $result;
  498.         } else {
  499.             throw new Exception("Column string index can not be " . (strlen($pString) != 0 ? "longer than 2 characters" : "empty") . ".");
  500.         }
  501.     }
  502.     
  503.     /**
  504.      * String from columnindex
  505.      *
  506.      * @param int $pColumnIndex Column index (base 0 !!!)
  507.      * @return string 
  508.      */
  509.     public static function stringFromColumnIndex($pColumnIndex = 0)
  510.     {
  511.         // Convert column to string
  512.         $returnValue = '';
  513.         // Determine column string
  514.         if ($pColumnIndex < 26) {
  515.             $returnValue = chr(65 + $pColumnIndex);
  516.         } else {
  517.             $iRemainder = (int)($pColumnIndex / 26) -1;
  518.             $returnValue = PHPExcel_Cell::stringFromColumnIndex( $iRemainder  ).chr(65 + $pColumnIndex%26) ;
  519.         }
  520.         // Return
  521.         return $returnValue;
  522.     }
  523.     
  524.     /**
  525.      * Extract all cell references in range
  526.      *
  527.      * @param     string     $pRange        Range (e.g. A1 or A1:A10 or A1:A10 A100:A1000)
  528.      * @return     array    Array containing single cell references
  529.      */
  530.     public static function extractAllCellReferencesInRange($pRange = 'A1') {
  531.         // Returnvalue
  532.         $returnValue = array();
  533.         
  534.         // Explode spaces
  535.         $aExplodeSpaces = explode(' ', str_replace('$', '', strtoupper($pRange)));
  536.         foreach ($aExplodeSpaces as $explodedSpaces) {
  537.             // Single cell?
  538.             if (strpos($explodedSpaces, ':') === false) {
  539.                 $col = 'A';
  540.                 $row = 1;
  541.                 list($col, $row) = PHPExcel_Cell::coordinateFromString($explodedSpaces);
  542.  
  543.                 if (strlen($col) <= 2) {
  544.                     $returnValue[] = $explodedSpaces;
  545.                 }
  546.             
  547.                 continue;
  548.             }
  549.             
  550.             // Range...
  551.             $rangeStart        = '';
  552.             $rangeEnd        = '';
  553.             $startingCol     = 0;
  554.             $startingRow     = 0;
  555.             $endingCol         = 0;
  556.             $endingRow         = 0;
  557.                                         
  558.             list($rangeStart, $rangeEnd)         = explode(':', $explodedSpaces);
  559.             list($startingCol, $startingRow)    = PHPExcel_Cell::coordinateFromString($rangeStart);
  560.             list($endingCol, $endingRow)          = PHPExcel_Cell::coordinateFromString($rangeEnd);
  561.                                         
  562.             // Conversions...
  563.             $startingCol     = PHPExcel_Cell::columnIndexFromString($startingCol) - 1;
  564.             $endingCol         = PHPExcel_Cell::columnIndexFromString($endingCol) - 1;
  565.                                             
  566.             // Current data
  567.             $currentCol     = $startingCol;
  568.             $currentRow     = $startingRow;
  569.     
  570.             // Loop cells
  571.             while ($currentCol >= $startingCol && $currentCol <= $endingCol) {    
  572.                 while ($currentRow >= $startingRow && $currentRow <= $endingRow) {
  573.                     if (strlen(PHPExcel_Cell::stringFromColumnIndex($currentCol)) <= 2) {
  574.                         $returnValue[] = PHPExcel_Cell::stringFromColumnIndex($currentCol) . $currentRow;
  575.                     }
  576.     
  577.                     $currentRow++;    
  578.                 }
  579.                                                                 
  580.                 $currentCol++;    
  581.                 $currentRow = $startingRow;
  582.             }
  583.         }
  584.         
  585.         // Return value
  586.         return $returnValue;
  587.     }
  588.     
  589.     /**
  590.      * Compare 2 cells
  591.      *
  592.      * @param     PHPExcel_Cell    $a    Cell a
  593.      * @param     PHPExcel_Cell    $a    Cell b
  594.      * @return     int        Result of comparison (always -1 or 1, never zero!)
  595.      */
  596.     public static function compareCells(PHPExcel_Cell $a, PHPExcel_Cell $b)
  597.     {
  598.         if ($a->_row < $b->_row) {
  599.             return -1;
  600.         } elseif ($a->_row > $b->_row) {
  601.             return 1;
  602.         } elseif (PHPExcel_Cell::columnIndexFromString($a->_column) < PHPExcel_Cell::columnIndexFromString($b->_column)) {
  603.             return -1;
  604.         } else {
  605.             return 1;
  606.         }
  607.     }
  608.         
  609.     /**
  610.      * Implement PHP __clone to create a deep clone, not just a shallow copy.
  611.      */
  612.     public function __clone() {
  613.         $vars = get_object_vars($this);
  614.         foreach ($vars as $key => $value) {
  615.             if (is_object($value)) {
  616.                 $this->$key = clone $value;
  617.             } else {
  618.                 $this->$key = $value;
  619.             }
  620.         }
  621.     }
  622. }

Documentation generated on Mon, 25 Aug 2008 13:32:01 +0200 by phpDocumentor 1.4.1