VLookup.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
  3. use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
  4. use PhpOffice\PhpSpreadsheet\Calculation\Exception;
  5. use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
  6. use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
  7. class VLookup extends LookupBase
  8. {
  9. use ArrayEnabled;
  10. /**
  11. * VLOOKUP
  12. * The VLOOKUP function searches for value in the left-most column of lookup_array and returns the value
  13. * in the same row based on the index_number.
  14. *
  15. * @param mixed $lookupValue The value that you want to match in lookup_array
  16. * @param mixed $lookupArray The range of cells being searched
  17. * @param mixed $indexNumber The column number in table_array from which the matching value must be returned.
  18. * The first column is 1.
  19. * @param mixed $notExactMatch determines if you are looking for an exact match based on lookup_value
  20. *
  21. * @return mixed The value of the found cell
  22. */
  23. public static function lookup($lookupValue, $lookupArray, $indexNumber, $notExactMatch = true)
  24. {
  25. if (is_array($lookupValue)) {
  26. return self::evaluateArrayArgumentsIgnore([self::class, __FUNCTION__], 1, $lookupValue, $lookupArray, $indexNumber, $notExactMatch);
  27. }
  28. $notExactMatch = (bool) ($notExactMatch ?? true);
  29. try {
  30. self::validateLookupArray($lookupArray);
  31. $indexNumber = self::validateIndexLookup($lookupArray, $indexNumber);
  32. } catch (Exception $e) {
  33. return $e->getMessage();
  34. }
  35. $f = array_keys($lookupArray);
  36. $firstRow = array_pop($f);
  37. if ((!is_array($lookupArray[$firstRow])) || ($indexNumber > count($lookupArray[$firstRow]))) {
  38. return ExcelError::REF();
  39. }
  40. $columnKeys = array_keys($lookupArray[$firstRow]);
  41. $returnColumn = $columnKeys[--$indexNumber];
  42. $firstColumn = array_shift($columnKeys) ?? 1;
  43. if (!$notExactMatch) {
  44. /** @var callable */
  45. $callable = [self::class, 'vlookupSort'];
  46. uasort($lookupArray, $callable);
  47. }
  48. $rowNumber = self::vLookupSearch($lookupValue, $lookupArray, $firstColumn, $notExactMatch);
  49. if ($rowNumber !== null) {
  50. // return the appropriate value
  51. return $lookupArray[$rowNumber][$returnColumn];
  52. }
  53. return ExcelError::NA();
  54. }
  55. private static function vlookupSort(array $a, array $b): int
  56. {
  57. reset($a);
  58. $firstColumn = key($a);
  59. $aLower = StringHelper::strToLower((string) $a[$firstColumn]);
  60. $bLower = StringHelper::strToLower((string) $b[$firstColumn]);
  61. if ($aLower == $bLower) {
  62. return 0;
  63. }
  64. return ($aLower < $bLower) ? -1 : 1;
  65. }
  66. /**
  67. * @param mixed $lookupValue The value that you want to match in lookup_array
  68. * @param int|string $column
  69. */
  70. private static function vLookupSearch($lookupValue, array $lookupArray, $column, bool $notExactMatch): ?int
  71. {
  72. $lookupLower = StringHelper::strToLower((string) $lookupValue);
  73. $rowNumber = null;
  74. foreach ($lookupArray as $rowKey => $rowData) {
  75. $bothNumeric = is_numeric($lookupValue) && is_numeric($rowData[$column]);
  76. $bothNotNumeric = !is_numeric($lookupValue) && !is_numeric($rowData[$column]);
  77. $cellDataLower = StringHelper::strToLower((string) $rowData[$column]);
  78. // break if we have passed possible keys
  79. if (
  80. $notExactMatch &&
  81. (($bothNumeric && ($rowData[$column] > $lookupValue)) ||
  82. ($bothNotNumeric && ($cellDataLower > $lookupLower)))
  83. ) {
  84. break;
  85. }
  86. $rowNumber = self::checkMatch(
  87. $bothNumeric,
  88. $bothNotNumeric,
  89. $notExactMatch,
  90. $rowKey,
  91. $cellDataLower,
  92. $lookupLower,
  93. $rowNumber
  94. );
  95. }
  96. return $rowNumber;
  97. }
  98. }