Add functionality to shift RowRange and ColumnRange

This commit is contained in:
MarkBaker 2022-04-01 11:26:47 +02:00
parent 62238bc011
commit 1849737abc
5 changed files with 82 additions and 0 deletions

View File

@ -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);
}

View File

@ -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);

View File

@ -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(

View File

@ -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);
}
}

View File

@ -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);
}
}