-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcond-formatting.sql
264 lines (228 loc) · 9.09 KB
/
cond-formatting.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
declare
ctx ExcelGen.ctxHandle := ExcelGen.createContext(ExcelGen.FILE_XLSX);
sheet1 ExcelGen.sheetHandle;
sheet2 ExcelGen.sheetHandle;
sheet3 ExcelGen.sheetHandle;
sheet4 ExcelGen.sheetHandle;
table1 ExcelGen.tableHandle;
whites1 varchar2(8 char) := unistr('\2659\2659\2659\2659\2659\2659\2659\2659');
whites2 varchar2(8 char) := unistr('\2656\2658\2657\2655\2654\2657\2658\2656');
blacks1 varchar2(8 char) := unistr('\265F\265F\265F\265F\265F\265F\265F\265F');
blacks2 varchar2(8 char) := unistr('\265C\265E\265D\265B\265A\265D\265E\265C');
color1 varchar2(128);
color2 varchar2(128);
col1 pls_integer;
col2 pls_integer;
function hsl2rgb (H in number, SL in number, L in number)
return varchar2
is
a number := SL * least(L, 1 - L);
function f (n in number) return number is
k number := mod(n + H/30, 12);
begin
return L - a * greatest(-1, least(k - 3, 9 - k, 1));
end;
begin
return ExcelGen.makeRgbColor(round(f(0) * 255), round(f(8) * 255), round(f(4) * 255));
end;
begin
-- S H E E T 1
-- Miscellaneous conditional formatting rules
sheet1 := ExcelGen.addSheet(ctx, 'Misc.');
for i in 1 .. 9 loop
for j in 1 .. 5 loop
if i = 8 and j in (1,2) then
ExcelGen.putNumberCell(ctx, sheet1, i, j, 1);
else
ExcelGen.putNumberCell(ctx, sheet1, i, j, j);
end if;
end loop;
end loop;
ExcelGen.putNumberCell(ctx, sheet1, 11, 1, 2); -- A11
ExcelGen.putNumberCell(ctx, sheet1, 11, 2, 4); -- B11
ExcelGen.putStringCell(ctx, sheet1, 11, 3, 'X'); -- C11
-- 3-color scale
ExcelGen.addCondFormattingRule(
p_ctxId => ctx
, p_sheetId => sheet1
, p_type => ExcelTypes.CF_TYPE_COLORSCALE
, p_cellRange => ExcelTypes.ST_Sqref('A1:E1')
, p_cfvoList => ExcelTypes.CT_CfvoList(
ExcelTypes.makeCfvo(ExcelTypes.CFVO_MIN, p_color => '#F8696B') -- Minimum
, ExcelTypes.makeCfvo(ExcelTypes.CFVO_PERCENTILE, p_value => 50, p_color => '#FFEB84') -- Midpoint
, ExcelTypes.makeCfvo(ExcelTypes.CFVO_MAX, p_color => '#63BE7B') -- Maximum
)
);
-- 2-color scale
ExcelGen.addCondFormattingRule(
p_ctxId => ctx
, p_sheetId => sheet1
, p_type => ExcelTypes.CF_TYPE_COLORSCALE
, p_cellRange => ExcelTypes.ST_Sqref('A2:E2')
, p_cfvoList => ExcelTypes.CT_CfvoList(
ExcelTypes.makeCfvo(ExcelTypes.CFVO_NUM, p_value => 0, p_color => 'white') -- Minimum
, ExcelTypes.makeCfvo(ExcelTypes.CFVO_NUM, p_value => 10, p_color => 'green') -- Maximum
)
);
-- top 1
ExcelGen.addCondFormattingRule(
p_ctxId => ctx
, p_sheetId => sheet1
, p_type => ExcelTypes.CF_TYPE_TOP
, p_cellRange => ExcelTypes.ST_Sqref('A3:E3')
, p_style => ExcelGen.makeCondFmtStyleCss(ctx, 'border:medium solid green')
, p_param => 1
);
-- Icon set
ExcelGen.addCondFormattingRule(
p_ctxId => ctx
, p_sheetId => sheet1
, p_type => ExcelTypes.CF_TYPE_ICONSET
, p_cellRange => ExcelTypes.ST_Sqref('A4:E4')
, p_iconSet => ExcelTypes.CF_ICONSET_5QUARTERS
, p_cfvoList => ExcelTypes.CT_CfvoList(
ExcelTypes.makeCfvo(ExcelTypes.CFVO_NUM, 1) -- Threshold #1
, ExcelTypes.makeCfvo(ExcelTypes.CFVO_NUM, 2) -- Threshold #2
, ExcelTypes.makeCfvo(ExcelTypes.CFVO_NUM, 3) -- Threshold #3
, ExcelTypes.makeCfvo(ExcelTypes.CFVO_NUM, 4) -- Threshold #4
)
);
-- Cell value
ExcelGen.addCondFormattingRule(
p_ctxId => ctx
, p_sheetId => sheet1
, p_type => ExcelTypes.CF_TYPE_CELLIS
, p_cellRange => ExcelTypes.ST_Sqref('A5:E5')
, p_style => ExcelGen.makeCondFmtStyleCss(ctx, 'border:thin solid black;background-color:yellow')
, p_operator => ExcelTypes.CF_OPER_BN
, p_value1 => '$A$11'
, p_value2 => '$B$11'
);
-- Data bar
ExcelGen.addCondFormattingRule(
p_ctxId => ctx
, p_sheetId => sheet1
, p_type => ExcelTypes.CF_TYPE_DATABAR
, p_cellRange => ExcelTypes.ST_Sqref('A6:E6')
, p_cfvoList => ExcelTypes.CT_CfvoList(
ExcelTypes.makeCfvo(ExcelTypes.CFVO_MIN) -- Minimum
, ExcelTypes.makeCfvo(ExcelTypes.CFVO_MAX) -- Maximum
, ExcelTypes.makeCfvo(p_color => 'hotpink') -- Bar color
)
, p_hideValue => true
);
-- Equal or Above Average
ExcelGen.addCondFormattingRule(
p_ctxId => ctx
, p_sheetId => sheet1
, p_type => ExcelTypes.CF_TYPE_EQUALABOVEAVERAGE
, p_cellRange => ExcelTypes.ST_Sqref('A7:E7')
, p_style => ExcelGen.makeCondFmtStyleCss(ctx, 'background-color:orange')
);
-- Duplicate values
ExcelGen.addCondFormattingRule(
p_ctxId => ctx
, p_sheetId => sheet1
, p_type => ExcelTypes.CF_TYPE_DUPLICATES
, p_cellRange => ExcelTypes.ST_Sqref('A8:E8')
, p_style => ExcelGen.makeCondFmtStyleCss(ctx, 'border:medium solid red;background-color:#FFA7A7')
);
-- Formula-based
ExcelGen.addCondFormattingRule(
p_ctxId => ctx
, p_sheetId => sheet1
, p_type => ExcelTypes.CF_TYPE_EXPR
, p_cellRange => ExcelTypes.ST_Sqref('A9:E9')
, p_style => ExcelGen.makeCondFmtStyleCss(ctx, 'font-style:italic;color:#BFBFBF;background-color:#F2F2F2')
, p_value1 => 'R11C3="X"'
, p_refstyle1 => ExcelFmla.REF_R1C1
);
-- S H E E T 2
-- Chessboard
sheet2 := ExcelGen.addSheet(ctx, 'Chessboard');
for i in 1 .. 8 loop
ExcelGen.setColumnProperties(ctx, sheet2, i+1, p_width => ExcelGen.colPxToCharWidth(40));
ExcelGen.setRowProperties(ctx, sheet2, i+1, p_height => ExcelGen.rowPxToPt(40));
end loop;
ExcelGen.setColumnProperties(ctx, sheet2, 1, p_width => ExcelGen.colPxToCharWidth(20));
ExcelGen.setSheetProperties(ctx, sheet2, p_showGridLines => false);
for i in 1 .. 8 loop
ExcelGen.putStringCell(ctx, sheet2, 3, i+1, substr(blacks1,i,1));
ExcelGen.putStringCell(ctx, sheet2, 2, i+1, substr(blacks2,i,1));
ExcelGen.putStringCell(ctx, sheet2, 8, i+1, substr(whites1,i,1));
ExcelGen.putStringCell(ctx, sheet2, 9, i+1, substr(whites2,i,1));
end loop;
ExcelGen.setRangeStyle(ctx, sheet2, 'B2:I9', p_style => ExcelGen.makeCellStyleCss(ctx, 'text-align:center;vertical-align:middle;font-size:20pt;border:medium solid black'), p_outsideBorders => true);
-- check pattern achieved via the following conditional formatting rule
ExcelGen.addCondFormattingRule(
p_ctxId => ctx
, p_sheetId => sheet2
, p_cellRange => ExcelTypes.ST_Sqref('B2:I9')
, p_type => ExcelTypes.CF_TYPE_EXPR
, p_style => ExcelGen.makeCondFmtStyleCss(ctx, 'background-color:tan')
, p_value1 => 'MOD(ROW(),2)<>MOD(COLUMN(),2)'
);
-- S H E E T 3
-- HSL color scale
sheet3 := ExcelGen.addSheet(ctx, 'Colorscale');
ExcelGen.setSheetProperties(ctx, sheet3, p_showGridLines => false, p_showRowColHeaders => false);
for i in 1 .. 101 loop
for j in 1 .. 360 loop
ExcelGen.putNumberCell(ctx, sheet3, i, j, j);
end loop;
end loop;
for lt in 0 .. 100 loop
color1 := null;
for v in 0 .. 6 loop
color2 := hsl2rgb(v*60, 1, lt/100);
col2 := case when v = 0 then 1 else v*60 end;
if color1 is not null then
ExcelGen.addCondFormattingRule(
ctx
, sheet3
, ExcelTypes.CF_TYPE_COLORSCALE
, p_cellRange => ExcelTypes.ST_Sqref(ExcelGen.makeCellRange(lt+1, col1, lt+1, col2))
, p_cfvoList => ExcelTypes.CT_CfvoList(
ExcelTypes.makeCfvo(ExcelTypes.CFVO_NUM, col1, p_color => color1)
, ExcelTypes.makeCfvo(ExcelTypes.CFVO_NUM, col2, p_color => color2)
)
);
end if;
col1 := col2;
color1 := color2;
end loop;
end loop;
for colIdx in 1 .. 360 loop
ExcelGen.setColumnProperties(ctx, sheet3, colIdx, p_width => 3);
end loop;
-- S H E E T 4
-- Table-level conditional formatting
sheet4 := ExcelGen.addSheet(ctx, 'Employees');
table1 := ExcelGen.addTable(ctx, sheet4, 'SELECT * FROM HR.EMPLOYEES');
ExcelGen.setDateFormat(ctx, sheet4, 'dd/mm/yyyy');
ExcelGen.setTableHeader(ctx, sheet4, table1, p_autoFilter => true);
ExcelGen.setSheetProperties(ctx, sheet4, p_activePaneAnchorRef => 'A2');
-- highlight salaries above average
ExcelGen.addTableCondFmtRule(
p_ctxId => ctx
, p_sheetId => sheet4
, p_tableId => table1
, p_columnId => 8 -- SALARY
, p_type => ExcelTypes.CF_TYPE_ABOVEAVERAGE
, p_style => ExcelGen.makeCondFmtStyleCss(ctx, 'background-color:lightskyblue;font-weight:bold')
);
-- highlight rows where employee has no manager (column #10)
ExcelGen.addTableCondFmtRule(
p_ctxId => ctx
, p_sheetId => sheet4
, p_tableId => table1
, p_columnId => null
, p_type => ExcelTypes.CF_TYPE_EXPR
, p_style => ExcelGen.makeCondFmtStyleCss(ctx, 'color:red;font-style:italic')
, p_value1 => 'ISBLANK(RC10)' -- MANAGER_ID
, p_refstyle1 => ExcelFmla.REF_R1C1
);
ExcelGen.createFile(ctx, 'TEST_DIR', 'cond-formatting.xlsx');
ExcelGen.closeContext(ctx);
end;
/