| 1441 |
ariadna |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace PhpOffice\PhpSpreadsheet\Reader\Xml;
|
|
|
4 |
|
|
|
5 |
use PhpOffice\PhpSpreadsheet\Cell\AddressHelper;
|
|
|
6 |
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
|
|
|
7 |
use PhpOffice\PhpSpreadsheet\Cell\DataValidation;
|
|
|
8 |
use PhpOffice\PhpSpreadsheet\Reader\Xlsx\Namespaces;
|
|
|
9 |
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
|
10 |
use SimpleXMLElement;
|
|
|
11 |
|
|
|
12 |
class DataValidations
|
|
|
13 |
{
|
|
|
14 |
private const OPERATOR_MAPPINGS = [
|
|
|
15 |
'between' => DataValidation::OPERATOR_BETWEEN,
|
|
|
16 |
'equal' => DataValidation::OPERATOR_EQUAL,
|
|
|
17 |
'greater' => DataValidation::OPERATOR_GREATERTHAN,
|
|
|
18 |
'greaterorequal' => DataValidation::OPERATOR_GREATERTHANOREQUAL,
|
|
|
19 |
'less' => DataValidation::OPERATOR_LESSTHAN,
|
|
|
20 |
'lessorequal' => DataValidation::OPERATOR_LESSTHANOREQUAL,
|
|
|
21 |
'notbetween' => DataValidation::OPERATOR_NOTBETWEEN,
|
|
|
22 |
'notequal' => DataValidation::OPERATOR_NOTEQUAL,
|
|
|
23 |
];
|
|
|
24 |
|
|
|
25 |
private const TYPE_MAPPINGS = [
|
|
|
26 |
'textlength' => DataValidation::TYPE_TEXTLENGTH,
|
|
|
27 |
];
|
|
|
28 |
|
|
|
29 |
private int $thisRow = 0;
|
|
|
30 |
|
|
|
31 |
private int $thisColumn = 0;
|
|
|
32 |
|
|
|
33 |
private function replaceR1C1(array $matches): string
|
|
|
34 |
{
|
|
|
35 |
return AddressHelper::convertToA1($matches[0], $this->thisRow, $this->thisColumn, false);
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
public function loadDataValidations(SimpleXMLElement $worksheet, Spreadsheet $spreadsheet): void
|
|
|
39 |
{
|
|
|
40 |
$xmlX = $worksheet->children(Namespaces::URN_EXCEL);
|
|
|
41 |
$sheet = $spreadsheet->getActiveSheet();
|
|
|
42 |
/** @var callable $pregCallback */
|
|
|
43 |
$pregCallback = [$this, 'replaceR1C1'];
|
|
|
44 |
foreach ($xmlX->DataValidation as $dataValidation) {
|
|
|
45 |
$combinedCells = '';
|
|
|
46 |
$separator = '';
|
|
|
47 |
$validation = new DataValidation();
|
|
|
48 |
|
|
|
49 |
// set defaults
|
|
|
50 |
$validation->setShowDropDown(true);
|
|
|
51 |
$validation->setShowInputMessage(true);
|
|
|
52 |
$validation->setShowErrorMessage(true);
|
|
|
53 |
$validation->setShowDropDown(true);
|
|
|
54 |
$this->thisRow = 1;
|
|
|
55 |
$this->thisColumn = 1;
|
|
|
56 |
|
|
|
57 |
foreach ($dataValidation as $tagName => $tagValue) {
|
|
|
58 |
$tagValue = (string) $tagValue;
|
|
|
59 |
$tagValueLower = strtolower($tagValue);
|
|
|
60 |
switch ($tagName) {
|
|
|
61 |
case 'Range':
|
|
|
62 |
foreach (explode(',', $tagValue) as $range) {
|
|
|
63 |
$cell = '';
|
|
|
64 |
if (preg_match('/^R(\d+)C(\d+):R(\d+)C(\d+)$/', (string) $range, $selectionMatches) === 1) {
|
|
|
65 |
// range
|
|
|
66 |
$firstCell = Coordinate::stringFromColumnIndex((int) $selectionMatches[2])
|
|
|
67 |
. $selectionMatches[1];
|
|
|
68 |
$cell = $firstCell
|
|
|
69 |
. ':'
|
|
|
70 |
. Coordinate::stringFromColumnIndex((int) $selectionMatches[4])
|
|
|
71 |
. $selectionMatches[3];
|
|
|
72 |
$this->thisRow = (int) $selectionMatches[1];
|
|
|
73 |
$this->thisColumn = (int) $selectionMatches[2];
|
|
|
74 |
$sheet->getCell($firstCell);
|
|
|
75 |
$combinedCells .= "$separator$cell";
|
|
|
76 |
$separator = ' ';
|
|
|
77 |
} elseif (preg_match('/^R(\d+)C(\d+)$/', (string) $range, $selectionMatches) === 1) {
|
|
|
78 |
// cell
|
|
|
79 |
$cell = Coordinate::stringFromColumnIndex((int) $selectionMatches[2])
|
|
|
80 |
. $selectionMatches[1];
|
|
|
81 |
$sheet->getCell($cell);
|
|
|
82 |
$this->thisRow = (int) $selectionMatches[1];
|
|
|
83 |
$this->thisColumn = (int) $selectionMatches[2];
|
|
|
84 |
$combinedCells .= "$separator$cell";
|
|
|
85 |
$separator = ' ';
|
|
|
86 |
} elseif (preg_match('/^C(\d+)(:C(]\d+))?$/', (string) $range, $selectionMatches) === 1) {
|
|
|
87 |
// column
|
|
|
88 |
$firstCol = $selectionMatches[1];
|
|
|
89 |
$firstColString = Coordinate::stringFromColumnIndex((int) $firstCol);
|
|
|
90 |
$lastCol = $selectionMatches[3] ?? $firstCol;
|
|
|
91 |
$lastColString = Coordinate::stringFromColumnIndex((int) $lastCol);
|
|
|
92 |
$firstCell = "{$firstColString}1";
|
|
|
93 |
$cell = "$firstColString:$lastColString";
|
|
|
94 |
$this->thisColumn = (int) $firstCol;
|
|
|
95 |
$sheet->getCell($firstCell);
|
|
|
96 |
$combinedCells .= "$separator$cell";
|
|
|
97 |
$separator = ' ';
|
|
|
98 |
} elseif (preg_match('/^R(\d+)(:R(]\d+))?$/', (string) $range, $selectionMatches)) {
|
|
|
99 |
// row
|
|
|
100 |
$firstRow = $selectionMatches[1];
|
|
|
101 |
$lastRow = $selectionMatches[3] ?? $firstRow;
|
|
|
102 |
$firstCell = "A$firstRow";
|
|
|
103 |
$cell = "$firstRow:$lastRow";
|
|
|
104 |
$this->thisRow = (int) $firstRow;
|
|
|
105 |
$sheet->getCell($firstCell);
|
|
|
106 |
$combinedCells .= "$separator$cell";
|
|
|
107 |
$separator = ' ';
|
|
|
108 |
}
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
break;
|
|
|
112 |
case 'Type':
|
|
|
113 |
$validation->setType(self::TYPE_MAPPINGS[$tagValueLower] ?? $tagValueLower);
|
|
|
114 |
|
|
|
115 |
break;
|
|
|
116 |
case 'Qualifier':
|
|
|
117 |
$validation->setOperator(self::OPERATOR_MAPPINGS[$tagValueLower] ?? $tagValueLower);
|
|
|
118 |
|
|
|
119 |
break;
|
|
|
120 |
case 'InputTitle':
|
|
|
121 |
$validation->setPromptTitle($tagValue);
|
|
|
122 |
|
|
|
123 |
break;
|
|
|
124 |
case 'InputMessage':
|
|
|
125 |
$validation->setPrompt($tagValue);
|
|
|
126 |
|
|
|
127 |
break;
|
|
|
128 |
case 'InputHide':
|
|
|
129 |
$validation->setShowInputMessage(false);
|
|
|
130 |
|
|
|
131 |
break;
|
|
|
132 |
case 'ErrorStyle':
|
|
|
133 |
$validation->setErrorStyle($tagValueLower);
|
|
|
134 |
|
|
|
135 |
break;
|
|
|
136 |
case 'ErrorTitle':
|
|
|
137 |
$validation->setErrorTitle($tagValue);
|
|
|
138 |
|
|
|
139 |
break;
|
|
|
140 |
case 'ErrorMessage':
|
|
|
141 |
$validation->setError($tagValue);
|
|
|
142 |
|
|
|
143 |
break;
|
|
|
144 |
case 'ErrorHide':
|
|
|
145 |
$validation->setShowErrorMessage(false);
|
|
|
146 |
|
|
|
147 |
break;
|
|
|
148 |
case 'ComboHide':
|
|
|
149 |
$validation->setShowDropDown(false);
|
|
|
150 |
|
|
|
151 |
break;
|
|
|
152 |
case 'UseBlank':
|
|
|
153 |
$validation->setAllowBlank(true);
|
|
|
154 |
|
|
|
155 |
break;
|
|
|
156 |
case 'CellRangeList':
|
|
|
157 |
// FIXME missing FIXME
|
|
|
158 |
|
|
|
159 |
break;
|
|
|
160 |
case 'Min':
|
|
|
161 |
case 'Value':
|
|
|
162 |
$tagValue = (string) preg_replace_callback(AddressHelper::R1C1_COORDINATE_REGEX, $pregCallback, $tagValue);
|
|
|
163 |
$validation->setFormula1($tagValue);
|
|
|
164 |
|
|
|
165 |
break;
|
|
|
166 |
case 'Max':
|
|
|
167 |
$tagValue = (string) preg_replace_callback(AddressHelper::R1C1_COORDINATE_REGEX, $pregCallback, $tagValue);
|
|
|
168 |
$validation->setFormula2($tagValue);
|
|
|
169 |
|
|
|
170 |
break;
|
|
|
171 |
}
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
$sheet->setDataValidation($combinedCells, $validation);
|
|
|
175 |
}
|
|
|
176 |
}
|
|
|
177 |
}
|