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

Source for file LookupRef.php

Documentation is available at LookupRef.php

  1. <?php
  2. /**
  3.  * PHPExcel
  4.  *
  5.  * Copyright (c) 2006 - 2011 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_Calculation
  23.  * @copyright    Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license        http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version        1.7.6, 2011-02-27
  26.  */
  27.  
  28.  
  29. /** PHPExcel root directory */
  30. if (!defined('PHPEXCEL_ROOT')) {
  31.     /**
  32.      * @ignore
  33.      */
  34.     define('PHPEXCEL_ROOT'dirname(__FILE__'/../../');
  35.     require(PHPEXCEL_ROOT 'PHPExcel/Autoloader.php');
  36. }
  37.  
  38.  
  39. /**
  40.  * PHPExcel_Calculation_LookupRef
  41.  *
  42.  * @category    PHPExcel
  43.  * @package        PHPExcel_Calculation
  44.  * @copyright    Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  45.  */
  46.  
  47.  
  48.     /**
  49.      *    CELL_ADDRESS
  50.      *
  51.      *    Creates a cell address as text, given specified row and column numbers.
  52.      *
  53.      *    @param    row                Row number to use in the cell reference
  54.      *    @param    column            Column number to use in the cell reference
  55.      *    @param    relativity        Flag indicating the type of reference to return
  56.      *                                 1 or omitted    Absolute
  57.      *                                 2                Absolute row; relative column
  58.      *                                 3                Relative row; absolute column
  59.      *                                 4                Relative
  60.      *    @param    referenceStyle    A logical value that specifies the A1 or R1C1 reference style.
  61.      *                                 TRUE or omitted        CELL_ADDRESS returns an A1-style reference
  62.      *                                 FALSE                CELL_ADDRESS returns an R1C1-style reference
  63.      *    @param    sheetText        Optional Name of worksheet to use
  64.      *    @return    string 
  65.      */
  66.     public static function CELL_ADDRESS($row$column$relativity=1$referenceStyle=True$sheetText=''{
  67.         $row        PHPExcel_Calculation_Functions::flattenSingleValue($row);
  68.         $column        PHPExcel_Calculation_Functions::flattenSingleValue($column);
  69.         $relativity    PHPExcel_Calculation_Functions::flattenSingleValue($relativity);
  70.         $sheetText    PHPExcel_Calculation_Functions::flattenSingleValue($sheetText);
  71.  
  72.         if (($row 1|| ($column 1)) {
  73.             return PHPExcel_Calculation_Functions::VALUE();
  74.         }
  75.  
  76.         if ($sheetText ''{
  77.             if (strpos($sheetText,' '!== False$sheetText "'".$sheetText."'"}
  78.             $sheetText .='!';
  79.         }
  80.         if ((!is_bool($referenceStyle)) || $referenceStyle{
  81.             $rowRelative $columnRelative '$';
  82.             $column PHPExcel_Cell::stringFromColumnIndex($column-1);
  83.             if (($relativity == 2|| ($relativity == 4)) $columnRelative ''}
  84.             if (($relativity == 3|| ($relativity == 4)) $rowRelative ''}
  85.             return $sheetText.$columnRelative.$column.$rowRelative.$row;
  86.         else {
  87.             if (($relativity == 2|| ($relativity == 4)) $column '['.$column.']'}
  88.             if (($relativity == 3|| ($relativity == 4)) $row '['.$row.']'}
  89.             return $sheetText.'R'.$row.'C'.$column;
  90.         }
  91.     }    //    function CELL_ADDRESS()
  92.  
  93.  
  94.     /**
  95.      *    COLUMN
  96.      *
  97.      *    Returns the column number of the given cell reference
  98.      *    If the cell reference is a range of cells, COLUMN returns the column numbers of each column in the reference as a horizontal array.
  99.      *    If cell reference is omitted, and the function is being called through the calculation engine, then it is assumed to be the
  100.      *        reference of the cell in which the COLUMN function appears; otherwise this function returns 0.
  101.      *
  102.      *    @param    cellAddress        A reference to a range of cells for which you want the column numbers
  103.      *    @return    integer or array of integer
  104.      */
  105.     public static function COLUMN($cellAddress=Null{
  106.         if (is_null($cellAddress|| trim($cellAddress=== ''return 0}
  107.  
  108.         if (is_array($cellAddress)) {
  109.             foreach($cellAddress as $columnKey => $value{
  110.                 $columnKey preg_replace('/[^a-z]/i','',$columnKey);
  111.                 return (integer) PHPExcel_Cell::columnIndexFromString($columnKey);
  112.             }
  113.         else {
  114.             if (strpos($cellAddress,'!'!== false{
  115.                 list($sheet,$cellAddressexplode('!',$cellAddress);
  116.             }
  117.             if (strpos($cellAddress,':'!== false{
  118.                 list($startAddress,$endAddressexplode(':',$cellAddress);
  119.                 $startAddress preg_replace('/[^a-z]/i','',$startAddress);
  120.                 $endAddress preg_replace('/[^a-z]/i','',$endAddress);
  121.                 $returnValue array();
  122.                 do {
  123.                     $returnValue[= (integer) PHPExcel_Cell::columnIndexFromString($startAddress);
  124.                 while ($startAddress++ != $endAddress);
  125.                 return $returnValue;
  126.             else {
  127.                 $cellAddress preg_replace('/[^a-z]/i','',$cellAddress);
  128.                 return (integer) PHPExcel_Cell::columnIndexFromString($cellAddress);
  129.             }
  130.         }
  131.     }    //    function COLUMN()
  132.  
  133.  
  134.     /**
  135.      *    COLUMNS
  136.      *
  137.      *    Returns the number of columns in an array or reference.
  138.      *
  139.      *    @param    cellAddress        An array or array formula, or a reference to a range of cells for which you want the number of columns
  140.      *    @return    integer 
  141.      */
  142.     public static function COLUMNS($cellAddress=Null{
  143.         if (is_null($cellAddress|| $cellAddress === ''{
  144.             return 1;
  145.         elseif (!is_array($cellAddress)) {
  146.             return PHPExcel_Calculation_Functions::VALUE();
  147.         }
  148.  
  149.         $x array_keys($cellAddress);
  150.         $x array_shift($x);
  151.         $isMatrix (is_numeric($x));
  152.         list($columns,$rowsPHPExcel_Calculation::_getMatrixDimensions($cellAddress);
  153.  
  154.         if ($isMatrix{
  155.             return $rows;
  156.         else {
  157.             return $columns;
  158.         }
  159.     }    //    function COLUMNS()
  160.  
  161.  
  162.     /**
  163.      *    ROW
  164.      *
  165.      *    Returns the row number of the given cell reference
  166.      *    If the cell reference is a range of cells, ROW returns the row numbers of each row in the reference as a vertical array.
  167.      *    If cell reference is omitted, and the function is being called through the calculation engine, then it is assumed to be the
  168.      *        reference of the cell in which the ROW function appears; otherwise this function returns 0.
  169.      *
  170.      *    @param    cellAddress        A reference to a range of cells for which you want the row numbers
  171.      *    @return    integer or array of integer
  172.      */
  173.     public static function ROW($cellAddress=Null{
  174.         if (is_null($cellAddress|| trim($cellAddress=== ''return 0}
  175.  
  176.         if (is_array($cellAddress)) {
  177.             foreach($cellAddress as $columnKey => $rowValue{
  178.                 foreach($rowValue as $rowKey => $cellValue{
  179.                     return (integer) preg_replace('/[^0-9]/i','',$rowKey);
  180.                 }
  181.             }
  182.         else {
  183.             if (strpos($cellAddress,'!'!== false{
  184.                 list($sheet,$cellAddressexplode('!',$cellAddress);
  185.             }
  186.             if (strpos($cellAddress,':'!== false{
  187.                 list($startAddress,$endAddressexplode(':',$cellAddress);
  188.                 $startAddress preg_replace('/[^0-9]/','',$startAddress);
  189.                 $endAddress preg_replace('/[^0-9]/','',$endAddress);
  190.                 $returnValue array();
  191.                 do {
  192.                     $returnValue[][= (integer) $startAddress;
  193.                 while ($startAddress++ != $endAddress);
  194.                 return $returnValue;
  195.             else {
  196.                 list($cellAddressexplode(':',$cellAddress);
  197.                 return (integer) preg_replace('/[^0-9]/','',$cellAddress);
  198.             }
  199.         }
  200.     }    //    function ROW()
  201.  
  202.  
  203.     /**
  204.      *    ROWS
  205.      *
  206.      *    Returns the number of rows in an array or reference.
  207.      *
  208.      *    @param    cellAddress        An array or array formula, or a reference to a range of cells for which you want the number of rows
  209.      *    @return    integer 
  210.      */
  211.     public static function ROWS($cellAddress=Null{
  212.         if (is_null($cellAddress|| $cellAddress === ''{
  213.             return 1;
  214.         elseif (!is_array($cellAddress)) {
  215.             return PHPExcel_Calculation_Functions::VALUE();
  216.         }
  217.  
  218.         $i array_keys($cellAddress);
  219.         $isMatrix (is_numeric(array_shift($i)));
  220.         list($columns,$rowsPHPExcel_Calculation::_getMatrixDimensions($cellAddress);
  221.  
  222.         if ($isMatrix{
  223.             return $columns;
  224.         else {
  225.             return $rows;
  226.         }
  227.     }    //    function ROWS()
  228.  
  229.  
  230.     /**
  231.      *    HYPERLINK
  232.      *
  233.      *    Excel Function:
  234.      *        =HYPERLINK(linkURL,displayName)
  235.      *
  236.      *    @access    public
  237.      *    @category Logical Functions
  238.      *    @param    string    $linkURL        Value to check, is also the value returned when no error
  239.      *    @param    string    $displayName    Value to return when testValue is an error condition
  240.      *    @return    mixed    The value of errorpart or testValue determined by error condition
  241.      */
  242.     public static function HYPERLINK($linkURL ''$displayName nullPHPExcel_Cell $pCell null{
  243.         $args func_get_args();
  244.         $pCell array_pop($args);
  245.  
  246.         $linkURL        (is_null($linkURL))        '' :    PHPExcel_Calculation_Functions::flattenSingleValue($linkURL);
  247.         $displayName    (is_null($displayName))    '' :    PHPExcel_Calculation_Functions::flattenSingleValue($displayName);
  248.  
  249.         if ((!is_object($pCell)) || (trim($linkURL== '')) {
  250.             return PHPExcel_Calculation_Functions::REF();
  251.         }
  252.  
  253.         if ((is_object($displayName)) || trim($displayName== ''{
  254.             $displayName $linkURL;
  255.         }
  256.  
  257.         $pCell->getHyperlink()->setUrl($linkURL);
  258.  
  259.         return $displayName;
  260.     }    //    function HYPERLINK()
  261.  
  262.  
  263.     /**
  264.      *    INDIRECT
  265.      *
  266.      *    Returns the number of rows in an array or reference.
  267.      *
  268.      *    @param    cellAddress        An array or array formula, or a reference to a range of cells for which you want the number of rows
  269.      *    @return    integer 
  270.      */
  271.     public static function INDIRECT($cellAddress=NullPHPExcel_Cell $pCell null{
  272.         $cellAddress    PHPExcel_Calculation_Functions::flattenSingleValue($cellAddress);
  273.         if (is_null($cellAddress|| $cellAddress === ''{
  274.             return PHPExcel_Calculation_Functions::REF();
  275.         }
  276.  
  277.         $cellAddress1 $cellAddress;
  278.         $cellAddress2 NULL;
  279.         if (strpos($cellAddress,':'!== false{
  280.             list($cellAddress1,$cellAddress2explode(':',$cellAddress);
  281.         }
  282.  
  283.         if ((!preg_match('/^'.PHPExcel_Calculation::CALCULATION_REGEXP_CELLREF.'$/i'$cellAddress1$matches)) ||
  284.             ((!is_null($cellAddress2)) && (!preg_match('/^'.PHPExcel_Calculation::CALCULATION_REGEXP_CELLREF.'$/i'$cellAddress2$matches)))) {
  285.             return PHPExcel_Calculation_Functions::REF();
  286.         }
  287.  
  288.         if (strpos($cellAddress,'!'!== false{
  289.             list($sheetName,$cellAddressexplode('!',$cellAddress);
  290.             $pSheet $pCell->getParent()->getParent()->getSheetByName($sheetName);
  291.         else {
  292.             $pSheet $pCell->getParent();
  293.         }
  294.  
  295.         return PHPExcel_Calculation::getInstance()->extractCellRange($cellAddress$pSheetFalse);
  296.     }    //    function INDIRECT()
  297.  
  298.  
  299.     /**
  300.      *    OFFSET
  301.      *
  302.      *    Returns a reference to a range that is a specified number of rows and columns from a cell or range of cells.
  303.      *    The reference that is returned can be a single cell or a range of cells. You can specify the number of rows and
  304.      *    the number of columns to be returned.
  305.      *
  306.      *    @param    cellAddress        The reference from which you want to base the offset. Reference must refer to a cell or
  307.      *                                 range of adjacent cells; otherwise, OFFSET returns the #VALUE! error value.
  308.      *    @param    rows            The number of rows, up or down, that you want the upper-left cell to refer to.
  309.      *                                 Using 5 as the rows argument specifies that the upper-left cell in the reference is
  310.      *                                 five rows below reference. Rows can be positive (which means below the starting reference)
  311.      *                                 or negative (which means above the starting reference).
  312.      *    @param    cols            The number of columns, to the left or right, that you want the upper-left cell of the result
  313.      *                                 to refer to. Using 5 as the cols argument specifies that the upper-left cell in the
  314.      *                                 reference is five columns to the right of reference. Cols can be positive (which means
  315.      *                                 to the right of the starting reference) or negative (which means to the left of the
  316.      *                                 starting reference).
  317.      *    @param    height            The height, in number of rows, that you want the returned reference to be. Height must be a positive number.
  318.      *    @param    width            The width, in number of columns, that you want the returned reference to be. Width must be a positive number.
  319.      *    @return    string            A reference to a cell or range of cells
  320.      */
  321.     public static function OFFSET($cellAddress=Null,$rows=0,$columns=0,$height=null,$width=null{
  322.         $rows        PHPExcel_Calculation_Functions::flattenSingleValue($rows);
  323.         $columns    PHPExcel_Calculation_Functions::flattenSingleValue($columns);
  324.         $height        PHPExcel_Calculation_Functions::flattenSingleValue($height);
  325.         $width        PHPExcel_Calculation_Functions::flattenSingleValue($width);
  326.         if ($cellAddress == Null{
  327.             return 0;
  328.         }
  329.  
  330.         $args func_get_args();
  331.         $pCell array_pop($args);
  332.         if (!is_object($pCell)) {
  333.             return PHPExcel_Calculation_Functions::REF();
  334.         }
  335.  
  336.         $sheetName null;
  337.         if (strpos($cellAddress,"!")) {
  338.             list($sheetName,$cellAddressexplode("!",$cellAddress);
  339.         }
  340.         if (strpos($cellAddress,":")) {
  341.             list($startCell,$endCellexplode(":",$cellAddress);
  342.         else {
  343.             $startCell $endCell $cellAddress;
  344.         }
  345.         list($startCellColumn,$startCellRowPHPExcel_Cell::coordinateFromString($startCell);
  346.         list($endCellColumn,$endCellRowPHPExcel_Cell::coordinateFromString($endCell);
  347.  
  348.         $startCellRow += $rows;
  349.         $startCellColumn PHPExcel_Cell::columnIndexFromString($startCellColumn1;
  350.         $startCellColumn += $columns;
  351.  
  352.         if (($startCellRow <= 0|| ($startCellColumn 0)) {
  353.             return PHPExcel_Calculation_Functions::REF();
  354.         }
  355.         $endCellColumn PHPExcel_Cell::columnIndexFromString($endCellColumn1;
  356.         if (($width != null&& (!is_object($width))) {
  357.             $endCellColumn $startCellColumn $width 1;
  358.         else {
  359.             $endCellColumn += $columns;
  360.         }
  361.         $startCellColumn PHPExcel_Cell::stringFromColumnIndex($startCellColumn);
  362.  
  363.         if (($height != null&& (!is_object($height))) {
  364.             $endCellRow $startCellRow $height 1;
  365.         else {
  366.             $endCellRow += $rows;
  367.         }
  368.  
  369.         if (($endCellRow <= 0|| ($endCellColumn 0)) {
  370.             return PHPExcel_Calculation_Functions::REF();
  371.         }
  372.         $endCellColumn PHPExcel_Cell::stringFromColumnIndex($endCellColumn);
  373.  
  374.         $cellAddress $startCellColumn.$startCellRow;
  375.         if (($startCellColumn != $endCellColumn|| ($startCellRow != $endCellRow)) {
  376.             $cellAddress .= ':'.$endCellColumn.$endCellRow;
  377.         }
  378.  
  379.         if ($sheetName !== null{
  380.             $pSheet $pCell->getParent()->getParent()->getSheetByName($sheetName);
  381.         else {
  382.             $pSheet $pCell->getParent();
  383.         }
  384.  
  385.         return PHPExcel_Calculation::getInstance()->extractCellRange($cellAddress$pSheetFalse);
  386.     }    //    function OFFSET()
  387.  
  388.  
  389.     public static function CHOOSE({
  390.         $chooseArgs func_get_args();
  391.         $chosenEntry PHPExcel_Calculation_Functions::flattenArray(array_shift($chooseArgs));
  392.         $entryCount count($chooseArgs1;
  393.  
  394.         if(is_array($chosenEntry)) {
  395.             $chosenEntry array_shift($chosenEntry);
  396.         }
  397.         if ((is_numeric($chosenEntry)) && (!is_bool($chosenEntry))) {
  398.             --$chosenEntry;
  399.         else {
  400.             return PHPExcel_Calculation_Functions::VALUE();
  401.         }
  402.         $chosenEntry floor($chosenEntry);
  403.         if (($chosenEntry <= 0|| ($chosenEntry $entryCount)) {
  404.             return PHPExcel_Calculation_Functions::VALUE();
  405.         }
  406.  
  407.         if (is_array($chooseArgs[$chosenEntry])) {
  408.             return PHPExcel_Calculation_Functions::flattenArray($chooseArgs[$chosenEntry]);
  409.         else {
  410.             return $chooseArgs[$chosenEntry];
  411.         }
  412.     }    //    function CHOOSE()
  413.  
  414.  
  415.     /**
  416.      *    MATCH
  417.      *
  418.      *    The MATCH function searches for a specified item in a range of cells
  419.      *
  420.      *    @param    lookup_value    The value that you want to match in lookup_array
  421.      *    @param    lookup_array    The range of cells being searched
  422.      *    @param    match_type        The number -1, 0, or 1. -1 means above, 0 means exact match, 1 means below. If match_type is 1 or -1, the list has to be ordered.
  423.      *    @return    integer            The relative position of the found item
  424.      */
  425.     public static function MATCH($lookup_value$lookup_array$match_type=1{
  426.         $lookup_array PHPExcel_Calculation_Functions::flattenArray($lookup_array);
  427.         $lookup_value PHPExcel_Calculation_Functions::flattenSingleValue($lookup_value);
  428.         $match_type    (is_null($match_type)) : (int) PHPExcel_Calculation_Functions::flattenSingleValue($match_type);
  429.         //    MATCH is not case sensitive
  430.         $lookup_value strtolower($lookup_value);
  431.  
  432.         //    lookup_value type has to be number, text, or logical values
  433.         if ((!is_numeric($lookup_value)) && (!is_string($lookup_value)) && (!is_bool($lookup_value))) {
  434.             return PHPExcel_Calculation_Functions::NA();
  435.         }
  436.  
  437.         //    match_type is 0, 1 or -1
  438.         if (($match_type !== 0&& ($match_type !== -1&& ($match_type !== 1)) {
  439.             return PHPExcel_Calculation_Functions::NA();
  440.         }
  441.  
  442.         //    lookup_array should not be empty
  443.         $lookupArraySize count($lookup_array);
  444.         if ($lookupArraySize <= 0{
  445.             return PHPExcel_Calculation_Functions::NA();
  446.         }
  447.  
  448.         //    lookup_array should contain only number, text, or logical values, or empty (null) cells
  449.         foreach($lookup_array as $i => $lookupArrayValue{
  450.             //    check the type of the value
  451.             if ((!is_numeric($lookupArrayValue)) && (!is_string($lookupArrayValue)) &&
  452.                 (!is_bool($lookupArrayValue)) && (!is_null($lookupArrayValue))) {
  453.                 return PHPExcel_Calculation_Functions::NA();
  454.             }
  455.             //    convert strings to lowercase for case-insensitive testing
  456.             if (is_string($lookupArrayValue)) {
  457.                 $lookup_array[$istrtolower($lookupArrayValue);
  458.             }
  459.             if ((is_null($lookupArrayValue)) && (($match_type == 1|| ($match_type == -1))) {
  460.                 $lookup_array array_slice($lookup_array,0,$i-1);
  461.             }
  462.         }
  463.  
  464.         // if match_type is 1 or -1, the list has to be ordered
  465.         if ($match_type == 1{
  466.             asort($lookup_array);
  467.             $keySet array_keys($lookup_array);
  468.         elseif($match_type == -1{
  469.             arsort($lookup_array);
  470.             $keySet array_keys($lookup_array);
  471.         }
  472.  
  473.         // **
  474.         // find the match
  475.         // **
  476.         // loop on the cells
  477. //        var_dump($lookup_array);
  478. //        echo '<br />';
  479.         foreach($lookup_array as $i => $lookupArrayValue{
  480.             if (($match_type == 0&& ($lookupArrayValue == $lookup_value)) {
  481.                 //    exact match
  482.                 return ++$i;
  483.             elseif (($match_type == -1&& ($lookupArrayValue <= $lookup_value)) {
  484. //                echo '$i = '.$i.' => ';
  485. //                var_dump($lookupArrayValue);
  486. //                echo '<br />';
  487. //                echo 'Keyset = ';
  488. //                var_dump($keySet);
  489. //                echo '<br />';
  490.                 $i array_search($i,$keySet);
  491. //                echo '$i='.$i.'<br />';
  492.                 // if match_type is -1 <=> find the smallest value that is greater than or equal to lookup_value
  493.                 if ($i 1){
  494.                     // 1st cell was allready smaller than the lookup_value
  495.                     break;
  496.                 else {
  497.                     // the previous cell was the match
  498.                     return $keySet[$i-1]+1;
  499.                 }
  500.             elseif (($match_type == 1&& ($lookupArrayValue >= $lookup_value)) {
  501. //                echo '$i = '.$i.' => ';
  502. //                var_dump($lookupArrayValue);
  503. //                echo '<br />';
  504. //                echo 'Keyset = ';
  505. //                var_dump($keySet);
  506. //                echo '<br />';
  507.                 $i array_search($i,$keySet);
  508. //                echo '$i='.$i.'<br />';
  509.                 // if match_type is 1 <=> find the largest value that is less than or equal to lookup_value
  510.                 if ($i 1){
  511.                     // 1st cell was allready bigger than the lookup_value
  512.                     break;
  513.                 else {
  514.                     // the previous cell was the match
  515.                     return $keySet[$i-1]+1;
  516.                 }
  517.             }
  518.         }
  519.  
  520.         //    unsuccessful in finding a match, return #N/A error value
  521.         return PHPExcel_Calculation_Functions::NA();
  522.     }    //    function MATCH()
  523.  
  524.  
  525.     /**
  526.      *    INDEX
  527.      *
  528.      * Uses an index to choose a value from a reference or array
  529.      * implemented: Return the value of a specified cell or array of cells    Array form
  530.      * not implemented: Return a reference to specified cells    Reference form
  531.      *
  532.      * @param    range_array    a range of cells or an array constant
  533.      * @param    row_num        selects the row in array from which to return a value. If row_num is omitted, column_num is required.
  534.      * @param    column_num    selects the column in array from which to return a value. If column_num is omitted, row_num is required.
  535.      */
  536.     public static function INDEX($arrayValues,$rowNum 0,$columnNum 0{
  537.  
  538.         if (($rowNum 0|| ($columnNum 0)) {
  539.             return PHPExcel_Calculation_Functions::VALUE();
  540.         }
  541.  
  542.         if (!is_array($arrayValues)) {
  543.             return PHPExcel_Calculation_Functions::REF();
  544.         }
  545.  
  546.         $rowKeys array_keys($arrayValues);
  547.         $columnKeys @array_keys($arrayValues[$rowKeys[0]]);
  548.  
  549.         if ($columnNum count($columnKeys)) {
  550.             return PHPExcel_Calculation_Functions::VALUE();
  551.         elseif ($columnNum == 0{
  552.             if ($rowNum == 0{
  553.                 return $arrayValues;
  554.             }
  555.             $rowNum $rowKeys[--$rowNum];
  556.             $returnArray array();
  557.             foreach($arrayValues as $arrayColumn{
  558.                 if (is_array($arrayColumn)) {
  559.                     if (isset($arrayColumn[$rowNum])) {
  560.                         $returnArray[$arrayColumn[$rowNum];
  561.                     else {
  562.                         return $arrayValues[$rowNum];
  563.                     }
  564.                 else {
  565.                     return $arrayValues[$rowNum];
  566.                 }
  567.             }
  568.             return $returnArray;
  569.         }
  570.         $columnNum $columnKeys[--$columnNum];
  571.         if ($rowNum count($rowKeys)) {
  572.             return PHPExcel_Calculation_Functions::VALUE();
  573.         elseif ($rowNum == 0{
  574.             return $arrayValues[$columnNum];
  575.         }
  576.         $rowNum $rowKeys[--$rowNum];
  577.  
  578.         return $arrayValues[$rowNum][$columnNum];
  579.     }    //    function INDEX()
  580.  
  581.  
  582.     /**
  583.      * TRANSPOSE
  584.      *
  585.      * @param    array    $matrixData    A matrix of values
  586.      * @return    array 
  587.      *
  588.      *  Unlike the Excel TRANSPOSE function, which will only work on a single row or column, this function will transpose a full matrix.
  589.      */
  590.     public static function TRANSPOSE($matrixData{
  591.         $returnMatrix array();
  592.         if (!is_array($matrixData)) $matrixData array(array($matrixData))}
  593.  
  594.         $column 0;
  595.         foreach($matrixData as $matrixRow{
  596.             $row 0;
  597.             foreach($matrixRow as $matrixCell{
  598.                 $returnMatrix[$row][$column$matrixCell;
  599.                 ++$row;
  600.             }
  601.             ++$column;
  602.         }
  603.         return $returnMatrix;
  604.     }    //    function TRANSPOSE()
  605.  
  606.  
  607.     private static function _vlookupSort($a,$b{
  608.         $f array_keys($a);
  609.         $firstColumn array_shift($f);
  610.         if (strtolower($a[$firstColumn]== strtolower($b[$firstColumn])) {
  611.             return 0;
  612.         }
  613.         return (strtolower($a[$firstColumn]strtolower($b[$firstColumn])) ? -1;
  614.     }    //    function _vlookupSort()
  615.  
  616.  
  617.     /**
  618.     * VLOOKUP
  619.     * The VLOOKUP function searches for value in the left-most column of lookup_array and returns the value in the same row based on the index_number.
  620.     * @param    lookup_value    The value that you want to match in lookup_array
  621.     * @param    lookup_array    The range of cells being searched
  622.     * @param    index_number    The column number in table_array from which the matching value must be returned. The first column is 1.
  623.     * @param    not_exact_match    Determines if you are looking for an exact match based on lookup_value.
  624.     * @return    mixed            The value of the found cell
  625.     */
  626.     public static function VLOOKUP($lookup_value$lookup_array$index_number$not_exact_match=true{
  627.         $lookup_value    PHPExcel_Calculation_Functions::flattenSingleValue($lookup_value);
  628.         $index_number    PHPExcel_Calculation_Functions::flattenSingleValue($index_number);
  629.         $not_exact_match    PHPExcel_Calculation_Functions::flattenSingleValue($not_exact_match);
  630.  
  631.         // index_number must be greater than or equal to 1
  632.         if ($index_number 1{
  633.             return PHPExcel_Calculation_Functions::VALUE();
  634.         }
  635.  
  636.         // index_number must be less than or equal to the number of columns in lookup_array
  637.         if ((!is_array($lookup_array)) || (count($lookup_array1)) {
  638.             return PHPExcel_Calculation_Functions::REF();
  639.         else {
  640.             $f array_keys($lookup_array);
  641.             $firstRow array_pop($f);
  642.             if ((!is_array($lookup_array[$firstRow])) || ($index_number count($lookup_array[$firstRow]))) {
  643.                 return PHPExcel_Calculation_Functions::REF();
  644.             else {
  645.                 $columnKeys array_keys($lookup_array[$firstRow]);
  646.                 $returnColumn $columnKeys[--$index_number];
  647.                 $firstColumn array_shift($columnKeys);
  648.             }
  649.         }
  650.  
  651.         if (!$not_exact_match{
  652.             uasort($lookup_array,array('self','_vlookupSort'));
  653.         }
  654.  
  655.         $rowNumber $rowValue False;
  656.         foreach($lookup_array as $rowKey => $rowData{
  657.             if (strtolower($rowData[$firstColumn]strtolower($lookup_value)) {
  658.                 break;
  659.             }
  660.             $rowNumber $rowKey;
  661.             $rowValue $rowData[$firstColumn];
  662.         }
  663.  
  664.         if ($rowNumber !== false{
  665.             if ((!$not_exact_match&& ($rowValue != $lookup_value)) {
  666.                 //    if an exact match is required, we have what we need to return an appropriate response
  667.                 return PHPExcel_Calculation_Functions::NA();
  668.             else {
  669.                 //    otherwise return the appropriate value
  670.                 return $lookup_array[$rowNumber][$returnColumn];
  671.             }
  672.         }
  673.  
  674.         return PHPExcel_Calculation_Functions::NA();
  675.     }    //    function VLOOKUP()
  676.  
  677.  
  678.     /**
  679.      * LOOKUP
  680.      * The LOOKUP function searches for value either from a one-row or one-column range or from an array.
  681.      * @param    lookup_value    The value that you want to match in lookup_array
  682.      * @param    lookup_vector    The range of cells being searched
  683.      * @param    result_vector    The column from which the matching value must be returned
  684.      * @return    mixed            The value of the found cell
  685.      */
  686.     public static function LOOKUP($lookup_value$lookup_vector$result_vector=null{
  687.         $lookup_value    PHPExcel_Calculation_Functions::flattenSingleValue($lookup_value);
  688.  
  689.         if (!is_array($lookup_vector)) {
  690.             return PHPExcel_Calculation_Functions::NA();
  691.         }
  692.         $lookupRows count($lookup_vector);
  693.         $l array_keys($lookup_vector);
  694.         $l array_shift($l);
  695.         $lookupColumns count($lookup_vector[$l]);
  696.         if ((($lookupRows == 1&& ($lookupColumns 1)) || (($lookupRows == 2&& ($lookupColumns != 2))) {
  697.             $lookup_vector self::TRANSPOSE($lookup_vector);
  698.             $lookupRows count($lookup_vector);
  699.             $l array_keys($lookup_vector);
  700.             $lookupColumns count($lookup_vector[array_shift($l)]);
  701.         }
  702.  
  703.         if (is_null($result_vector)) {
  704.             $result_vector $lookup_vector;
  705.         }
  706.         $resultRows count($result_vector);
  707.         $l array_keys($result_vector);
  708.         $l array_shift($l);
  709.         $resultColumns count($result_vector[$l]);
  710.         if ((($resultRows == 1&& ($resultColumns 1)) || (($resultRows == 2&& ($resultColumns != 2))) {
  711.             $result_vector self::TRANSPOSE($result_vector);
  712.             $resultRows count($result_vector);
  713.             $r array_keys($result_vector);
  714.             $resultColumns count($result_vector[array_shift($r)]);
  715.         }
  716.  
  717.         if ($lookupRows == 2{
  718.             $result_vector array_pop($lookup_vector);
  719.             $lookup_vector array_shift($lookup_vector);
  720.         }
  721.         if ($lookupColumns != 2{
  722.             foreach($lookup_vector as &$value{
  723.                 if (is_array($value)) {
  724.                     $k array_keys($value);
  725.                     $key1 $key2 array_shift($k);
  726.                     $key2++;
  727.                     $dataValue1 $value[$key1];
  728.                 else {
  729.                     $key1 0;
  730.                     $key2 1;
  731.                     $dataValue1 $value;
  732.                 }
  733.                 $dataValue2 array_shift($result_vector);
  734.                 if (is_array($dataValue2)) {
  735.                     $dataValue2 array_shift($dataValue2);
  736.                 }
  737.                 $value array($key1 => $dataValue1$key2 => $dataValue2);
  738.             }
  739.             unset($value);
  740.         }
  741.  
  742.         return self::VLOOKUP($lookup_value,$lookup_vector,2);
  743.      }    //    function LOOKUP()
  744.  
  745. }    //    class PHPExcel_Calculation_LookupRef

Documentation generated on Sun, 27 Feb 2011 16:32:32 -0800 by phpDocumentor 1.4.3