COLUMNS and ROWS functions crashed in some cases

Fixes #336
Fixes https://github.com/PHPOffice/PHPExcel/issues/1383
This commit is contained in:
Adrien Crivelli 2018-01-13 17:58:23 +09:00
parent 4635d39b4a
commit bf2dbbaf10
No known key found for this signature in database
GPG Key ID: B182FD79DC6DE92E
6 changed files with 109 additions and 4 deletions

View File

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

View File

@ -2784,11 +2784,11 @@ class Calculation
/**
* Read the dimensions of a matrix, and re-index it with straight numeric keys starting from row 0, column 0.
*
* @param mixed &$matrix matrix operand
* @param array &$matrix matrix operand
*
* @return int[] An array comprising the number of rows, and number of columns
*/
private static function getMatrixDimensions(&$matrix)
public static function getMatrixDimensions(array &$matrix)
{
$matrixRows = count($matrix);
$matrixColumns = 0;

View File

@ -138,7 +138,7 @@ class LookupRef
reset($cellAddress);
$isMatrix = (is_numeric(key($cellAddress)));
list($columns, $rows) = Calculation::_getMatrixDimensions($cellAddress);
list($columns, $rows) = Calculation::getMatrixDimensions($cellAddress);
if ($isMatrix) {
return $rows;
@ -218,7 +218,7 @@ class LookupRef
reset($cellAddress);
$isMatrix = (is_numeric(key($cellAddress)));
list($columns, $rows) = Calculation::_getMatrixDimensions($cellAddress);
list($columns, $rows) = Calculation::getMatrixDimensions($cellAddress);
if ($isMatrix) {
return $columns;

View File

@ -79,4 +79,36 @@ class LookupRefTest extends TestCase
{
return require 'data/Calculation/LookupRef/INDEX.php';
}
/**
* @dataProvider providerCOLUMNS
*
* @param mixed $expectedResult
*/
public function testCOLUMNS($expectedResult, ...$args)
{
$result = LookupRef::COLUMNS(...$args);
self::assertEquals($expectedResult, $result);
}
public function providerCOLUMNS()
{
return require 'data/Calculation/LookupRef/COLUMNS.php';
}
/**
* @dataProvider providerROWS
*
* @param mixed $expectedResult
*/
public function testROWS($expectedResult, ...$args)
{
$result = LookupRef::ROWS(...$args);
self::assertEquals($expectedResult, $result);
}
public function providerROWS()
{
return require 'data/Calculation/LookupRef/ROWS.php';
}
}

View File

@ -0,0 +1,36 @@
<?php
return [
[
1,
null,
],
[
1,
'',
],
[
'#VALUE!',
'foo',
],
[
0,
[],
],
[
1,
[1],
],
[
1,
[1, 1],
],
[
2,
[[1, 1]],
],
[
1,
['a' => [1, 1]],
],
];

View File

@ -0,0 +1,36 @@
<?php
return [
[
1,
null,
],
[
1,
'',
],
[
'#VALUE!',
'foo',
],
[
0,
[],
],
[
1,
[1],
],
[
2,
[1, 1],
],
[
1,
[[1, 1]],
],
[
2,
['a' => [1, 1]],
],
];