forked from PHPOffice/PhpSpreadsheet
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix for Issue 2158 (AverageIf Calculation Problem)
Issue PHPOffice#2158 reports an error calculating AverageIf because a function returns null rather than a string. There turn out to be several components to this problem: - The nominal fix to the problem is to add some null-to-nullstring coercion in DatabaseAbstract. - This fixes the error, but does not necessarily lead to the correct result because buildQuery treats values of null and null-string identically, whereas Excel does not. So change that to treat null-string as any other string. - But that doesn't lead to the correct result either. That's because Functions/ifCondition recognizes a null string, but then continues to (over-)process it until it returns the wrong result. Fix this problem in conjunction with the other two, and we finally get the correct result. A new unit test is added for AVERAGEIF, and new test cases are added for SUMIF. In each case, there are complementary tests for conditions of null and null-string, and the results agree with Excel. There may or may not be value in adding new tests to other functions, and I will be glad to do so for any functions which you care to identify, but no existing tests broke as a result of these changes.
- Loading branch information
Showing
5 changed files
with
63 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
tests/PhpSpreadsheetTests/Calculation/Functions/Statistical/AverageIf2Test.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Statistical; | ||
|
||
use PhpOffice\PhpSpreadsheet\Spreadsheet; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class AverageIf2Test extends TestCase | ||
{ | ||
public function testAVERAGEIF(): void | ||
{ | ||
$table = [ | ||
['n', 1], | ||
['', 2], | ||
['y', 3], | ||
['', 4], | ||
['n', 5], | ||
['n', 6], | ||
['n', 7], | ||
['', 8], | ||
[null, 9], | ||
]; | ||
|
||
$spreadsheet = new Spreadsheet(); | ||
$sheet = $spreadsheet->getActiveSheet(); | ||
$sheet->fromArray($table, null, 'A1', true); | ||
$sheet->getCell('C1')->setValue('=AVERAGEIF(A:A,"",B:B)'); | ||
$sheet->getCell('C2')->setValue('=AVERAGEIF(A:A,,B:B)'); | ||
$sheet->getCell('C3')->setValue('=AVERAGEIF(A:A,"n",B:B)'); | ||
$sheet->getCell('C4')->setValue('=AVERAGEIF(A:A,"y",B:B)'); | ||
$sheet->getCell('C5')->setValue('=AVERAGEIF(A:A,"x",B:B)'); | ||
|
||
self::assertEqualsWithDelta(5.75, $sheet->getCell('C1')->getCalculatedValue(), 1E-12); | ||
self::assertEquals('#DIV/0!', $sheet->getCell('C2')->getCalculatedValue()); | ||
self::assertEqualsWithDelta(4.75, $sheet->getCell('C3')->getCalculatedValue(), 1E-12); | ||
self::assertEqualsWithDelta(3, $sheet->getCell('C4')->getCalculatedValue(), 1E-12); | ||
self::assertEquals('#DIV/0!', $sheet->getCell('C5')->getCalculatedValue()); | ||
$spreadsheet->disconnectWorksheets(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters