From 7f0ca404fc035603ff1607d8811b1bb408c65102 Mon Sep 17 00:00:00 2001 From: Jonathan Goode Date: Sun, 7 Aug 2022 01:28:26 +0100 Subject: [PATCH] Ensure multiplication is performed on a non-array value (#2964) * Ensure multiplication is performed on a non-array value * Simplify formula Numbers should be numbers * Provide test coverage for SUM combined with INDEX/MATCH * PHPStan --- phpstan-baseline.neon | 2 +- src/PhpSpreadsheet/Shared/JAMA/Matrix.php | 4 +++ .../Functions/MathTrig/SumTest.php | 25 +++++++++++++++++++ .../Functions/TextData/ConcatenateTest.php | 4 +-- 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 49ab167e..d6e95eae 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -2377,7 +2377,7 @@ parameters: - message: "#^Result of && is always false\\.$#" - count: 10 + count: 11 path: src/PhpSpreadsheet/Shared/JAMA/Matrix.php - diff --git a/src/PhpSpreadsheet/Shared/JAMA/Matrix.php b/src/PhpSpreadsheet/Shared/JAMA/Matrix.php index ab78ef18..8aab07e3 100644 --- a/src/PhpSpreadsheet/Shared/JAMA/Matrix.php +++ b/src/PhpSpreadsheet/Shared/JAMA/Matrix.php @@ -3,6 +3,7 @@ namespace PhpOffice\PhpSpreadsheet\Shared\JAMA; use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalculationException; +use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; use PhpOffice\PhpSpreadsheet\Shared\StringHelper; @@ -742,6 +743,9 @@ class Matrix $value = trim($value, '"'); $validValues &= StringHelper::convertToNumberIfFraction($value); } + if (!is_numeric($value) && is_array($value)) { + $value = Functions::flattenArray($value)[0]; + } if ($validValues) { $this->A[$i][$j] *= $value; } else { diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SumTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SumTest.php index 3f80b8ec..738c203e 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SumTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SumTest.php @@ -2,6 +2,8 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig; +use PhpOffice\PhpSpreadsheet\Spreadsheet; + class SumTest extends AllSetupTeardown { /** @@ -44,4 +46,27 @@ class SumTest extends AllSetupTeardown { return require 'tests/data/Calculation/MathTrig/SUMLITERALS.php'; } + + public function testSumWithIndexMatch(): void + { + $spreadsheet = new Spreadsheet(); + $sheet1 = $spreadsheet->getActiveSheet(); + $sheet1->setTitle('Formula'); + $sheet1->fromArray( + [ + ['Number', 'Formula'], + [83, '=SUM(4 * INDEX(Lookup!B2, MATCH(A2, Lookup!A2, 0)))'], + ] + ); + $sheet2 = $spreadsheet->createSheet(); + $sheet2->setTitle('Lookup'); + $sheet2->fromArray( + [ + ['Lookup', 'Match'], + [83, 16], + ] + ); + self::assertSame(64, $sheet1->getCell('B2')->getCalculatedValue()); + $spreadsheet->disconnectWorksheets(); + } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/ConcatenateTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/ConcatenateTest.php index 31fb94fa..53ce2435 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/ConcatenateTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/ConcatenateTest.php @@ -41,7 +41,7 @@ class ConcatenateTest extends AllSetupTeardown $sheet1->fromArray( [ ['Number', 'Formula'], - ['52101293', '=CONCAT(INDEX(Lookup!$B$2, MATCH($A2, Lookup!$A$2, 0)))'], + [52101293, '=CONCAT(INDEX(Lookup!B2, MATCH(A2, Lookup!A2, 0)))'], ] ); $sheet2 = $spreadsheet->createSheet(); @@ -49,7 +49,7 @@ class ConcatenateTest extends AllSetupTeardown $sheet2->fromArray( [ ['Lookup', 'Match'], - ['52101293', 'PHP'], + [52101293, 'PHP'], ] ); self::assertSame('PHP', $sheet1->getCell('B2')->getCalculatedValue());