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
This commit is contained in:
Jonathan Goode 2022-08-07 01:28:26 +01:00 committed by GitHub
parent ada583f3cf
commit 7f0ca404fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 3 deletions

View File

@ -2377,7 +2377,7 @@ parameters:
-
message: "#^Result of && is always false\\.$#"
count: 10
count: 11
path: src/PhpSpreadsheet/Shared/JAMA/Matrix.php
-

View File

@ -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 {

View File

@ -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();
}
}

View File

@ -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());