Source for file LookupRef.php
Documentation is available at LookupRef.php
* Copyright (c) 2006 - 2011 PHPExcel
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
* @package PHPExcel_Calculation
* @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
* @version 1.7.6, 2011-02-27
/** PHPExcel root directory */
define('PHPEXCEL_ROOT', dirname(__FILE__ ) . '/../../');
require (PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
* PHPExcel_Calculation_LookupRef
* @package PHPExcel_Calculation
* @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
* Creates a cell address as text, given specified row and column numbers.
* @param row Row number to use in the cell reference
* @param column Column number to use in the cell reference
* @param relativity Flag indicating the type of reference to return
* 2 Absolute row; relative column
* 3 Relative row; absolute column
* @param referenceStyle A logical value that specifies the A1 or R1C1 reference style.
* TRUE or omitted CELL_ADDRESS returns an A1-style reference
* FALSE CELL_ADDRESS returns an R1C1-style reference
* @param sheetText Optional Name of worksheet to use
public static function CELL_ADDRESS($row, $column, $relativity= 1, $referenceStyle= True, $sheetText= '') {
if (($row < 1) || ($column < 1)) {
if (strpos($sheetText,' ') !== False) { $sheetText = "'". $sheetText. "'"; }
if ((!is_bool($referenceStyle)) || $referenceStyle) {
$rowRelative = $columnRelative = '$';
if (($relativity == 2) || ($relativity == 4)) { $columnRelative = ''; }
if (($relativity == 3) || ($relativity == 4)) { $rowRelative = ''; }
return $sheetText. $columnRelative. $column. $rowRelative. $row;
if (($relativity == 2) || ($relativity == 4)) { $column = '['. $column. ']'; }
if (($relativity == 3) || ($relativity == 4)) { $row = '['. $row. ']'; }
return $sheetText. 'R'. $row. 'C'. $column;
} // function CELL_ADDRESS()
* Returns the column number of the given cell reference
* If the cell reference is a range of cells, COLUMN returns the column numbers of each column in the reference as a horizontal array.
* If cell reference is omitted, and the function is being called through the calculation engine, then it is assumed to be the
* reference of the cell in which the COLUMN function appears; otherwise this function returns 0.
* @param cellAddress A reference to a range of cells for which you want the column numbers
* @return integer or array of integer
public static function COLUMN($cellAddress= Null) {
if (is_null($cellAddress) || trim($cellAddress) === '') { return 0; }
foreach($cellAddress as $columnKey => $value) {
if (strpos($cellAddress,'!') !== false) {
list ($sheet,$cellAddress) = explode('!',$cellAddress);
if (strpos($cellAddress,':') !== false) {
list ($startAddress,$endAddress) = explode(':',$cellAddress);
} while ($startAddress++ != $endAddress);
* Returns the number of columns in an array or reference.
* @param cellAddress An array or array formula, or a reference to a range of cells for which you want the number of columns
public static function COLUMNS($cellAddress= Null) {
if (is_null($cellAddress) || $cellAddress === '') {
* Returns the row number of the given cell reference
* If the cell reference is a range of cells, ROW returns the row numbers of each row in the reference as a vertical array.
* If cell reference is omitted, and the function is being called through the calculation engine, then it is assumed to be the
* reference of the cell in which the ROW function appears; otherwise this function returns 0.
* @param cellAddress A reference to a range of cells for which you want the row numbers
* @return integer or array of integer
public static function ROW($cellAddress= Null) {
if (is_null($cellAddress) || trim($cellAddress) === '') { return 0; }
foreach($cellAddress as $columnKey => $rowValue) {
foreach($rowValue as $rowKey => $cellValue) {
if (strpos($cellAddress,'!') !== false) {
list ($sheet,$cellAddress) = explode('!',$cellAddress);
if (strpos($cellAddress,':') !== false) {
list ($startAddress,$endAddress) = explode(':',$cellAddress);
$returnValue[][] = (integer) $startAddress;
} while ($startAddress++ != $endAddress);
list ($cellAddress) = explode(':',$cellAddress);
* Returns the number of rows in an array or reference.
* @param cellAddress An array or array formula, or a reference to a range of cells for which you want the number of rows
public static function ROWS($cellAddress= Null) {
if (is_null($cellAddress) || $cellAddress === '') {
* =HYPERLINK(linkURL,displayName)
* @category Logical Functions
* @param string $linkURL Value to check, is also the value returned when no error
* @param string $displayName Value to return when testValue is an error condition
* @return mixed The value of errorpart or testValue determined by error condition
public static function HYPERLINK($linkURL = '', $displayName = null, PHPExcel_Cell $pCell = null) {
$pCell->getHyperlink()->setUrl($linkURL);
} // function HYPERLINK()
* Returns the number of rows in an array or reference.
* @param cellAddress An array or array formula, or a reference to a range of cells for which you want the number of rows
public static function INDIRECT($cellAddress= Null, PHPExcel_Cell $pCell = null) {
if (is_null($cellAddress) || $cellAddress === '') {
$cellAddress1 = $cellAddress;
if (strpos($cellAddress,':') !== false) {
list ($cellAddress1,$cellAddress2) = explode(':',$cellAddress);
if (strpos($cellAddress,'!') !== false) {
list ($sheetName,$cellAddress) = explode('!',$cellAddress);
$pSheet = $pCell->getParent()->getParent()->getSheetByName($sheetName);
$pSheet = $pCell->getParent();
* Returns a reference to a range that is a specified number of rows and columns from a cell or range of cells.
* The reference that is returned can be a single cell or a range of cells. You can specify the number of rows and
* the number of columns to be returned.
* @param cellAddress The reference from which you want to base the offset. Reference must refer to a cell or
* range of adjacent cells; otherwise, OFFSET returns the #VALUE! error value.
* @param rows The number of rows, up or down, that you want the upper-left cell to refer to.
* Using 5 as the rows argument specifies that the upper-left cell in the reference is
* five rows below reference. Rows can be positive (which means below the starting reference)
* or negative (which means above the starting reference).
* @param cols The number of columns, to the left or right, that you want the upper-left cell of the result
* to refer to. Using 5 as the cols argument specifies that the upper-left cell in the
* reference is five columns to the right of reference. Cols can be positive (which means
* to the right of the starting reference) or negative (which means to the left of the
* @param height The height, in number of rows, that you want the returned reference to be. Height must be a positive number.
* @param width The width, in number of columns, that you want the returned reference to be. Width must be a positive number.
* @return string A reference to a cell or range of cells
public static function OFFSET($cellAddress= Null,$rows= 0,$columns= 0,$height= null,$width= null) {
if ($cellAddress == Null) {
if (strpos($cellAddress,"!")) {
list ($sheetName,$cellAddress) = explode("!",$cellAddress);
if (strpos($cellAddress,":")) {
list ($startCell,$endCell) = explode(":",$cellAddress);
$startCell = $endCell = $cellAddress;
$startCellColumn += $columns;
if (($startCellRow <= 0) || ($startCellColumn < 0)) {
if (($width != null) && (!is_object($width))) {
$endCellColumn = $startCellColumn + $width - 1;
$endCellColumn += $columns;
if (($height != null) && (!is_object($height))) {
$endCellRow = $startCellRow + $height - 1;
if (($endCellRow <= 0) || ($endCellColumn < 0)) {
$cellAddress = $startCellColumn. $startCellRow;
if (($startCellColumn != $endCellColumn) || ($startCellRow != $endCellRow)) {
$cellAddress .= ':'. $endCellColumn. $endCellRow;
if ($sheetName !== null) {
$pSheet = $pCell->getParent()->getParent()->getSheetByName($sheetName);
$pSheet = $pCell->getParent();
public static function CHOOSE() {
$entryCount = count($chooseArgs) - 1;
$chosenEntry = floor($chosenEntry);
if (($chosenEntry <= 0) || ($chosenEntry > $entryCount)) {
if (is_array($chooseArgs[$chosenEntry])) {
return $chooseArgs[$chosenEntry];
* The MATCH function searches for a specified item in a range of cells
* @param lookup_value The value that you want to match in lookup_array
* @param lookup_array The range of cells being searched
* @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.
* @return integer The relative position of the found item
public static function MATCH($lookup_value, $lookup_array, $match_type= 1) {
// MATCH is not case sensitive
// lookup_value type has to be number, text, or logical values
// match_type is 0, 1 or -1
if (($match_type !== 0) && ($match_type !== - 1) && ($match_type !== 1)) {
// lookup_array should not be empty
$lookupArraySize = count($lookup_array);
if ($lookupArraySize <= 0) {
// lookup_array should contain only number, text, or logical values, or empty (null) cells
foreach($lookup_array as $i => $lookupArrayValue) {
// check the type of the value
// convert strings to lowercase for case-insensitive testing
$lookup_array[$i] = strtolower($lookupArrayValue);
if ((is_null($lookupArrayValue)) && (($match_type == 1) || ($match_type == - 1))) {
// if match_type is 1 or -1, the list has to be ordered
} elseif($match_type == - 1) {
// var_dump($lookup_array);
foreach($lookup_array as $i => $lookupArrayValue) {
if (($match_type == 0) && ($lookupArrayValue == $lookup_value)) {
} elseif (($match_type == - 1) && ($lookupArrayValue <= $lookup_value)) {
// echo '$i = '.$i.' => ';
// var_dump($lookupArrayValue);
// echo '$i='.$i.'<br />';
// if match_type is -1 <=> find the smallest value that is greater than or equal to lookup_value
// 1st cell was allready smaller than the lookup_value
// the previous cell was the match
} elseif (($match_type == 1) && ($lookupArrayValue >= $lookup_value)) {
// echo '$i = '.$i.' => ';
// var_dump($lookupArrayValue);
// echo '$i='.$i.'<br />';
// if match_type is 1 <=> find the largest value that is less than or equal to lookup_value
// 1st cell was allready bigger than the lookup_value
// the previous cell was the match
// unsuccessful in finding a match, return #N/A error value
* Uses an index to choose a value from a reference or array
* implemented: Return the value of a specified cell or array of cells Array form
* not implemented: Return a reference to specified cells Reference form
* @param range_array a range of cells or an array constant
* @param row_num selects the row in array from which to return a value. If row_num is omitted, column_num is required.
* @param column_num selects the column in array from which to return a value. If column_num is omitted, row_num is required.
public static function INDEX($arrayValues,$rowNum = 0,$columnNum = 0) {
if (($rowNum < 0) || ($columnNum < 0)) {
$columnKeys = @array_keys($arrayValues[$rowKeys[0]]);
if ($columnNum > count($columnKeys)) {
} elseif ($columnNum == 0) {
$rowNum = $rowKeys[-- $rowNum];
foreach($arrayValues as $arrayColumn) {
if (isset ($arrayColumn[$rowNum])) {
$returnArray[] = $arrayColumn[$rowNum];
return $arrayValues[$rowNum];
return $arrayValues[$rowNum];
$columnNum = $columnKeys[-- $columnNum];
if ($rowNum > count($rowKeys)) {
} elseif ($rowNum == 0) {
return $arrayValues[$columnNum];
$rowNum = $rowKeys[-- $rowNum];
return $arrayValues[$rowNum][$columnNum];
* @param array $matrixData A matrix of values
* Unlike the Excel TRANSPOSE function, which will only work on a single row or column, this function will transpose a full matrix.
public static function TRANSPOSE($matrixData) {
if (!is_array($matrixData)) { $matrixData = array(array($matrixData)); }
foreach($matrixData as $matrixRow) {
foreach($matrixRow as $matrixCell) {
$returnMatrix[$row][$column] = $matrixCell;
} // function TRANSPOSE()
private static function _vlookupSort($a,$b) {
} // function _vlookupSort()
* 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.
* @param lookup_value The value that you want to match in lookup_array
* @param lookup_array The range of cells being searched
* @param index_number The column number in table_array from which the matching value must be returned. The first column is 1.
* @param not_exact_match Determines if you are looking for an exact match based on lookup_value.
* @return mixed The value of the found cell
public static function VLOOKUP($lookup_value, $lookup_array, $index_number, $not_exact_match= true) {
// index_number must be greater than or equal to 1
// index_number must be less than or equal to the number of columns in lookup_array
if ((!is_array($lookup_array[$firstRow])) || ($index_number > count($lookup_array[$firstRow]))) {
$columnKeys = array_keys($lookup_array[$firstRow]);
$returnColumn = $columnKeys[-- $index_number];
uasort($lookup_array,array('self','_vlookupSort'));
$rowNumber = $rowValue = False;
foreach($lookup_array as $rowKey => $rowData) {
$rowValue = $rowData[$firstColumn];
if ($rowNumber !== false) {
if ((!$not_exact_match) && ($rowValue != $lookup_value)) {
// if an exact match is required, we have what we need to return an appropriate response
// otherwise return the appropriate value
return $lookup_array[$rowNumber][$returnColumn];
* The LOOKUP function searches for value either from a one-row or one-column range or from an array.
* @param lookup_value The value that you want to match in lookup_array
* @param lookup_vector The range of cells being searched
* @param result_vector The column from which the matching value must be returned
* @return mixed The value of the found cell
public static function LOOKUP($lookup_value, $lookup_vector, $result_vector= null) {
$lookupRows = count($lookup_vector);
$lookupColumns = count($lookup_vector[$l]);
if ((($lookupRows == 1) && ($lookupColumns > 1)) || (($lookupRows == 2) && ($lookupColumns != 2))) {
$lookup_vector = self::TRANSPOSE($lookup_vector);
$lookupRows = count($lookup_vector);
$result_vector = $lookup_vector;
$resultRows = count($result_vector);
$resultColumns = count($result_vector[$l]);
if ((($resultRows == 1) && ($resultColumns > 1)) || (($resultRows == 2) && ($resultColumns != 2))) {
$result_vector = self::TRANSPOSE($result_vector);
$resultRows = count($result_vector);
if ($lookupColumns != 2) {
foreach($lookup_vector as &$value) {
$dataValue1 = $value[$key1];
$value = array($key1 => $dataValue1, $key2 => $dataValue2);
return self::VLOOKUP($lookup_value,$lookup_vector,2);
} // class PHPExcel_Calculation_LookupRef
|