mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-27 06:28:32 +00:00
4bf4278a39
Fix #2934. Null is passed to StringHelper::strtolower which expects string. Same problem appears to be applicable to HLOOKUP. I noted the following problem in the code, but will document it here as well. Excel's results are not consistent when a non-numeric string is passed as the third parameter. For example, if cell Z1 contains `xyz`, Excel will return a REF error for function `VLOOKUP(whatever,whatever,Z1)`, but it returns a VALUE error for function `VLOOKUP(whatever,whatever,"xyz")`. I don't think PhpSpreadsheet can match both behaviors. For now, it will return VALUE for both, with similar results for other errors. While studying the returned errors, I realized there is something that needs to be deprecated. `ExcelError::$errorCodes` is a public static array. This means that a user can change its value, which should not be allowed. It is replaced by a constant. Since the original is public, I think it needs to stay, but with a deprecation notice; users can reference and change it, but it will be unused in the rest of the code. I suppose this might be considered a break in functionality (that should not have been allowed in the first place).
73 lines
2.2 KiB
PHP
73 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class VLookupTest extends TestCase
|
|
{
|
|
/**
|
|
* @dataProvider providerVLOOKUP
|
|
*
|
|
* @param mixed $expectedResult
|
|
* @param mixed $value
|
|
* @param mixed $table
|
|
* @param mixed $index
|
|
*/
|
|
public function testVLOOKUP($expectedResult, $value, $table, $index, ?bool $lookup = null): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
if (is_array($table)) {
|
|
$sheet->fromArray($table);
|
|
$dimension = $sheet->calculateWorksheetDimension();
|
|
} else {
|
|
$sheet->getCell('A1')->setValue($table);
|
|
$dimension = 'A1';
|
|
}
|
|
if ($lookup === null) {
|
|
$lastarg = '';
|
|
} else {
|
|
$lastarg = $lookup ? ',TRUE' : ',FALSE';
|
|
}
|
|
$sheet->getCell('Z98')->setValue($value);
|
|
$sheet->getCell('Z97')->setValue($index);
|
|
|
|
$sheet->getCell('Z99')->setValue("=VLOOKUP(Z98,$dimension,Z97$lastarg)");
|
|
$result = $sheet->getCell('Z99')->getCalculatedValue();
|
|
self::assertEquals($expectedResult, $result);
|
|
$spreadsheet->disconnectWorksheets();
|
|
}
|
|
|
|
public function providerVLOOKUP(): array
|
|
{
|
|
return require 'tests/data/Calculation/LookupRef/VLOOKUP.php';
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerVLookupArray
|
|
*/
|
|
public function testVLookupArray(array $expectedResult, string $values, string $database, string $index): void
|
|
{
|
|
$calculation = Calculation::getInstance();
|
|
|
|
$formula = "=VLOOKUP({$values}, {$database}, {$index}, false)";
|
|
$result = $calculation->_calculateFormulaValue($formula);
|
|
self::assertEquals($expectedResult, $result);
|
|
}
|
|
|
|
public function providerVLookupArray(): array
|
|
{
|
|
return [
|
|
'row vector' => [
|
|
[[4.19, 5.77, 4.14]],
|
|
'{"Orange", "Green", "Red"}',
|
|
'{"Red", 4.14; "Orange", 4.19; "Yellow", 5.17; "Green", 5.77; "Blue", 6.39}',
|
|
'2',
|
|
],
|
|
];
|
|
}
|
|
}
|