diff --git a/src/PhpSpreadsheet/Calculation/MathTrig.php b/src/PhpSpreadsheet/Calculation/MathTrig.php index 474b2cdd..b6bd01cb 100644 --- a/src/PhpSpreadsheet/Calculation/MathTrig.php +++ b/src/PhpSpreadsheet/Calculation/MathTrig.php @@ -1073,6 +1073,7 @@ class MathTrig $args, function ($index) use ($cellReference) { list(, $row, $column) = explode('.', $index); + return $cellReference->getWorksheet()->getRowDimension($row)->getVisible() && $cellReference->getWorksheet()->getColumnDimension($column)->getVisible(); }, @@ -1090,8 +1091,10 @@ class MathTrig //take this cell out if it contains the SUBTOTAL formula $isFormula = $cellReference->getWorksheet()->getCell($column . $row)->isFormula(); $cellFormula = !preg_match('/^=.*\bSUBTOTAL\s*\(/', strtoupper($cellReference->getWorksheet()->getCell($column . $row)->getValue())); + return !$isFormula || $cellFormula; } + return true; }, ARRAY_FILTER_USE_KEY diff --git a/tests/PhpSpreadsheetTests/Calculation/MathTrigTest.php b/tests/PhpSpreadsheetTests/Calculation/MathTrigTest.php index 0447ffcb..dbe40664 100644 --- a/tests/PhpSpreadsheetTests/Calculation/MathTrigTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/MathTrigTest.php @@ -5,10 +5,10 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation; use PhpOffice\PhpSpreadsheet\Calculation\Calculation; use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PhpOffice\PhpSpreadsheet\Calculation\MathTrig; -use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; -use PhpOffice\PhpSpreadsheet\Worksheet\RowDimension; -use PhpOffice\PhpSpreadsheet\Worksheet\ColumnDimension; use PhpOffice\PhpSpreadsheet\Cell\Cell; +use PhpOffice\PhpSpreadsheet\Worksheet\ColumnDimension; +use PhpOffice\PhpSpreadsheet\Worksheet\RowDimension; +use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PHPUnit\Framework\TestCase; class MathTrigTest extends TestCase @@ -542,8 +542,12 @@ class MathTrigTest extends TestCase return require 'data/Calculation/MathTrig/SUBTOTAL.php'; } - protected function rowVisibility() { - yield from [1 => false, 2 => true, 3 => false, 4 => true, 5 => false, 6 => false, 7 => false, 8 => true, 9 => false, 10 => true, 11 =>true]; + protected function rowVisibility() + { + $data = [1 => false, 2 => true, 3 => false, 4 => true, 5 => false, 6 => false, 7 => false, 8 => true, 9 => false, 10 => true, 11 => true]; + foreach ($data as $k => $v) { + yield $k => $v; + } } /** @@ -560,7 +564,12 @@ class MathTrigTest extends TestCase ->disableOriginalConstructor() ->getMock(); $rowDimension->method('getVisible') - ->will($this->returnCallback(function() use ($visibilityGenerator) { $result = $visibilityGenerator->current(); $visibilityGenerator->next(); return $result; })); + ->will($this->returnCallback(function () use ($visibilityGenerator) { + $result = $visibilityGenerator->current(); + $visibilityGenerator->next(); + + return $result; + })); $columnDimension = $this->getMockBuilder(ColumnDimension::class) ->setMethods(['getVisible']) ->disableOriginalConstructor() @@ -602,12 +611,16 @@ class MathTrigTest extends TestCase return require 'data/Calculation/MathTrig/SUBTOTALHIDDEN.php'; } - protected function cellValues($cellValues) { - yield from $cellValues; + protected function cellValues(array $cellValues) + { + foreach ($cellValues as $k => $v) { + yield $k => $v; + } } - protected function cellIsFormula($cellValues) { - foreach($cellValues as $cellValue) { + protected function cellIsFormula(array $cellValues) + { + foreach ($cellValues as $cellValue) { yield $cellValue[0] === '='; } } @@ -627,9 +640,19 @@ class MathTrigTest extends TestCase ->disableOriginalConstructor() ->getMock(); $cell->method('getValue') - ->will($this->returnCallback(function() use ($cellValueGenerator) { $result = $cellValueGenerator->current(); $cellValueGenerator->next(); return $result; })); + ->will($this->returnCallback(function () use ($cellValueGenerator) { + $result = $cellValueGenerator->current(); + $cellValueGenerator->next(); + + return $result; + })); $cell->method('isFormula') - ->will($this->returnCallback(function() use ($cellIsFormulaGenerator) { $result = $cellIsFormulaGenerator->current(); $cellIsFormulaGenerator->next(); return $result; })); + ->will($this->returnCallback(function () use ($cellIsFormulaGenerator) { + $result = $cellIsFormulaGenerator->current(); + $cellIsFormulaGenerator->next(); + + return $result; + })); $worksheet = $this->getMockBuilder(Worksheet::class) ->setMethods(['cellExists', 'getCell']) ->disableOriginalConstructor() diff --git a/tests/data/Calculation/MathTrig/SUBTOTAL.php b/tests/data/Calculation/MathTrig/SUBTOTAL.php index 419c71c5..97ee3e15 100644 --- a/tests/data/Calculation/MathTrig/SUBTOTAL.php +++ b/tests/data/Calculation/MathTrig/SUBTOTAL.php @@ -12,7 +12,7 @@ $baseTestData = [ 9 => ['A' => 21], 10 => ['A' => 34], 11 => ['A' => 55], - 12 => ['A' => 89] + 12 => ['A' => 89], ]; return [ diff --git a/tests/data/Calculation/MathTrig/SUBTOTALHIDDEN.php b/tests/data/Calculation/MathTrig/SUBTOTALHIDDEN.php index e70b81d5..001531f8 100644 --- a/tests/data/Calculation/MathTrig/SUBTOTALHIDDEN.php +++ b/tests/data/Calculation/MathTrig/SUBTOTALHIDDEN.php @@ -12,7 +12,7 @@ $baseTestData = [ 9 => ['A' => 21], 10 => ['A' => 34], 11 => ['A' => 55], - 12 => ['A' => 89] + 12 => ['A' => 89], ]; return [