1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Calculation; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Coordinate; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Style; |
9
|
|
|
|
10
|
|
|
abstract class WizardAbstract |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var ?Style |
14
|
|
|
*/ |
15
|
|
|
protected $style; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $expression; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $cellRange; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $referenceCell; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var int |
34
|
|
|
*/ |
35
|
|
|
protected $referenceRow; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var bool |
39
|
|
|
*/ |
40
|
|
|
protected $stopIfTrue = false; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var int |
44
|
|
|
*/ |
45
|
|
|
protected $referenceColumn; |
46
|
|
|
|
47
|
99 |
|
public function __construct(string $cellRange) |
48
|
|
|
{ |
49
|
99 |
|
$this->setCellRange($cellRange); |
50
|
|
|
} |
51
|
|
|
|
52
|
17 |
|
public function getCellRange(): string |
53
|
|
|
{ |
54
|
17 |
|
return $this->cellRange; |
55
|
|
|
} |
56
|
|
|
|
57
|
99 |
|
public function setCellRange(string $cellRange): void |
58
|
|
|
{ |
59
|
99 |
|
$this->cellRange = $cellRange; |
60
|
99 |
|
$this->setReferenceCellForExpressions($cellRange); |
61
|
|
|
} |
62
|
|
|
|
63
|
99 |
|
protected function setReferenceCellForExpressions(string $conditionalRange): void |
64
|
|
|
{ |
65
|
99 |
|
$conditionalRange = Coordinate::splitRange(str_replace('$', '', strtoupper($conditionalRange))); |
66
|
99 |
|
[$this->referenceCell] = $conditionalRange[0]; |
67
|
|
|
|
68
|
99 |
|
[$this->referenceColumn, $this->referenceRow] = Coordinate::indexesFromString($this->referenceCell); |
69
|
|
|
} |
70
|
|
|
|
71
|
81 |
|
public function getStopIfTrue(): bool |
72
|
|
|
{ |
73
|
81 |
|
return $this->stopIfTrue; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function setStopIfTrue(bool $stopIfTrue): void |
77
|
|
|
{ |
78
|
|
|
$this->stopIfTrue = $stopIfTrue; |
79
|
|
|
} |
80
|
|
|
|
81
|
81 |
|
public function getStyle(): Style |
82
|
|
|
{ |
83
|
81 |
|
return $this->style ?? new Style(false, true); |
84
|
|
|
} |
85
|
|
|
|
86
|
66 |
|
public function setStyle(Style $style): void |
87
|
|
|
{ |
88
|
66 |
|
$this->style = $style; |
89
|
|
|
} |
90
|
|
|
|
91
|
35 |
|
protected function validateOperand(string $operand, string $operandValueType = Wizard::VALUE_TYPE_LITERAL): string |
92
|
|
|
{ |
93
|
|
|
if ( |
94
|
|
|
$operandValueType === Wizard::VALUE_TYPE_LITERAL && |
95
|
35 |
|
substr($operand, 0, 1) === '"' && |
96
|
35 |
|
substr($operand, -1) === '"' |
97
|
|
|
) { |
98
|
|
|
$operand = str_replace('""', '"', substr($operand, 1, -1)); |
99
|
35 |
|
} elseif ($operandValueType === Wizard::VALUE_TYPE_FORMULA && substr($operand, 0, 1) === '=') { |
100
|
1 |
|
$operand = substr($operand, 1); |
101
|
|
|
} |
102
|
|
|
|
103
|
35 |
|
return $operand; |
104
|
|
|
} |
105
|
|
|
|
106
|
22 |
|
protected static function reverseCellAdjustment(array $matches, int $referenceColumn, int $referenceRow): string |
107
|
|
|
{ |
108
|
22 |
|
$worksheet = $matches[1]; |
109
|
22 |
|
$column = $matches[6]; |
110
|
22 |
|
$row = $matches[7]; |
111
|
|
|
|
112
|
22 |
|
if (strpos($column, '$') === false) { |
113
|
10 |
|
$column = Coordinate::columnIndexFromString($column); |
114
|
10 |
|
$column -= $referenceColumn - 1; |
115
|
10 |
|
$column = Coordinate::stringFromColumnIndex($column); |
116
|
|
|
} |
117
|
|
|
|
118
|
22 |
|
if (strpos($row, '$') === false) { |
119
|
14 |
|
$row -= $referenceRow - 1; |
120
|
|
|
} |
121
|
|
|
|
122
|
22 |
|
return "{$worksheet}{$column}{$row}"; |
123
|
|
|
} |
124
|
|
|
|
125
|
25 |
|
public static function reverseAdjustCellRef(string $condition, string $cellRange): string |
126
|
|
|
{ |
127
|
25 |
|
$conditionalRange = Coordinate::splitRange(str_replace('$', '', strtoupper($cellRange))); |
128
|
25 |
|
[$referenceCell] = $conditionalRange[0]; |
129
|
25 |
|
[$referenceColumnIndex, $referenceRow] = Coordinate::indexesFromString($referenceCell); |
130
|
|
|
|
131
|
25 |
|
$splitCondition = explode(Calculation::FORMULA_STRING_QUOTE, $condition); |
132
|
25 |
|
$i = false; |
133
|
25 |
|
foreach ($splitCondition as &$value) { |
134
|
|
|
// Only count/replace in alternating array entries (ie. not in quoted strings) |
135
|
25 |
|
$i = $i === false; |
136
|
25 |
|
if ($i) { |
137
|
25 |
|
$value = (string) preg_replace_callback( |
138
|
|
|
'/' . Calculation::CALCULATION_REGEXP_CELLREF_RELATIVE . '/i', |
139
|
25 |
|
function ($matches) use ($referenceColumnIndex, $referenceRow) { |
140
|
22 |
|
return self::reverseCellAdjustment($matches, $referenceColumnIndex, $referenceRow); |
141
|
|
|
}, |
142
|
|
|
$value |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
} |
146
|
25 |
|
unset($value); |
147
|
|
|
|
148
|
|
|
// Then rebuild the condition string to return it |
149
|
25 |
|
return implode(Calculation::FORMULA_STRING_QUOTE, $splitCondition); |
150
|
|
|
} |
151
|
|
|
|
152
|
27 |
|
protected function conditionCellAdjustment(array $matches): string |
153
|
|
|
{ |
154
|
27 |
|
$worksheet = $matches[1]; |
155
|
27 |
|
$column = $matches[6]; |
156
|
27 |
|
$row = $matches[7]; |
157
|
|
|
|
158
|
27 |
|
if (strpos($column, '$') === false) { |
159
|
11 |
|
$column = Coordinate::columnIndexFromString($column); |
160
|
11 |
|
$column += $this->referenceColumn - 1; |
161
|
11 |
|
$column = Coordinate::stringFromColumnIndex($column); |
162
|
|
|
} |
163
|
|
|
|
164
|
27 |
|
if (strpos($row, '$') === false) { |
165
|
16 |
|
$row += $this->referenceRow - 1; |
166
|
|
|
} |
167
|
|
|
|
168
|
27 |
|
return "{$worksheet}{$column}{$row}"; |
169
|
|
|
} |
170
|
|
|
|
171
|
28 |
|
protected function cellConditionCheck(string $condition): string |
172
|
|
|
{ |
173
|
28 |
|
$splitCondition = explode(Calculation::FORMULA_STRING_QUOTE, $condition); |
174
|
28 |
|
$i = false; |
175
|
28 |
|
foreach ($splitCondition as &$value) { |
176
|
|
|
// Only count/replace in alternating array entries (ie. not in quoted strings) |
177
|
28 |
|
$i = $i === false; |
178
|
28 |
|
if ($i) { |
179
|
28 |
|
$value = (string) preg_replace_callback( |
180
|
|
|
'/' . Calculation::CALCULATION_REGEXP_CELLREF_RELATIVE . '/i', |
181
|
|
|
[$this, 'conditionCellAdjustment'], |
182
|
|
|
$value |
183
|
|
|
); |
184
|
|
|
} |
185
|
|
|
} |
186
|
28 |
|
unset($value); |
187
|
|
|
|
188
|
|
|
// Then rebuild the condition string to return it |
189
|
28 |
|
return implode(Calculation::FORMULA_STRING_QUOTE, $splitCondition); |
190
|
|
|
} |
191
|
|
|
|
192
|
6 |
|
protected function adjustConditionsForCellReferences(array $conditions): array |
193
|
|
|
{ |
194
|
6 |
|
return array_map( |
195
|
|
|
[$this, 'cellConditionCheck'], |
196
|
|
|
$conditions |
197
|
|
|
); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|