Skip to content

Commit cda404d

Browse files
committed
Handling Unions as Function Arguments
Fix PHPOffice#4656. (Also fix PHPOffice#503, which went stale years ago, and which I reopened, and which I re-closed in a state of confusion.) Continuing the work of PR PHPOffice#4596. Calculation engine was unable to parse a formula which used union arguments. (I find it very difficult to parse as well.) This PR will, I hope, fix that. More tests are needed.
1 parent 21ef57e commit cda404d

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

src/PhpSpreadsheet/Calculation/Calculation.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,16 +1236,14 @@ private function internalParseFormula(string $formula, ?Cell $cell = null): bool
12361236
// because at least the braces are paired up (at this stage in the formula)
12371237
// MS Excel allows this if the content is cell references; but doesn't allow actual values,
12381238
// but at this point, we can't differentiate (so allow both)
1239-
return $this->raiseFormulaError('Formula Error: Unexpected ,');
1240-
/* The following code may be a better choice, but, with
1241-
the other changes for this PR, I can no longer come up
1242-
with a test case that gets here
1239+
//return $this->raiseFormulaError('Formula Error: Unexpected ,');
1240+
12431241
$stack->push('Binary Operator', '');
12441242

12451243
++$index;
12461244
$expectingOperator = false;
12471245

1248-
continue;*/
1246+
continue;
12491247
}
12501248

12511249
/** @var array<string, int> $d */
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests\Calculation;
6+
7+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class Issue4656Test extends TestCase
11+
{
12+
public function testIssue4656(): void
13+
{
14+
$spreadsheet = new Spreadsheet();
15+
$sheet = $spreadsheet->getActiveSheet();
16+
$sheet->setCellValue('B1', 2);
17+
$sheet->setCellValue('C1', 1);
18+
$sheet->setCellValue('D1', 3);
19+
$sheet->setCellValue('E1', 2);
20+
$sheet->setCellValue('A1', '=RANK(B1,(C1,E1))');
21+
$sheet->setCellValue('A2', '=RANK(B1,C1:E1)');
22+
self::assertSame(1, $sheet->getCell('A1')->getCalculatedValue());
23+
self::assertSame(2, $sheet->getCell('A2')->getCalculatedValue());
24+
$spreadsheet->disconnectWorksheets();
25+
}
26+
27+
public function testIssue4656Original(): void
28+
{
29+
$spreadsheet = new Spreadsheet();
30+
$sheet = $spreadsheet->getActiveSheet();
31+
$sheet->setCellValue('B1', 1);
32+
$sheet->setCellValue('C1', 1);
33+
$sheet->setCellValue('D1', 2);
34+
$sheet->setCellValue('A1', '=RANK(B1,(C1,D1))');
35+
self::assertSame(2, $sheet->getCell('A1')->getCalculatedValue());
36+
$spreadsheet->disconnectWorksheets();
37+
}
38+
}

0 commit comments

Comments
 (0)