Skip to content

Commit 34bb6f2

Browse files
author
MarkBaker
committed
Throw an exception if we have three or more array arguments (after flattening) until we identify how Excel handles building, using and presenting those n-dimensional result arrays
1 parent 50e9505 commit 34bb6f2

File tree

3 files changed

+60
-17
lines changed

3 files changed

+60
-17
lines changed

src/PhpSpreadsheet/Calculation/ArrayEnabled.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ protected static function evaluateArrayArguments(callable $method, ...$arguments
4141
return [$method(...$arguments)];
4242
}
4343

44-
if (self::$arrayArgumentHelper->arrayArgumentCount() === 1) {
44+
if (self::$arrayArgumentHelper->arrayArguments() === 1) {
4545
$nthArgument = self::$arrayArgumentHelper->getFirstArrayArgumentNumber();
4646

4747
return self::evaluateNthArgumentAsArray($method, $nthArgument, ...$arguments);
@@ -70,6 +70,7 @@ protected static function evaluateArrayArguments(callable $method, ...$arguments
7070
}
7171

7272
// Still need to work out the logic for more than two array arguments,
73+
// For the moment, we're throwing an Exception when we initialise the ArrayArgumentHelper
7374
return ['#VALUE!'];
7475
}
7576

src/PhpSpreadsheet/Calculation/Engine/ArrayArgumentHelper.php

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

33
namespace PhpOffice\PhpSpreadsheet\Calculation\Engine;
44

5+
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
6+
57
class ArrayArgumentHelper
68
{
79
/**
@@ -34,6 +36,10 @@ public function initialise(array $arguments): void
3436

3537
$this->rows = $this->rows($arguments);
3638
$this->columns = $this->columns($arguments);
39+
40+
if ($this->arrayArguments() > 2) {
41+
throw new Exception('Formulae with more than two array arguments are not supported');
42+
}
3743
}
3844

3945
public function arguments(): array
@@ -43,22 +49,7 @@ public function arguments(): array
4349

4450
public function hasArrayArgument(): bool
4551
{
46-
return $this->arrayArgumentCount() > 0;
47-
}
48-
49-
public function arrayArgumentCount(): int
50-
{
51-
$rowArrays = $this->filterArray($this->rows);
52-
$columnArrays = $this->filterArray($this->columns);
53-
54-
$count = 0;
55-
for ($index = 0; $index < $this->argumentCount; ++$index) {
56-
if (isset($rowArrays[$index]) || isset($columnArrays[$index])) {
57-
++$count;
58-
}
59-
}
60-
61-
return $count;
52+
return $this->arrayArguments() > 0;
6253
}
6354

6455
public function getFirstArrayArgumentNumber(): int
@@ -173,6 +164,18 @@ function ($argument) {
173164
);
174165
}
175166

167+
public function arrayArguments(): int
168+
{
169+
$count = 0;
170+
foreach (array_keys($this->arguments) as $argument) {
171+
if ($this->rows[$argument] > 1 || $this->columns[$argument] > 1) {
172+
++$count;
173+
}
174+
}
175+
176+
return $count;
177+
}
178+
176179
private function flattenSingleCellArrays(array $arguments, array $rows, array $columns): array
177180
{
178181
foreach ($arguments as $index => $argument) {

tests/PhpSpreadsheetTests/Calculation/Functions/DateTime/DateTest.php

+39
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
66
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Date;
7+
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
78

89
class DateTest extends AllSetupTeardown
910
{
@@ -112,6 +113,44 @@ public function providerDateArray(): array
112113
'{1; 4; 7; 10}',
113114
'{0,1}',
114115
],
116+
'matrices year and month' => [
117+
[
118+
[43831, 44287],
119+
[44743, 45200],
120+
],
121+
'{2020, 2021; 2022, 2023}',
122+
'{1, 4; 7, 10}',
123+
'1',
124+
],
125+
];
126+
}
127+
128+
/**
129+
* @dataProvider providerDateArrayException
130+
*/
131+
public function testDateArrayException(array $expectedResult, string $year, string $month, string $day): void
132+
{
133+
$calculation = Calculation::getInstance();
134+
135+
$this->expectException(Exception::class);
136+
$this->expectExceptionMessage('Formulae with more than two array arguments are not supported');
137+
138+
$formula = "=DATE({$year}, {$month}, {$day})";
139+
$calculation->_calculateFormulaValue($formula);
140+
}
141+
142+
public function providerDateArrayException(): array
143+
{
144+
return [
145+
'matrix arguments with 3 array values' => [
146+
[
147+
[43830, 44287],
148+
[44742, 45200],
149+
],
150+
'{2020, 2021; 2022, 2023}',
151+
'{1, 4; 7, 10}',
152+
'{0,1}',
153+
],
115154
];
116155
}
117156
}

0 commit comments

Comments
 (0)