Skip to content

Commit 782610d

Browse files
committed
Add sample for Conditional Formatting with Icon Sets in Xlsx
1 parent a0f9706 commit 782610d

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
4+
use PhpOffice\PhpSpreadsheet\Style\Conditional;
5+
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\ConditionalFormatValueObject;
6+
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\ConditionalIconSet;
7+
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\IconSetValues;
8+
9+
require __DIR__ . '/../Header.php';
10+
/** @var PhpOffice\PhpSpreadsheet\Helper\Sample $helper */
11+
12+
// Create new Spreadsheet object
13+
$helper->log('Create new Spreadsheet object');
14+
$spreadsheet = new Spreadsheet();
15+
$sheet = $spreadsheet->getActiveSheet();
16+
17+
// Set document properties
18+
$helper->log('Set document properties');
19+
$spreadsheet->getProperties()->setCreator('issakujitsuk')
20+
->setLastModifiedBy('issakujitsuk')
21+
->setTitle('PhpSpreadsheet Test Document')
22+
->setSubject('PhpSpreadsheet Test Document')
23+
->setDescription('Test document for PhpSpreadsheet, generated using PHP classes.')
24+
->setKeywords('office PhpSpreadsheet php')
25+
->setCategory('Test result file');
26+
27+
// Create the worksheet
28+
$helper->log('Add data');
29+
foreach (['A', 'B', 'C'] as $columnIndex) {
30+
$sheet
31+
->setCellValue("{$columnIndex}1", 1)
32+
->setCellValue("{$columnIndex}2", 2)
33+
->setCellValue("{$columnIndex}3", 8)
34+
->setCellValue("{$columnIndex}4", 4)
35+
->setCellValue("{$columnIndex}5", 5)
36+
->setCellValue("{$columnIndex}6", 6)
37+
->setCellValue("{$columnIndex}7", 7)
38+
->setCellValue("{$columnIndex}8", 3)
39+
->setCellValue("{$columnIndex}9", 9)
40+
->setCellValue("{$columnIndex}10", 10);
41+
}
42+
43+
// Set conditional formatting rules and styles
44+
$helper->log('Define conditional formatting using Icon Set');
45+
46+
// 3 icons
47+
$sheet->getStyle('A1:A10')
48+
->setConditionalStyles([
49+
makeConditionalIconSet(
50+
IconSetValues::ThreeSymbols,
51+
[
52+
new ConditionalFormatValueObject('percent', 0),
53+
new ConditionalFormatValueObject('percent', 33),
54+
new ConditionalFormatValueObject('percent', 67),
55+
]
56+
),
57+
]);
58+
59+
// 4 icons
60+
$sheet->getStyle('B1:B10')
61+
->setConditionalStyles([
62+
makeConditionalIconSet(
63+
IconSetValues::FourArrows,
64+
[
65+
new ConditionalFormatValueObject('percent', 0),
66+
new ConditionalFormatValueObject('percent', 25),
67+
new ConditionalFormatValueObject('percent', 50),
68+
new ConditionalFormatValueObject('percent', 75),
69+
]
70+
),
71+
]);
72+
73+
// 5 icons
74+
$sheet->getStyle('C1:C10')
75+
->setConditionalStyles([
76+
makeConditionalIconSet(
77+
IconSetValues::FiveQuarters,
78+
[
79+
new ConditionalFormatValueObject('percent', 0),
80+
new ConditionalFormatValueObject('percent', 20),
81+
new ConditionalFormatValueObject('percent', 40),
82+
new ConditionalFormatValueObject('percent', 60),
83+
new ConditionalFormatValueObject('percent', 80),
84+
]
85+
),
86+
]);
87+
88+
// Save
89+
$sheet->setSelectedCells('A1');
90+
$helper->write($spreadsheet, __FILE__, ['Xlsx']);
91+
92+
/**
93+
* Helper function to create a Conditional object with an IconSet.
94+
*
95+
* @param IconSetValues $type The type of icon set
96+
* @param ConditionalFormatValueObject[] $cfvos The conditional format value objects
97+
*/
98+
function makeConditionalIconSet(
99+
IconSetValues $type,
100+
array $cfvos,
101+
): Conditional {
102+
$condition = new Conditional();
103+
$condition->setConditionType(Conditional::CONDITION_ICONSET);
104+
$iconSet = new ConditionalIconSet();
105+
$condition->setIconSet($iconSet);
106+
$iconSet->setIconSetType($type)
107+
->setCfvos($cfvos);
108+
109+
return $condition;
110+
}

0 commit comments

Comments
 (0)