Skip to content

Commit

Permalink
Ensure multiplication is performed on a non-array value (#2964)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
u01jmg3 authored Aug 7, 2022
1 parent ada583f commit 7f0ca40
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2377,7 +2377,7 @@ parameters:

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

-
Expand Down
4 changes: 4 additions & 0 deletions src/PhpSpreadsheet/Shared/JAMA/Matrix.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -742,6 +743,9 @@ public function arrayTimesEquals(...$args)
$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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;

use PhpOffice\PhpSpreadsheet\Spreadsheet;

class SumTest extends AllSetupTeardown
{
/**
Expand Down Expand Up @@ -44,4 +46,27 @@ public function providerSUMLiterals(): array
{
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ public function testConcatenateWithIndexMatch(): void
$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();
$sheet2->setTitle('Lookup');
$sheet2->fromArray(
[
['Lookup', 'Match'],
['52101293', 'PHP'],
[52101293, 'PHP'],
]
);
self::assertSame('PHP', $sheet1->getCell('B2')->getCalculatedValue());
Expand Down

0 comments on commit 7f0ca40

Please sign in to comment.