Additional unit tests for HLOOKUP() and VLOOKUP() and Examples for VLOOKUP()

This commit is contained in:
MarkBaker 2021-05-28 14:19:24 +02:00 committed by Mark Baker
parent 3540a275b9
commit 70a518981c
3 changed files with 91 additions and 509 deletions

View File

@ -0,0 +1,35 @@
<?php
use PhpOffice\PhpSpreadsheet\Spreadsheet;
require __DIR__ . '/../../Header.php';
$helper->log('Searches for a value in the top row of a table or an array of values,
and then returns a value in the same column from a row you specify
in the table or array.');
// Create new PhpSpreadsheet object
$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();
$data = [
['ID', 'First Name', 'Last Name', 'Salary'],
[72, 'Emily', 'Smith', 64901, null, 'ID', 53, 66, 56],
[66, 'James', 'Anderson', 70855, null, 'Salary'],
[14, 'Mia', 'Clark', 188657],
[30, 'John', 'Lewis', 97566],
[53, 'Jessica', 'Walker', 58339],
[56, 'Mark', 'Reed', 125180],
[79, 'Richard', 'Lopez', 91632],
];
$worksheet->fromArray($data, null, 'B2');
$worksheet->getCell('H4')->setValue('=VLOOKUP(H3, B3:E9, 4, FALSE)');
$worksheet->getCell('I4')->setValue('=VLOOKUP(I3, B3:E9, 4, FALSE)');
$worksheet->getCell('J4')->setValue('=VLOOKUP(J3, B3:E9, 4, FALSE)');
for ($column = 'H'; $column !== 'K'; ++$column) {
$cell = $worksheet->getCell("{$column}4");
$helper->log("{$column}4: {$cell->getValue()} => {$cell->getCalculatedValue()}");
}

View File

@ -1,304 +1,94 @@
<?php <?php
function orderGrid()
{
return [
['Order ID', 10247, 10249, 10250, 10251, 10252, 10253],
['Unit Price', 14.00, 18.60, 7.70, 16.80, 16.80, 64.80],
['Quantity', 12, 9, 10, 6, 20, 40],
];
}
function partsGrid()
{
return [
['Axles', 'Bearings', 'Bolts'],
[4, 4, 9],
[5, 7, 10],
[6, 8, 11],
];
}
return [ return [
[ [
16.800000000000001, 16.80,
10251, 10251,
[ orderGrid(),
[
'Order ID',
10247,
10249,
10250,
10251,
10252,
10253,
],
[
'Unit Price',
14.0,
18.600000000000001,
7.7000000000000002,
16.800000000000001,
16.800000000000001,
64.799999999999997,
],
[
'Quantity',
12,
9,
10,
6,
20,
40,
],
],
2, 2,
false, false,
], ],
[ [
6.0, 6.0,
10251, 10251,
[ orderGrid(),
[
'Order ID',
10247,
10249,
10250,
10251,
10252,
10253,
],
[
'Unit Price',
14.0,
18.600000000000001,
7.7000000000000002,
16.800000000000001,
16.800000000000001,
64.799999999999997,
],
[
'Quantity',
12,
9,
10,
6,
20,
40,
],
],
3, 3,
false, false,
], ],
[ [
'#N/A', '#N/A',
10248, 10248,
[ orderGrid(),
[
'Order ID',
10247,
10249,
10250,
10251,
10252,
10253,
],
[
'Unit Price',
14.0,
18.600000000000001,
7.7000000000000002,
16.800000000000001,
16.800000000000001,
64.799999999999997,
],
[
'Quantity',
12,
9,
10,
6,
20,
40,
],
],
2, 2,
false, false,
], ],
[ [
14.0, 14.0,
10248, 10248,
[ orderGrid(),
[
'Order ID',
10247,
10249,
10250,
10251,
10252,
10253,
],
[
'Unit Price',
14.0,
18.600000000000001,
7.7000000000000002,
16.800000000000001,
16.800000000000001,
64.799999999999997,
],
[
'Quantity',
12,
9,
10,
6,
20,
40,
],
],
2, 2,
true, true,
], ],
[ [
4, 4,
'Axles', 'Axles',
[ partsGrid(),
[
'Axles',
'Bearings',
'Bolts',
],
[
4,
4,
9,
],
[
5,
7,
10,
],
[
6,
8,
11,
],
],
2, 2,
true, true,
], ],
[ [
7, 7,
'Bearings', 'Bearings',
[ partsGrid(),
[
'Axles',
'Bearings',
'Bolts',
],
[
4,
4,
9,
],
[
5,
7,
10,
],
[
6,
8,
11,
],
],
3, 3,
false, false,
], ],
[ [
5, 5,
'B', 'B',
[ partsGrid(),
[
'Axles',
'Bearings',
'Bolts',
],
[
4,
4,
9,
],
[
5,
7,
10,
],
[
6,
8,
11,
],
],
3, 3,
true, true,
], ],
[ [
5, 5,
'B', 'B',
[ partsGrid(),
[
'Axles',
'Bearings',
'Bolts',
],
[
4,
4,
9,
],
[
5,
7,
10,
],
[
6,
8,
11,
],
],
3, 3,
null, null,
], ],
[ [
11, 11,
'Bolts', 'Bolts',
[ partsGrid(),
[
'Axles',
'Bearings',
'Bolts',
],
[
4,
4,
9,
],
[
5,
7,
10,
],
[
6,
8,
11,
],
],
4, 4,
], ],
[ [
'c', 'c',
3, 3,
[ [
[ [1, 2, 3],
1, ['a', 'b', 'c'],
2, ['d', 'e', 'f'],
3,
],
[
'a',
'b',
'c',
],
[
'd',
'e',
'f',
],
], ],
2, 2,
true, true,
@ -307,11 +97,7 @@ return [
3, 3,
3, 3,
[ [
[ [1, 2, 3],
1,
2,
3,
],
], ],
1, 1,
true, true,

View File

@ -1,293 +1,54 @@
<?php <?php
function densityGrid()
{
return [
['Density', 'Viscosity', 'Temperature'],
[0.457, 3.55, 500],
[0.525, 3.25, 400],
[0.616, 2.93, 300],
[0.675, 2.75, 250],
[0.746, 2.57, 200],
[0.835, 2.38, 150],
[0.946, 2.17, 100],
[1.090, 1.95, 50],
[1.290, 1.71, 0],
];
}
return [ return [
[ [
'#N/A', '#N/A',
1, 1,
[ densityGrid(),
[
'Density',
'Viscosity',
'Temperature',
],
[
0.45700000000000002,
3.5499999999999998,
500,
],
[
0.52500000000000002,
3.25,
400,
],
[
0.61599999999999999,
2.9300000000000002,
300,
],
[
0.67500000000000004,
2.75,
250,
],
[
0.746,
2.5699999999999998,
200,
],
[
0.83499999999999996,
2.3799999999999999,
150,
],
[
0.94599999999999995,
2.1699999999999999,
100,
],
[
1.0900000000000001,
1.95,
50,
],
[
1.29,
1.71,
0,
],
],
2, 2,
false, false,
], ],
[ [
100, 100,
1, 1,
[ densityGrid(),
[
'Density',
'Viscosity',
'Temperature',
],
[
0.45700000000000002,
3.5499999999999998,
500,
],
[
0.52500000000000002,
3.25,
400,
],
[
0.61599999999999999,
2.9300000000000002,
300,
],
[
0.67500000000000004,
2.75,
250,
],
[
0.746,
2.5699999999999998,
200,
],
[
0.83499999999999996,
2.3799999999999999,
150,
],
[
0.94599999999999995,
2.1699999999999999,
100,
],
[
1.0900000000000001,
1.95,
50,
],
[
1.29,
1.71,
0,
],
],
3, 3,
true, true,
], ],
[ [
'#N/A', '#N/A',
0.69999999999999996, 0.70,
[ densityGrid(),
[
'Density',
'Viscosity',
'Temperature',
],
[
0.45700000000000002,
3.5499999999999998,
500,
],
[
0.52500000000000002,
3.25,
400,
],
[
0.61599999999999999,
2.9300000000000002,
300,
],
[
0.67500000000000004,
2.75,
250,
],
[
0.746,
2.5699999999999998,
200,
],
[
0.83499999999999996,
2.3799999999999999,
150,
],
[
0.94599999999999995,
2.1699999999999999,
100,
],
[
1.0900000000000001,
1.95,
50,
],
[
1.29,
1.71,
0,
],
],
3, 3,
false, false,
], ],
[ [
'#N/A', '#N/A',
0.10000000000000001, 0.100,
[ densityGrid(),
[
'Density',
'Viscosity',
'Temperature',
],
[
0.45700000000000002,
3.5499999999999998,
500,
],
[
0.52500000000000002,
3.25,
400,
],
[
0.61599999999999999,
2.9300000000000002,
300,
],
[
0.67500000000000004,
2.75,
250,
],
[
0.746,
2.5699999999999998,
200,
],
[
0.83499999999999996,
2.3799999999999999,
150,
],
[
0.94599999999999995,
2.1699999999999999,
100,
],
[
1.0900000000000001,
1.95,
50,
],
[
1.29,
1.71,
0,
],
],
2, 2,
true, true,
], ],
[ [
1.71, 1.71,
2, 2,
[ densityGrid(),
[
'Density',
'Viscosity',
'Temperature',
],
[
0.45700000000000002,
3.5499999999999998,
500,
],
[
0.52500000000000002,
3.25,
400,
],
[
0.61599999999999999,
2.9300000000000002,
300,
],
[
0.67500000000000004,
2.75,
250,
],
[
0.746,
2.5699999999999998,
200,
],
[
0.83499999999999996,
2.3799999999999999,
150,
],
[
0.94599999999999995,
2.1699999999999999,
100,
],
[
1.0900000000000001,
1.95,
50,
],
[
1.29,
1.71,
0,
],
],
2, 2,
true, true,
], ],