OpenXLSX 1.9.1
Loading...
Searching...
No Matches
XLConditionalFormatting.hpp
Go to the documentation of this file.
1#ifndef OPENXLSX_XLCONDITIONALFORMATTING_HPP
2#define OPENXLSX_XLCONDITIONALFORMATTING_HPP
3
4#include "OpenXLSX-Exports.hpp"
5#include "XLColor.hpp"
6#include "XLStyles.hpp"
7#include "XLXmlParser.hpp"
8#include <cstdint>
9#include <memory>
10#include <string>
11#include <string_view>
12#include <vector>
13
14namespace OpenXLSX
15{
16 constexpr const uint16_t XLPriorityNotSet = 0;
17
18 enum class XLCfType : uint8_t {
19 Expression = 0,
20 CellIs = 1,
21 ColorScale = 2,
22 DataBar = 3,
23 IconSet = 4,
24 Top10 = 5,
25 UniqueValues = 6,
27 ContainsText = 8,
29 BeginsWith = 10,
30 EndsWith = 11,
31 ContainsBlanks = 12,
33 ContainsErrors = 14,
35 TimePeriod = 16,
36 AboveAverage = 17,
37 Invalid = 255
38 };
39
40 enum class XLCfOperator : uint8_t {
41 LessThan = 0,
43 Equal = 2,
44 NotEqual = 3,
46 GreaterThan = 5,
47 Between = 6,
48 NotBetween = 7,
49 ContainsText = 8,
50 NotContains = 9,
51 BeginsWith = 10,
52 EndsWith = 11,
53 Invalid = 255
54 };
55
56 enum class XLCfTimePeriod : uint8_t {
57 Today = 0,
58 Yesterday = 1,
59 Tomorrow = 2,
60 Last7Days = 3,
61 ThisMonth = 4,
62 LastMonth = 5,
63 NextMonth = 6,
64 ThisWeek = 7,
65 LastWeek = 8,
66 NextWeek = 9,
67 Invalid = 255
68 };
69
70 enum class XLCfvoType : uint8_t { Min = 0, Max = 1, Number = 2, Percent = 3, Formula = 4, Percentile = 5, Invalid = 255 };
71
72 enum class XLDataBarDirection : uint8_t { Context = 0, LeftToRight = 1, RightToLeft = 2 };
73 enum class XLDataBarAxisPosition : uint8_t { Automatic = 0, Middle = 1, None = 2 };
74
75 OPENXLSX_EXPORT XLCfType XLCfTypeFromString(std::string const& typeString);
76 OPENXLSX_EXPORT std::string XLCfTypeToString(XLCfType cfType);
77 OPENXLSX_EXPORT XLCfOperator XLCfOperatorFromString(std::string const& operatorString);
78 OPENXLSX_EXPORT std::string XLCfOperatorToString(XLCfOperator cfOperator);
79 OPENXLSX_EXPORT XLCfTimePeriod XLCfTimePeriodFromString(std::string const& timePeriodString);
80 OPENXLSX_EXPORT std::string XLCfTimePeriodToString(XLCfTimePeriod cfTimePeriod);
81 OPENXLSX_EXPORT XLCfvoType XLCfvoTypeFromString(std::string const& cfvoTypeString);
82 OPENXLSX_EXPORT std::string XLCfvoTypeToString(XLCfvoType cfvoType);
83 OPENXLSX_EXPORT XLDataBarDirection XLDataBarDirectionFromString(std::string const& directionString);
84 OPENXLSX_EXPORT std::string XLDataBarDirectionToString(XLDataBarDirection direction);
85 OPENXLSX_EXPORT XLDataBarAxisPosition XLDataBarAxisPositionFromString(std::string const& positionString);
86 OPENXLSX_EXPORT std::string XLDataBarAxisPositionToString(XLDataBarAxisPosition position);
87
88 class OPENXLSX_EXPORT XLCfvo
89 {
90 friend class XLCfColorScale;
91 friend class XLCfDataBar;
92 friend class XLCfIconSet;
93 friend class XLCfRule;
94
95 public:
96 XLCfvo();
97 explicit XLCfvo(const XMLNode& node);
98 XLCfvo(const XLCfvo& other);
99 XLCfvo(XLCfvo&& other) noexcept;
101
102 XLCfvo& operator=(const XLCfvo& other);
103 XLCfvo& operator=(XLCfvo&& other) noexcept;
104
105 XLCfvoType type() const;
106 std::string value() const;
107 bool gte() const;
108
109 void setType(XLCfvoType type);
110 void setValue(const std::string& value);
111 void setGte(bool gte);
112
113 XMLNode node() const { return m_cfvoNode; }
114
115 private:
116 XMLNode getOrCreateExtNode() const;
117 XMLNode getExtNode() const;
118
119 std::unique_ptr<XMLDocument> m_xmlDocument;
120 mutable XMLNode m_cfvoNode;
121 };
122
123 class OPENXLSX_EXPORT XLCfColorScale
124 {
125 public:
127 explicit XLCfColorScale(const XMLNode& node);
128 XLCfColorScale(const XLCfColorScale& other);
129 XLCfColorScale(XLCfColorScale&& other) noexcept;
131
132 XLCfColorScale& operator=(const XLCfColorScale& other);
133 XLCfColorScale& operator=(XLCfColorScale&& other) noexcept;
134
135 std::vector<XLCfvo> cfvos() const;
136 std::vector<XLColor> colors() const;
137
138 void addValue(XLCfvoType type, const std::string& value, const XLColor& color);
139 void clear();
140
141 XMLNode node() const { return m_colorScaleNode; }
142
143 private:
144 XMLNode getOrCreateExtNode() const;
145 XMLNode getExtNode() const;
146
147 std::unique_ptr<XMLDocument> m_xmlDocument;
148 mutable XMLNode m_colorScaleNode;
149 };
150
151 class OPENXLSX_EXPORT XLCfDataBar
152 {
153 public:
154 XLCfDataBar();
155 explicit XLCfDataBar(const XMLNode& node);
156 XLCfDataBar(const XLCfDataBar& other);
157 XLCfDataBar(XLCfDataBar&& other) noexcept;
159
160 XLCfDataBar& operator=(const XLCfDataBar& other);
161 XLCfDataBar& operator=(XLCfDataBar&& other) noexcept;
162
163 XLCfvo min() const;
164 XLCfvo max() const;
165 XLColor color() const;
166
167 void setMin(XLCfvoType type, const std::string& value);
168 void setMax(XLCfvoType type, const std::string& value);
169 void setColor(const XLColor& color);
170
171 bool showValue() const;
172 void setShowValue(bool show);
173
174 // Extended properties (Excel 2010+)
175 uint32_t minLength() const;
176 void setMinLength(uint32_t length);
177 uint32_t maxLength() const;
178 void setMaxLength(uint32_t length);
179 bool border() const;
180 void setBorder(bool border);
181 bool gradient() const;
182 void setGradient(bool gradient);
183 XLDataBarDirection direction() const;
184 void setDirection(XLDataBarDirection direction);
185 XLColor borderColor() const;
186 void setBorderColor(const XLColor& color);
187 XLColor negativeFillColor() const;
188 void setNegativeFillColor(const XLColor& color);
189 XLColor negativeBorderColor() const;
190 void setNegativeBorderColor(const XLColor& color);
191 XLDataBarAxisPosition axisPosition() const;
192 void setAxisPosition(XLDataBarAxisPosition position);
193 XLColor axisColor() const;
194 void setAxisColor(const XLColor& color);
195
196
197 XMLNode node() const { return m_dataBarNode; }
198
199 private:
200 XMLNode getOrCreateExtNode() const;
201 XMLNode getExtNode() const;
202
203 std::unique_ptr<XMLDocument> m_xmlDocument;
204 mutable XMLNode m_dataBarNode;
205 };
206
207 class OPENXLSX_EXPORT XLCfIconSet
208 {
209 public:
210 XLCfIconSet();
211 explicit XLCfIconSet(const XMLNode& node);
212 XLCfIconSet(const XLCfIconSet& other);
213 XLCfIconSet(XLCfIconSet&& other) noexcept;
215
216 XLCfIconSet& operator=(const XLCfIconSet& other);
217 XLCfIconSet& operator=(XLCfIconSet&& other) noexcept;
218
219 std::string iconSet() const;
220 void setIconSet(const std::string& iconSetName);
221
222 std::vector<XLCfvo> cfvos() const;
223 void addValue(XLCfvoType type, const std::string& value);
224 void clear();
225
226 bool showValue() const;
227 void setShowValue(bool show);
228
229 // Extended properties (Excel 2010+)
230 uint32_t minLength() const;
231 void setMinLength(uint32_t length);
232 uint32_t maxLength() const;
233 void setMaxLength(uint32_t length);
234 bool border() const;
235 void setBorder(bool border);
236 bool gradient() const;
237 void setGradient(bool gradient);
241 void setBorderColor(const XLColor& color);
243 void setNegativeFillColor(const XLColor& color);
245 void setNegativeBorderColor(const XLColor& color);
249 void setAxisColor(const XLColor& color);
250
251 bool percent() const;
252 void setPercent(bool percent);
253 bool reverse() const;
254 void setReverse(bool reverse);
255
256 XMLNode node() const { return m_iconSetNode; }
257
258 private:
259 XMLNode getOrCreateExtNode() const;
260 XMLNode getExtNode() const;
261
262 std::unique_ptr<XMLDocument> m_xmlDocument;
263 mutable XMLNode m_iconSetNode;
264 };
265
266 class OPENXLSX_EXPORT XLCfRule
267 {
268 friend class XLCfRules;
269
270 public:
271 XLCfRule();
272 explicit XLCfRule(const XMLNode& node);
273 XLCfRule(const XLCfRule& other);
274 XLCfRule(XLCfRule&& other) noexcept;
276
277 XLCfRule& operator=(const XLCfRule& other);
278 XLCfRule& operator=(XLCfRule&& other) noexcept;
279
280 bool empty() const;
281 std::string formula() const;
282 std::vector<std::string> formulas() const;
283 XLCfColorScale colorScale() const;
284 XLCfDataBar dataBar() const;
285 XLCfIconSet iconSet() const;
286 XLUnsupportedElement extLst() const;
287
288 XLCfType type() const;
289 XLStyleIndex dxfId() const;
290 uint16_t priority() const;
291 bool stopIfTrue() const;
292 bool aboveAverage() const;
293 bool percent() const;
294 bool bottom() const;
295 XLCfOperator Operator() const;
296 std::string text() const;
297 XLCfTimePeriod timePeriod() const;
298 uint16_t rank() const;
299 int16_t stdDev() const;
300 bool equalAverage() const;
301
302 XLCfRule& setFormula(std::string const& newFormula);
303 bool addFormula(std::string const& newFormula);
304 void clearFormulas();
305 bool setColorScale(XLCfColorScale const& newColorScale);
306 bool setDataBar(XLCfDataBar const& newDataBar);
307 bool setIconSet(XLCfIconSet const& newIconSet);
308 bool setExtLst(XLUnsupportedElement const& newExtLst);
309
310 XLCfRule& setType(XLCfType newType);
311 XLCfRule& setDxfId(XLStyleIndex newDxfId);
312
313 XLCfRule& setPriority(uint16_t newPriority);
314
315 public:
316 XLCfRule& setStopIfTrue(bool set = true);
317 XLCfRule& setAboveAverage(bool set = true);
318 XLCfRule& setPercent(bool set = true);
319 XLCfRule& setBottom(bool set = true);
320 XLCfRule& setOperator(XLCfOperator newOperator);
321 XLCfRule& setText(std::string const& newText);
322 XLCfRule& setTimePeriod(XLCfTimePeriod newTimePeriod);
323 XLCfRule& setRank(uint16_t newRank);
324 XLCfRule& setStdDev(int16_t newStdDev);
325 XLCfRule& setEqualAverage(bool set = true);
326
327 std::string summary() const;
328
329 XMLNode node() const { return m_cfRuleNode; }
330
331 private:
332 std::unique_ptr<XMLDocument> m_xmlDocument;
333 mutable XMLNode m_cfRuleNode;
334 inline static const std::vector<std::string_view> m_nodeOrder = {"formula", "colorScale", "dataBar", "iconSet", "extLst"};
335 };
336
337 constexpr const char* XLDefaultCfRulePrefix = "\n\t\t";
338
339 class OPENXLSX_EXPORT XLCfRules
340 {
341 public:
342 XLCfRules();
343 explicit XLCfRules(const XMLNode& node);
344 XLCfRules(const XLCfRules& other);
345 XLCfRules(XLCfRules&& other) noexcept = default;
347
348 XLCfRules& operator=(const XLCfRules& other);
349 XLCfRules& operator=(XLCfRules&& other) noexcept = default;
350
351 bool empty() const;
352 uint16_t maxPriorityValue() const;
353 bool setPriority(size_t cfRuleIndex, uint16_t newPriority);
354 void renumberPriorities(uint16_t increment = 1);
355 size_t count() const;
356 XLCfRule cfRuleByIndex(size_t index) const;
357 XLCfRule operator[](size_t index) const { return cfRuleByIndex(index); }
358 size_t create(XLCfRule copyFrom = XLCfRule{}, std::string cfRulePrefix = XLDefaultCfRulePrefix);
359 std::string summary() const;
360
361 private:
362 mutable XMLNode m_conditionalFormattingNode;
363 inline static const std::vector<std::string_view> m_nodeOrder = {"cfRule", "extLst"};
364 };
365
366 class OPENXLSX_EXPORT XLConditionalFormat
367 {
369
370 public:
372 explicit XLConditionalFormat(const XMLNode& node);
374 XLConditionalFormat(XLConditionalFormat&& other) noexcept = default;
376
377 XLConditionalFormat& operator=(const XLConditionalFormat& other);
379
380 bool empty() const;
381 std::string sqref() const;
382 XLCfRules cfRules() const;
384
385 bool setSqref(std::string newSqref);
386 bool setExtLst(XLUnsupportedElement const& newExtLst);
387 std::string summary() const;
388
389 private:
390 mutable XMLNode m_conditionalFormattingNode;
391 inline static const std::vector<std::string_view> m_nodeOrder = {"cfRule", "extLst"};
392 };
393
394 constexpr const char* XLDefaultConditionalFormattingPrefix = "\n\t";
395
396 class OPENXLSX_EXPORT XLConditionalFormats
397 {
398 public:
400 explicit XLConditionalFormats(const XMLNode& sheet);
404
405 XLConditionalFormats& operator=(const XLConditionalFormats& other);
407
408 bool empty() const;
409 size_t count() const;
410 XLConditionalFormat conditionalFormatByIndex(size_t index) const;
411 XLConditionalFormat operator[](size_t index) const { return conditionalFormatByIndex(index); }
412 size_t create(XLConditionalFormat copyFrom = XLConditionalFormat{},
413 std::string conditionalFormattingPrefix = XLDefaultConditionalFormattingPrefix);
414 std::string summary() const;
415
416 private:
417 mutable XMLNode m_sheetNode;
418 const std::vector<std::string_view>* m_nodeOrder;
419 };
420
421 // ----- Helper Builder Functions for XLCfRule -----
422
423 OPENXLSX_EXPORT XLCfRule XLColorScaleRule(const XLColor& minColor, const XLColor& maxColor);
424 OPENXLSX_EXPORT XLCfRule XLColorScaleRule(const XLColor& minColor, const XLColor& midColor, const XLColor& maxColor);
425 OPENXLSX_EXPORT XLCfRule XLDataBarRule(const XLColor& color, bool showValue = true);
426 OPENXLSX_EXPORT XLCfRule XLCellIsRule(XLCfOperator op, const std::string& value);
427 OPENXLSX_EXPORT XLCfRule XLCellIsRule(const std::string& op, const std::string& value);
428 OPENXLSX_EXPORT XLCfRule XLFormulaRule(const std::string& formula);
429
430 // Advanced builders
431 OPENXLSX_EXPORT XLCfRule XLIconSetRule(const std::string& iconSetName = "3TrafficLights1", bool showValue = true, bool reverse = false);
432 OPENXLSX_EXPORT XLCfRule XLTop10Rule(uint16_t rank = 10, bool percent = false, bool bottom = false);
433 OPENXLSX_EXPORT XLCfRule XLAboveAverageRule(bool aboveAverage = true, bool equalAverage = false, int16_t stdDev = 0);
434 OPENXLSX_EXPORT XLCfRule XLDuplicateValuesRule(bool unique = false);
435 OPENXLSX_EXPORT XLCfRule XLContainsTextRule(const std::string& text);
436 OPENXLSX_EXPORT XLCfRule XLNotContainsTextRule(const std::string& text);
437 OPENXLSX_EXPORT XLCfRule XLContainsBlanksRule();
438 OPENXLSX_EXPORT XLCfRule XLNotContainsBlanksRule();
439 OPENXLSX_EXPORT XLCfRule XLContainsErrorsRule();
440 OPENXLSX_EXPORT XLCfRule XLNotContainsErrorsRule();
441
442} // namespace OpenXLSX
443
444#endif
Definition XLXmlParser.hpp:84
Definition XLConditionalFormatting.hpp:124
XMLNode node() const
Definition XLConditionalFormatting.hpp:141
Definition XLConditionalFormatting.hpp:152
XMLNode node() const
Definition XLConditionalFormatting.hpp:197
Definition XLConditionalFormatting.hpp:208
XLDataBarDirection direction() const
uint32_t maxLength() const
void setDirection(XLDataBarDirection direction)
void setBorderColor(const XLColor &color)
XLColor borderColor() const
uint32_t minLength() const
void setAxisPosition(XLDataBarAxisPosition position)
void setMinLength(uint32_t length)
void setNegativeBorderColor(const XLColor &color)
XLDataBarAxisPosition axisPosition() const
void setNegativeFillColor(const XLColor &color)
void setBorder(bool border)
XLColor negativeFillColor() const
XMLNode node() const
Definition XLConditionalFormatting.hpp:256
XLColor negativeBorderColor() const
void setMaxLength(uint32_t length)
XLColor axisColor() const
bool gradient() const
void setAxisColor(const XLColor &color)
void setGradient(bool gradient)
Definition XLConditionalFormatting.hpp:267
XMLNode node() const
Definition XLConditionalFormatting.hpp:329
Definition XLConditionalFormatting.hpp:340
XLCfRules & operator=(XLCfRules &&other) noexcept=default
XLCfRules(XLCfRules &&other) noexcept=default
XLCfRule operator[](size_t index) const
Definition XLConditionalFormatting.hpp:357
Definition XLConditionalFormatting.hpp:89
XMLNode node() const
Definition XLConditionalFormatting.hpp:113
Definition XLColor.hpp:22
Definition XLConditionalFormatting.hpp:367
XLConditionalFormat & operator=(XLConditionalFormat &&other) noexcept=default
XLUnsupportedElement extLst() const
Definition XLConditionalFormatting.hpp:383
XLConditionalFormat(XLConditionalFormat &&other) noexcept=default
Definition XLConditionalFormatting.hpp:397
XLConditionalFormat operator[](size_t index) const
Definition XLConditionalFormatting.hpp:411
XLConditionalFormats & operator=(XLConditionalFormats &&other) noexcept=default
The XLUnsupportedElement class provides a stub implementation that can be used as function parameter ...
Definition XLXmlFile.hpp:28
Definition IZipArchive.hpp:18
XLDataBarDirection
Definition XLConditionalFormatting.hpp:72
constexpr const char * XLDefaultConditionalFormattingPrefix
Definition XLConditionalFormatting.hpp:394
OPENXLSX_EXPORT XLCfRule XLContainsErrorsRule()
Definition XLConditionalFormatting.cpp:1404
OPENXLSX_EXPORT XLCfRule XLAboveAverageRule(bool aboveAverage=true, bool equalAverage=false, int16_t stdDev=0)
Definition XLConditionalFormatting.cpp:1355
OPENXLSX_EXPORT std::string XLCfvoTypeToString(XLCfvoType cfvoType)
Definition XLConditionalFormatting.cpp:217
OPENXLSX_EXPORT XLCfRule XLCellIsRule(XLCfOperator op, const std::string &value)
Definition XLConditionalFormatting.cpp:1282
OPENXLSX_EXPORT XLCfRule XLNotContainsBlanksRule()
Definition XLConditionalFormatting.cpp:1397
OPENXLSX_EXPORT XLCfRule XLDataBarRule(const XLColor &color, bool showValue=true)
Definition XLConditionalFormatting.cpp:1269
OPENXLSX_EXPORT XLCfRule XLContainsBlanksRule()
Definition XLConditionalFormatting.cpp:1390
constexpr const char * XLDefaultCfRulePrefix
Definition XLConditionalFormatting.hpp:337
OPENXLSX_EXPORT std::string XLDataBarDirectionToString(XLDataBarDirection direction)
Definition XLConditionalFormatting.cpp:244
XLCfTimePeriod
Definition XLConditionalFormatting.hpp:56
OPENXLSX_EXPORT std::string XLCfTimePeriodToString(XLCfTimePeriod cfTimePeriod)
inverse of XLCfTimePeriodFromString
Definition XLConditionalFormatting.cpp:176
size_t XLStyleIndex
Definition XLStyles.hpp:31
OPENXLSX_EXPORT XLCfType XLCfTypeFromString(std::string const &typeString)
get the correct XLCfType from the OOXML cfRule type attribute string
Definition XLConditionalFormatting.cpp:19
OPENXLSX_EXPORT XLCfvoType XLCfvoTypeFromString(std::string const &cfvoTypeString)
Definition XLConditionalFormatting.cpp:206
OPENXLSX_EXPORT XLDataBarAxisPosition XLDataBarAxisPositionFromString(std::string const &positionString)
Definition XLConditionalFormatting.cpp:253
OPENXLSX_EXPORT XLDataBarDirection XLDataBarDirectionFromString(std::string const &directionString)
Definition XLConditionalFormatting.cpp:237
OPENXLSX_EXPORT XLCfRule XLTop10Rule(uint16_t rank=10, bool percent=false, bool bottom=false)
Definition XLConditionalFormatting.cpp:1345
OPENXLSX_EXPORT std::string XLCfTypeToString(XLCfType cfType)
inverse of XLCfTypeFromString
Definition XLConditionalFormatting.cpp:46
XLCfvoType
Definition XLConditionalFormatting.hpp:70
OPENXLSX_EXPORT XLCfRule XLNotContainsErrorsRule()
Definition XLConditionalFormatting.cpp:1411
OPENXLSX_EXPORT XLCfOperator XLCfOperatorFromString(std::string const &operatorString)
get the correct XLCfOperator from the OOXML cfRule operator attribute string
Definition XLConditionalFormatting.cpp:97
OPENXLSX_EXPORT XLCfRule XLFormulaRule(const std::string &formula)
Definition XLConditionalFormatting.cpp:1312
OPENXLSX_EXPORT std::string XLDataBarAxisPositionToString(XLDataBarAxisPosition position)
Definition XLConditionalFormatting.cpp:260
XLCfType
Definition XLConditionalFormatting.hpp:18
OPENXLSX_EXPORT std::string XLCfOperatorToString(XLCfOperator cfOperator)
inverse of XLCfOperatorFromString
Definition XLConditionalFormatting.cpp:118
OPENXLSX_EXPORT XLCfRule XLIconSetRule(const std::string &iconSetName="3TrafficLights1", bool showValue=true, bool reverse=false)
Definition XLConditionalFormatting.cpp:1321
OPENXLSX_EXPORT XLCfRule XLContainsTextRule(const std::string &text)
Definition XLConditionalFormatting.cpp:1372
OPENXLSX_EXPORT XLCfRule XLColorScaleRule(const XLColor &minColor, const XLColor &maxColor)
Definition XLConditionalFormatting.cpp:1246
OPENXLSX_EXPORT XLCfTimePeriod XLCfTimePeriodFromString(std::string const &timePeriodString)
get the correct XLCfTimePeriod from the OOXML cfRule timePeriod attribute string
Definition XLConditionalFormatting.cpp:157
XLDataBarAxisPosition
Definition XLConditionalFormatting.hpp:73
OPENXLSX_EXPORT XLCfRule XLDuplicateValuesRule(bool unique=false)
Definition XLConditionalFormatting.cpp:1365
constexpr const uint16_t XLPriorityNotSet
Definition XLConditionalFormatting.hpp:16
OPENXLSX_EXPORT XLCfRule XLNotContainsTextRule(const std::string &text)
Definition XLConditionalFormatting.cpp:1381
XLCfOperator
Definition XLConditionalFormatting.hpp:40