Add functionality to shift RowRange and ColumnRange
This commit is contained in:
parent
62238bc011
commit
1849737abc
|
|
@ -66,6 +66,7 @@ class CellAddress
|
||||||
{
|
{
|
||||||
self::validateColumnAndRow($columnId, $rowId);
|
self::validateColumnAndRow($columnId, $rowId);
|
||||||
|
|
||||||
|
/** @phpstan-ignore-next-line */
|
||||||
return new static(Coordinate::stringFromColumnIndex($columnId) . ((string) $rowId), $worksheet);
|
return new static(Coordinate::stringFromColumnIndex($columnId) . ((string) $rowId), $worksheet);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -81,6 +82,7 @@ class CellAddress
|
||||||
*/
|
*/
|
||||||
public static function fromCellAddress($cellAddress, ?Worksheet $worksheet = null): self
|
public static function fromCellAddress($cellAddress, ?Worksheet $worksheet = null): self
|
||||||
{
|
{
|
||||||
|
/** @phpstan-ignore-next-line */
|
||||||
return new static($cellAddress, $worksheet);
|
return new static($cellAddress, $worksheet);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -67,6 +67,22 @@ class ColumnRange
|
||||||
return $this->to - $this->from + 1;
|
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
|
public function from(): string
|
||||||
{
|
{
|
||||||
return Coordinate::stringFromColumnIndex($this->from);
|
return Coordinate::stringFromColumnIndex($this->from);
|
||||||
|
|
|
||||||
|
|
@ -58,6 +58,22 @@ class RowRange
|
||||||
return $this->to - $this->from + 1;
|
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
|
public function toCellRange(): CellRange
|
||||||
{
|
{
|
||||||
return new CellRange(
|
return new CellRange(
|
||||||
|
|
|
||||||
|
|
@ -62,4 +62,28 @@ class ColumnRangeTest extends TestCase
|
||||||
self::assertSame(3, $columnRange->columnCount());
|
self::assertSame(3, $columnRange->columnCount());
|
||||||
self::assertSame('C1:E1048576', (string) $columnRange->toCellRange());
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -48,4 +48,28 @@ class RowRangeTest extends TestCase
|
||||||
self::assertSame(3, $rowRange->rowCount());
|
self::assertSame(3, $rowRange->rowCount());
|
||||||
self::assertSame('A3:XFD5', (string) $rowRange->toCellRange());
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue