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