From 83c083392ae163bd030f98e27319df9736847eb2 Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Tue, 31 Jul 2012 13:00:09 +0100 Subject: [PATCH] GH18 - Modify extractAllCellReferencesInRange() behaviour for , and space range separators --- Classes/PHPExcel/Cell.php | 21 +++++++--- unitTests/PHPExcel/CellTest.php | 38 ++++++++++++++++++- .../CellExtractAllCellReferencesInRange.data | 9 +++++ .../rawTestData/CellGetRangeBoundaries.data | 2 + 4 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 unitTests/rawTestData/CellExtractAllCellReferencesInRange.data create mode 100644 unitTests/rawTestData/CellGetRangeBoundaries.data diff --git a/Classes/PHPExcel/Cell.php b/Classes/PHPExcel/Cell.php index e63d821..6bf16ee 100644 --- a/Classes/PHPExcel/Cell.php +++ b/Classes/PHPExcel/Cell.php @@ -593,9 +593,10 @@ class PHPExcel_Cell /** * Split range into coordinate strings * - * @param string $pRange e.g. 'B4:D9' or 'B4:D9,H2:O11' + * @param string $pRange e.g. 'B4:D9' or 'B4:D9,H2:O11' or 'B4' * @return array Array containg one or more arrays containing one or two coordinate strings * e.g. array('B4','D9') or array(array('B4','D9'),array('H2','O11')) + * or array('B4') */ public static function splitRange($pRange = 'A1:A1') { @@ -636,7 +637,8 @@ class PHPExcel_Cell * Calculate range boundaries * * @param string $pRange Cell range (e.g. A1:A1) - * @return array Range coordinates (Start Cell, End Cell) where Start Cell and End Cell are arrays (Column Number, Row Number) + * @return array Range coordinates array(Start Cell, End Cell) + * where Start Cell and End Cell are arrays (Column Number, Row Number) */ public static function rangeBoundaries($pRange = 'A1:A1') { @@ -679,7 +681,8 @@ class PHPExcel_Cell * Calculate range boundaries * * @param string $pRange Cell range (e.g. A1:A1) - * @return array Range boundaries (staring Column, starting Row, Final Column, Final Row) + * @return array Range coordinates array(Start Cell, End Cell) + * where Start Cell and End Cell are arrays (Column ID, Row Number) */ public static function getRangeBoundaries($pRange = 'A1:A1') { @@ -772,7 +775,7 @@ class PHPExcel_Cell /** * Extract all cell references in range * - * @param string $pRange Range (e.g. A1 or A1:A10 or A1:A10 A100:A1000) + * @param string $pRange Range (e.g. A1 or A1:C10 or A1:E10 A20:E25) * @return array Array containing single cell references */ public static function extractAllCellReferencesInRange($pRange = 'A1') { @@ -819,8 +822,16 @@ class PHPExcel_Cell } } + // Sort the result by column and row + $sortKeys = array(); + foreach (array_unique($returnValue) as $coord) { + list($column,$row) = sscanf($coord,'%[A-Z]%d'); + $sortKeys[sprintf('%3s%09d',$column,$row)] = $coord; + } + ksort($sortKeys); + // Return value - return $returnValue; + return array_values($sortKeys); } /** diff --git a/unitTests/PHPExcel/CellTest.php b/unitTests/PHPExcel/CellTest.php index 0f7f3eb..11df404 100644 --- a/unitTests/PHPExcel/CellTest.php +++ b/unitTests/PHPExcel/CellTest.php @@ -187,7 +187,11 @@ class CellTest extends PHPUnit_Framework_TestCase $expectedResult = array_pop($args); $result = call_user_func_array(array('PHPExcel_Cell','splitRange'),$args); foreach($result as $key => $split) { - $this->assertEquals($expectedResult[$key], $split); + if (!is_array($expectedResult[$key])) { + $this->assertEquals($expectedResult[$key], $split[0]); + } else { + $this->assertEquals($expectedResult[$key], $split); + } } } @@ -256,4 +260,36 @@ class CellTest extends PHPUnit_Framework_TestCase return new testDataFileIterator('rawTestData/CellRangeDimension.data'); } + /** + * @dataProvider providerGetRangeBoundaries + */ + public function testGetRangeBoundaries() + { + $args = func_get_args(); + $expectedResult = array_pop($args); + $result = call_user_func_array(array('PHPExcel_Cell','getRangeBoundaries'),$args); + $this->assertEquals($expectedResult, $result); + } + + public function providerGetRangeBoundaries() + { + return new testDataFileIterator('rawTestData/CellGetRangeBoundaries.data'); + } + + /** + * @dataProvider providerExtractAllCellReferencesInRange + */ + public function testExtractAllCellReferencesInRange() + { + $args = func_get_args(); + $expectedResult = array_pop($args); + $result = call_user_func_array(array('PHPExcel_Cell','extractAllCellReferencesInRange'),$args); + $this->assertEquals($expectedResult, $result); + } + + public function providerExtractAllCellReferencesInRange() + { + return new testDataFileIterator('rawTestData/CellExtractAllCellReferencesInRange.data'); + } + } diff --git a/unitTests/rawTestData/CellExtractAllCellReferencesInRange.data b/unitTests/rawTestData/CellExtractAllCellReferencesInRange.data new file mode 100644 index 0000000..4b44a83 --- /dev/null +++ b/unitTests/rawTestData/CellExtractAllCellReferencesInRange.data @@ -0,0 +1,9 @@ +"B4:B6", {"B4";"B5";"B6"} +'"B4:B6,D4:D6"', {"B4";"B5";"B6";"D4";"D5";"D6"} +'"B4:B6 D4:D6"', {"B4";"B5";"B6";"D4";"D5";"D6"} +"B4:D6", {"B4";"B5";"B6";"C4";"C5";"C6";"D4";"D5";"D6"} +'"B4:D6,C5:E7"', {"B4";"B5";"B6";"C4";"C5";"C6";"C7";"D4";"D5";"D6";"D7";"E5";"E6";"E7"} +'"B4:D6 C5:E7"', {"B4";"B5";"B6";"C4";"C5";"C6";"C7";"D4";"D5";"D6";"D7";"E5";"E6";"E7"} +"B2:D4 C5:D5 E3:E5 D6:E6 F4:F6", {"B2";"B3";"B4";"C2";"C3";"C4";"C5";"D2";"D3";"D4";"D5";"D6";"E3";"E4";"E5";"E6";"F4";"F5";"F6"} +"B2:D4 C3:E5 D4:F6", {"B2";"B3";"B4";"C2";"C3";"C4";"C5";"D2";"D3";"D4";"D5";"D6";"E3";"E4";"E5";"E6";"F4";"F5";"F6"} +"B4:B6 B8", {"B4";"B5";"B6";"B8"} diff --git a/unitTests/rawTestData/CellGetRangeBoundaries.data b/unitTests/rawTestData/CellGetRangeBoundaries.data new file mode 100644 index 0000000..f01a5fd --- /dev/null +++ b/unitTests/rawTestData/CellGetRangeBoundaries.data @@ -0,0 +1,2 @@ +"B4:E9", {"B"|4;"E"|9} +"B4", {"B"|4;"B"|4}