Skip to content

Commit 83c0833

Browse files
author
Mark Baker
committed
GH18 - Modify extractAllCellReferencesInRange() behaviour for , and space
range separators
1 parent 5c32ece commit 83c0833

File tree

4 files changed

+64
-6
lines changed

4 files changed

+64
-6
lines changed

Classes/PHPExcel/Cell.php

+16-5
Original file line numberDiff line numberDiff line change
@@ -593,9 +593,10 @@ public static function absoluteCoordinate($pCoordinateString = 'A1')
593593
/**
594594
* Split range into coordinate strings
595595
*
596-
* @param string $pRange e.g. 'B4:D9' or 'B4:D9,H2:O11'
596+
* @param string $pRange e.g. 'B4:D9' or 'B4:D9,H2:O11' or 'B4'
597597
* @return array Array containg one or more arrays containing one or two coordinate strings
598598
* e.g. array('B4','D9') or array(array('B4','D9'),array('H2','O11'))
599+
* or array('B4')
599600
*/
600601
public static function splitRange($pRange = 'A1:A1')
601602
{
@@ -636,7 +637,8 @@ public static function buildRange($pRange)
636637
* Calculate range boundaries
637638
*
638639
* @param string $pRange Cell range (e.g. A1:A1)
639-
* @return array Range coordinates (Start Cell, End Cell) where Start Cell and End Cell are arrays (Column Number, Row Number)
640+
* @return array Range coordinates array(Start Cell, End Cell)
641+
* where Start Cell and End Cell are arrays (Column Number, Row Number)
640642
*/
641643
public static function rangeBoundaries($pRange = 'A1:A1')
642644
{
@@ -679,7 +681,8 @@ public static function rangeDimension($pRange = 'A1:A1')
679681
* Calculate range boundaries
680682
*
681683
* @param string $pRange Cell range (e.g. A1:A1)
682-
* @return array Range boundaries (staring Column, starting Row, Final Column, Final Row)
684+
* @return array Range coordinates array(Start Cell, End Cell)
685+
* where Start Cell and End Cell are arrays (Column ID, Row Number)
683686
*/
684687
public static function getRangeBoundaries($pRange = 'A1:A1')
685688
{
@@ -772,7 +775,7 @@ public static function stringFromColumnIndex($pColumnIndex = 0)
772775
/**
773776
* Extract all cell references in range
774777
*
775-
* @param string $pRange Range (e.g. A1 or A1:A10 or A1:A10 A100:A1000)
778+
* @param string $pRange Range (e.g. A1 or A1:C10 or A1:E10 A20:E25)
776779
* @return array Array containing single cell references
777780
*/
778781
public static function extractAllCellReferencesInRange($pRange = 'A1') {
@@ -819,8 +822,16 @@ public static function extractAllCellReferencesInRange($pRange = 'A1') {
819822
}
820823
}
821824

825+
// Sort the result by column and row
826+
$sortKeys = array();
827+
foreach (array_unique($returnValue) as $coord) {
828+
list($column,$row) = sscanf($coord,'%[A-Z]%d');
829+
$sortKeys[sprintf('%3s%09d',$column,$row)] = $coord;
830+
}
831+
ksort($sortKeys);
832+
822833
// Return value
823-
return $returnValue;
834+
return array_values($sortKeys);
824835
}
825836

826837
/**

unitTests/PHPExcel/CellTest.php

+37-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,11 @@ public function testSplitRange()
187187
$expectedResult = array_pop($args);
188188
$result = call_user_func_array(array('PHPExcel_Cell','splitRange'),$args);
189189
foreach($result as $key => $split) {
190-
$this->assertEquals($expectedResult[$key], $split);
190+
if (!is_array($expectedResult[$key])) {
191+
$this->assertEquals($expectedResult[$key], $split[0]);
192+
} else {
193+
$this->assertEquals($expectedResult[$key], $split);
194+
}
191195
}
192196
}
193197

@@ -256,4 +260,36 @@ public function providerRangeDimension()
256260
return new testDataFileIterator('rawTestData/CellRangeDimension.data');
257261
}
258262

263+
/**
264+
* @dataProvider providerGetRangeBoundaries
265+
*/
266+
public function testGetRangeBoundaries()
267+
{
268+
$args = func_get_args();
269+
$expectedResult = array_pop($args);
270+
$result = call_user_func_array(array('PHPExcel_Cell','getRangeBoundaries'),$args);
271+
$this->assertEquals($expectedResult, $result);
272+
}
273+
274+
public function providerGetRangeBoundaries()
275+
{
276+
return new testDataFileIterator('rawTestData/CellGetRangeBoundaries.data');
277+
}
278+
279+
/**
280+
* @dataProvider providerExtractAllCellReferencesInRange
281+
*/
282+
public function testExtractAllCellReferencesInRange()
283+
{
284+
$args = func_get_args();
285+
$expectedResult = array_pop($args);
286+
$result = call_user_func_array(array('PHPExcel_Cell','extractAllCellReferencesInRange'),$args);
287+
$this->assertEquals($expectedResult, $result);
288+
}
289+
290+
public function providerExtractAllCellReferencesInRange()
291+
{
292+
return new testDataFileIterator('rawTestData/CellExtractAllCellReferencesInRange.data');
293+
}
294+
259295
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"B4:B6", {"B4";"B5";"B6"}
2+
'"B4:B6,D4:D6"', {"B4";"B5";"B6";"D4";"D5";"D6"}
3+
'"B4:B6 D4:D6"', {"B4";"B5";"B6";"D4";"D5";"D6"}
4+
"B4:D6", {"B4";"B5";"B6";"C4";"C5";"C6";"D4";"D5";"D6"}
5+
'"B4:D6,C5:E7"', {"B4";"B5";"B6";"C4";"C5";"C6";"C7";"D4";"D5";"D6";"D7";"E5";"E6";"E7"}
6+
'"B4:D6 C5:E7"', {"B4";"B5";"B6";"C4";"C5";"C6";"C7";"D4";"D5";"D6";"D7";"E5";"E6";"E7"}
7+
"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"}
8+
"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"}
9+
"B4:B6 B8", {"B4";"B5";"B6";"B8"}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"B4:E9", {"B"|4;"E"|9}
2+
"B4", {"B"|4;"B"|4}

0 commit comments

Comments
 (0)