Skip to content

Commit 62e2259

Browse files
committed
A Bit of Refactoring in CellReferenceHelper
1 parent caf12eb commit 62e2259

File tree

1 file changed

+48
-35
lines changed

1 file changed

+48
-35
lines changed

src/PhpSpreadsheet/CellReferenceHelper.php

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -71,44 +71,12 @@ public function updateCellReference(string $cellReference = 'A1', bool $includeA
7171
$updateColumn = (($absoluteColumn !== '$') && $newColumnIndex >= $this->beforeColumn);
7272
$updateRow = (($absoluteRow !== '$') && $newRowIndex >= $this->beforeRow);
7373
} else {
74-
// A special case is removing the left/top or bottom/right edge of a range
75-
// $topLeft is null if we aren't adjusting a range at all.
76-
if (
77-
$topLeft !== null
78-
&& $this->numberOfColumns < 0
79-
&& $newColumnIndex >= $this->beforeColumn + $this->numberOfColumns
80-
&& $newColumnIndex <= $this->beforeColumn - 1
81-
) {
82-
if ($topLeft) {
83-
$newColumnIndex = $this->beforeColumn + $this->numberOfColumns;
84-
} else {
85-
$newColumnIndex = $this->beforeColumn + $this->numberOfColumns - 1;
86-
}
87-
} elseif ($newColumnIndex >= $this->beforeColumn) {
88-
// Create new column reference
89-
$newColumnIndex += $this->numberOfColumns;
90-
}
74+
$newColumnIndex = $this->computeNewColumnIndex($newColumnIndex, $topLeft);
9175
$newColumn = $absoluteColumn . Coordinate::stringFromColumnIndex($newColumnIndex);
92-
//$updateColumn = ($newColumnIndex >= $this->beforeColumn);
9376
$updateColumn = false;
94-
// A special case is removing the left/top or bottom/right edge of a range
95-
// $topLeft is null if we aren't adjusting a range at all.
96-
if (
97-
$topLeft !== null
98-
&& $this->numberOfRows < 0
99-
&& $newRowIndex >= $this->beforeRow + $this->numberOfRows
100-
&& $newRowIndex <= $this->beforeRow - 1
101-
) {
102-
if ($topLeft) {
103-
$newRowIndex = $this->beforeRow + $this->numberOfRows;
104-
} else {
105-
$newRowIndex = $this->beforeRow + $this->numberOfRows - 1;
106-
}
107-
} elseif ($newRowIndex >= $this->beforeRow) {
108-
$newRowIndex = $newRowIndex + $this->numberOfRows;
109-
}
77+
78+
$newRowIndex = $this->computeNewRowIndex($newRowIndex, $topLeft);
11079
$newRow = $absoluteRow . $newRowIndex;
111-
//$updateRow = ($newRowIndex >= $this->beforeRow);
11280
$updateRow = false;
11381
}
11482

@@ -126,6 +94,51 @@ public function updateCellReference(string $cellReference = 'A1', bool $includeA
12694
return "{$newColumn}{$newRow}";
12795
}
12896

97+
public function computeNewColumnIndex(int $newColumnIndex, ?bool $topLeft): int
98+
{
99+
// A special case is removing the left/top or bottom/right edge of a range
100+
// $topLeft is null if we aren't adjusting a range at all.
101+
if (
102+
$topLeft !== null
103+
&& $this->numberOfColumns < 0
104+
&& $newColumnIndex >= $this->beforeColumn + $this->numberOfColumns
105+
&& $newColumnIndex <= $this->beforeColumn - 1
106+
) {
107+
if ($topLeft) {
108+
$newColumnIndex = $this->beforeColumn + $this->numberOfColumns;
109+
} else {
110+
$newColumnIndex = $this->beforeColumn + $this->numberOfColumns - 1;
111+
}
112+
} elseif ($newColumnIndex >= $this->beforeColumn) {
113+
// Create new column reference
114+
$newColumnIndex += $this->numberOfColumns;
115+
}
116+
117+
return $newColumnIndex;
118+
}
119+
120+
public function computeNewRowIndex(int $newRowIndex, ?bool $topLeft): int
121+
{
122+
// A special case is removing the left/top or bottom/right edge of a range
123+
// $topLeft is null if we aren't adjusting a range at all.
124+
if (
125+
$topLeft !== null
126+
&& $this->numberOfRows < 0
127+
&& $newRowIndex >= $this->beforeRow + $this->numberOfRows
128+
&& $newRowIndex <= $this->beforeRow - 1
129+
) {
130+
if ($topLeft) {
131+
$newRowIndex = $this->beforeRow + $this->numberOfRows;
132+
} else {
133+
$newRowIndex = $this->beforeRow + $this->numberOfRows - 1;
134+
}
135+
} elseif ($newRowIndex >= $this->beforeRow) {
136+
$newRowIndex = $newRowIndex + $this->numberOfRows;
137+
}
138+
139+
return $newRowIndex;
140+
}
141+
129142
public function cellAddressInDeleteRange(string $cellAddress): bool
130143
{
131144
[$cellColumn, $cellRow] = Coordinate::coordinateFromString($cellAddress);

0 commit comments

Comments
 (0)