1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Functional; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
6
|
|
|
|
7
|
|
|
class ConditionalStopIfTrueTest extends AbstractFunctional |
8
|
|
|
{ |
9
|
|
|
const COLOR_GREEN = 'FF99FF66'; |
10
|
|
|
const COLOR_RED = 'FFFF5050'; |
11
|
|
|
|
12
|
|
|
public function providerFormats() |
13
|
|
|
{ |
14
|
|
|
return [ |
15
|
|
|
['Xlsx'], |
16
|
|
|
]; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @dataProvider providerFormats |
21
|
|
|
* |
22
|
|
|
* @param string $format |
23
|
|
|
*/ |
24
|
|
|
public function testConditionalStopIfTrue($format) |
25
|
|
|
{ |
26
|
|
|
$pCoordinate = 'A1:A3'; |
27
|
|
|
|
28
|
|
|
// if blank cell -> no styling |
29
|
|
|
$condition0 = new \PhpOffice\PhpSpreadsheet\Style\Conditional(); |
30
|
|
|
$condition0->setConditionType(\PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_EXPRESSION); |
31
|
|
|
$condition0->addCondition('LEN(TRIM(A1))=0'); |
32
|
|
|
$condition0->setStopIfTrue(true); // ! stop here |
33
|
|
|
|
34
|
|
|
// if value below 0.6 (matches also blank cells!) -> red background |
35
|
|
|
$condition1 = new \PhpOffice\PhpSpreadsheet\Style\Conditional(); |
36
|
|
|
$condition1->setConditionType(\PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CELLIS); |
37
|
|
|
$condition1->setOperatorType(\PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_LESSTHAN); |
38
|
|
|
$condition1->addCondition(0.6); |
39
|
|
|
$condition1->getStyle()->getFill() |
40
|
|
|
->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID) |
41
|
|
|
->getEndColor()->setARGB(self::COLOR_RED); |
42
|
|
|
|
43
|
|
|
// if value above 0.6 -> green background |
44
|
|
|
$condition2 = new \PhpOffice\PhpSpreadsheet\Style\Conditional(); |
45
|
|
|
$condition2->setConditionType(\PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CELLIS); |
46
|
|
|
$condition2->setOperatorType(\PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_GREATERTHAN); |
47
|
|
|
$condition2->addCondition(0.6); |
48
|
|
|
$condition2->getStyle()->getFill() |
49
|
|
|
->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID) |
50
|
|
|
->getEndColor()->setARGB(self::COLOR_GREEN); |
51
|
|
|
|
52
|
|
|
$spreadsheet = new Spreadsheet(); |
53
|
|
|
$spreadsheet->getActiveSheet()->getCell('A1')->setValue(0.7); |
54
|
|
|
$spreadsheet->getActiveSheet()->getCell('A2')->setValue(''); |
55
|
|
|
$spreadsheet->getActiveSheet()->getCell('A3')->setValue(0.4); |
56
|
|
|
|
57
|
|
|
// put all three conditions in sheet |
58
|
|
|
$conditionalStyles = []; |
59
|
|
|
array_push($conditionalStyles, $condition0); |
60
|
|
|
array_push($conditionalStyles, $condition1); |
61
|
|
|
array_push($conditionalStyles, $condition2); |
62
|
|
|
$spreadsheet->getActiveSheet()->setConditionalStyles($pCoordinate, $conditionalStyles); |
63
|
|
|
|
64
|
|
|
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format); |
65
|
|
|
|
66
|
|
|
// see if we successfully written "StopIfTrue" |
67
|
|
|
$newConditionalStyles = $reloadedSpreadsheet->getActiveSheet()->getConditionalStyles($pCoordinate); |
68
|
|
|
self::assertTrue($newConditionalStyles[0]->getStopIfTrue(), 'StopIfTrue should be set (=true) on first condition'); |
69
|
|
|
self::assertFalse($newConditionalStyles[1]->getStopIfTrue(), 'StopIfTrue should not be set (=false) on second condition'); |
70
|
|
|
self::assertFalse($newConditionalStyles[2]->getStopIfTrue(), 'StopIfTrue should not be set (=false) on third condition'); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|