LookupRef.php 26 KB

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