|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PhpOffice\PhpSpreadsheet; |
| 4 | + |
| 5 | +use PhpOffice\PhpSpreadsheet\Cell\Coordinate; |
| 6 | + |
| 7 | +class CellReferenceHelper |
| 8 | +{ |
| 9 | + /** |
| 10 | + * @var string |
| 11 | + */ |
| 12 | + protected $beforeCellAddress; |
| 13 | + |
| 14 | + /** |
| 15 | + * @var int |
| 16 | + */ |
| 17 | + protected $beforeColumn; |
| 18 | + |
| 19 | + /** |
| 20 | + * @var int |
| 21 | + */ |
| 22 | + protected $beforeRow; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var int |
| 26 | + */ |
| 27 | + protected $numberOfColumns; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var int |
| 31 | + */ |
| 32 | + protected $numberOfRows; |
| 33 | + |
| 34 | + public function __construct(string $beforeCellAddress = 'A1', int $numberOfColumns = 0, int $numberOfRows = 0) |
| 35 | + { |
| 36 | + $this->beforeCellAddress = str_replace('$', '', $beforeCellAddress); |
| 37 | + $this->numberOfColumns = $numberOfColumns; |
| 38 | + $this->numberOfRows = $numberOfRows; |
| 39 | + |
| 40 | + // Get coordinate of $beforeCellAddress |
| 41 | + [$beforeColumn, $beforeRow] = Coordinate::coordinateFromString($beforeCellAddress); |
| 42 | + $this->beforeColumn = (int) Coordinate::columnIndexFromString($beforeColumn); |
| 43 | + $this->beforeRow = (int) $beforeRow; |
| 44 | + } |
| 45 | + |
| 46 | + public function refreshRequired(string $beforeCellAddress, int $numberOfColumns, int $numberOfRows): bool |
| 47 | + { |
| 48 | + return $this->beforeCellAddress !== $beforeCellAddress || |
| 49 | + $this->numberOfColumns !== $numberOfColumns || |
| 50 | + $this->numberOfRows !== $numberOfRows; |
| 51 | + } |
| 52 | + |
| 53 | + public function updateCellReference(string $cellReference = 'A1'): string |
| 54 | + { |
| 55 | + if (Coordinate::coordinateIsRange($cellReference)) { |
| 56 | + throw new Exception('Only single cell references may be passed to this method.'); |
| 57 | + } |
| 58 | + |
| 59 | + // Get coordinate of $cellReference |
| 60 | + [$newColumn, $newRow] = Coordinate::coordinateFromString($cellReference); |
| 61 | + $newColumnIndex = (int) Coordinate::columnIndexFromString(str_replace('$', '', $newColumn)); |
| 62 | + $newRowIndex = (int) str_replace('$', '', $newRow); |
| 63 | + |
| 64 | + // Verify which parts should be updated |
| 65 | + $updateColumn = (($newColumn[0] !== '$') && $newColumnIndex >= $this->beforeColumn); |
| 66 | + $updateRow = (($newRow[0] !== '$') && $newRow >= $this->beforeRow); |
| 67 | + |
| 68 | + // Create new column reference |
| 69 | + if ($updateColumn) { |
| 70 | + $newColumn = Coordinate::stringFromColumnIndex($newColumnIndex + $this->numberOfColumns); |
| 71 | + } |
| 72 | + |
| 73 | + // Create new row reference |
| 74 | + if ($updateRow) { |
| 75 | + $newRow = $newRowIndex + $this->numberOfRows; |
| 76 | + } |
| 77 | + |
| 78 | + // Return new reference |
| 79 | + return "{$newColumn}{$newRow}"; |
| 80 | + } |
| 81 | + |
| 82 | + public function cellAddressInDeleteRange(string $cellAddress): bool |
| 83 | + { |
| 84 | + [$cellColumn, $cellRow] = Coordinate::coordinateFromString($cellAddress); |
| 85 | + $cellColumnIndex = Coordinate::columnIndexFromString($cellColumn); |
| 86 | + // Is cell within the range of rows/columns if we're deleting |
| 87 | + if ( |
| 88 | + $this->numberOfRows < 0 && |
| 89 | + ($cellRow >= ($this->beforeRow + $this->numberOfRows)) && |
| 90 | + ($cellRow < $this->beforeRow) |
| 91 | + ) { |
| 92 | + return true; |
| 93 | + } elseif ( |
| 94 | + $this->numberOfColumns < 0 && |
| 95 | + ($cellColumnIndex >= ($this->beforeColumn + $this->numberOfColumns)) && |
| 96 | + ($cellColumnIndex < $this->beforeColumn) |
| 97 | + ) { |
| 98 | + return true; |
| 99 | + } |
| 100 | + |
| 101 | + return false; |
| 102 | + } |
| 103 | +} |
0 commit comments