mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-09 01:26:51 +00:00
Merge pull request #4144 from oleibman/issue1402
VLOOKUP Handling of Strings and Number
This commit is contained in:
@@ -88,8 +88,8 @@ class VLookup extends LookupBase
|
||||
|
||||
$rowNumber = null;
|
||||
foreach ($lookupArray as $rowKey => $rowData) {
|
||||
$bothNumeric = is_numeric($lookupValue) && is_numeric($rowData[$column]);
|
||||
$bothNotNumeric = !is_numeric($lookupValue) && !is_numeric($rowData[$column]);
|
||||
$bothNumeric = self::numeric($lookupValue) && self::numeric($rowData[$column]);
|
||||
$bothNotNumeric = !self::numeric($lookupValue) && !self::numeric($rowData[$column]);
|
||||
$cellDataLower = StringHelper::strToLower((string) $rowData[$column]);
|
||||
|
||||
// break if we have passed possible keys
|
||||
@@ -114,4 +114,9 @@ class VLookup extends LookupBase
|
||||
|
||||
return $rowNumber;
|
||||
}
|
||||
|
||||
private static function numeric(mixed $value): bool
|
||||
{
|
||||
return is_int($value) || is_float($value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
|
||||
use PhpOffice\PhpSpreadsheet\Cell\DataType;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
@@ -78,4 +79,24 @@ class VLookupTest extends TestCase
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function testIssue1402(): void
|
||||
{
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$worksheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
$worksheet->setCellValueExplicit('A1', 1, DataType::TYPE_STRING);
|
||||
$worksheet->setCellValue('B1', 'Text Nr 1');
|
||||
$worksheet->setCellValue('A2', 2);
|
||||
$worksheet->setCellValue('B2', 'Numeric result');
|
||||
$worksheet->setCellValueExplicit('A3', 2, DataType::TYPE_STRING);
|
||||
$worksheet->setCellValue('B3', 'Text Nr 2');
|
||||
$worksheet->setCellValueExplicit('A4', 2, DataType::TYPE_STRING);
|
||||
$worksheet->setCellValue('B4', '=VLOOKUP(A4,$A$1:$B$3,2,0)');
|
||||
self::assertSame('Text Nr 2', $worksheet->getCell('B4')->getCalculatedValue());
|
||||
$worksheet->setCellValue('A5', 2);
|
||||
$worksheet->setCellValue('B5', '=VLOOKUP(A5,$A$1:$B$3,2,0)');
|
||||
self::assertSame('Numeric result', $worksheet->getCell('B5')->getCalculatedValue());
|
||||
$spreadsheet->disconnectWorksheets();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user