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