Skip to content

Commit f48044c

Browse files
authored
Merge pull request #2764 from PHPOffice/CellCollection-Performance-Tweaks
Performance tweaks to cell collection
2 parents ee24f59 + b5e11b1 commit f48044c

File tree

1 file changed

+18
-26
lines changed

1 file changed

+18
-26
lines changed

src/PhpSpreadsheet/Collection/Cells.php

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,9 @@ public function getHighestRowAndColumn()
172172
// Lookup highest column and highest row
173173
$col = ['A' => '1A'];
174174
$row = [1];
175+
$c = '';
176+
$r = 0;
175177
foreach ($this->getCoordinates() as $coord) {
176-
$c = '';
177-
$r = 0;
178178
sscanf($coord, '%[A-Z]%d', $c, $r);
179179
$row[$r] = $r;
180180
$col[$c] = strlen($c) . $c;
@@ -241,24 +241,21 @@ public function getCurrentRow()
241241
public function getHighestColumn($row = null)
242242
{
243243
if ($row === null) {
244-
$colRow = $this->getHighestRowAndColumn();
245-
246-
return $colRow['column'];
244+
return $this->getHighestRowAndColumn()['column'];
247245
}
248246

249-
$columnList = [1];
247+
$maxColumn = '1A';
248+
$c = '';
249+
$r = 0;
250250
foreach ($this->getCoordinates() as $coord) {
251-
$c = '';
252-
$r = 0;
253-
254251
sscanf($coord, '%[A-Z]%d', $c, $r);
255252
if ($r != $row) {
256253
continue;
257254
}
258-
$columnList[] = Coordinate::columnIndexFromString($c);
255+
$maxColumn = max($maxColumn, strlen($c) . $c);
259256
}
260257

261-
return Coordinate::stringFromColumnIndex((int) @max($columnList));
258+
return substr($maxColumn, 1);
262259
}
263260

264261
/**
@@ -272,24 +269,21 @@ public function getHighestColumn($row = null)
272269
public function getHighestRow($column = null)
273270
{
274271
if ($column === null) {
275-
$colRow = $this->getHighestRowAndColumn();
276-
277-
return $colRow['row'];
272+
return $this->getHighestRowAndColumn()['row'];
278273
}
279274

280-
$rowList = [0];
275+
$maxRow = 1;
276+
$c = '';
277+
$r = 0;
281278
foreach ($this->getCoordinates() as $coord) {
282-
$c = '';
283-
$r = 0;
284-
285279
sscanf($coord, '%[A-Z]%d', $c, $r);
286280
if ($c != $column) {
287281
continue;
288282
}
289-
$rowList[] = $r;
283+
$maxRow = max($maxRow, $r);
290284
}
291285

292-
return max($rowList);
286+
return $maxRow;
293287
}
294288

295289
/**
@@ -347,10 +341,9 @@ public function cloneCellCollection(Worksheet $worksheet)
347341
*/
348342
public function removeRow($row): void
349343
{
344+
$c = '';
345+
$r = 0;
350346
foreach ($this->getCoordinates() as $coord) {
351-
$c = '';
352-
$r = 0;
353-
354347
sscanf($coord, '%[A-Z]%d', $c, $r);
355348
if ($r == $row) {
356349
$this->delete($coord);
@@ -365,10 +358,9 @@ public function removeRow($row): void
365358
*/
366359
public function removeColumn($column): void
367360
{
361+
$c = '';
362+
$r = 0;
368363
foreach ($this->getCoordinates() as $coord) {
369-
$c = '';
370-
$r = 0;
371-
372364
sscanf($coord, '%[A-Z]%d', $c, $r);
373365
if ($c == $column) {
374366
$this->delete($coord);

0 commit comments

Comments
 (0)