diff --git a/changelog.txt b/changelog.txt index 14dae427..aa93fbd4 100644 --- a/changelog.txt +++ b/changelog.txt @@ -28,6 +28,7 @@ Planned for 1.9 - Bugfix: (ncrypthic) Work Item GH-570 - Ignore inlineStr type if formula element exists - Bugfix: (MBaker) Work Item GH-554 - Whitespace after toRichTextObject() - General: (umpirsky) Work Item GH-548 - Optimize vlookup() sort +- Feature: (MBaker) - Initial implementation of SUMIFS() function 2015-04-30 (v1.8.1): - Bugfix: (goncons) Work Item GH-397 - Fix for Writing an Open Document cell with non-numeric formula @@ -68,7 +69,7 @@ Planned for 1.9 Reference CVE-2015-3542 - Identification of problem courtesy of Dawid Golunski (Pentest Ltd.) - 2014-03-02 (v1.8.0): +2014-03-02 (v1.8.0): - Bugfix: (MBaker) Work item CP19830 - Undefined variable: fileHandle in CSV Reader - Bugfix: (MBaker) Work item CP19968 - Out of memory in style/supervisor.php - Bugfix: (MBaker) - Style error with merged cells in PDF Writer diff --git a/src/PhpSpreadsheet/Calculation.php b/src/PhpSpreadsheet/Calculation.php index 8b98d37f..269fabcf 100644 --- a/src/PhpSpreadsheet/Calculation.php +++ b/src/PhpSpreadsheet/Calculation.php @@ -1807,8 +1807,8 @@ class Calculation ), 'SUMIFS' => array( 'category' => Calculation\Categories::CATEGORY_MATH_AND_TRIG, - 'functionCall' => 'Calculation\Categories::DUMMY', - 'argumentCount' => '?' + 'functionCall' => '\\PHPExcel\\Calculation\\MathTrig::SUMIFS', + 'argumentCount' => '3+' ), 'SUMPRODUCT' => array( 'category' => Calculation\Categories::CATEGORY_MATH_AND_TRIG, diff --git a/src/PhpSpreadsheet/Calculation/MathTrig.php b/src/PhpSpreadsheet/Calculation/MathTrig.php index 85e3d69e..45a32da0 100644 --- a/src/PhpSpreadsheet/Calculation/MathTrig.php +++ b/src/PhpSpreadsheet/Calculation/MathTrig.php @@ -1212,6 +1212,53 @@ class MathTrig return $returnValue; } + /** + * SUMIFS + * + * Counts the number of cells that contain numbers within the list of arguments + * + * Excel Function: + * SUMIFS(value1[,value2[, ...]],condition) + * + * @access public + * @category Mathematical and Trigonometric Functions + * @param mixed $arg,... Data values + * @param string $condition The criteria that defines which cells will be summed. + * @return float + */ + public static function SUMIFS() { + $arrayList = func_get_args(); + + // Return value + $returnValue = 0; + + $sumArgs = Functions::flattenArray(array_shift($arrayList)); + + while (count($arrayList) > 0) { + $aArgsArray[] = Functions::flattenArray(array_shift($arrayList)); + $conditions[] = Functions::ifCondition(array_shift($arrayList)); + } + + // Loop through each set of arguments and conditions + foreach ($conditions as $index => $condition) { + $aArgs = $aArgsArray[$index]; + + // Loop through arguments + foreach ($aArgs as $key => $arg) { + if (!is_numeric($arg)) { + $arg = \PHPExcel\Calculation::wrapResult(strtoupper($arg)); + } + $testCondition = '='.$arg.$condition; + if (\PHPExcel\Calculation::getInstance()->_calculateFormulaValue($testCondition)) { + // Is it a value within our criteria + $returnValue += $sumArgs[$key]; + } + } + } + + // Return + return $returnValue; + } /** * SUMPRODUCT