1+ using System ;
2+ using System . Collections . Generic ;
3+ using DocumentFormat . OpenXml ;
4+ using DocumentFormat . OpenXml . Spreadsheet ;
5+
6+ namespace Openize . Cells
7+ {
8+ /// <summary>
9+ /// Represents the type of conditional formatting rule.
10+ /// </summary>
11+ public enum ConditionalFormattingType
12+ {
13+ /// <summary>Cell value based formatting</summary>
14+ CellIs ,
15+ /// <summary>Data bar visualization</summary>
16+ DataBar ,
17+ /// <summary>Color scale visualization</summary>
18+ ColorScale ,
19+ /// <summary>Icon set visualization</summary>
20+ IconSet ,
21+ /// <summary>Top/Bottom rules</summary>
22+ Top10 ,
23+ /// <summary>Above/Below average</summary>
24+ AboveAverage ,
25+ /// <summary>Duplicate values</summary>
26+ DuplicateValues ,
27+ /// <summary>Contains text</summary>
28+ ContainsText ,
29+ /// <summary>Date occurring</summary>
30+ TimePeriod ,
31+ /// <summary>Expression based</summary>
32+ Expression
33+ }
34+
35+ /// <summary>
36+ /// Represents the operator for conditional formatting rules.
37+ /// </summary>
38+ public enum ConditionalFormattingOperator
39+ {
40+ /// <summary>Less than</summary>
41+ LessThan ,
42+ /// <summary>Less than or equal</summary>
43+ LessThanOrEqual ,
44+ /// <summary>Equal</summary>
45+ Equal ,
46+ /// <summary>Not equal</summary>
47+ NotEqual ,
48+ /// <summary>Greater than or equal</summary>
49+ GreaterThanOrEqual ,
50+ /// <summary>Greater than</summary>
51+ GreaterThan ,
52+ /// <summary>Between</summary>
53+ Between ,
54+ /// <summary>Not between</summary>
55+ NotBetween ,
56+ /// <summary>Contains</summary>
57+ Contains ,
58+ /// <summary>Not contains</summary>
59+ NotContains ,
60+ /// <summary>Begins with</summary>
61+ BeginsWith ,
62+ /// <summary>Ends with</summary>
63+ EndsWith
64+ }
65+
66+ /// <summary>
67+ /// Represents a conditional formatting style definition.
68+ /// </summary>
69+ public class ConditionalFormattingStyle
70+ {
71+ /// <summary>Gets or sets the background color in hex format (e.g., "FF92D050" for green)</summary>
72+ public string BackgroundColor { get ; set ; }
73+
74+ /// <summary>Gets or sets the font color in hex format (e.g., "FFFFFFFF" for white)</summary>
75+ public string FontColor { get ; set ; }
76+
77+ /// <summary>Gets or sets whether the font is bold</summary>
78+ public bool ? IsBold { get ; set ; }
79+
80+ /// <summary>Gets or sets whether the font is italic</summary>
81+ public bool ? IsItalic { get ; set ; }
82+
83+ /// <summary>Gets or sets the border color in hex format</summary>
84+ public string BorderColor { get ; set ; }
85+
86+ /// <summary>Gets or sets the pattern type for fill</summary>
87+ public PatternValues ? PatternType { get ; set ; } = PatternValues . Solid ;
88+ }
89+
90+ /// <summary>
91+ /// Represents a data bar configuration for conditional formatting.
92+ /// </summary>
93+ public class DataBarOptions
94+ {
95+ /// <summary>Gets or sets the color of the data bar in hex format</summary>
96+ public string BarColor { get ; set ; } = "FF638EC6" ; // Default blue
97+
98+ /// <summary>Gets or sets whether to show the cell value</summary>
99+ public bool ShowValue { get ; set ; } = true ;
100+
101+ /// <summary>Gets or sets the minimum length percentage (0-100)</summary>
102+ public int MinLength { get ; set ; } = 10 ;
103+
104+ /// <summary>Gets or sets the maximum length percentage (0-100)</summary>
105+ public int MaxLength { get ; set ; } = 90 ;
106+ }
107+
108+ /// <summary>
109+ /// Represents a color scale configuration for conditional formatting.
110+ /// </summary>
111+ public class ColorScaleOptions
112+ {
113+ /// <summary>Gets or sets the minimum value color in hex format</summary>
114+ public string MinColor { get ; set ; } = "FFF8696B" ; // Red
115+
116+ /// <summary>Gets or sets the midpoint value color in hex format</summary>
117+ public string MidColor { get ; set ; } = "FFFFEB84" ; // Yellow
118+
119+ /// <summary>Gets or sets the maximum value color in hex format</summary>
120+ public string MaxColor { get ; set ; } = "FF63BE7B" ; // Green
121+
122+ /// <summary>Gets or sets whether to use a 2-color scale (if false, uses 3-color scale)</summary>
123+ public bool TwoColorScale { get ; set ; } = false ;
124+ }
125+
126+ /// <summary>
127+ /// Represents a conditional formatting rule.
128+ /// </summary>
129+ public class ConditionalFormattingRule
130+ {
131+ private static uint _nextPriority = 1 ;
132+
133+ /// <summary>Gets or sets the type of conditional formatting</summary>
134+ public ConditionalFormattingType Type { get ; set ; }
135+
136+ /// <summary>Gets or sets the operator for comparison</summary>
137+ public ConditionalFormattingOperator ? Operator { get ; set ; }
138+
139+ /// <summary>Gets or sets the first value or formula for comparison</summary>
140+ public string Value1 { get ; set ; }
141+
142+ /// <summary>Gets or sets the second value for between/not between operators</summary>
143+ public string Value2 { get ; set ; }
144+
145+ /// <summary>Gets or sets the style to apply when condition is met</summary>
146+ public ConditionalFormattingStyle Style { get ; set ; }
147+
148+ /// <summary>Gets or sets the data bar options</summary>
149+ public DataBarOptions DataBar { get ; set ; }
150+
151+ /// <summary>Gets or sets the color scale options</summary>
152+ public ColorScaleOptions ColorScale { get ; set ; }
153+
154+ /// <summary>Gets or sets the priority of the rule</summary>
155+ public uint Priority { get ; private set ; }
156+
157+ /// <summary>Gets or sets whether to stop if this rule is true</summary>
158+ public bool StopIfTrue { get ; set ; } = false ;
159+
160+ /// <summary>Gets or sets the cell range(s) to apply the formatting to</summary>
161+ public string CellRange { get ; set ; }
162+
163+ /// <summary>
164+ /// Initializes a new instance of the ConditionalFormattingRule class.
165+ /// </summary>
166+ public ConditionalFormattingRule ( )
167+ {
168+ Priority = _nextPriority ++ ;
169+ }
170+
171+ /// <summary>
172+ /// Creates a rule for highlighting cells greater than a value.
173+ /// </summary>
174+ public static ConditionalFormattingRule GreaterThan ( string value , ConditionalFormattingStyle style )
175+ {
176+ return new ConditionalFormattingRule
177+ {
178+ Type = ConditionalFormattingType . CellIs ,
179+ Operator = ConditionalFormattingOperator . GreaterThan ,
180+ Value1 = value ,
181+ Style = style
182+ } ;
183+ }
184+
185+ /// <summary>
186+ /// Creates a rule for highlighting cells less than a value.
187+ /// </summary>
188+ public static ConditionalFormattingRule LessThan ( string value , ConditionalFormattingStyle style )
189+ {
190+ return new ConditionalFormattingRule
191+ {
192+ Type = ConditionalFormattingType . CellIs ,
193+ Operator = ConditionalFormattingOperator . LessThan ,
194+ Value1 = value ,
195+ Style = style
196+ } ;
197+ }
198+
199+ /// <summary>
200+ /// Creates a rule for highlighting cells between two values.
201+ /// </summary>
202+ public static ConditionalFormattingRule Between ( string value1 , string value2 , ConditionalFormattingStyle style )
203+ {
204+ return new ConditionalFormattingRule
205+ {
206+ Type = ConditionalFormattingType . CellIs ,
207+ Operator = ConditionalFormattingOperator . Between ,
208+ Value1 = value1 ,
209+ Value2 = value2 ,
210+ Style = style
211+ } ;
212+ }
213+
214+ /// <summary>
215+ /// Creates a rule for highlighting cells equal to a value.
216+ /// </summary>
217+ public static ConditionalFormattingRule EqualTo ( string value , ConditionalFormattingStyle style )
218+ {
219+ return new ConditionalFormattingRule
220+ {
221+ Type = ConditionalFormattingType . CellIs ,
222+ Operator = ConditionalFormattingOperator . Equal ,
223+ Value1 = value ,
224+ Style = style
225+ } ;
226+ }
227+
228+ /// <summary>
229+ /// Creates a rule for data bar visualization.
230+ /// </summary>
231+ public static ConditionalFormattingRule DataBarRule ( DataBarOptions options = null )
232+ {
233+ return new ConditionalFormattingRule
234+ {
235+ Type = ConditionalFormattingType . DataBar ,
236+ DataBar = options ?? new DataBarOptions ( )
237+ } ;
238+ }
239+
240+ /// <summary>
241+ /// Creates a rule for color scale visualization.
242+ /// </summary>
243+ public static ConditionalFormattingRule ColorScaleRule ( ColorScaleOptions options = null )
244+ {
245+ return new ConditionalFormattingRule
246+ {
247+ Type = ConditionalFormattingType . ColorScale ,
248+ ColorScale = options ?? new ColorScaleOptions ( )
249+ } ;
250+ }
251+
252+ /// <summary>
253+ /// Creates a rule for highlighting duplicate values.
254+ /// </summary>
255+ public static ConditionalFormattingRule DuplicateValues ( ConditionalFormattingStyle style )
256+ {
257+ return new ConditionalFormattingRule
258+ {
259+ Type = ConditionalFormattingType . DuplicateValues ,
260+ Style = style
261+ } ;
262+ }
263+
264+ /// <summary>
265+ /// Creates a rule for highlighting cells containing specific text.
266+ /// </summary>
267+ public static ConditionalFormattingRule ContainsText ( string text , ConditionalFormattingStyle style )
268+ {
269+ return new ConditionalFormattingRule
270+ {
271+ Type = ConditionalFormattingType . ContainsText ,
272+ Operator = ConditionalFormattingOperator . Contains ,
273+ Value1 = text ,
274+ Style = style
275+ } ;
276+ }
277+
278+ /// <summary>
279+ /// Creates a rule for highlighting top N values.
280+ /// </summary>
281+ public static ConditionalFormattingRule Top10 ( int count , bool isPercent , bool isBottom , ConditionalFormattingStyle style )
282+ {
283+ return new ConditionalFormattingRule
284+ {
285+ Type = ConditionalFormattingType . Top10 ,
286+ Value1 = count . ToString ( ) ,
287+ Value2 = $ "{ isPercent } ,{ isBottom } ",
288+ Style = style
289+ } ;
290+ }
291+
292+ /// <summary>
293+ /// Creates a rule for highlighting above average values.
294+ /// </summary>
295+ public static ConditionalFormattingRule AboveAverage ( bool isAbove , ConditionalFormattingStyle style )
296+ {
297+ return new ConditionalFormattingRule
298+ {
299+ Type = ConditionalFormattingType . AboveAverage ,
300+ Value1 = isAbove . ToString ( ) ,
301+ Style = style
302+ } ;
303+ }
304+
305+ /// <summary>
306+ /// Creates a rule based on a custom formula.
307+ /// </summary>
308+ public static ConditionalFormattingRule Formula ( string formula , ConditionalFormattingStyle style )
309+ {
310+ return new ConditionalFormattingRule
311+ {
312+ Type = ConditionalFormattingType . Expression ,
313+ Value1 = formula ,
314+ Style = style
315+ } ;
316+ }
317+ }
318+ }
0 commit comments