Skip to content

Commit 0171709

Browse files
author
MarkBaker
committed
The sortCellReferenceArray() in Coordinate should have returned cells ordered by row, then by column... but instead sorted by column, then row.
Fixed that bug, using a slightly faster algorithm for the sort index than the simple fix would have used, and modified the tests that didn't have the correct expected result :-(
1 parent 1924f3c commit 0171709

File tree

2 files changed

+40
-40
lines changed

2 files changed

+40
-40
lines changed

src/PhpSpreadsheet/Cell/Coordinate.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,8 @@ public static function extractAllCellReferencesInRange($cellRange): array
353353
}
354354

355355
$cellList = array_merge(...$cells);
356-
$cellList = self::sortCellReferenceArray($cellList);
357356

358-
return $cellList;
357+
return self::sortCellReferenceArray($cellList);
359358
}
360359

361360
private static function processRangeSetOperators(array $operators, array $cells): array
@@ -382,9 +381,10 @@ private static function sortCellReferenceArray(array $cellList): array
382381
{
383382
// Sort the result by column and row
384383
$sortKeys = [];
385-
foreach ($cellList as $coord) {
386-
sscanf($coord, '%[A-Z]%d', $column, $row);
387-
$sortKeys[sprintf('%3s%09d', $column, $row)] = $coord;
384+
foreach ($cellList as $coordinate) {
385+
sscanf($coordinate, '%[A-Z]%d', $column, $row);
386+
$key = (--$row * 16384) + self::columnIndexFromString($column);
387+
$sortKeys[$key] = $coordinate;
388388
}
389389
ksort($sortKeys);
390390

tests/data/CellExtractAllCellReferencesInRange.php

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
[
1313
[
1414
'B4',
15-
'B5',
16-
'B6',
1715
'D4',
16+
'B5',
1817
'D5',
18+
'B6',
1919
'D6',
2020
],
2121
'B4:B6,D4:D6',
@@ -28,89 +28,89 @@
2828
[
2929
[
3030
'B4',
31-
'B5',
32-
'B6',
3331
'C4',
34-
'C5',
35-
'C6',
3632
'D4',
33+
'B5',
34+
'C5',
3735
'D5',
36+
'B6',
37+
'C6',
3838
'D6',
3939
],
4040
'B4:D6',
4141
],
4242
[
4343
[
4444
'B4',
45-
'B5',
46-
'B6',
4745
'C4',
48-
'C5',
49-
'C6',
50-
'C7',
5146
'D4',
47+
'B5',
48+
'C5',
5249
'D5',
53-
'D6',
54-
'D7',
5550
'E5',
51+
'B6',
52+
'C6',
53+
'D6',
5654
'E6',
55+
'C7',
56+
'D7',
5757
'E7',
5858
],
5959
'B4:D6,C5:E7',
6060
],
6161
[
6262
[
6363
'C5',
64-
'C6',
6564
'D5',
65+
'C6',
6666
'D6',
6767
],
6868
'B4:D6 C5:E7',
6969
],
7070
[
7171
[
7272
'B2',
73-
'B3',
74-
'B4',
7573
'C2',
76-
'C3',
77-
'C4',
78-
'C5',
7974
'D2',
75+
'B3',
76+
'C3',
8077
'D3',
81-
'D4',
82-
'D5',
83-
'D6',
8478
'E3',
79+
'B4',
80+
'C4',
81+
'D4',
8582
'E4',
86-
'E5',
87-
'E6',
8883
'F4',
84+
'C5',
85+
'D5',
86+
'E5',
8987
'F5',
88+
'D6',
89+
'E6',
9090
'F6',
9191
],
9292
'B2:D4,C5:D5,E3:E5,D6:E6,F4:F6',
9393
],
9494
[
9595
[
9696
'B2',
97-
'B3',
98-
'B4',
9997
'C2',
100-
'C3',
101-
'C4',
102-
'C5',
10398
'D2',
99+
'B3',
100+
'C3',
104101
'D3',
105-
'D4',
106-
'D5',
107-
'D6',
108102
'E3',
103+
'B4',
104+
'C4',
105+
'D4',
109106
'E4',
110-
'E5',
111-
'E6',
112107
'F4',
108+
'C5',
109+
'D5',
110+
'E5',
113111
'F5',
112+
'D6',
113+
'E6',
114114
'F6',
115115
],
116116
'B2:D4,C3:E5,D4:F6',
@@ -124,8 +124,8 @@
124124
[
125125
[
126126
'Z2',
127-
'Z3',
128127
'AA2',
128+
'Z3',
129129
'AA3',
130130
],
131131
'Z2:AA3',

0 commit comments

Comments
 (0)