Skip to content

Commit

Permalink
Excel Inconsistent Handling of Empty Argument MIN/MAX/MINA/MAXA
Browse files Browse the repository at this point in the history
Fix #3866. Excel normally treats a missing argument (e.g. `PRODUCT(2,3,)`) as null and ignores it. Not so for MIN/MAX/MINA/MAXA; for those, an empty argument is treated as zero. PhpSpreadsheet is changed to do the same.
  • Loading branch information
oleibman committed Jan 15, 2024
1 parent ae72efe commit 0d6673d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/topics/recipes.md
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ At present, the following locale settings are supported:

Language | | Locale Code
---------------------|----------------------|-------------
Bulgarian | български | bg
Czech | Ceština | cs
Danish | Dansk | da
German | Deutsch | de
Expand Down
7 changes: 6 additions & 1 deletion src/PhpSpreadsheet/Calculation/Calculation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4997,7 +4997,12 @@ private function processTokenStack(mixed $tokens, ?string $cellID = null, ?Cell
}
} else {
$emptyArguments[] = ($arg['type'] === 'Empty Argument');
$args[] = self::unwrapResult($arg['value']);
if ($arg['type'] === 'Empty Argument' && in_array($functionName, ['MIN', 'MINA', 'MAX', 'MAXA'], true)) {
$args[] = $arg['value'] = 0;
$this->debugLog->writeDebugLog('Empty Argument reevaluated as 0');
} else {
$args[] = self::unwrapResult($arg['value']);
}
if ($functionName !== 'MKMATRIX') {
$argArrayVals[] = $this->showValue($arg['value']);
}
Expand Down
40 changes: 40 additions & 0 deletions tests/PhpSpreadsheetTests/Calculation/MissingArgumentsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace PhpOffice\PhpSpreadsheetTests\Calculation;

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PHPUnit\Framework\TestCase;

class MissingArgumentsTest extends TestCase
{
/**
* @dataProvider providerMissingArguments
*/
public function testMissingArguments(mixed $expected, string $formula): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->getCell('A1')->setValue($formula);
self::assertSame($expected, $sheet->getCell('A1')->getCalculatedValue());
$spreadsheet->disconnectWorksheets();
}

public static function providerMissingArguments(): array
{
return [
'argument missing at end' => [0, '=min(3,2,)'],
'argument missing at beginning' => [0, '=mina(,3,2)'],
'argument missing in middle' => [0, '=min(,3,2)'],
'missing argument is not result' => [-2, '=min(3,-2,)'],
'max with missing argument' => [0, '=max(-3,-2,)'],
'maxa with missing argument' => [0, '=maxa(-3,-2,)'],
'max with null cell' => [-2, '=max(-3,-2,Z1)'],
'min with null cell' => [2, '=min(3,2,Z1)'],
'product ignores null argument' => [6.0, '=product(3,2,)'],
'embedded function' => [5, '=sum(3,2,min(3,2,))'],
'unaffected embedded function' => [8, '=sum(3,2,max(3,2,))'],
];
}
}

0 comments on commit 0d6673d

Please sign in to comment.