Skip to content

Commit db6e6eb

Browse files
committed
3rd Parameter for AVERAGEIF/SUMIF Can Also Be #REF!
1 parent 990e3ec commit db6e6eb

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

src/PhpSpreadsheet/Calculation/Statistical/Conditional.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ class Conditional
3232
public static function AVERAGEIF(mixed $range, null|array|string $condition, mixed $averageRange = []): null|int|float|string
3333
{
3434
if (!is_array($range) || !is_array($averageRange) || array_key_exists(0, $range) || array_key_exists(0, $averageRange)) {
35-
if ($range === ExcelError::REF()) {
36-
return $range;
35+
$refError = ExcelError::REF();
36+
if (in_array($refError, [$range, $averageRange], true)) {
37+
return $refError;
3738
}
3839

3940
throw new CalcException('Must specify range of cells, not any kind of literal');
@@ -185,15 +186,19 @@ public static function MINIFS(mixed ...$args): null|float|string
185186
* SUMIF(range, criteria, [sum_range])
186187
*
187188
* @param mixed $range Data values, expecting array
189+
* @param mixed $sumRange Data values, expecting array
188190
*/
189-
public static function SUMIF(mixed $range, mixed $condition, array $sumRange = []): null|float|string
191+
public static function SUMIF(mixed $range, mixed $condition, mixed $sumRange = []): null|float|string
190192
{
191193
if (
192194
!is_array($range)
193195
|| array_key_exists(0, $range)
196+
|| !is_array($sumRange)
197+
|| array_key_exists(0, $sumRange)
194198
) {
195-
if ($range === ExcelError::REF()) {
196-
return $range;
199+
$refError = ExcelError::REF();
200+
if (in_array($refError, [$range, $sumRange], true)) {
201+
return $refError;
197202
}
198203

199204
throw new CalcException('Must specify range of cells, not any kind of literal');

tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SumIfTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,7 @@ public function testOutliers(): void
5454

5555
$sheet->getCell('A4')->setValue('=SUMIF(#REF!,"<32")');
5656
self::assertSame('#REF!', $sheet->getCell('A4')->getCalculatedValue());
57+
$sheet->getCell('A5')->setValue('=SUMIF(D1:D4, 1, #REF!)');
58+
self::assertSame('#REF!', $sheet->getCell('A5')->getCalculatedValue());
5759
}
5860
}

tests/PhpSpreadsheetTests/Calculation/Functions/Statistical/AverageIfTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Statistical;
66

77
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcException;
8+
use PHPUnit\Framework\Attributes\DataProvider;
89

910
class AverageIfTest extends AllSetupTeardown
1011
{
11-
#[\PHPUnit\Framework\Attributes\DataProvider('providerAVERAGEIF')]
12+
#[DataProvider('providerAVERAGEIF')]
1213
public function testAVERAGEIF(mixed $expectedResult, mixed ...$args): void
1314
{
1415
$this->runTestCaseNoBracket('AVERAGEIF', $expectedResult, ...$args);
@@ -44,5 +45,8 @@ public function testOutliers(): void
4445

4546
$sheet->getCell('A4')->setValue('=AVERAGEIF(#REF!,1)');
4647
self::assertSame('#REF!', $sheet->getCell('A4')->getCalculatedValue());
48+
49+
$sheet->getCell('A5')->setValue('=AVERAGEIF(D1:D4, 1, #REF!)');
50+
self::assertSame('#REF!', $sheet->getCell('A5')->getCalculatedValue());
4751
}
4852
}

0 commit comments

Comments
 (0)