PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/application/third_party/PHPExcel/Calculation/LookupRef.php

https://bitbucket.org/matyhaty/senses-designertravelv3
PHP | 808 lines | 449 code | 89 blank | 270 comment | 182 complexity | 4caec871a782de8a887a1011ccf7a499 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2012 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 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  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 - 2012 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. * Excel Function:
  49. * =ADDRESS(row, column, [relativity], [referenceStyle], [sheetText])
  50. *
  51. * @param row Row number to use in the cell reference
  52. * @param column Column number to use in the cell reference
  53. * @param relativity Flag indicating the type of reference to return
  54. * 1 or omitted Absolute
  55. * 2 Absolute row; relative column
  56. * 3 Relative row; absolute column
  57. * 4 Relative
  58. * @param referenceStyle A logical value that specifies the A1 or R1C1 reference style.
  59. * TRUE or omitted CELL_ADDRESS returns an A1-style reference
  60. * FALSE CELL_ADDRESS returns an R1C1-style reference
  61. * @param sheetText Optional Name of worksheet to use
  62. * @return string
  63. */
  64. public static function CELL_ADDRESS($row, $column, $relativity=1, $referenceStyle=True, $sheetText='') {
  65. $row = PHPExcel_Calculation_Functions::flattenSingleValue($row);
  66. $column = PHPExcel_Calculation_Functions::flattenSingleValue($column);
  67. $relativity = PHPExcel_Calculation_Functions::flattenSingleValue($relativity);
  68. $sheetText = PHPExcel_Calculation_Functions::flattenSingleValue($sheetText);
  69. if (($row < 1) || ($column < 1)) {
  70. return PHPExcel_Calculation_Functions::VALUE();
  71. }
  72. if ($sheetText > '') {
  73. if (strpos($sheetText,' ') !== False) { $sheetText = "'".$sheetText."'"; }
  74. $sheetText .='!';
  75. }
  76. if ((!is_bool($referenceStyle)) || $referenceStyle) {
  77. $rowRelative = $columnRelative = '$';
  78. $column = PHPExcel_Cell::stringFromColumnIndex($column-1);
  79. if (($relativity == 2) || ($relativity == 4)) { $columnRelative = ''; }
  80. if (($relativity == 3) || ($relativity == 4)) { $rowRelative = ''; }
  81. return $sheetText.$columnRelative.$column.$rowRelative.$row;
  82. } else {
  83. if (($relativity == 2) || ($relativity == 4)) { $column = '['.$column.']'; }
  84. if (($relativity == 3) || ($relativity == 4)) { $row = '['.$row.']'; }
  85. return $sheetText.'R'.$row.'C'.$column;
  86. }
  87. } // function CELL_ADDRESS()
  88. /**
  89. * COLUMN
  90. *
  91. * Returns the column number of the given cell reference
  92. * If the cell reference is a range of cells, COLUMN returns the column numbers of each column in the reference as a horizontal array.
  93. * If cell reference is omitted, and the function is being called through the calculation engine, then it is assumed to be the
  94. * reference of the cell in which the COLUMN function appears; otherwise this function returns 0.
  95. *
  96. * Excel Function:
  97. * =COLUMN([cellAddress])
  98. *
  99. * @param cellAddress A reference to a range of cells for which you want the column numbers
  100. * @return integer or array of integer
  101. */
  102. public static function COLUMN($cellAddress=Null) {
  103. if (is_null($cellAddress) || trim($cellAddress) === '') { return 0; }
  104. if (is_array($cellAddress)) {
  105. foreach($cellAddress as $columnKey => $value) {
  106. $columnKey = preg_replace('/[^a-z]/i','',$columnKey);
  107. return (integer) PHPExcel_Cell::columnIndexFromString($columnKey);
  108. }
  109. } else {
  110. if (strpos($cellAddress,'!') !== false) {
  111. list($sheet,$cellAddress) = explode('!',$cellAddress);
  112. }
  113. if (strpos($cellAddress,':') !== false) {
  114. list($startAddress,$endAddress) = explode(':',$cellAddress);
  115. $startAddress = preg_replace('/[^a-z]/i','',$startAddress);
  116. $endAddress = preg_replace('/[^a-z]/i','',$endAddress);
  117. $returnValue = array();
  118. do {
  119. $returnValue[] = (integer) PHPExcel_Cell::columnIndexFromString($startAddress);
  120. } while ($startAddress++ != $endAddress);
  121. return $returnValue;
  122. } else {
  123. $cellAddress = preg_replace('/[^a-z]/i','',$cellAddress);
  124. return (integer) PHPExcel_Cell::columnIndexFromString($cellAddress);
  125. }
  126. }
  127. } // function COLUMN()
  128. /**
  129. * COLUMNS
  130. *
  131. * Returns the number of columns in an array or reference.
  132. *
  133. * Excel Function:
  134. * =COLUMNS(cellAddress)
  135. *
  136. * @param cellAddress An array or array formula, or a reference to a range of cells for which you want the number of columns
  137. * @return integer The number of columns in cellAddress
  138. */
  139. public static function COLUMNS($cellAddress=Null) {
  140. if (is_null($cellAddress) || $cellAddress === '') {
  141. return 1;
  142. } elseif (!is_array($cellAddress)) {
  143. return PHPExcel_Calculation_Functions::VALUE();
  144. }
  145. $x = array_keys($cellAddress);
  146. $x = array_shift($x);
  147. $isMatrix = (is_numeric($x));
  148. list($columns,$rows) = PHPExcel_Calculation::_getMatrixDimensions($cellAddress);
  149. if ($isMatrix) {
  150. return $rows;
  151. } else {
  152. return $columns;
  153. }
  154. } // function COLUMNS()
  155. /**
  156. * ROW
  157. *
  158. * Returns the row number of the given cell reference
  159. * If the cell reference is a range of cells, ROW returns the row numbers of each row in the reference as a vertical array.
  160. * If cell reference is omitted, and the function is being called through the calculation engine, then it is assumed to be the
  161. * reference of the cell in which the ROW function appears; otherwise this function returns 0.
  162. *
  163. * Excel Function:
  164. * =ROW([cellAddress])
  165. *
  166. * @param cellAddress A reference to a range of cells for which you want the row numbers
  167. * @return integer or array of integer
  168. */
  169. public static function ROW($cellAddress=Null) {
  170. if (is_null($cellAddress) || trim($cellAddress) === '') { return 0; }
  171. if (is_array($cellAddress)) {
  172. foreach($cellAddress as $columnKey => $rowValue) {
  173. foreach($rowValue as $rowKey => $cellValue) {
  174. return (integer) preg_replace('/[^0-9]/i','',$rowKey);
  175. }
  176. }
  177. } else {
  178. if (strpos($cellAddress,'!') !== false) {
  179. list($sheet,$cellAddress) = explode('!',$cellAddress);
  180. }
  181. if (strpos($cellAddress,':') !== false) {
  182. list($startAddress,$endAddress) = explode(':',$cellAddress);
  183. $startAddress = preg_replace('/[^0-9]/','',$startAddress);
  184. $endAddress = preg_replace('/[^0-9]/','',$endAddress);
  185. $returnValue = array();
  186. do {
  187. $returnValue[][] = (integer) $startAddress;
  188. } while ($startAddress++ != $endAddress);
  189. return $returnValue;
  190. } else {
  191. list($cellAddress) = explode(':',$cellAddress);
  192. return (integer) preg_replace('/[^0-9]/','',$cellAddress);
  193. }
  194. }
  195. } // function ROW()
  196. /**
  197. * ROWS
  198. *
  199. * Returns the number of rows in an array or reference.
  200. *
  201. * Excel Function:
  202. * =ROWS(cellAddress)
  203. *
  204. * @param cellAddress An array or array formula, or a reference to a range of cells for which you want the number of rows
  205. * @return integer The number of rows in cellAddress
  206. */
  207. public static function ROWS($cellAddress=Null) {
  208. if (is_null($cellAddress) || $cellAddress === '') {
  209. return 1;
  210. } elseif (!is_array($cellAddress)) {
  211. return PHPExcel_Calculation_Functions::VALUE();
  212. }
  213. $i = array_keys($cellAddress);
  214. $isMatrix = (is_numeric(array_shift($i)));
  215. list($columns,$rows) = PHPExcel_Calculation::_getMatrixDimensions($cellAddress);
  216. if ($isMatrix) {
  217. return $columns;
  218. } else {
  219. return $rows;
  220. }
  221. } // function ROWS()
  222. /**
  223. * HYPERLINK
  224. *
  225. * Excel Function:
  226. * =HYPERLINK(linkURL,displayName)
  227. *
  228. * @access public
  229. * @category Logical Functions
  230. * @param string $linkURL Value to check, is also the value returned when no error
  231. * @param string $displayName Value to return when testValue is an error condition
  232. * @return mixed The value of $displayName (or $linkURL if $displayName was blank)
  233. */
  234. public static function HYPERLINK($linkURL = '', $displayName = null, PHPExcel_Cell $pCell = null) {
  235. $args = func_get_args();
  236. $pCell = array_pop($args);
  237. $linkURL = (is_null($linkURL)) ? '' : PHPExcel_Calculation_Functions::flattenSingleValue($linkURL);
  238. $displayName = (is_null($displayName)) ? '' : PHPExcel_Calculation_Functions::flattenSingleValue($displayName);
  239. if ((!is_object($pCell)) || (trim($linkURL) == '')) {
  240. return PHPExcel_Calculation_Functions::REF();
  241. }
  242. if ((is_object($displayName)) || trim($displayName) == '') {
  243. $displayName = $linkURL;
  244. }
  245. $pCell->getHyperlink()->setUrl($linkURL);
  246. return $displayName;
  247. } // function HYPERLINK()
  248. /**
  249. * INDIRECT
  250. *
  251. * Returns the reference specified by a text string.
  252. * References are immediately evaluated to display their contents.
  253. *
  254. * Excel Function:
  255. * =INDIRECT(cellAddress)
  256. *
  257. * NOTE - INDIRECT() does not yet support the optional a1 parameter introduced in Excel 2010
  258. *
  259. * @param cellAddress An array or array formula, or a reference to a range of cells for which you want the number of rows
  260. * @return mixed The cells referenced by cellAddress
  261. *
  262. * @todo Support for the optional a1 parameter introduced in Excel 2010
  263. *
  264. */
  265. public static function INDIRECT($cellAddress=Null, PHPExcel_Cell $pCell = null) {
  266. $cellAddress = PHPExcel_Calculation_Functions::flattenSingleValue($cellAddress);
  267. if (is_null($cellAddress) || $cellAddress === '') {
  268. return PHPExcel_Calculation_Functions::REF();
  269. }
  270. $cellAddress1 = $cellAddress;
  271. $cellAddress2 = NULL;
  272. if (strpos($cellAddress,':') !== false) {
  273. list($cellAddress1,$cellAddress2) = explode(':',$cellAddress);
  274. }
  275. if ((!preg_match('/^'.PHPExcel_Calculation::CALCULATION_REGEXP_CELLREF.'$/i', $cellAddress1, $matches)) ||
  276. ((!is_null($cellAddress2)) && (!preg_match('/^'.PHPExcel_Calculation::CALCULATION_REGEXP_CELLREF.'$/i', $cellAddress2, $matches)))) {
  277. if (!preg_match('/^'.PHPExcel_Calculation::CALCULATION_REGEXP_NAMEDRANGE.'$/i', $cellAddress1, $matches)) {
  278. return PHPExcel_Calculation_Functions::REF();
  279. }
  280. if (strpos($cellAddress,'!') !== false) {
  281. list($sheetName,$cellAddress) = explode('!',$cellAddress);
  282. $pSheet = $pCell->getParent()->getParent()->getSheetByName($sheetName);
  283. } else {
  284. $pSheet = $pCell->getParent();
  285. }
  286. return PHPExcel_Calculation::getInstance()->extractNamedRange($cellAddress, $pSheet, False);
  287. }
  288. if (strpos($cellAddress,'!') !== false) {
  289. list($sheetName,$cellAddress) = explode('!',$cellAddress);
  290. $pSheet = $pCell->getParent()->getParent()->getSheetByName($sheetName);
  291. } else {
  292. $pSheet = $pCell->getParent();
  293. }
  294. return PHPExcel_Calculation::getInstance()->extractCellRange($cellAddress, $pSheet, False);
  295. } // function INDIRECT()
  296. /**
  297. * OFFSET
  298. *
  299. * Returns a reference to a range that is a specified number of rows and columns from a cell or range of cells.
  300. * The reference that is returned can be a single cell or a range of cells. You can specify the number of rows and
  301. * the number of columns to be returned.
  302. *
  303. * Excel Function:
  304. * =OFFSET(cellAddress, rows, cols, [height], [width])
  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. $args = func_get_args();
  330. $pCell = array_pop($args);
  331. if (!is_object($pCell)) {
  332. return PHPExcel_Calculation_Functions::REF();
  333. }
  334. $sheetName = null;
  335. if (strpos($cellAddress,"!")) {
  336. list($sheetName,$cellAddress) = explode("!",$cellAddress);
  337. }
  338. if (strpos($cellAddress,":")) {
  339. list($startCell,$endCell) = explode(":",$cellAddress);
  340. } else {
  341. $startCell = $endCell = $cellAddress;
  342. }
  343. list($startCellColumn,$startCellRow) = PHPExcel_Cell::coordinateFromString($startCell);
  344. list($endCellColumn,$endCellRow) = PHPExcel_Cell::coordinateFromString($endCell);
  345. $startCellRow += $rows;
  346. $startCellColumn = PHPExcel_Cell::columnIndexFromString($startCellColumn) - 1;
  347. $startCellColumn += $columns;
  348. if (($startCellRow <= 0) || ($startCellColumn < 0)) {
  349. return PHPExcel_Calculation_Functions::REF();
  350. }
  351. $endCellColumn = PHPExcel_Cell::columnIndexFromString($endCellColumn) - 1;
  352. if (($width != null) && (!is_object($width))) {
  353. $endCellColumn = $startCellColumn + $width - 1;
  354. } else {
  355. $endCellColumn += $columns;
  356. }
  357. $startCellColumn = PHPExcel_Cell::stringFromColumnIndex($startCellColumn);
  358. if (($height != null) && (!is_object($height))) {
  359. $endCellRow = $startCellRow + $height - 1;
  360. } else {
  361. $endCellRow += $rows;
  362. }
  363. if (($endCellRow <= 0) || ($endCellColumn < 0)) {
  364. return PHPExcel_Calculation_Functions::REF();
  365. }
  366. $endCellColumn = PHPExcel_Cell::stringFromColumnIndex($endCellColumn);
  367. $cellAddress = $startCellColumn.$startCellRow;
  368. if (($startCellColumn != $endCellColumn) || ($startCellRow != $endCellRow)) {
  369. $cellAddress .= ':'.$endCellColumn.$endCellRow;
  370. }
  371. if ($sheetName !== null) {
  372. $pSheet = $pCell->getParent()->getParent()->getSheetByName($sheetName);
  373. } else {
  374. $pSheet = $pCell->getParent();
  375. }
  376. return PHPExcel_Calculation::getInstance()->extractCellRange($cellAddress, $pSheet, False);
  377. } // function OFFSET()
  378. /**
  379. * CHOOSE
  380. *
  381. * Uses lookup_value to return a value from the list of value arguments.
  382. * Use CHOOSE to select one of up to 254 values based on the lookup_value.
  383. *
  384. * Excel Function:
  385. * =CHOOSE(index_num, value1, [value2], ...)
  386. *
  387. * @param index_num Specifies which value argument is selected.
  388. * Index_num must be a number between 1 and 254, or a formula or reference to a cell containing a number
  389. * between 1 and 254.
  390. * @param value1... Value1 is required, subsequent values are optional.
  391. * Between 1 to 254 value arguments from which CHOOSE selects a value or an action to perform based on
  392. * index_num. The arguments can be numbers, cell references, defined names, formulas, functions, or
  393. * text.
  394. * @return mixed The selected value
  395. */
  396. public static function CHOOSE() {
  397. $chooseArgs = func_get_args();
  398. $chosenEntry = PHPExcel_Calculation_Functions::flattenArray(array_shift($chooseArgs));
  399. $entryCount = count($chooseArgs) - 1;
  400. if(is_array($chosenEntry)) {
  401. $chosenEntry = array_shift($chosenEntry);
  402. }
  403. if ((is_numeric($chosenEntry)) && (!is_bool($chosenEntry))) {
  404. --$chosenEntry;
  405. } else {
  406. return PHPExcel_Calculation_Functions::VALUE();
  407. }
  408. $chosenEntry = floor($chosenEntry);
  409. if (($chosenEntry <= 0) || ($chosenEntry > $entryCount)) {
  410. return PHPExcel_Calculation_Functions::VALUE();
  411. }
  412. if (is_array($chooseArgs[$chosenEntry])) {
  413. return PHPExcel_Calculation_Functions::flattenArray($chooseArgs[$chosenEntry]);
  414. } else {
  415. return $chooseArgs[$chosenEntry];
  416. }
  417. } // function CHOOSE()
  418. /**
  419. * MATCH
  420. *
  421. * The MATCH function searches for a specified item in a range of cells
  422. *
  423. * Excel Function:
  424. * =MATCH(lookup_value, lookup_array, [match_type])
  425. *
  426. * @param lookup_value The value that you want to match in lookup_array
  427. * @param lookup_array The range of cells being searched
  428. * @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.
  429. * @return integer The relative position of the found item
  430. */
  431. public static function MATCH($lookup_value, $lookup_array, $match_type=1) {
  432. $lookup_array = PHPExcel_Calculation_Functions::flattenArray($lookup_array);
  433. $lookup_value = PHPExcel_Calculation_Functions::flattenSingleValue($lookup_value);
  434. $match_type = (is_null($match_type)) ? 1 : (int) PHPExcel_Calculation_Functions::flattenSingleValue($match_type);
  435. // MATCH is not case sensitive
  436. $lookup_value = strtolower($lookup_value);
  437. // lookup_value type has to be number, text, or logical values
  438. if ((!is_numeric($lookup_value)) && (!is_string($lookup_value)) && (!is_bool($lookup_value))) {
  439. return PHPExcel_Calculation_Functions::NA();
  440. }
  441. // match_type is 0, 1 or -1
  442. if (($match_type !== 0) && ($match_type !== -1) && ($match_type !== 1)) {
  443. return PHPExcel_Calculation_Functions::NA();
  444. }
  445. // lookup_array should not be empty
  446. $lookupArraySize = count($lookup_array);
  447. if ($lookupArraySize <= 0) {
  448. return PHPExcel_Calculation_Functions::NA();
  449. }
  450. // lookup_array should contain only number, text, or logical values, or empty (null) cells
  451. foreach($lookup_array as $i => $lookupArrayValue) {
  452. // check the type of the value
  453. if ((!is_numeric($lookupArrayValue)) && (!is_string($lookupArrayValue)) &&
  454. (!is_bool($lookupArrayValue)) && (!is_null($lookupArrayValue))) {
  455. return PHPExcel_Calculation_Functions::NA();
  456. }
  457. // convert strings to lowercase for case-insensitive testing
  458. if (is_string($lookupArrayValue)) {
  459. $lookup_array[$i] = strtolower($lookupArrayValue);
  460. }
  461. if ((is_null($lookupArrayValue)) && (($match_type == 1) || ($match_type == -1))) {
  462. $lookup_array = array_slice($lookup_array,0,$i-1);
  463. }
  464. }
  465. // if match_type is 1 or -1, the list has to be ordered
  466. if ($match_type == 1) {
  467. asort($lookup_array);
  468. $keySet = array_keys($lookup_array);
  469. } elseif($match_type == -1) {
  470. arsort($lookup_array);
  471. $keySet = array_keys($lookup_array);
  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. // unsuccessful in finding a match, return #N/A error value
  520. return PHPExcel_Calculation_Functions::NA();
  521. } // function MATCH()
  522. /**
  523. * INDEX
  524. *
  525. * Uses an index to choose a value from a reference or array
  526. *
  527. * Excel Function:
  528. * =INDEX(range_array, row_num, [column_num])
  529. *
  530. * @param range_array A range of cells or an array constant
  531. * @param row_num The row in array from which to return a value. If row_num is omitted, column_num is required.
  532. * @param column_num The column in array from which to return a value. If column_num is omitted, row_num is required.
  533. * @return mixed the value of a specified cell or array of cells
  534. */
  535. public static function INDEX($arrayValues,$rowNum = 0,$columnNum = 0) {
  536. if (($rowNum < 0) || ($columnNum < 0)) {
  537. return PHPExcel_Calculation_Functions::VALUE();
  538. }
  539. if (!is_array($arrayValues)) {
  540. return PHPExcel_Calculation_Functions::REF();
  541. }
  542. $rowKeys = array_keys($arrayValues);
  543. $columnKeys = @array_keys($arrayValues[$rowKeys[0]]);
  544. if ($columnNum > count($columnKeys)) {
  545. return PHPExcel_Calculation_Functions::VALUE();
  546. } elseif ($columnNum == 0) {
  547. if ($rowNum == 0) {
  548. return $arrayValues;
  549. }
  550. $rowNum = $rowKeys[--$rowNum];
  551. $returnArray = array();
  552. foreach($arrayValues as $arrayColumn) {
  553. if (is_array($arrayColumn)) {
  554. if (isset($arrayColumn[$rowNum])) {
  555. $returnArray[] = $arrayColumn[$rowNum];
  556. } else {
  557. return $arrayValues[$rowNum];
  558. }
  559. } else {
  560. return $arrayValues[$rowNum];
  561. }
  562. }
  563. return $returnArray;
  564. }
  565. $columnNum = $columnKeys[--$columnNum];
  566. if ($rowNum > count($rowKeys)) {
  567. return PHPExcel_Calculation_Functions::VALUE();
  568. } elseif ($rowNum == 0) {
  569. return $arrayValues[$columnNum];
  570. }
  571. $rowNum = $rowKeys[--$rowNum];
  572. return $arrayValues[$rowNum][$columnNum];
  573. } // function INDEX()
  574. /**
  575. * TRANSPOSE
  576. *
  577. * @param array $matrixData A matrix of values
  578. * @return array
  579. *
  580. * Unlike the Excel TRANSPOSE function, which will only work on a single row or column, this function will transpose a full matrix.
  581. */
  582. public static function TRANSPOSE($matrixData) {
  583. $returnMatrix = array();
  584. if (!is_array($matrixData)) { $matrixData = array(array($matrixData)); }
  585. $column = 0;
  586. foreach($matrixData as $matrixRow) {
  587. $row = 0;
  588. foreach($matrixRow as $matrixCell) {
  589. $returnMatrix[$row][$column] = $matrixCell;
  590. ++$row;
  591. }
  592. ++$column;
  593. }
  594. return $returnMatrix;
  595. } // function TRANSPOSE()
  596. private static function _vlookupSort($a,$b) {
  597. $f = array_keys($a);
  598. $firstColumn = array_shift($f);
  599. if (strtolower($a[$firstColumn]) == strtolower($b[$firstColumn])) {
  600. return 0;
  601. }
  602. return (strtolower($a[$firstColumn]) < strtolower($b[$firstColumn])) ? -1 : 1;
  603. } // function _vlookupSort()
  604. /**
  605. * VLOOKUP
  606. * 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.
  607. * @param lookup_value The value that you want to match in lookup_array
  608. * @param lookup_array The range of cells being searched
  609. * @param index_number The column number in table_array from which the matching value must be returned. The first column is 1.
  610. * @param not_exact_match Determines if you are looking for an exact match based on lookup_value.
  611. * @return mixed The value of the found cell
  612. */
  613. public static function VLOOKUP($lookup_value, $lookup_array, $index_number, $not_exact_match=true) {
  614. $lookup_value = PHPExcel_Calculation_Functions::flattenSingleValue($lookup_value);
  615. $index_number = PHPExcel_Calculation_Functions::flattenSingleValue($index_number);
  616. $not_exact_match = PHPExcel_Calculation_Functions::flattenSingleValue($not_exact_match);
  617. // index_number must be greater than or equal to 1
  618. if ($index_number < 1) {
  619. return PHPExcel_Calculation_Functions::VALUE();
  620. }
  621. // index_number must be less than or equal to the number of columns in lookup_array
  622. if ((!is_array($lookup_array)) || (empty($lookup_array))) {
  623. return PHPExcel_Calculation_Functions::REF();
  624. } else {
  625. $f = array_keys($lookup_array);
  626. $firstRow = array_pop($f);
  627. if ((!is_array($lookup_array[$firstRow])) || ($index_number > count($lookup_array[$firstRow]))) {
  628. return PHPExcel_Calculation_Functions::REF();
  629. } else {
  630. $columnKeys = array_keys($lookup_array[$firstRow]);
  631. $returnColumn = $columnKeys[--$index_number];
  632. $firstColumn = array_shift($columnKeys);
  633. }
  634. }
  635. if (!$not_exact_match) {
  636. uasort($lookup_array,array('self','_vlookupSort'));
  637. }
  638. $rowNumber = $rowValue = False;
  639. foreach($lookup_array as $rowKey => $rowData) {
  640. if (strtolower($rowData[$firstColumn]) > strtolower($lookup_value)) {
  641. break;
  642. }
  643. $rowNumber = $rowKey;
  644. $rowValue = $rowData[$firstColumn];
  645. }
  646. if ($rowNumber !== false) {
  647. if ((!$not_exact_match) && ($rowValue != $lookup_value)) {
  648. // if an exact match is required, we have what we need to return an appropriate response
  649. return PHPExcel_Calculation_Functions::NA();
  650. } else {
  651. // otherwise return the appropriate value
  652. return $lookup_array[$rowNumber][$returnColumn];
  653. }
  654. }
  655. return PHPExcel_Calculation_Functions::NA();
  656. } // function VLOOKUP()
  657. /**
  658. * LOOKUP
  659. * The LOOKUP function searches for value either from a one-row or one-column range or from an array.
  660. * @param lookup_value The value that you want to match in lookup_array
  661. * @param lookup_vector The range of cells being searched
  662. * @param result_vector The column from which the matching value must be returned
  663. * @return mixed The value of the found cell
  664. */
  665. public static function LOOKUP($lookup_value, $lookup_vector, $result_vector=null) {
  666. $lookup_value = PHPExcel_Calculation_Functions::flattenSingleValue($lookup_value);
  667. if (!is_array($lookup_vector)) {
  668. return PHPExcel_Calculation_Functions::NA();
  669. }
  670. $lookupRows = count($lookup_vector);
  671. $l = array_keys($lookup_vector);
  672. $l = array_shift($l);
  673. $lookupColumns = count($lookup_vector[$l]);
  674. if ((($lookupRows == 1) && ($lookupColumns > 1)) || (($lookupRows == 2) && ($lookupColumns != 2))) {
  675. $lookup_vector = self::TRANSPOSE($lookup_vector);
  676. $lookupRows = count($lookup_vector);
  677. $l = array_keys($lookup_vector);
  678. $lookupColumns = count($lookup_vector[array_shift($l)]);
  679. }
  680. if (is_null($result_vector)) {
  681. $result_vector = $lookup_vector;
  682. }
  683. $resultRows = count($result_vector);
  684. $l = array_keys($result_vector);
  685. $l = array_shift($l);
  686. $resultColumns = count($result_vector[$l]);
  687. if ((($resultRows == 1) && ($resultColumns > 1)) || (($resultRows == 2) && ($resultColumns != 2))) {
  688. $result_vector = self::TRANSPOSE($result_vector);
  689. $resultRows = count($result_vector);
  690. $r = array_keys($result_vector);
  691. $resultColumns = count($result_vector[array_shift($r)]);
  692. }
  693. if ($lookupRows == 2) {
  694. $result_vector = array_pop($lookup_vector);
  695. $lookup_vector = array_shift($lookup_vector);
  696. }
  697. if ($lookupColumns != 2) {
  698. foreach($lookup_vector as &$value) {
  699. if (is_array($value)) {
  700. $k = array_keys($value);
  701. $key1 = $key2 = array_shift($k);
  702. $key2++;
  703. $dataValue1 = $value[$key1];
  704. } else {
  705. $key1 = 0;
  706. $key2 = 1;
  707. $dataValue1 = $value;
  708. }
  709. $dataValue2 = array_shift($result_vector);
  710. if (is_array($dataValue2)) {
  711. $dataValue2 = array_shift($dataValue2);
  712. }
  713. $value = array($key1 => $dataValue1, $key2 => $dataValue2);
  714. }
  715. unset($value);
  716. }
  717. return self::VLOOKUP($lookup_value,$lookup_vector,2);
  718. } // function LOOKUP()
  719. } // class PHPExcel_Calculation_LookupRef