Skip to content

Commit

Permalink
Merge pull request #3868 from oleibman/issue3866
Browse files Browse the repository at this point in the history
Excel Inconsistent Handling of Empty Argument MIN/MAX/MINA/MAXA
  • Loading branch information
oleibman authored Jan 21, 2024
2 parents d50b8b5 + b5e3ca3 commit 0824fd5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
- Do not include unparsed drawings when new drawing added. [Issue #3861](https://github.com/PHPOffice/PhpSpreadsheet/issues/3861) [PR #3862](https://github.com/PHPOffice/PhpSpreadsheet/pull/3862)
- Excel omits `between` operator for data validation. [Issue #3863](https://github.com/PHPOffice/PhpSpreadsheet/issues/3863) [PR #3865](https://github.com/PHPOffice/PhpSpreadsheet/pull/3865)
- Use less space when inserting rows and columns. [Issue #3687](https://github.com/PHPOffice/PhpSpreadsheet/issues/3687) [PR #3856](https://github.com/PHPOffice/PhpSpreadsheet/pull/3856)
- Excel inconsistent handling of MIN/MAX/MINA/MAXA. [Issue #3866](https://github.com/PHPOffice/PhpSpreadsheet/issues/3866) [PR #3868](https://github.com/PHPOffice/PhpSpreadsheet/pull/3868)

## 1.29.0 - 2023-06-15

Expand Down
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 0824fd5

Please sign in to comment.