From df34b15079cedaed206b5628acceffaa51a44bac Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Fri, 13 Apr 2018 12:24:54 +0100 Subject: [PATCH] Apply filter check on cells that already contain a subtotal calculation... will add unit tests later --- src/PhpSpreadsheet/Calculation/MathTrig.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/PhpSpreadsheet/Calculation/MathTrig.php b/src/PhpSpreadsheet/Calculation/MathTrig.php index 46e9f1aa..86598f87 100644 --- a/src/PhpSpreadsheet/Calculation/MathTrig.php +++ b/src/PhpSpreadsheet/Calculation/MathTrig.php @@ -1081,6 +1081,21 @@ class MathTrig ); } + protected static function filterFormulaArgs($cellReference, $args) + { + return array_filter( + $args, + function ($index) use ($cellReference) { + list(, $row, $column) = explode('.', $index); + + //take this cell out if it contains the SUBTOTAL formula + return strpos(strtoupper($cellReference->getWorksheet()->getCell($column . $row)->getValue()), '=SUBTOTAL(') === false; + + }, + ARRAY_FILTER_USE_KEY + ); + } + /** * SUBTOTAL. * @@ -1105,6 +1120,7 @@ class MathTrig $subtotal = $subtotal - 100; } + $aArgs = self::filterFormulaArgs($cellReference, $aArgs); switch ($subtotal) { case 1: return Statistical::AVERAGE($aArgs);