Skip to content

Commit 1849737

Browse files
author
MarkBaker
committed
Add functionality to shift RowRange and ColumnRange
1 parent 62238bc commit 1849737

File tree

5 files changed

+82
-0
lines changed

5 files changed

+82
-0
lines changed

src/PhpSpreadsheet/Cell/CellAddress.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public static function fromColumnAndRow($columnId, $rowId, ?Worksheet $worksheet
6666
{
6767
self::validateColumnAndRow($columnId, $rowId);
6868

69+
/** @phpstan-ignore-next-line */
6970
return new static(Coordinate::stringFromColumnIndex($columnId) . ((string) $rowId), $worksheet);
7071
}
7172

@@ -81,6 +82,7 @@ public static function fromColumnRowArray(array $array, ?Worksheet $worksheet =
8182
*/
8283
public static function fromCellAddress($cellAddress, ?Worksheet $worksheet = null): self
8384
{
85+
/** @phpstan-ignore-next-line */
8486
return new static($cellAddress, $worksheet);
8587
}
8688

src/PhpSpreadsheet/Cell/ColumnRange.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ public function columnCount(): int
6767
return $this->to - $this->from + 1;
6868
}
6969

70+
public function shiftDown(int $offset = 1): self
71+
{
72+
$newFrom = $this->from + $offset;
73+
$newFrom = ($newFrom < 1) ? 1 : $newFrom;
74+
75+
$newTo = $this->to + $offset;
76+
$newTo = ($newTo < 1) ? 1 : $newTo;
77+
78+
return self::fromColumnIndexes($newFrom, $newTo, $this->worksheet);
79+
}
80+
81+
public function shiftUp(int $offset = 1): self
82+
{
83+
return $this->shiftDown(0 - $offset);
84+
}
85+
7086
public function from(): string
7187
{
7288
return Coordinate::stringFromColumnIndex($this->from);

src/PhpSpreadsheet/Cell/RowRange.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,22 @@ public function rowCount(): int
5858
return $this->to - $this->from + 1;
5959
}
6060

61+
public function shiftRight(int $offset = 1): self
62+
{
63+
$newFrom = $this->from + $offset;
64+
$newFrom = ($newFrom < 1) ? 1 : $newFrom;
65+
66+
$newTo = $this->to + $offset;
67+
$newTo = ($newTo < 1) ? 1 : $newTo;
68+
69+
return new self($newFrom, $newTo, $this->worksheet);
70+
}
71+
72+
public function shiftLeft(int $offset = 1): self
73+
{
74+
return $this->shiftRight(0 - $offset);
75+
}
76+
6177
public function toCellRange(): CellRange
6278
{
6379
return new CellRange(

tests/PhpSpreadsheetTests/Cell/ColumnRangeTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,28 @@ public function testCreateColumnRangeFromIndexes(): void
6262
self::assertSame(3, $columnRange->columnCount());
6363
self::assertSame('C1:E1048576', (string) $columnRange->toCellRange());
6464
}
65+
66+
public function testColumnRangeNext(): void
67+
{
68+
$columnRange = new ColumnRange('C', 'E');
69+
$columnRangeNext = $columnRange->shiftDown(3);
70+
71+
self::assertSame('F', $columnRangeNext->from());
72+
self::assertSame('H', $columnRangeNext->to());
73+
74+
// Check that original Column Range isn't changed
75+
self::assertSame('C:E', (string) $columnRange);
76+
}
77+
78+
public function testColumnRangePrevious(): void
79+
{
80+
$columnRange = new ColumnRange('C', 'E');
81+
$columnRangeNext = $columnRange->shiftUp();
82+
83+
self::assertSame('B', $columnRangeNext->from());
84+
self::assertSame('D', $columnRangeNext->to());
85+
86+
// Check that original Column Range isn't changed
87+
self::assertSame('C:E', (string) $columnRange);
88+
}
6589
}

tests/PhpSpreadsheetTests/Cell/RowRangeTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,28 @@ public function testCreateRowRangeFromArray(): void
4848
self::assertSame(3, $rowRange->rowCount());
4949
self::assertSame('A3:XFD5', (string) $rowRange->toCellRange());
5050
}
51+
52+
public function testRowRangeNext(): void
53+
{
54+
$rowRange = new RowRange(3, 5);
55+
$rowRangeNext = $rowRange->shiftRight(3);
56+
57+
self::assertSame(6, $rowRangeNext->from());
58+
self::assertSame(8, $rowRangeNext->to());
59+
60+
// Check that original Row Range isn't changed
61+
self::assertSame('3:5', (string) $rowRange);
62+
}
63+
64+
public function testRowRangePrevious(): void
65+
{
66+
$rowRange = new RowRange(3, 5);
67+
$rowRangeNext = $rowRange->shiftLeft();
68+
69+
self::assertSame(2, $rowRangeNext->from());
70+
self::assertSame(4, $rowRangeNext->to());
71+
72+
// Check that original Row Range isn't changed
73+
self::assertSame('3:5', (string) $rowRange);
74+
}
5175
}

0 commit comments

Comments
 (0)