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

Xls Writer Parser Mis-handling TRUE/FALSE As VLOOKUP Arguments #4333

Merged
merged 2 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).

### Fixed

- Nothing yet.
- Xls writer Parser Mishandling True/False Argument. [Issue #4331](https://github.com/PHPOffice/PhpSpreadsheet/issues/4331) [PR #4333](https://github.com/PHPOffice/PhpSpreadsheet/pull/4333)

## 2025-01-26 - 3.9.0

Expand Down
4 changes: 3 additions & 1 deletion src/PhpSpreadsheet/Writer/Xls/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -1624,7 +1624,9 @@ public function toReversePolish(array $tree = []): string
}

// add its left subtree and return.
return $left_tree . $this->convertFunction($tree['value'], $tree['right']);
if ($left_tree !== '' || $tree['right'] !== '') {
return $left_tree . $this->convertFunction($tree['value'], $tree['right'] ?: 0);
}
}
$converted_tree = $this->convert($tree['value']);

Expand Down
56 changes: 56 additions & 0 deletions tests/PhpSpreadsheetTests/Writer/Xls/Issue4331Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace PhpOffice\PhpSpreadsheetTests\Writer\Xls;

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;

class Issue4331Test extends AbstractFunctional
{
public function testIssue4331(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$c3 = '=VLOOKUP(B3,$B$10:$C$13,2,FALSE)';
$d3 = '=VLOOKUP("intermediate",$B$10:$C$13,2,TRUE)';
$c4 = '=VLOOKUP(B3,$B$10:$C$13,2,FALSE())';
$d4 = '=VLOOKUP("intermediate",$B$10:$C$13,2,TRUE())';
$sheet->fromArray(
[
['level', 'result'],
['medium', $c3, $d3],
[null, $c4, $d4],
],
null,
'B2',
true
);
$sheet->fromArray(
[
['high', 6],
['low', 2],
['medium', 4],
['none', 0],
],
null,
'B10',
true
);

$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xls');
$spreadsheet->disconnectWorksheets();

$worksheet = $reloadedSpreadsheet->getActiveSheet();
self::assertSame($c3, $worksheet->getCell('C3')->getValue());
self::assertSame(4, $worksheet->getCell('C3')->getCalculatedValue());
self::assertSame($d3, $worksheet->getCell('D3')->getValue());
self::assertSame(6, $worksheet->getCell('D3')->getCalculatedValue());
self::assertSame($c4, $worksheet->getCell('C4')->getValue());
self::assertSame(4, $worksheet->getCell('C4')->getCalculatedValue());
self::assertSame($d4, $worksheet->getCell('D4')->getValue());
self::assertSame(6, $worksheet->getCell('D4')->getCalculatedValue());
$reloadedSpreadsheet->disconnectWorksheets();
}
}