1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Calculation\LookupRef; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Exception; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Coordinate; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\StringHelper; |
10
|
|
|
|
11
|
|
|
class HLookup extends LookupBase |
12
|
|
|
{ |
13
|
|
|
use ArrayEnabled; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* HLOOKUP |
17
|
|
|
* The HLOOKUP function searches for value in the top-most row of lookup_array and returns the value |
18
|
|
|
* in the same column based on the index_number. |
19
|
|
|
* |
20
|
|
|
* @param mixed $lookupValue The value that you want to match in lookup_array |
21
|
|
|
* @param mixed[][] $lookupArray The range of cells being searched |
22
|
|
|
* @param array<mixed>|float|int|string $indexNumber The row number in table_array from which the matching value must be returned. |
23
|
|
|
* The first row is 1. |
24
|
|
|
* @param mixed $notExactMatch determines if you are looking for an exact match based on lookup_value |
25
|
|
|
* |
26
|
|
|
* @return mixed The value of the found cell |
27
|
|
|
*/ |
28
|
31 |
|
public static function lookup(mixed $lookupValue, $lookupArray, $indexNumber, mixed $notExactMatch = true): mixed |
29
|
|
|
{ |
30
|
31 |
|
if (is_array($lookupValue) || is_array($indexNumber)) { |
31
|
29 |
|
return self::evaluateArrayArgumentsIgnore([self::class, __FUNCTION__], 1, $lookupValue, $lookupArray, $indexNumber, $notExactMatch); |
32
|
|
|
} |
33
|
|
|
|
34
|
31 |
|
$notExactMatch = (bool) ($notExactMatch ?? true); |
35
|
|
|
|
36
|
|
|
try { |
37
|
31 |
|
self::validateLookupArray($lookupArray); |
38
|
31 |
|
$lookupArray = self::convertLiteralArray($lookupArray); |
39
|
31 |
|
$indexNumber = self::validateIndexLookup($lookupArray, $indexNumber); |
40
|
1 |
|
} catch (Exception $e) { |
41
|
1 |
|
return $e->getMessage(); |
42
|
|
|
} |
43
|
|
|
|
44
|
30 |
|
$f = array_keys($lookupArray); |
45
|
30 |
|
$firstRow = reset($f); |
46
|
30 |
|
if ((!is_array($lookupArray[$firstRow])) || ($indexNumber > count($lookupArray))) { |
47
|
1 |
|
return ExcelError::REF(); |
48
|
|
|
} |
49
|
|
|
|
50
|
29 |
|
$firstkey = $f[0] - 1; |
51
|
29 |
|
$returnColumn = $firstkey + $indexNumber; |
52
|
|
|
/** @var mixed[][] $lookupArray */ |
53
|
29 |
|
$firstColumn = array_shift($f) ?? 1; |
54
|
29 |
|
$rowNumber = self::hLookupSearch($lookupValue, $lookupArray, $firstColumn, $notExactMatch); |
55
|
|
|
|
56
|
29 |
|
if ($rowNumber !== null) { |
57
|
|
|
// otherwise return the appropriate value |
58
|
27 |
|
return $lookupArray[$returnColumn][Coordinate::stringFromColumnIndex($rowNumber)]; |
59
|
|
|
} |
60
|
|
|
|
61
|
2 |
|
return ExcelError::NA(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param mixed $lookupValue The value that you want to match in lookup_array |
66
|
|
|
* @param mixed[][] $lookupArray |
67
|
|
|
* @param int|string $column |
68
|
|
|
*/ |
69
|
29 |
|
private static function hLookupSearch(mixed $lookupValue, array $lookupArray, $column, bool $notExactMatch): ?int |
70
|
|
|
{ |
71
|
29 |
|
$lookupLower = StringHelper::strToLower(StringHelper::convertToString($lookupValue)); |
72
|
|
|
|
73
|
29 |
|
$rowNumber = null; |
74
|
29 |
|
foreach ($lookupArray[$column] as $rowKey => $rowData) { |
75
|
|
|
// break if we have passed possible keys |
76
|
|
|
/** @var string $rowKey */ |
77
|
29 |
|
$bothNumeric = is_numeric($lookupValue) && is_numeric($rowData); |
78
|
29 |
|
$bothNotNumeric = !is_numeric($lookupValue) && !is_numeric($rowData); |
79
|
|
|
/** @var scalar $rowData */ |
80
|
29 |
|
$cellDataLower = StringHelper::strToLower((string) $rowData); |
81
|
|
|
|
82
|
|
|
if ( |
83
|
29 |
|
$notExactMatch |
84
|
29 |
|
&& (($bothNumeric && $rowData > $lookupValue) || ($bothNotNumeric && $cellDataLower > $lookupLower)) |
85
|
|
|
) { |
86
|
7 |
|
break; |
87
|
|
|
} |
88
|
|
|
|
89
|
29 |
|
$rowNumber = self::checkMatch( |
90
|
29 |
|
$bothNumeric, |
91
|
29 |
|
$bothNotNumeric, |
92
|
29 |
|
$notExactMatch, |
93
|
29 |
|
Coordinate::columnIndexFromString($rowKey), |
94
|
29 |
|
$cellDataLower, |
95
|
29 |
|
$lookupLower, |
96
|
29 |
|
$rowNumber |
97
|
29 |
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
29 |
|
return $rowNumber; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param mixed[] $lookupArray |
105
|
|
|
* |
106
|
|
|
* @return mixed[] |
107
|
|
|
*/ |
108
|
31 |
|
private static function convertLiteralArray(array $lookupArray): array |
109
|
|
|
{ |
110
|
31 |
|
if (array_key_exists(0, $lookupArray)) { |
111
|
5 |
|
$lookupArray2 = []; |
112
|
5 |
|
$row = 0; |
113
|
5 |
|
foreach ($lookupArray as $arrayVal) { |
114
|
5 |
|
++$row; |
115
|
5 |
|
if (!is_array($arrayVal)) { |
116
|
|
|
$arrayVal = [$arrayVal]; |
117
|
|
|
} |
118
|
5 |
|
$arrayVal2 = []; |
119
|
5 |
|
foreach ($arrayVal as $key2 => $val2) { |
120
|
5 |
|
$index = Coordinate::stringFromColumnIndex($key2 + 1); |
121
|
5 |
|
$arrayVal2[$index] = $val2; |
122
|
|
|
} |
123
|
5 |
|
$lookupArray2[$row] = $arrayVal2; |
124
|
|
|
} |
125
|
5 |
|
$lookupArray = $lookupArray2; |
126
|
|
|
} |
127
|
|
|
|
128
|
31 |
|
return $lookupArray; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|