Performance tweaks to cell collection

This commit is contained in:
MarkBaker 2022-04-21 11:22:40 +02:00
parent ee24f59af1
commit b5e11b1307
1 changed files with 18 additions and 26 deletions

View File

@ -172,9 +172,9 @@ class Cells
// Lookup highest column and highest row // Lookup highest column and highest row
$col = ['A' => '1A']; $col = ['A' => '1A'];
$row = [1]; $row = [1];
$c = '';
$r = 0;
foreach ($this->getCoordinates() as $coord) { foreach ($this->getCoordinates() as $coord) {
$c = '';
$r = 0;
sscanf($coord, '%[A-Z]%d', $c, $r); sscanf($coord, '%[A-Z]%d', $c, $r);
$row[$r] = $r; $row[$r] = $r;
$col[$c] = strlen($c) . $c; $col[$c] = strlen($c) . $c;
@ -241,24 +241,21 @@ class Cells
public function getHighestColumn($row = null) public function getHighestColumn($row = null)
{ {
if ($row === null) { if ($row === null) {
$colRow = $this->getHighestRowAndColumn(); return $this->getHighestRowAndColumn()['column'];
return $colRow['column'];
} }
$columnList = [1]; $maxColumn = '1A';
$c = '';
$r = 0;
foreach ($this->getCoordinates() as $coord) { foreach ($this->getCoordinates() as $coord) {
$c = '';
$r = 0;
sscanf($coord, '%[A-Z]%d', $c, $r); sscanf($coord, '%[A-Z]%d', $c, $r);
if ($r != $row) { if ($r != $row) {
continue; continue;
} }
$columnList[] = Coordinate::columnIndexFromString($c); $maxColumn = max($maxColumn, strlen($c) . $c);
} }
return Coordinate::stringFromColumnIndex((int) @max($columnList)); return substr($maxColumn, 1);
} }
/** /**
@ -272,24 +269,21 @@ class Cells
public function getHighestRow($column = null) public function getHighestRow($column = null)
{ {
if ($column === null) { if ($column === null) {
$colRow = $this->getHighestRowAndColumn(); return $this->getHighestRowAndColumn()['row'];
return $colRow['row'];
} }
$rowList = [0]; $maxRow = 1;
$c = '';
$r = 0;
foreach ($this->getCoordinates() as $coord) { foreach ($this->getCoordinates() as $coord) {
$c = '';
$r = 0;
sscanf($coord, '%[A-Z]%d', $c, $r); sscanf($coord, '%[A-Z]%d', $c, $r);
if ($c != $column) { if ($c != $column) {
continue; continue;
} }
$rowList[] = $r; $maxRow = max($maxRow, $r);
} }
return max($rowList); return $maxRow;
} }
/** /**
@ -347,10 +341,9 @@ class Cells
*/ */
public function removeRow($row): void public function removeRow($row): void
{ {
$c = '';
$r = 0;
foreach ($this->getCoordinates() as $coord) { foreach ($this->getCoordinates() as $coord) {
$c = '';
$r = 0;
sscanf($coord, '%[A-Z]%d', $c, $r); sscanf($coord, '%[A-Z]%d', $c, $r);
if ($r == $row) { if ($r == $row) {
$this->delete($coord); $this->delete($coord);
@ -365,10 +358,9 @@ class Cells
*/ */
public function removeColumn($column): void public function removeColumn($column): void
{ {
$c = '';
$r = 0;
foreach ($this->getCoordinates() as $coord) { foreach ($this->getCoordinates() as $coord) {
$c = '';
$r = 0;
sscanf($coord, '%[A-Z]%d', $c, $r); sscanf($coord, '%[A-Z]%d', $c, $r);
if ($c == $column) { if ($c == $column) {
$this->delete($coord); $this->delete($coord);