SUBTOTAL within a SUBTOAL range should be ignored

This commit is contained in:
MarkBaker 2018-04-21 01:33:26 +01:00
parent 04b4e74ec7
commit 36afa01d33
3 changed files with 59 additions and 4 deletions

View File

@ -1088,7 +1088,7 @@ class MathTrig
list(, $row, $column) = explode('.', $index); list(, $row, $column) = explode('.', $index);
if ($cellReference->getWorksheet()->cellExists($column . $row)) { if ($cellReference->getWorksheet()->cellExists($column . $row)) {
//take this cell out if it contains the SUBTOTAL formula //take this cell out if it contains the SUBTOTAL formula
return strpos(strtoupper($cellReference->getWorksheet()->getCell($column . $row)->getValue()), '=SUBTOTAL(') === false; return !preg_match('/=.*\b(SUBTOTAL)\s*\(/', strtoupper($cellReference->getWorksheet()->getCell($column . $row)->getValue()));
} }
return true; return true;
}, },

View File

@ -547,11 +547,11 @@ class MathTrigTest extends TestCase
} }
/** /**
* @dataProvider providerSUBTOTALHIDDEN * @dataProvider providerHiddenSUBTOTAL
* *
* @param mixed $expectedResult * @param mixed $expectedResult
*/ */
public function testSUBTOTALHIDDEN($expectedResult, ...$args) public function testHiddenSUBTOTAL($expectedResult, ...$args)
{ {
$generator = \PhpOffice\PhpSpreadsheetTests\Calculation\MathTrigTest::rowVisibility(); $generator = \PhpOffice\PhpSpreadsheetTests\Calculation\MathTrigTest::rowVisibility();
$rowDimension = $this->getMockBuilder(RowDimension::class) $rowDimension = $this->getMockBuilder(RowDimension::class)
@ -596,8 +596,47 @@ class MathTrigTest extends TestCase
self::assertEquals($expectedResult, $result, null, 1E-12); self::assertEquals($expectedResult, $result, null, 1E-12);
} }
public function providerSUBTOTALHIDDEN() public function providerHiddenSUBTOTAL()
{ {
return require 'data/Calculation/MathTrig/SUBTOTALHIDDEN.php'; return require 'data/Calculation/MathTrig/SUBTOTALHIDDEN.php';
} }
/**
* @dataProvider providerNestedSUBTOTAL
*
* @param mixed $expectedResult
*/
public function testNestedSUBTOTAL($expectedResult, ...$args)
{
$cell = $this->getMockBuilder(Cell::class)
->setMethods(['getValue'])
->disableOriginalConstructor()
->getMock();
$cell->method('getValue')
->willReturn(null);
$worksheet = $this->getMockBuilder(Worksheet::class)
->setMethods(['cellExists', 'getCell'])
->disableOriginalConstructor()
->getMock();
$worksheet->method('cellExists')
->willReturn(true);
$worksheet->method('getCell')
->willReturn($cell);
$cellReference = $this->getMockBuilder(Cell::class)
->setMethods(['getWorksheet'])
->disableOriginalConstructor()
->getMock();
$cellReference->method('getWorksheet')
->willReturn($worksheet);
array_push($args, $cellReference);
$result = MathTrig::SUBTOTAL(...$args);
self::assertEquals($expectedResult, $result, null, 1E-12);
}
public function providerNestedSUBTOTAL()
{
return require 'data/Calculation/MathTrig/SUBTOTALNESTED.php';
}
} }

View File

@ -0,0 +1,16 @@
<?php
$baseTestData = [
1 => ['A' => 1],
2 => ['A' => 1],
3 => ['A' => '=SUBTOTAL(1, A1:A2)'],
4 => ['A' => '=ROMAN(SUBTOTAL(1, A1:A2))']
];
return [
[
2,
2,
$baseTestData,
],
];