| 1 |
<?php |
| 2 |
|
| 3 |
namespace PhpOffice\PhpSpreadsheet\Cell; |
| 4 |
|
| 5 |
use PhpOffice\PhpSpreadsheet\Exception; |
| 6 |
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; |
| 7 |
|
| 8 |
class CellAddress |
| 9 |
{ |
| 10 |
/** |
| 11 |
* @var ?Worksheet |
| 12 |
*/ |
| 13 |
protected $worksheet; |
| 14 |
|
| 15 |
/** |
| 16 |
* @var string |
| 17 |
*/ |
| 18 |
protected $cellAddress; |
| 19 |
|
| 20 |
/** |
| 21 |
* @var string |
| 22 |
*/ |
| 23 |
protected $columnName; |
| 24 |
|
| 25 |
/** |
| 26 |
* @var int |
| 27 |
*/ |
| 28 |
protected $columnId; |
| 29 |
|
| 30 |
/** |
| 31 |
* @var int |
| 32 |
*/ |
| 33 |
protected $rowId; |
| 34 |
|
| 35 |
public function __construct(string $cellAddress, ?Worksheet $worksheet = null) |
| 36 |
{ |
| 37 |
$this->cellAddress = str_replace('$', '', $cellAddress); |
| 38 |
[$this->columnId, $this->rowId, $this->columnName] = Coordinate::indexesFromString($this->cellAddress); |
| 39 |
$this->worksheet = $worksheet; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* @param mixed $columnId |
| 44 |
* @param mixed $rowId |
| 45 |
*/ |
| 46 |
private static function validateColumnAndRow($columnId, $rowId): void |
| 47 |
{ |
| 48 |
if (!is_numeric($columnId) || $columnId <= 0 || !is_numeric($rowId) || $rowId <= 0) { |
| 49 |
throw new Exception('Row and Column Ids must be positive integer values'); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* @param mixed $columnId |
| 55 |
* @param mixed $rowId |
| 56 |
*/ |
| 57 |
public static function fromColumnAndRow($columnId, $rowId, ?Worksheet $worksheet = null): self |
| 58 |
{ |
| 59 |
self::validateColumnAndRow($columnId, $rowId); |
| 60 |
|
| 61 |
/** @phpstan-ignore-next-line */ |
| 62 |
return new static(Coordinate::stringFromColumnIndex($columnId) . ((string) $rowId), $worksheet); |
| 63 |
} |
| 64 |
|
| 65 |
public static function fromColumnRowArray(array $array, ?Worksheet $worksheet = null): self |
| 66 |
{ |
| 67 |
[$columnId, $rowId] = $array; |
| 68 |
|
| 69 |
return static::fromColumnAndRow($columnId, $rowId, $worksheet); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* @param mixed $cellAddress |
| 74 |
*/ |
| 75 |
public static function fromCellAddress($cellAddress, ?Worksheet $worksheet = null): self |
| 76 |
{ |
| 77 |
/** @phpstan-ignore-next-line */ |
| 78 |
return new static($cellAddress, $worksheet); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* The returned address string will contain the worksheet name as well, if available, |
| 83 |
* (ie. if a Worksheet was provided to the constructor). |
| 84 |
* e.g. "'Mark''s Worksheet'!C5". |
| 85 |
*/ |
| 86 |
public function fullCellAddress(): string |
| 87 |
{ |
| 88 |
if ($this->worksheet !== null) { |
| 89 |
$title = str_replace("'", "''", $this->worksheet->getTitle()); |
| 90 |
|
| 91 |
return "'{$title}'!{$this->cellAddress}"; |
| 92 |
} |
| 93 |
|
| 94 |
return $this->cellAddress; |
| 95 |
} |
| 96 |
|
| 97 |
public function worksheet(): ?Worksheet |
| 98 |
{ |
| 99 |
return $this->worksheet; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* The returned address string will contain just the column/row address, |
| 104 |
* (even if a Worksheet was provided to the constructor). |
| 105 |
* e.g. "C5". |
| 106 |
*/ |
| 107 |
public function cellAddress(): string |
| 108 |
{ |
| 109 |
return $this->cellAddress; |
| 110 |
} |
| 111 |
|
| 112 |
public function rowId(): int |
| 113 |
{ |
| 114 |
return $this->rowId; |
| 115 |
} |
| 116 |
|
| 117 |
public function columnId(): int |
| 118 |
{ |
| 119 |
return $this->columnId; |
| 120 |
} |
| 121 |
|
| 122 |
public function columnName(): string |
| 123 |
{ |
| 124 |
return $this->columnName; |
| 125 |
} |
| 126 |
|
| 127 |
public function nextRow(int $offset = 1): self |
| 128 |
{ |
| 129 |
$newRowId = $this->rowId + $offset; |
| 130 |
if ($newRowId < 1) { |
| 131 |
$newRowId = 1; |
| 132 |
} |
| 133 |
|
| 134 |
return static::fromColumnAndRow($this->columnId, $newRowId); |
| 135 |
} |
| 136 |
|
| 137 |
public function previousRow(int $offset = 1): self |
| 138 |
{ |
| 139 |
return $this->nextRow(0 - $offset); |
| 140 |
} |
| 141 |
|
| 142 |
public function nextColumn(int $offset = 1): self |
| 143 |
{ |
| 144 |
$newColumnId = $this->columnId + $offset; |
| 145 |
if ($newColumnId < 1) { |
| 146 |
$newColumnId = 1; |
| 147 |
} |
| 148 |
|
| 149 |
return static::fromColumnAndRow($newColumnId, $this->rowId); |
| 150 |
} |
| 151 |
|
| 152 |
public function previousColumn(int $offset = 1): self |
| 153 |
{ |
| 154 |
return $this->nextColumn(0 - $offset); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* The returned address string will contain the worksheet name as well, if available, |
| 159 |
* (ie. if a Worksheet was provided to the constructor). |
| 160 |
* e.g. "'Mark''s Worksheet'!C5". |
| 161 |
*/ |
| 162 |
public function __toString() |
| 163 |
{ |
| 164 |
return $this->fullCellAddress(); |
| 165 |
} |
| 166 |
} |
| 167 |
|