Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix HLOOKUP on single row #1512

Merged
merged 2 commits into from
Jun 19, 2020
Merged

Fix HLOOKUP on single row #1512

merged 2 commits into from
Jun 19, 2020

Conversation

arnested
Copy link
Contributor

@arnested arnested commented Jun 4, 2020

This is:

- [x] a bugfix
- [ ] a new feature

Checklist:

Why this change is needed?

Fixes a bug when doing a HLOOKUP on a single row.

<?php

require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

/**
 * Single row.
 */
$singleRow = "=HLOOKUP(10, {5, 10, 15}, 1, 0)";
$sheet->getCell('A1')->setValue($singleRow);

// Should echo 10, but echos '#N/A' and some PHP notices and warnings.
echo $sheet->getCell('A1')->getCalculatedValue() . PHP_EOL;

/**
 * Multiple rows.
 */
$multipleRows = "=HLOOKUP(10, {5, 10, 15; 20, 25, 30}, 1, 0)";
$sheet->getCell('A2')->setValue($multipleRows);

// Should echo: 10 and also does.
echo $sheet->getCell('A2')->getCalculatedValue() . PHP_EOL;

Before fix:

PHP Notice:  Undefined offset: 0 in /Users/arne/PhpSpreadsheet/src/PhpSpreadsheet/Calculation/LookupRef.php on line 815

Notice: Undefined offset: 0 in /Users/arne/PhpSpreadsheet/src/PhpSpreadsheet/Calculation/LookupRef.php on line 815
PHP Notice:  Undefined index:  in /Users/arne/PhpSpreadsheet/src/PhpSpreadsheet/Calculation/LookupRef.php on line 819

Notice: Undefined index:  in /Users/arne/PhpSpreadsheet/src/PhpSpreadsheet/Calculation/LookupRef.php on line 819
PHP Warning:  Invalid argument supplied for foreach() in /Users/arne/PhpSpreadsheet/src/PhpSpreadsheet/Calculation/LookupRef.php on line 819

Warning: Invalid argument supplied for foreach() in /Users/arne/PhpSpreadsheet/src/PhpSpreadsheet/Calculation/LookupRef.php on line 819
#N/A
10

After fix:

10
10

arnested and others added 2 commits June 13, 2020 18:54
Fixes a bug when doing a HLOOKUP on a single row.

```php
<?php

require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

/**
 * Single row.
 */
$singleRow = "=HLOOKUP(10, {5, 10, 15}, 1, 0)";
$sheet->getCell('A1')->setValue($singleRow);

// Should echo 10, but echos '#N/A' and some PHP notices and warnings.
echo $sheet->getCell('A1')->getCalculatedValue() . PHP_EOL;

/**
 * Multiple rows.
 */
$multipleRows = "=HLOOKUP(10, {5, 10, 15; 20, 25, 30}, 1, 0)";
$sheet->getCell('A2')->setValue($multipleRows);

// Should echo: 10 and also does.
echo $sheet->getCell('A2')->getCalculatedValue() . PHP_EOL;
```
@MarkBaker
Copy link
Member

Thank you for identifying and fixing a case that we hadn't considered when doing the implementation of HLOOKUP()

@MarkBaker MarkBaker merged commit a5a0268 into PHPOffice:master Jun 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants