LookupRef.php 37 KB

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