LookupRef.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2013 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 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.9, 2013-06-02
  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 - 2013 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. * @param PHPExcel_Cell $pCell The cell to set the hyperlink in
  233. * @return mixed The value of $displayName (or $linkURL if $displayName was blank)
  234. */
  235. public static function HYPERLINK($linkURL = '', $displayName = null, PHPExcel_Cell $pCell = null) {
  236. $args = func_get_args();
  237. $pCell = array_pop($args);
  238. $linkURL = (is_null($linkURL)) ? '' : PHPExcel_Calculation_Functions::flattenSingleValue($linkURL);
  239. $displayName = (is_null($displayName)) ? '' : PHPExcel_Calculation_Functions::flattenSingleValue($displayName);
  240. if ((!is_object($pCell)) || (trim($linkURL) == '')) {
  241. return PHPExcel_Calculation_Functions::REF();
  242. }
  243. if ((is_object($displayName)) || trim($displayName) == '') {
  244. $displayName = $linkURL;
  245. }
  246. $pCell->getHyperlink()->setUrl($linkURL);
  247. return $displayName;
  248. } // function HYPERLINK()
  249. /**
  250. * INDIRECT
  251. *
  252. * Returns the reference specified by a text string.
  253. * References are immediately evaluated to display their contents.
  254. *
  255. * Excel Function:
  256. * =INDIRECT(cellAddress)
  257. *
  258. * NOTE - INDIRECT() does not yet support the optional a1 parameter introduced in Excel 2010
  259. *
  260. * @param cellAddress $cellAddress The cell address of the current cell (containing this formula)
  261. * @param PHPExcel_Cell $pCell The current cell (containing this formula)
  262. * @return mixed The cells referenced by cellAddress
  263. *
  264. * @todo Support for the optional a1 parameter introduced in Excel 2010
  265. *
  266. */
  267. public static function INDIRECT($cellAddress = NULL, PHPExcel_Cell $pCell = NULL) {
  268. $cellAddress = PHPExcel_Calculation_Functions::flattenSingleValue($cellAddress);
  269. if (is_null($cellAddress) || $cellAddress === '') {
  270. return PHPExcel_Calculation_Functions::REF();
  271. }
  272. $cellAddress1 = $cellAddress;
  273. $cellAddress2 = NULL;
  274. if (strpos($cellAddress,':') !== false) {
  275. list($cellAddress1,$cellAddress2) = explode(':',$cellAddress);
  276. }
  277. if ((!preg_match('/^'.PHPExcel_Calculation::CALCULATION_REGEXP_CELLREF.'$/i', $cellAddress1, $matches)) ||
  278. ((!is_null($cellAddress2)) && (!preg_match('/^'.PHPExcel_Calculation::CALCULATION_REGEXP_CELLREF.'$/i', $cellAddress2, $matches)))) {
  279. if (!preg_match('/^'.PHPExcel_Calculation::CALCULATION_REGEXP_NAMEDRANGE.'$/i', $cellAddress1, $matches)) {
  280. return PHPExcel_Calculation_Functions::REF();
  281. }
  282. if (strpos($cellAddress,'!') !== FALSE) {
  283. list($sheetName, $cellAddress) = explode('!',$cellAddress);
  284. $sheetName = trim($sheetName, "'");
  285. $pSheet = $pCell->getParent()->getParent()->getSheetByName($sheetName);
  286. } else {
  287. $pSheet = $pCell->getParent();
  288. }
  289. return PHPExcel_Calculation::getInstance()->extractNamedRange($cellAddress, $pSheet, FALSE);
  290. }
  291. if (strpos($cellAddress,'!') !== FALSE) {
  292. list($sheetName,$cellAddress) = explode('!',$cellAddress);
  293. $sheetName = trim($sheetName, "'");
  294. $pSheet = $pCell->getParent()->getParent()->getSheetByName($sheetName);
  295. } else {
  296. $pSheet = $pCell->getParent();
  297. }
  298. return PHPExcel_Calculation::getInstance()->extractCellRange($cellAddress, $pSheet, FALSE);
  299. } // function INDIRECT()
  300. /**
  301. * OFFSET
  302. *
  303. * Returns a reference to a range that is a specified number of rows and columns from a cell or range of cells.
  304. * The reference that is returned can be a single cell or a range of cells. You can specify the number of rows and
  305. * the number of columns to be returned.
  306. *
  307. * Excel Function:
  308. * =OFFSET(cellAddress, rows, cols, [height], [width])
  309. *
  310. * @param cellAddress The reference from which you want to base the offset. Reference must refer to a cell or
  311. * range of adjacent cells; otherwise, OFFSET returns the #VALUE! error value.
  312. * @param rows The number of rows, up or down, that you want the upper-left cell to refer to.
  313. * Using 5 as the rows argument specifies that the upper-left cell in the reference is
  314. * five rows below reference. Rows can be positive (which means below the starting reference)
  315. * or negative (which means above the starting reference).
  316. * @param cols The number of columns, to the left or right, that you want the upper-left cell of the result
  317. * to refer to. Using 5 as the cols argument specifies that the upper-left cell in the
  318. * reference is five columns to the right of reference. Cols can be positive (which means
  319. * to the right of the starting reference) or negative (which means to the left of the
  320. * starting reference).
  321. * @param height The height, in number of rows, that you want the returned reference to be. Height must be a positive number.
  322. * @param width The width, in number of columns, that you want the returned reference to be. Width must be a positive number.
  323. * @return string A reference to a cell or range of cells
  324. */
  325. public static function OFFSET($cellAddress=Null,$rows=0,$columns=0,$height=null,$width=null) {
  326. $rows = PHPExcel_Calculation_Functions::flattenSingleValue($rows);
  327. $columns = PHPExcel_Calculation_Functions::flattenSingleValue($columns);
  328. $height = PHPExcel_Calculation_Functions::flattenSingleValue($height);
  329. $width = PHPExcel_Calculation_Functions::flattenSingleValue($width);
  330. if ($cellAddress == Null) {
  331. return 0;
  332. }
  333. $args = func_get_args();
  334. $pCell = array_pop($args);
  335. if (!is_object($pCell)) {
  336. return PHPExcel_Calculation_Functions::REF();
  337. }
  338. $sheetName = NULL;
  339. if (strpos($cellAddress,"!")) {
  340. list($sheetName,$cellAddress) = explode("!",$cellAddress);
  341. $sheetName = trim($sheetName, "'");
  342. }
  343. if (strpos($cellAddress,":")) {
  344. list($startCell,$endCell) = explode(":",$cellAddress);
  345. } else {
  346. $startCell = $endCell = $cellAddress;
  347. }
  348. list($startCellColumn,$startCellRow) = PHPExcel_Cell::coordinateFromString($startCell);
  349. list($endCellColumn,$endCellRow) = PHPExcel_Cell::coordinateFromString($endCell);
  350. $startCellRow += $rows;
  351. $startCellColumn = PHPExcel_Cell::columnIndexFromString($startCellColumn) - 1;
  352. $startCellColumn += $columns;
  353. if (($startCellRow <= 0) || ($startCellColumn < 0)) {
  354. return PHPExcel_Calculation_Functions::REF();
  355. }
  356. $endCellColumn = PHPExcel_Cell::columnIndexFromString($endCellColumn) - 1;
  357. if (($width != null) && (!is_object($width))) {
  358. $endCellColumn = $startCellColumn + $width - 1;
  359. } else {
  360. $endCellColumn += $columns;
  361. }
  362. $startCellColumn = PHPExcel_Cell::stringFromColumnIndex($startCellColumn);
  363. if (($height != null) && (!is_object($height))) {
  364. $endCellRow = $startCellRow + $height - 1;
  365. } else {
  366. $endCellRow += $rows;
  367. }
  368. if (($endCellRow <= 0) || ($endCellColumn < 0)) {
  369. return PHPExcel_Calculation_Functions::REF();
  370. }
  371. $endCellColumn = PHPExcel_Cell::stringFromColumnIndex($endCellColumn);
  372. $cellAddress = $startCellColumn.$startCellRow;
  373. if (($startCellColumn != $endCellColumn) || ($startCellRow != $endCellRow)) {
  374. $cellAddress .= ':'.$endCellColumn.$endCellRow;
  375. }
  376. if ($sheetName !== NULL) {
  377. $pSheet = $pCell->getParent()->getParent()->getSheetByName($sheetName);
  378. } else {
  379. $pSheet = $pCell->getParent();
  380. }
  381. return PHPExcel_Calculation::getInstance()->extractCellRange($cellAddress, $pSheet, False);
  382. } // function OFFSET()
  383. /**
  384. * CHOOSE
  385. *
  386. * Uses lookup_value to return a value from the list of value arguments.
  387. * Use CHOOSE to select one of up to 254 values based on the lookup_value.
  388. *
  389. * Excel Function:
  390. * =CHOOSE(index_num, value1, [value2], ...)
  391. *
  392. * @param index_num Specifies which value argument is selected.
  393. * Index_num must be a number between 1 and 254, or a formula or reference to a cell containing a number
  394. * between 1 and 254.
  395. * @param value1... Value1 is required, subsequent values are optional.
  396. * Between 1 to 254 value arguments from which CHOOSE selects a value or an action to perform based on
  397. * index_num. The arguments can be numbers, cell references, defined names, formulas, functions, or
  398. * text.
  399. * @return mixed The selected value
  400. */
  401. public static function CHOOSE() {
  402. $chooseArgs = func_get_args();
  403. $chosenEntry = PHPExcel_Calculation_Functions::flattenArray(array_shift($chooseArgs));
  404. $entryCount = count($chooseArgs) - 1;
  405. if(is_array($chosenEntry)) {
  406. $chosenEntry = array_shift($chosenEntry);
  407. }
  408. if ((is_numeric($chosenEntry)) && (!is_bool($chosenEntry))) {
  409. --$chosenEntry;
  410. } else {
  411. return PHPExcel_Calculation_Functions::VALUE();
  412. }
  413. $chosenEntry = floor($chosenEntry);
  414. if (($chosenEntry <= 0) || ($chosenEntry > $entryCount)) {
  415. return PHPExcel_Calculation_Functions::VALUE();
  416. }
  417. if (is_array($chooseArgs[$chosenEntry])) {
  418. return PHPExcel_Calculation_Functions::flattenArray($chooseArgs[$chosenEntry]);
  419. } else {
  420. return $chooseArgs[$chosenEntry];
  421. }
  422. } // function CHOOSE()
  423. /**
  424. * MATCH
  425. *
  426. * The MATCH function searches for a specified item in a range of cells
  427. *
  428. * Excel Function:
  429. * =MATCH(lookup_value, lookup_array, [match_type])
  430. *
  431. * @param lookup_value The value that you want to match in lookup_array
  432. * @param lookup_array The range of cells being searched
  433. * @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.
  434. * @return integer The relative position of the found item
  435. */
  436. public static function MATCH($lookup_value, $lookup_array, $match_type=1) {
  437. $lookup_array = PHPExcel_Calculation_Functions::flattenArray($lookup_array);
  438. $lookup_value = PHPExcel_Calculation_Functions::flattenSingleValue($lookup_value);
  439. $match_type = (is_null($match_type)) ? 1 : (int) PHPExcel_Calculation_Functions::flattenSingleValue($match_type);
  440. // MATCH is not case sensitive
  441. $lookup_value = strtolower($lookup_value);
  442. // lookup_value type has to be number, text, or logical values
  443. if ((!is_numeric($lookup_value)) && (!is_string($lookup_value)) && (!is_bool($lookup_value))) {
  444. return PHPExcel_Calculation_Functions::NA();
  445. }
  446. // match_type is 0, 1 or -1
  447. if (($match_type !== 0) && ($match_type !== -1) && ($match_type !== 1)) {
  448. return PHPExcel_Calculation_Functions::NA();
  449. }
  450. // lookup_array should not be empty
  451. $lookupArraySize = count($lookup_array);
  452. if ($lookupArraySize <= 0) {
  453. return PHPExcel_Calculation_Functions::NA();
  454. }
  455. // lookup_array should contain only number, text, or logical values, or empty (null) cells
  456. foreach($lookup_array as $i => $lookupArrayValue) {
  457. // check the type of the value
  458. if ((!is_numeric($lookupArrayValue)) && (!is_string($lookupArrayValue)) &&
  459. (!is_bool($lookupArrayValue)) && (!is_null($lookupArrayValue))) {
  460. return PHPExcel_Calculation_Functions::NA();
  461. }
  462. // convert strings to lowercase for case-insensitive testing
  463. if (is_string($lookupArrayValue)) {
  464. $lookup_array[$i] = strtolower($lookupArrayValue);
  465. }
  466. if ((is_null($lookupArrayValue)) && (($match_type == 1) || ($match_type == -1))) {
  467. $lookup_array = array_slice($lookup_array,0,$i-1);
  468. }
  469. }
  470. // if match_type is 1 or -1, the list has to be ordered
  471. if ($match_type == 1) {
  472. asort($lookup_array);
  473. $keySet = array_keys($lookup_array);
  474. } elseif($match_type == -1) {
  475. arsort($lookup_array);
  476. $keySet = array_keys($lookup_array);
  477. }
  478. // **
  479. // find the match
  480. // **
  481. // loop on the cells
  482. // var_dump($lookup_array);
  483. // echo '<br />';
  484. foreach($lookup_array as $i => $lookupArrayValue) {
  485. if (($match_type == 0) && ($lookupArrayValue == $lookup_value)) {
  486. // exact match
  487. return ++$i;
  488. } elseif (($match_type == -1) && ($lookupArrayValue <= $lookup_value)) {
  489. // echo '$i = '.$i.' => ';
  490. // var_dump($lookupArrayValue);
  491. // echo '<br />';
  492. // echo 'Keyset = ';
  493. // var_dump($keySet);
  494. // echo '<br />';
  495. $i = array_search($i,$keySet);
  496. // echo '$i='.$i.'<br />';
  497. // if match_type is -1 <=> find the smallest value that is greater than or equal to lookup_value
  498. if ($i < 1){
  499. // 1st cell was allready smaller than the lookup_value
  500. break;
  501. } else {
  502. // the previous cell was the match
  503. return $keySet[$i-1]+1;
  504. }
  505. } elseif (($match_type == 1) && ($lookupArrayValue >= $lookup_value)) {
  506. // echo '$i = '.$i.' => ';
  507. // var_dump($lookupArrayValue);
  508. // echo '<br />';
  509. // echo 'Keyset = ';
  510. // var_dump($keySet);
  511. // echo '<br />';
  512. $i = array_search($i,$keySet);
  513. // echo '$i='.$i.'<br />';
  514. // if match_type is 1 <=> find the largest value that is less than or equal to lookup_value
  515. if ($i < 1){
  516. // 1st cell was allready bigger than the lookup_value
  517. break;
  518. } else {
  519. // the previous cell was the match
  520. return $keySet[$i-1]+1;
  521. }
  522. }
  523. }
  524. // unsuccessful in finding a match, return #N/A error value
  525. return PHPExcel_Calculation_Functions::NA();
  526. } // function MATCH()
  527. /**
  528. * INDEX
  529. *
  530. * Uses an index to choose a value from a reference or array
  531. *
  532. * Excel Function:
  533. * =INDEX(range_array, row_num, [column_num])
  534. *
  535. * @param range_array A range of cells or an array constant
  536. * @param row_num The row in array from which to return a value. If row_num is omitted, column_num is required.
  537. * @param column_num The column in array from which to return a value. If column_num is omitted, row_num is required.
  538. * @return mixed the value of a specified cell or array of cells
  539. */
  540. public static function INDEX($arrayValues,$rowNum = 0,$columnNum = 0) {
  541. if (($rowNum < 0) || ($columnNum < 0)) {
  542. return PHPExcel_Calculation_Functions::VALUE();
  543. }
  544. if (!is_array($arrayValues)) {
  545. return PHPExcel_Calculation_Functions::REF();
  546. }
  547. $rowKeys = array_keys($arrayValues);
  548. $columnKeys = @array_keys($arrayValues[$rowKeys[0]]);
  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. return $arrayValues[$rowNum][$columnNum];
  578. } // function INDEX()
  579. /**
  580. * TRANSPOSE
  581. *
  582. * @param array $matrixData A matrix of values
  583. * @return array
  584. *
  585. * Unlike the Excel TRANSPOSE function, which will only work on a single row or column, this function will transpose a full matrix.
  586. */
  587. public static function TRANSPOSE($matrixData) {
  588. $returnMatrix = array();
  589. if (!is_array($matrixData)) { $matrixData = array(array($matrixData)); }
  590. $column = 0;
  591. foreach($matrixData as $matrixRow) {
  592. $row = 0;
  593. foreach($matrixRow as $matrixCell) {
  594. $returnMatrix[$row][$column] = $matrixCell;
  595. ++$row;
  596. }
  597. ++$column;
  598. }
  599. return $returnMatrix;
  600. } // function TRANSPOSE()
  601. private static function _vlookupSort($a,$b) {
  602. $f = array_keys($a);
  603. $firstColumn = array_shift($f);
  604. if (strtolower($a[$firstColumn]) == strtolower($b[$firstColumn])) {
  605. return 0;
  606. }
  607. return (strtolower($a[$firstColumn]) < strtolower($b[$firstColumn])) ? -1 : 1;
  608. } // function _vlookupSort()
  609. /**
  610. * VLOOKUP
  611. * 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.
  612. * @param lookup_value The value that you want to match in lookup_array
  613. * @param lookup_array The range of cells being searched
  614. * @param index_number The column number in table_array from which the matching value must be returned. The first column is 1.
  615. * @param not_exact_match Determines if you are looking for an exact match based on lookup_value.
  616. * @return mixed The value of the found cell
  617. */
  618. public static function VLOOKUP($lookup_value, $lookup_array, $index_number, $not_exact_match=true) {
  619. $lookup_value = PHPExcel_Calculation_Functions::flattenSingleValue($lookup_value);
  620. $index_number = PHPExcel_Calculation_Functions::flattenSingleValue($index_number);
  621. $not_exact_match = PHPExcel_Calculation_Functions::flattenSingleValue($not_exact_match);
  622. // index_number must be greater than or equal to 1
  623. if ($index_number < 1) {
  624. return PHPExcel_Calculation_Functions::VALUE();
  625. }
  626. // index_number must be less than or equal to the number of columns in lookup_array
  627. if ((!is_array($lookup_array)) || (empty($lookup_array))) {
  628. return PHPExcel_Calculation_Functions::REF();
  629. } else {
  630. $f = array_keys($lookup_array);
  631. $firstRow = array_pop($f);
  632. if ((!is_array($lookup_array[$firstRow])) || ($index_number > count($lookup_array[$firstRow]))) {
  633. return PHPExcel_Calculation_Functions::REF();
  634. } else {
  635. $columnKeys = array_keys($lookup_array[$firstRow]);
  636. $returnColumn = $columnKeys[--$index_number];
  637. $firstColumn = array_shift($columnKeys);
  638. }
  639. }
  640. if (!$not_exact_match) {
  641. uasort($lookup_array,array('self','_vlookupSort'));
  642. }
  643. $rowNumber = $rowValue = False;
  644. foreach($lookup_array as $rowKey => $rowData) {
  645. if (strtolower($rowData[$firstColumn]) > strtolower($lookup_value)) {
  646. break;
  647. }
  648. $rowNumber = $rowKey;
  649. $rowValue = $rowData[$firstColumn];
  650. }
  651. if ($rowNumber !== false) {
  652. if ((!$not_exact_match) && ($rowValue != $lookup_value)) {
  653. // if an exact match is required, we have what we need to return an appropriate response
  654. return PHPExcel_Calculation_Functions::NA();
  655. } else {
  656. // otherwise return the appropriate value
  657. return $lookup_array[$rowNumber][$returnColumn];
  658. }
  659. }
  660. return PHPExcel_Calculation_Functions::NA();
  661. } // function VLOOKUP()
  662. /**
  663. * LOOKUP
  664. * The LOOKUP function searches for value either from a one-row or one-column range or from an array.
  665. * @param lookup_value The value that you want to match in lookup_array
  666. * @param lookup_vector The range of cells being searched
  667. * @param result_vector The column from which the matching value must be returned
  668. * @return mixed The value of the found cell
  669. */
  670. public static function LOOKUP($lookup_value, $lookup_vector, $result_vector=null) {
  671. $lookup_value = PHPExcel_Calculation_Functions::flattenSingleValue($lookup_value);
  672. if (!is_array($lookup_vector)) {
  673. return PHPExcel_Calculation_Functions::NA();
  674. }
  675. $lookupRows = count($lookup_vector);
  676. $l = array_keys($lookup_vector);
  677. $l = array_shift($l);
  678. $lookupColumns = count($lookup_vector[$l]);
  679. if ((($lookupRows == 1) && ($lookupColumns > 1)) || (($lookupRows == 2) && ($lookupColumns != 2))) {
  680. $lookup_vector = self::TRANSPOSE($lookup_vector);
  681. $lookupRows = count($lookup_vector);
  682. $l = array_keys($lookup_vector);
  683. $lookupColumns = count($lookup_vector[array_shift($l)]);
  684. }
  685. if (is_null($result_vector)) {
  686. $result_vector = $lookup_vector;
  687. }
  688. $resultRows = count($result_vector);
  689. $l = array_keys($result_vector);
  690. $l = array_shift($l);
  691. $resultColumns = count($result_vector[$l]);
  692. if ((($resultRows == 1) && ($resultColumns > 1)) || (($resultRows == 2) && ($resultColumns != 2))) {
  693. $result_vector = self::TRANSPOSE($result_vector);
  694. $resultRows = count($result_vector);
  695. $r = array_keys($result_vector);
  696. $resultColumns = count($result_vector[array_shift($r)]);
  697. }
  698. if ($lookupRows == 2) {
  699. $result_vector = array_pop($lookup_vector);
  700. $lookup_vector = array_shift($lookup_vector);
  701. }
  702. if ($lookupColumns != 2) {
  703. foreach($lookup_vector as &$value) {
  704. if (is_array($value)) {
  705. $k = array_keys($value);
  706. $key1 = $key2 = array_shift($k);
  707. $key2++;
  708. $dataValue1 = $value[$key1];
  709. } else {
  710. $key1 = 0;
  711. $key2 = 1;
  712. $dataValue1 = $value;
  713. }
  714. $dataValue2 = array_shift($result_vector);
  715. if (is_array($dataValue2)) {
  716. $dataValue2 = array_shift($dataValue2);
  717. }
  718. $value = array($key1 => $dataValue1, $key2 => $dataValue2);
  719. }
  720. unset($value);
  721. }
  722. return self::VLOOKUP($lookup_value,$lookup_vector,2);
  723. } // function LOOKUP()
  724. } // class PHPExcel_Calculation_LookupRef