1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Functional; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Conditional; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Fill; |
8
|
|
|
|
9
|
|
|
class ConditionalTextTest extends AbstractFunctional |
10
|
|
|
{ |
11
|
|
|
const COLOR_GREEN = 'FF99FF66'; |
12
|
|
|
const COLOR_RED = 'FFFF5050'; |
13
|
|
|
const COLOR_BLUE = 'FF5050FF'; |
14
|
|
|
const COLOR_YELLOW = 'FFFFFF50'; |
15
|
|
|
|
16
|
|
|
public function testConditionalText(): void |
17
|
|
|
{ |
18
|
|
|
$format = 'Xlsx'; |
19
|
|
|
$spreadsheet = new Spreadsheet(); |
20
|
|
|
|
21
|
|
|
$conditionalStyles = []; |
22
|
|
|
// if text contains 'anywhere' - red background |
23
|
|
|
$condition0 = new Conditional(); |
24
|
|
|
$condition0->setConditionType(Conditional::CONDITION_CONTAINSTEXT); |
25
|
|
|
$condition0->setOperatorType(Conditional::CONDITION_CONTAINSTEXT); |
26
|
|
|
$condition0->setText('anywhere'); |
27
|
|
|
$condition0->getStyle()->getFill() |
28
|
|
|
->setFillType(Fill::FILL_SOLID) |
29
|
|
|
->getEndColor()->setARGB(self::COLOR_RED); |
30
|
|
|
array_push($conditionalStyles, $condition0); |
31
|
|
|
|
32
|
|
|
// if text contains 'Left' on left - green background |
33
|
|
|
$condition1 = new Conditional(); |
34
|
|
|
$condition1->setConditionType(Conditional::CONDITION_CONTAINSTEXT); |
35
|
|
|
$condition1->setOperatorType(Conditional::OPERATOR_BEGINSWITH); |
36
|
|
|
$condition1->setText('Left'); |
37
|
|
|
$condition1->getStyle()->getFill() |
38
|
|
|
->setFillType(Fill::FILL_SOLID) |
39
|
|
|
->getEndColor()->setARGB(self::COLOR_GREEN); |
40
|
|
|
array_push($conditionalStyles, $condition1); |
41
|
|
|
|
42
|
|
|
// if text contains 'right' on right - blue background |
43
|
|
|
$condition2 = new Conditional(); |
44
|
|
|
$condition2->setConditionType(Conditional::CONDITION_CONTAINSTEXT); |
45
|
|
|
$condition2->setOperatorType(Conditional::OPERATOR_ENDSWITH); |
46
|
|
|
$condition2->setText('right'); |
47
|
|
|
$condition2->getStyle()->getFill() |
48
|
|
|
->setFillType(Fill::FILL_SOLID) |
49
|
|
|
->getEndColor()->setARGB(self::COLOR_BLUE); |
50
|
|
|
array_push($conditionalStyles, $condition2); |
51
|
|
|
|
52
|
|
|
// if text contains no spaces - yellow background |
53
|
|
|
$condition3 = new Conditional(); |
54
|
|
|
$condition3->setConditionType(Conditional::CONDITION_CONTAINSTEXT); |
55
|
|
|
$condition3->setOperatorType(Conditional::OPERATOR_NOTCONTAINS); |
56
|
|
|
$condition3->setText(' '); |
57
|
|
|
$condition3->getStyle()->getFill() |
58
|
|
|
->setFillType(Fill::FILL_SOLID) |
59
|
|
|
->getEndColor()->setARGB(self::COLOR_YELLOW); |
60
|
|
|
array_push($conditionalStyles, $condition3); |
61
|
|
|
$sheet = $spreadsheet->getActiveSheet(); |
62
|
|
|
$sheet->setCellValue('B1', 'This should match anywhere, right?'); |
63
|
|
|
$sheet->setCellValue('B2', 'This should match nowhere, right?'); |
64
|
|
|
$sheet->setCellValue('B3', 'Left match'); |
65
|
|
|
$sheet->setCellValue('B4', 'Match on right'); |
66
|
|
|
$sheet->setCellValue('B5', 'nospaces'); |
67
|
|
|
$xpCoordinate = 'B1:B5'; |
68
|
|
|
|
69
|
|
|
$spreadsheet->getActiveSheet()->setConditionalStyles($xpCoordinate, $conditionalStyles); |
70
|
|
|
$sheet->getColumnDimension('B')->setAutoSize(true); |
71
|
|
|
|
72
|
|
|
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format); |
73
|
|
|
|
74
|
|
|
// see if we successfully written conditional text elements |
75
|
|
|
$newConditionalStyles = $reloadedSpreadsheet->getActiveSheet()->getConditionalStyles($xpCoordinate); |
76
|
|
|
$cnt = count($conditionalStyles); |
77
|
|
|
for ($i = 0; $i < $cnt; ++$i) { |
78
|
|
|
self::assertEquals( |
79
|
|
|
$conditionalStyles[$i]->getConditionType(), |
80
|
|
|
$newConditionalStyles[$i]->getConditionType(), |
81
|
|
|
"Failure on condition type $i" |
82
|
|
|
); |
83
|
|
|
self::assertEquals( |
84
|
|
|
$conditionalStyles[$i]->getOperatorType(), |
85
|
|
|
$newConditionalStyles[$i]->getOperatorType(), |
86
|
|
|
"Failure on operator type $i" |
87
|
|
|
); |
88
|
|
|
self::assertEquals( |
89
|
|
|
$conditionalStyles[$i]->getText(), |
90
|
|
|
$newConditionalStyles[$i]->getText(), |
91
|
|
|
"Failure on text $i" |
92
|
|
|
); |
93
|
|
|
$filCond = $conditionalStyles[$i]->getStyle()->getFill(); |
94
|
|
|
$newCond = $newConditionalStyles[$i]->getStyle()->getFill(); |
95
|
|
|
self::assertEquals( |
96
|
|
|
$filCond->getFillType(), |
97
|
|
|
$newCond->getFillType(), |
98
|
|
|
"Failure on fill type $i" |
99
|
|
|
); |
100
|
|
|
self::assertEquals( |
101
|
|
|
$filCond->getEndColor()->getARGB(), |
102
|
|
|
$newCond->getEndColor()->getARGB(), |
103
|
|
|
"Failure on end color $i" |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|