PluginProbe
TablePress – Tables in WordPress made easy / 3.4
TablePress – Tables in WordPress made easy v3.4
3.4 3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 All 45 releases
tablepress / libraries / vendor / PhpSpreadsheet / Calculation / LookupRef / HLookup.php

HLookup.php in TablePress – Tables in WordPress made easy 3.4, at libraries/vendor/PhpSpreadsheet/Calculation/LookupRef/HLookup.php

131 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
4
5 use TablePress\PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
6 use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Exception;
7 use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
8 use TablePress\PhpOffice\PhpSpreadsheet\Cell\Coordinate;
9 use TablePress\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 public static function lookup($lookupValue, $lookupArray, $indexNumber, $notExactMatch = true)
29 {
30 if (is_array($lookupValue) || is_array($indexNumber)) {
31 return self::evaluateArrayArgumentsIgnore([self::class, __FUNCTION__], 1, $lookupValue, $lookupArray, $indexNumber, $notExactMatch);
32 }
33
34 $notExactMatch = (bool) ($notExactMatch ?? true);
35
36 try {
37 self::validateLookupArray($lookupArray);
38 $lookupArray = self::convertLiteralArray($lookupArray);
39 $indexNumber = self::validateIndexLookup($lookupArray, $indexNumber);
40 } catch (Exception $e) {
41 return $e->getMessage();
42 }
43
44 $f = array_keys($lookupArray);
45 $firstRow = reset($f);
46 if ((!is_array($lookupArray[$firstRow])) || ($indexNumber > count($lookupArray))) {
47 return ExcelError::REF();
48 }
49
50 $firstkey = $f[0] - 1;
51 $returnColumn = $firstkey + $indexNumber;
52 /** @var mixed[][] $lookupArray */
53 $firstColumn = array_shift($f) ?? 1;
54 $rowNumber = self::hLookupSearch($lookupValue, $lookupArray, $firstColumn, $notExactMatch);
55
56 if ($rowNumber !== null) {
57 // otherwise return the appropriate value
58 return $lookupArray[$returnColumn][Coordinate::stringFromColumnIndex($rowNumber)];
59 }
60
61 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 private static function hLookupSearch($lookupValue, array $lookupArray, $column, bool $notExactMatch): ?int
70 {
71 $lookupLower = StringHelper::strToLower(StringHelper::convertToString($lookupValue));
72
73 $rowNumber = null;
74 foreach ($lookupArray[$column] as $rowKey => $rowData) {
75 // break if we have passed possible keys
76 /** @var string $rowKey */
77 $bothNumeric = is_numeric($lookupValue) && is_numeric($rowData);
78 $bothNotNumeric = !is_numeric($lookupValue) && !is_numeric($rowData);
79 /** @var scalar $rowData */
80 $cellDataLower = StringHelper::strToLower((string) $rowData);
81
82 if (
83 $notExactMatch
84 && (($bothNumeric && $rowData > $lookupValue) || ($bothNotNumeric && $cellDataLower > $lookupLower))
85 ) {
86 break;
87 }
88
89 $rowNumber = self::checkMatch(
90 $bothNumeric,
91 $bothNotNumeric,
92 $notExactMatch,
93 Coordinate::columnIndexFromString($rowKey),
94 $cellDataLower,
95 $lookupLower,
96 $rowNumber
97 );
98 }
99
100 return $rowNumber;
101 }
102
103 /**
104 * @param mixed[] $lookupArray
105 *
106 * @return mixed[]
107 */
108 private static function convertLiteralArray(array $lookupArray): array
109 {
110 if (array_key_exists(0, $lookupArray)) {
111 $lookupArray2 = [];
112 $row = 0;
113 foreach ($lookupArray as $arrayVal) {
114 ++$row;
115 if (!is_array($arrayVal)) {
116 $arrayVal = [$arrayVal];
117 }
118 $arrayVal2 = [];
119 foreach ($arrayVal as $key2 => $val2) {
120 $index = Coordinate::stringFromColumnIndex($key2 + 1);
121 $arrayVal2[$index] = $val2;
122 }
123 $lookupArray2[$row] = $arrayVal2;
124 }
125 $lookupArray = $lookupArray2;
126 }
127
128 return $lookupArray;
129 }
130 }
131