1 |
efrain |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace PhpOffice\PhpSpreadsheet\Worksheet;
|
|
|
4 |
|
|
|
5 |
use PhpOffice\PhpSpreadsheet\Cell\AddressRange;
|
|
|
6 |
use PhpOffice\PhpSpreadsheet\Cell\CellAddress;
|
|
|
7 |
use PhpOffice\PhpSpreadsheet\Cell\CellRange;
|
|
|
8 |
use PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException;
|
|
|
9 |
|
|
|
10 |
class Validations
|
|
|
11 |
{
|
|
|
12 |
/**
|
|
|
13 |
* Validate a cell address.
|
|
|
14 |
*
|
|
|
15 |
* @param null|array<int>|CellAddress|string $cellAddress Coordinate of the cell as a string, eg: 'C5';
|
|
|
16 |
* or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object.
|
|
|
17 |
*/
|
|
|
18 |
public static function validateCellAddress($cellAddress): string
|
|
|
19 |
{
|
|
|
20 |
if (is_string($cellAddress)) {
|
|
|
21 |
[$worksheet, $address] = Worksheet::extractSheetTitle($cellAddress, true);
|
|
|
22 |
// if (!empty($worksheet) && $worksheet !== $this->getTitle()) {
|
|
|
23 |
// throw new Exception('Reference is not for this worksheet');
|
|
|
24 |
// }
|
|
|
25 |
|
|
|
26 |
return empty($worksheet) ? strtoupper("$address") : $worksheet . '!' . strtoupper("$address");
|
|
|
27 |
}
|
|
|
28 |
|
|
|
29 |
if (is_array($cellAddress)) {
|
|
|
30 |
$cellAddress = CellAddress::fromColumnRowArray($cellAddress);
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
return (string) $cellAddress;
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* Validate a cell address or cell range.
|
|
|
38 |
*
|
|
|
39 |
* @param AddressRange|array<int>|CellAddress|int|string $cellRange Coordinate of the cells as a string, eg: 'C5:F12';
|
|
|
40 |
* or as an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 12]),
|
|
|
41 |
* or as a CellAddress or AddressRange object.
|
|
|
42 |
*/
|
|
|
43 |
public static function validateCellOrCellRange($cellRange): string
|
|
|
44 |
{
|
|
|
45 |
if (is_string($cellRange) || is_numeric($cellRange)) {
|
|
|
46 |
// Convert a single column reference like 'A' to 'A:A',
|
|
|
47 |
// a single row reference like '1' to '1:1'
|
|
|
48 |
$cellRange = (string) preg_replace('/^([A-Z]+|\d+)$/', '${1}:${1}', (string) $cellRange);
|
|
|
49 |
} elseif (is_object($cellRange) && $cellRange instanceof CellAddress) {
|
|
|
50 |
$cellRange = new CellRange($cellRange, $cellRange);
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
return self::validateCellRange($cellRange);
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
private const SETMAXROW = '${1}1:${2}' . AddressRange::MAX_ROW;
|
|
|
57 |
private const SETMAXCOL = 'A${1}:' . AddressRange::MAX_COLUMN . '${2}';
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
* Validate a cell range.
|
|
|
61 |
*
|
|
|
62 |
* @param AddressRange|array<int>|string $cellRange Coordinate of the cells as a string, eg: 'C5:F12';
|
|
|
63 |
* or as an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 12]),
|
|
|
64 |
* or as an AddressRange object.
|
|
|
65 |
*/
|
|
|
66 |
public static function validateCellRange($cellRange): string
|
|
|
67 |
{
|
|
|
68 |
if (is_string($cellRange)) {
|
|
|
69 |
[$worksheet, $addressRange] = Worksheet::extractSheetTitle($cellRange, true);
|
|
|
70 |
|
|
|
71 |
// Convert Column ranges like 'A:C' to 'A1:C1048576'
|
|
|
72 |
// or Row ranges like '1:3' to 'A1:XFD3'
|
|
|
73 |
$addressRange = (string) preg_replace(
|
|
|
74 |
['/^([A-Z]+):([A-Z]+)$/i', '/^(\\d+):(\\d+)$/'],
|
|
|
75 |
[self::SETMAXROW, self::SETMAXCOL],
|
|
|
76 |
$addressRange
|
|
|
77 |
);
|
|
|
78 |
|
|
|
79 |
return empty($worksheet) ? strtoupper($addressRange) : $worksheet . '!' . strtoupper($addressRange);
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
if (is_array($cellRange)) {
|
|
|
83 |
switch (count($cellRange)) {
|
|
|
84 |
case 2:
|
|
|
85 |
$from = [$cellRange[0], $cellRange[1]];
|
|
|
86 |
$to = [$cellRange[0], $cellRange[1]];
|
|
|
87 |
|
|
|
88 |
break;
|
|
|
89 |
case 4:
|
|
|
90 |
$from = [$cellRange[0], $cellRange[1]];
|
|
|
91 |
$to = [$cellRange[2], $cellRange[3]];
|
|
|
92 |
|
|
|
93 |
break;
|
|
|
94 |
default:
|
|
|
95 |
throw new SpreadsheetException('CellRange array length must be 2 or 4');
|
|
|
96 |
}
|
|
|
97 |
$cellRange = new CellRange(CellAddress::fromColumnRowArray($from), CellAddress::fromColumnRowArray($to));
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
return (string) $cellRange;
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
public static function definedNameToCoordinate(string $coordinate, Worksheet $worksheet): string
|
|
|
104 |
{
|
|
|
105 |
// Uppercase coordinate
|
|
|
106 |
$coordinate = strtoupper($coordinate);
|
|
|
107 |
// Eliminate leading equal sign
|
|
|
108 |
$testCoordinate = (string) preg_replace('/^=/', '', $coordinate);
|
|
|
109 |
$defined = $worksheet->getParentOrThrow()->getDefinedName($testCoordinate, $worksheet);
|
|
|
110 |
if ($defined !== null) {
|
|
|
111 |
if ($defined->getWorksheet() === $worksheet && !$defined->isFormula()) {
|
|
|
112 |
$coordinate = (string) preg_replace('/^=/', '', $defined->getValue());
|
|
|
113 |
}
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
return $coordinate;
|
|
|
117 |
}
|
|
|
118 |
}
|