3rd Parameter for AVERAGEIF/SUMIF Can Also Be #REF!

This commit is contained in:
oleibman
2025-02-25 20:11:23 -08:00
parent 990e3ec3a3
commit db6e6ebd72
3 changed files with 17 additions and 6 deletions
@@ -32,8 +32,9 @@ class Conditional
public static function AVERAGEIF(mixed $range, null|array|string $condition, mixed $averageRange = []): null|int|float|string
{
if (!is_array($range) || !is_array($averageRange) || array_key_exists(0, $range) || array_key_exists(0, $averageRange)) {
if ($range === ExcelError::REF()) {
return $range;
$refError = ExcelError::REF();
if (in_array($refError, [$range, $averageRange], true)) {
return $refError;
}
throw new CalcException('Must specify range of cells, not any kind of literal');
@@ -185,15 +186,19 @@ class Conditional
* SUMIF(range, criteria, [sum_range])
*
* @param mixed $range Data values, expecting array
* @param mixed $sumRange Data values, expecting array
*/
public static function SUMIF(mixed $range, mixed $condition, array $sumRange = []): null|float|string
public static function SUMIF(mixed $range, mixed $condition, mixed $sumRange = []): null|float|string
{
if (
!is_array($range)
|| array_key_exists(0, $range)
|| !is_array($sumRange)
|| array_key_exists(0, $sumRange)
) {
if ($range === ExcelError::REF()) {
return $range;
$refError = ExcelError::REF();
if (in_array($refError, [$range, $sumRange], true)) {
return $refError;
}
throw new CalcException('Must specify range of cells, not any kind of literal');
@@ -54,5 +54,7 @@ class SumIfTest extends AllSetupTeardown
$sheet->getCell('A4')->setValue('=SUMIF(#REF!,"<32")');
self::assertSame('#REF!', $sheet->getCell('A4')->getCalculatedValue());
$sheet->getCell('A5')->setValue('=SUMIF(D1:D4, 1, #REF!)');
self::assertSame('#REF!', $sheet->getCell('A5')->getCalculatedValue());
}
}
@@ -5,10 +5,11 @@ declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Statistical;
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcException;
use PHPUnit\Framework\Attributes\DataProvider;
class AverageIfTest extends AllSetupTeardown
{
#[\PHPUnit\Framework\Attributes\DataProvider('providerAVERAGEIF')]
#[DataProvider('providerAVERAGEIF')]
public function testAVERAGEIF(mixed $expectedResult, mixed ...$args): void
{
$this->runTestCaseNoBracket('AVERAGEIF', $expectedResult, ...$args);
@@ -44,5 +45,8 @@ class AverageIfTest extends AllSetupTeardown
$sheet->getCell('A4')->setValue('=AVERAGEIF(#REF!,1)');
self::assertSame('#REF!', $sheet->getCell('A4')->getCalculatedValue());
$sheet->getCell('A5')->setValue('=AVERAGEIF(D1:D4, 1, #REF!)');
self::assertSame('#REF!', $sheet->getCell('A5')->getCalculatedValue());
}
}