1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef; |
6
|
|
|
|
7
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Calculation; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\DataType; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
|
12
|
|
|
class VLookupTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @dataProvider providerVLOOKUP |
16
|
|
|
*/ |
17
|
|
|
public function testVLOOKUP(mixed $expectedResult, mixed $value, mixed $table, mixed $index, ?bool $lookup = null): void |
18
|
|
|
{ |
19
|
|
|
$spreadsheet = new Spreadsheet(); |
20
|
|
|
$sheet = $spreadsheet->getActiveSheet(); |
21
|
|
|
if (is_array($table)) { |
22
|
|
|
$sheet->fromArray($table); |
23
|
|
|
$dimension = $sheet->calculateWorksheetDimension(); |
24
|
|
|
} else { |
25
|
|
|
$sheet->getCell('A1')->setValue($table); |
26
|
|
|
$dimension = 'A1'; |
27
|
|
|
} |
28
|
|
|
if ($lookup === null) { |
29
|
|
|
$lastarg = ''; |
30
|
|
|
} else { |
31
|
|
|
$lastarg = $lookup ? ',TRUE' : ',FALSE'; |
32
|
|
|
} |
33
|
|
|
$sheet->getCell('Z98')->setValue($value); |
34
|
|
|
if (is_array($index)) { |
35
|
|
|
$sheet->fromArray($index, null, 'Z100', true); |
36
|
|
|
$indexarg = 'Z100:Z' . (string) (99 + count($index)); |
37
|
|
|
} else { |
38
|
|
|
$sheet->getCell('Z100')->setValue($index); |
39
|
|
|
$indexarg = 'Z100'; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$sheet->getCell('Z99')->setValue("=VLOOKUP(Z98,$dimension,$indexarg$lastarg)"); |
43
|
|
|
$result = $sheet->getCell('Z99')->getCalculatedValue(); |
44
|
|
|
self::assertEquals($expectedResult, $result); |
45
|
|
|
$spreadsheet->disconnectWorksheets(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public static function providerVLOOKUP(): array |
49
|
|
|
{ |
50
|
|
|
return require 'tests/data/Calculation/LookupRef/VLOOKUP.php'; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @dataProvider providerVLookupArray |
55
|
|
|
*/ |
56
|
|
|
public function testVLookupArray(array $expectedResult, string $values, string $database, string $index): void |
57
|
|
|
{ |
58
|
|
|
$calculation = Calculation::getInstance(); |
59
|
|
|
|
60
|
|
|
$formula = "=VLOOKUP({$values}, {$database}, {$index}, false)"; |
61
|
|
|
$result = $calculation->_calculateFormulaValue($formula); |
62
|
|
|
self::assertEquals($expectedResult, $result); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public static function providerVLookupArray(): array |
66
|
|
|
{ |
67
|
|
|
return [ |
68
|
|
|
'row vector' => [ |
69
|
|
|
[[4.19, 5.77, 4.14]], |
70
|
|
|
'{"Orange", "Green", "Red"}', |
71
|
|
|
'{"Red", 4.14; "Orange", 4.19; "Yellow", 5.17; "Green", 5.77; "Blue", 6.39}', |
72
|
|
|
'2', |
73
|
|
|
], |
74
|
|
|
'issue 3561' => [ |
75
|
|
|
[[7, 8, 7]], |
76
|
|
|
'6', |
77
|
|
|
'{1,2,3,4,5;6,7,8,9,10;11,12,13,14,15}', |
78
|
|
|
'{2,3,2}', |
79
|
|
|
], |
80
|
|
|
]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testIssue1402(): void |
84
|
|
|
{ |
85
|
|
|
$spreadsheet = new Spreadsheet(); |
86
|
|
|
$worksheet = $spreadsheet->getActiveSheet(); |
87
|
|
|
|
88
|
|
|
$worksheet->setCellValueExplicit('A1', 1, DataType::TYPE_STRING); |
89
|
|
|
$worksheet->setCellValue('B1', 'Text Nr 1'); |
90
|
|
|
$worksheet->setCellValue('A2', 2); |
91
|
|
|
$worksheet->setCellValue('B2', 'Numeric result'); |
92
|
|
|
$worksheet->setCellValueExplicit('A3', 2, DataType::TYPE_STRING); |
93
|
|
|
$worksheet->setCellValue('B3', 'Text Nr 2'); |
94
|
|
|
$worksheet->setCellValueExplicit('A4', 2, DataType::TYPE_STRING); |
95
|
|
|
$worksheet->setCellValue('B4', '=VLOOKUP(A4,$A$1:$B$3,2,0)'); |
96
|
|
|
self::assertSame('Text Nr 2', $worksheet->getCell('B4')->getCalculatedValue()); |
97
|
|
|
$worksheet->setCellValue('A5', 2); |
98
|
|
|
$worksheet->setCellValue('B5', '=VLOOKUP(A5,$A$1:$B$3,2,0)'); |
99
|
|
|
self::assertSame('Numeric result', $worksheet->getCell('B5')->getCalculatedValue()); |
100
|
|
|
$spreadsheet->disconnectWorksheets(); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|