Skip to content

Commit 7f0ca40

Browse files
authored
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
1 parent ada583f commit 7f0ca40

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

phpstan-baseline.neon

+1-1
Original file line numberDiff line numberDiff line change
@@ -2377,7 +2377,7 @@ parameters:
23772377

23782378
-
23792379
message: "#^Result of && is always false\\.$#"
2380-
count: 10
2380+
count: 11
23812381
path: src/PhpSpreadsheet/Shared/JAMA/Matrix.php
23822382

23832383
-

src/PhpSpreadsheet/Shared/JAMA/Matrix.php

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PhpOffice\PhpSpreadsheet\Shared\JAMA;
44

55
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalculationException;
6+
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
67
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
78
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
89

@@ -742,6 +743,9 @@ public function arrayTimesEquals(...$args)
742743
$value = trim($value, '"');
743744
$validValues &= StringHelper::convertToNumberIfFraction($value);
744745
}
746+
if (!is_numeric($value) && is_array($value)) {
747+
$value = Functions::flattenArray($value)[0];
748+
}
745749
if ($validValues) {
746750
$this->A[$i][$j] *= $value;
747751
} else {

tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/SumTest.php

+25
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
44

5+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
6+
57
class SumTest extends AllSetupTeardown
68
{
79
/**
@@ -44,4 +46,27 @@ public function providerSUMLiterals(): array
4446
{
4547
return require 'tests/data/Calculation/MathTrig/SUMLITERALS.php';
4648
}
49+
50+
public function testSumWithIndexMatch(): void
51+
{
52+
$spreadsheet = new Spreadsheet();
53+
$sheet1 = $spreadsheet->getActiveSheet();
54+
$sheet1->setTitle('Formula');
55+
$sheet1->fromArray(
56+
[
57+
['Number', 'Formula'],
58+
[83, '=SUM(4 * INDEX(Lookup!B2, MATCH(A2, Lookup!A2, 0)))'],
59+
]
60+
);
61+
$sheet2 = $spreadsheet->createSheet();
62+
$sheet2->setTitle('Lookup');
63+
$sheet2->fromArray(
64+
[
65+
['Lookup', 'Match'],
66+
[83, 16],
67+
]
68+
);
69+
self::assertSame(64, $sheet1->getCell('B2')->getCalculatedValue());
70+
$spreadsheet->disconnectWorksheets();
71+
}
4772
}

tests/PhpSpreadsheetTests/Calculation/Functions/TextData/ConcatenateTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ public function testConcatenateWithIndexMatch(): void
4141
$sheet1->fromArray(
4242
[
4343
['Number', 'Formula'],
44-
['52101293', '=CONCAT(INDEX(Lookup!$B$2, MATCH($A2, Lookup!$A$2, 0)))'],
44+
[52101293, '=CONCAT(INDEX(Lookup!B2, MATCH(A2, Lookup!A2, 0)))'],
4545
]
4646
);
4747
$sheet2 = $spreadsheet->createSheet();
4848
$sheet2->setTitle('Lookup');
4949
$sheet2->fromArray(
5050
[
5151
['Lookup', 'Match'],
52-
['52101293', 'PHP'],
52+
[52101293, 'PHP'],
5353
]
5454
);
5555
self::assertSame('PHP', $sheet1->getCell('B2')->getCalculatedValue());

0 commit comments

Comments
 (0)