diff --git a/src/PhpSpreadsheet/Cell/CellAddress.php b/src/PhpSpreadsheet/Cell/CellAddress.php index 389f09ac..412cff56 100644 --- a/src/PhpSpreadsheet/Cell/CellAddress.php +++ b/src/PhpSpreadsheet/Cell/CellAddress.php @@ -66,6 +66,7 @@ class CellAddress { self::validateColumnAndRow($columnId, $rowId); + /** @phpstan-ignore-next-line */ return new static(Coordinate::stringFromColumnIndex($columnId) . ((string) $rowId), $worksheet); } @@ -81,6 +82,7 @@ class CellAddress */ public static function fromCellAddress($cellAddress, ?Worksheet $worksheet = null): self { + /** @phpstan-ignore-next-line */ return new static($cellAddress, $worksheet); } diff --git a/src/PhpSpreadsheet/Cell/ColumnRange.php b/src/PhpSpreadsheet/Cell/ColumnRange.php index d38caea8..dbd91bb6 100644 --- a/src/PhpSpreadsheet/Cell/ColumnRange.php +++ b/src/PhpSpreadsheet/Cell/ColumnRange.php @@ -67,6 +67,22 @@ class ColumnRange return $this->to - $this->from + 1; } + public function shiftDown(int $offset = 1): self + { + $newFrom = $this->from + $offset; + $newFrom = ($newFrom < 1) ? 1 : $newFrom; + + $newTo = $this->to + $offset; + $newTo = ($newTo < 1) ? 1 : $newTo; + + return self::fromColumnIndexes($newFrom, $newTo, $this->worksheet); + } + + public function shiftUp(int $offset = 1): self + { + return $this->shiftDown(0 - $offset); + } + public function from(): string { return Coordinate::stringFromColumnIndex($this->from); diff --git a/src/PhpSpreadsheet/Cell/RowRange.php b/src/PhpSpreadsheet/Cell/RowRange.php index 07190702..bef97111 100644 --- a/src/PhpSpreadsheet/Cell/RowRange.php +++ b/src/PhpSpreadsheet/Cell/RowRange.php @@ -58,6 +58,22 @@ class RowRange return $this->to - $this->from + 1; } + public function shiftRight(int $offset = 1): self + { + $newFrom = $this->from + $offset; + $newFrom = ($newFrom < 1) ? 1 : $newFrom; + + $newTo = $this->to + $offset; + $newTo = ($newTo < 1) ? 1 : $newTo; + + return new self($newFrom, $newTo, $this->worksheet); + } + + public function shiftLeft(int $offset = 1): self + { + return $this->shiftRight(0 - $offset); + } + public function toCellRange(): CellRange { return new CellRange( diff --git a/tests/PhpSpreadsheetTests/Cell/ColumnRangeTest.php b/tests/PhpSpreadsheetTests/Cell/ColumnRangeTest.php index 6fa18d89..14455701 100644 --- a/tests/PhpSpreadsheetTests/Cell/ColumnRangeTest.php +++ b/tests/PhpSpreadsheetTests/Cell/ColumnRangeTest.php @@ -62,4 +62,28 @@ class ColumnRangeTest extends TestCase self::assertSame(3, $columnRange->columnCount()); self::assertSame('C1:E1048576', (string) $columnRange->toCellRange()); } + + public function testColumnRangeNext(): void + { + $columnRange = new ColumnRange('C', 'E'); + $columnRangeNext = $columnRange->shiftDown(3); + + self::assertSame('F', $columnRangeNext->from()); + self::assertSame('H', $columnRangeNext->to()); + + // Check that original Column Range isn't changed + self::assertSame('C:E', (string) $columnRange); + } + + public function testColumnRangePrevious(): void + { + $columnRange = new ColumnRange('C', 'E'); + $columnRangeNext = $columnRange->shiftUp(); + + self::assertSame('B', $columnRangeNext->from()); + self::assertSame('D', $columnRangeNext->to()); + + // Check that original Column Range isn't changed + self::assertSame('C:E', (string) $columnRange); + } } diff --git a/tests/PhpSpreadsheetTests/Cell/RowRangeTest.php b/tests/PhpSpreadsheetTests/Cell/RowRangeTest.php index 41d4992c..32605cf0 100644 --- a/tests/PhpSpreadsheetTests/Cell/RowRangeTest.php +++ b/tests/PhpSpreadsheetTests/Cell/RowRangeTest.php @@ -48,4 +48,28 @@ class RowRangeTest extends TestCase self::assertSame(3, $rowRange->rowCount()); self::assertSame('A3:XFD5', (string) $rowRange->toCellRange()); } + + public function testRowRangeNext(): void + { + $rowRange = new RowRange(3, 5); + $rowRangeNext = $rowRange->shiftRight(3); + + self::assertSame(6, $rowRangeNext->from()); + self::assertSame(8, $rowRangeNext->to()); + + // Check that original Row Range isn't changed + self::assertSame('3:5', (string) $rowRange); + } + + public function testRowRangePrevious(): void + { + $rowRange = new RowRange(3, 5); + $rowRangeNext = $rowRange->shiftLeft(); + + self::assertSame(2, $rowRangeNext->from()); + self::assertSame(4, $rowRangeNext->to()); + + // Check that original Row Range isn't changed + self::assertSame('3:5', (string) $rowRange); + } }