Skip to content

Commit bf2dbba

Browse files
committed
COLUMNS and ROWS functions crashed in some cases
Fixes #336 Fixes PHPOffice/PHPExcel#1383
1 parent 4635d39 commit bf2dbba

File tree

6 files changed

+109
-4
lines changed

6 files changed

+109
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1717
- Better auto-detection of CSV separators - [#305](https://github.com/PHPOffice/PhpSpreadsheet/issues/305)
1818
- Support for shape style ending with `;` - [#304](https://github.com/PHPOffice/PhpSpreadsheet/issues/304)
1919
- Freeze Panes takes wrong coordinates for XLSX - [#322](https://github.com/PHPOffice/PhpSpreadsheet/issues/322)
20+
- `COLUMNS` and `ROWS` functions crashed in some cases - [#336](https://github.com/PHPOffice/PhpSpreadsheet/issues/336)
2021

2122
## [1.0.0] - 2017-12-25
2223

src/PhpSpreadsheet/Calculation/Calculation.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2784,11 +2784,11 @@ private static function checkMatrixOperands(&$operand1, &$operand2, $resize = 1)
27842784
/**
27852785
* Read the dimensions of a matrix, and re-index it with straight numeric keys starting from row 0, column 0.
27862786
*
2787-
* @param mixed &$matrix matrix operand
2787+
* @param array &$matrix matrix operand
27882788
*
27892789
* @return int[] An array comprising the number of rows, and number of columns
27902790
*/
2791-
private static function getMatrixDimensions(&$matrix)
2791+
public static function getMatrixDimensions(array &$matrix)
27922792
{
27932793
$matrixRows = count($matrix);
27942794
$matrixColumns = 0;

src/PhpSpreadsheet/Calculation/LookupRef.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public static function COLUMNS($cellAddress = null)
138138

139139
reset($cellAddress);
140140
$isMatrix = (is_numeric(key($cellAddress)));
141-
list($columns, $rows) = Calculation::_getMatrixDimensions($cellAddress);
141+
list($columns, $rows) = Calculation::getMatrixDimensions($cellAddress);
142142

143143
if ($isMatrix) {
144144
return $rows;
@@ -218,7 +218,7 @@ public static function ROWS($cellAddress = null)
218218

219219
reset($cellAddress);
220220
$isMatrix = (is_numeric(key($cellAddress)));
221-
list($columns, $rows) = Calculation::_getMatrixDimensions($cellAddress);
221+
list($columns, $rows) = Calculation::getMatrixDimensions($cellAddress);
222222

223223
if ($isMatrix) {
224224
return $columns;

tests/PhpSpreadsheetTests/Calculation/LookupRefTest.php

+32
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,36 @@ public function providerINDEX()
7979
{
8080
return require 'data/Calculation/LookupRef/INDEX.php';
8181
}
82+
83+
/**
84+
* @dataProvider providerCOLUMNS
85+
*
86+
* @param mixed $expectedResult
87+
*/
88+
public function testCOLUMNS($expectedResult, ...$args)
89+
{
90+
$result = LookupRef::COLUMNS(...$args);
91+
self::assertEquals($expectedResult, $result);
92+
}
93+
94+
public function providerCOLUMNS()
95+
{
96+
return require 'data/Calculation/LookupRef/COLUMNS.php';
97+
}
98+
99+
/**
100+
* @dataProvider providerROWS
101+
*
102+
* @param mixed $expectedResult
103+
*/
104+
public function testROWS($expectedResult, ...$args)
105+
{
106+
$result = LookupRef::ROWS(...$args);
107+
self::assertEquals($expectedResult, $result);
108+
}
109+
110+
public function providerROWS()
111+
{
112+
return require 'data/Calculation/LookupRef/ROWS.php';
113+
}
82114
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
return [
4+
[
5+
1,
6+
null,
7+
],
8+
[
9+
1,
10+
'',
11+
],
12+
[
13+
'#VALUE!',
14+
'foo',
15+
],
16+
[
17+
0,
18+
[],
19+
],
20+
[
21+
1,
22+
[1],
23+
],
24+
[
25+
1,
26+
[1, 1],
27+
],
28+
[
29+
2,
30+
[[1, 1]],
31+
],
32+
[
33+
1,
34+
['a' => [1, 1]],
35+
],
36+
];
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
return [
4+
[
5+
1,
6+
null,
7+
],
8+
[
9+
1,
10+
'',
11+
],
12+
[
13+
'#VALUE!',
14+
'foo',
15+
],
16+
[
17+
0,
18+
[],
19+
],
20+
[
21+
1,
22+
[1],
23+
],
24+
[
25+
2,
26+
[1, 1],
27+
],
28+
[
29+
1,
30+
[[1, 1]],
31+
],
32+
[
33+
2,
34+
['a' => [1, 1]],
35+
],
36+
];

0 commit comments

Comments
 (0)