|
6 | 6 |
|
7 | 7 | use PhpOffice\PhpSpreadsheet\Calculation\Calculation; |
8 | 8 | use PhpOffice\PhpSpreadsheet\Cell\DataType; |
| 9 | +use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; |
9 | 10 | use PHPUnit\Framework\Attributes\DataProvider; |
10 | 11 |
|
11 | 12 | class VLookupTest extends AllSetupTeardown |
@@ -91,4 +92,55 @@ public function testIssue1402(): void |
91 | 92 | $worksheet->setCellValue('B5', '=VLOOKUP(A5,$A$1:$B$3,2,0)'); |
92 | 93 | self::assertSame('Numeric result', $worksheet->getCell('B5')->getCalculatedValue()); |
93 | 94 | } |
| 95 | + |
| 96 | + /** |
| 97 | + * VLOOKUP with a whole-column range where the end column contains no data. |
| 98 | + * getHighestDataRow() on the empty end column returned 1, producing an |
| 99 | + * inverted range (e.g. A4:F1) that caused a #N/A result. |
| 100 | + * |
| 101 | + * @see https://github.com/PHPOffice/PhpSpreadsheet/pull/4967 |
| 102 | + */ |
| 103 | + #[DataProvider('providerVlookupWholeColumnRange')] |
| 104 | + public function testVlookupWholeColumnRange(string $formula, string $expectedResult): void |
| 105 | + { |
| 106 | + $spreadsheet = $this->getSpreadsheet(); |
| 107 | + |
| 108 | + // Lookup sheet: col A = keys, col C = values; cols B, D, E, F are empty |
| 109 | + $lookupSheet = new Worksheet($spreadsheet, 'Sheet2'); |
| 110 | + $spreadsheet->addSheet($lookupSheet, 0); |
| 111 | + $lookupSheet->fromArray([ |
| 112 | + [1234, null, 'row1'], |
| 113 | + [2345, null, 'row2'], |
| 114 | + [3456, null, 'row3'], |
| 115 | + [4567, null, 'row4'], |
| 116 | + ], null, 'A1'); |
| 117 | + |
| 118 | + // Formula sheet: C1 holds the lookup value |
| 119 | + $formulaSheet = new Worksheet($spreadsheet, 'Sheet1'); |
| 120 | + $spreadsheet->addSheet($formulaSheet, 1); |
| 121 | + $formulaSheet->setCellValue('C1', 3456); |
| 122 | + $formulaSheet->setCellValue('A1', $formula); |
| 123 | + $spreadsheet->setActiveSheetIndexByName('Sheet1'); |
| 124 | + |
| 125 | + $result = $formulaSheet->getCell('A1')->getCalculatedValue(); |
| 126 | + self::assertSame($expectedResult, $result); |
| 127 | + } |
| 128 | + |
| 129 | + public static function providerVlookupWholeColumnRange(): array |
| 130 | + { |
| 131 | + return [ |
| 132 | + 'VLOOKUP with absolute whole-column range across sheets' => [ |
| 133 | + '=VLOOKUP($C1,Sheet2!$A:$F,3,FALSE)', |
| 134 | + 'row3', |
| 135 | + ], |
| 136 | + 'VLOOKUP with relative whole-column range across sheets' => [ |
| 137 | + '=VLOOKUP($C1,Sheet2!A:F,3,FALSE)', |
| 138 | + 'row3', |
| 139 | + ], |
| 140 | + 'VLOOKUP with mixed whole-column range across sheets' => [ |
| 141 | + '=VLOOKUP($C1,Sheet2!A:$F,3,FALSE)', |
| 142 | + 'row3', |
| 143 | + ], |
| 144 | + ]; |
| 145 | + } |
94 | 146 | } |
0 commit comments