OpenXLSX 1.9.1
Loading...
Searching...
No Matches
XLDataValidation.hpp
Go to the documentation of this file.
1#ifndef OPENXLSX_XLDATAVALIDATION_HPP
2#define OPENXLSX_XLDATAVALIDATION_HPP
3
4#include "OpenXLSX-Exports.hpp"
5#include "XLCellReference.hpp"
6#include "XLXmlParser.hpp"
7#include <string>
8#include <string_view>
9#include <vector>
10
11namespace OpenXLSX
12{
17
31
36
40 enum class XLIMEMode {
42 Off,
43 On,
52 };
53
54 class XLDataValidation;
55
59 struct OPENXLSX_EXPORT XLDataValidationConfig
60 {
61 XLDataValidationType type = XLDataValidationType::None;
62 XLDataValidationOperator operator_ = XLDataValidationOperator::Between;
63 bool allowBlank = true;
64 bool showDropDown = false;
65 bool showInputMessage = false;
66 bool showErrorMessage = false;
67 XLIMEMode imeMode = XLIMEMode::NoControl;
68 XLDataValidationErrorStyle errorStyle = XLDataValidationErrorStyle::Stop;
69 std::string promptTitle;
70 std::string prompt;
71 std::string errorTitle;
72 std::string error;
73 std::string formula1;
74 std::string formula2;
75
77 explicit XLDataValidationConfig(const XLDataValidation& dv);
78 };
79
83 class OPENXLSX_EXPORT XLDataValidation
84 {
85 public:
86 XLDataValidation() : m_node(nullptr) {}
87 explicit XLDataValidation(const XMLNode& node) : m_node(node) {}
88
89 [[nodiscard]] bool empty() const { return !m_node; }
90
97 XLDataValidation& requireList(const std::vector<std::string>& list, bool allowBlank = true);
98
105 XLDataValidation& requireList(std::string_view formula, bool allowBlank = true);
106
107 XLDataValidation& setErrorAlert(std::string_view title,
108 std::string_view message,
109 XLDataValidationErrorStyle style = XLDataValidationErrorStyle::Stop);
110 XLDataValidation& setPromptMessage(std::string_view title, std::string_view message);
111
112 // Config API
113 [[nodiscard]] XLDataValidationConfig config() const;
114 void applyConfig(const XLDataValidationConfig& config);
115
116 // Getters
117 [[nodiscard]] std::string sqref() const;
118 [[nodiscard]] XLDataValidationType type() const;
119 [[nodiscard]] XLDataValidationOperator operator_() const;
120 [[nodiscard]] bool allowBlank() const;
121 [[nodiscard]] std::string formula1() const;
122 [[nodiscard]] std::string formula2() const;
123 [[nodiscard]] bool showDropDown() const;
124 [[nodiscard]] bool showInputMessage() const;
125 [[nodiscard]] bool showErrorMessage() const;
126 [[nodiscard]] XLIMEMode imeMode() const;
127 [[nodiscard]] std::string promptTitle() const;
128 [[nodiscard]] std::string prompt() const;
129 [[nodiscard]] std::string errorTitle() const;
130 [[nodiscard]] std::string error() const;
131 [[nodiscard]] XLDataValidationErrorStyle errorStyle() const;
132
133 // Setters
134 void setSqref(std::string_view sqref);
135 void addCell(const XLCellReference& ref);
136 void addCell(const std::string& ref);
137 void addRange(const XLCellReference& topLeft, const XLCellReference& bottomRight);
138 void addRange(const std::string& range);
139 void removeCell(const XLCellReference& ref);
140 void removeCell(const std::string& ref);
141 void removeRange(const XLCellReference& topLeft, const XLCellReference& bottomRight);
142 void removeRange(const std::string& range);
143 void setType(XLDataValidationType type);
144 void setOperator(XLDataValidationOperator op);
145 void setAllowBlank(bool allow);
146 void setFormula1(std::string_view formula);
147 void setFormula2(std::string_view formula);
148 void setPrompt(std::string_view title, std::string_view msg);
149 void setError(std::string_view title, std::string_view msg, XLDataValidationErrorStyle style = XLDataValidationErrorStyle::Stop);
150
151 // Priority 1 New Properties
152 void setShowDropDown(bool show);
153 void setShowInputMessage(bool show);
154 void setShowErrorMessage(bool show);
155 void setIMEMode(XLIMEMode mode);
156
157 // Specific helpers
158 void setWholeNumberRange(double min, double max);
159 void setDecimalRange(double min, double max);
160 void setDateRange(std::string_view min, std::string_view max);
161 void setTimeRange(std::string_view min, std::string_view max);
162 void setTextLengthRange(int min, int max);
163 void setList(const std::vector<std::string>& items);
164 void setReferenceDropList(std::string_view targetSheet, std::string_view range);
165
166 private:
167 mutable XMLNode m_node;
168 };
169
173 class OPENXLSX_EXPORT XLDataValidations
174 {
175 public:
176 // Iterator class for XLDataValidations
178 {
179 public:
180 using iterator_category = std::forward_iterator_tag;
182 using difference_type = std::ptrdiff_t;
185
186 Iterator() : m_node(nullptr) {}
187 explicit Iterator(XMLNode node) : m_node(node) {}
188
189 value_type operator*() const { return XLDataValidation(m_node); }
190
191 class Proxy
192 {
193 value_type m_val;
194
195 public:
196 explicit Proxy(value_type val) : m_val(val) {}
197 value_type* operator->() { return &m_val; }
198 };
199
200 Proxy operator->() const { return Proxy(XLDataValidation(m_node)); }
201
203 {
204 if (m_node) m_node = m_node.next_sibling("dataValidation");
205 return *this;
206 }
208 {
209 Iterator tmp = *this;
210 ++(*this);
211 return tmp;
212 }
213 bool operator==(const Iterator& other) const { return m_node == other.m_node; }
214 bool operator!=(const Iterator& other) const { return m_node != other.m_node; }
215
216 private:
217 XMLNode m_node;
218 };
219
220 XLDataValidations() : m_sheetNode(nullptr) {}
221 explicit XLDataValidations(const XMLNode& node) : m_sheetNode(node) {}
222
223 [[nodiscard]] bool empty() const
224 {
225 if (!m_sheetNode) return true;
226 return !m_sheetNode.child("dataValidations");
227 }
228 [[nodiscard]] size_t count() const;
229 XLDataValidation append();
230 XLDataValidation addValidation(const XLDataValidationConfig& config, std::string_view sqref);
236 XLDataValidation add(std::string_view sqref);
237
238 [[nodiscard]] XLDataValidation at(size_t index);
239 [[nodiscard]] XLDataValidation at(std::string_view sqref);
240 void clear();
241
242 // Priority 1/4 New Properties: Precise Deletion and Iterators
243 void remove(size_t index);
244 void remove(std::string_view sqref);
245
246 // Global properties
247 [[nodiscard]] bool disablePrompts() const;
248 void setDisablePrompts(bool disable);
249
250 [[nodiscard]] uint32_t xWindow() const;
251 void setXWindow(uint32_t x);
252
253 [[nodiscard]] uint32_t yWindow() const;
254 void setYWindow(uint32_t y);
255
256 [[nodiscard]] Iterator begin() const
257 {
258 if (!m_sheetNode) return Iterator();
259 auto dvNode = m_sheetNode.child("dataValidations");
260 if (!dvNode) return Iterator();
261 return Iterator(dvNode.child("dataValidation"));
262 }
263
264 [[nodiscard]] Iterator end() const { return Iterator(); }
265
266 private:
267 mutable XMLNode m_sheetNode;
268 };
269} // namespace OpenXLSX
270
271#endif // OPENXLSX_XLDATAVALIDATION_HPP
Definition XLXmlParser.hpp:84
Definition XLCellReference.hpp:34
Definition XLDataValidation.hpp:84
XLDataValidation(const XMLNode &node)
Definition XLDataValidation.hpp:87
XLDataValidation()
Definition XLDataValidation.hpp:86
bool empty() const
Definition XLDataValidation.hpp:89
Definition XLDataValidation.hpp:192
Proxy(value_type val)
Definition XLDataValidation.hpp:196
value_type * operator->()
Definition XLDataValidation.hpp:197
Definition XLDataValidation.hpp:178
Iterator()
Definition XLDataValidation.hpp:186
Proxy operator->() const
Definition XLDataValidation.hpp:200
Iterator & operator++()
Definition XLDataValidation.hpp:202
std::ptrdiff_t difference_type
Definition XLDataValidation.hpp:182
value_type operator*() const
Definition XLDataValidation.hpp:189
std::forward_iterator_tag iterator_category
Definition XLDataValidation.hpp:180
Iterator(XMLNode node)
Definition XLDataValidation.hpp:187
Iterator operator++(int)
Definition XLDataValidation.hpp:207
bool operator==(const Iterator &other) const
Definition XLDataValidation.hpp:213
bool operator!=(const Iterator &other) const
Definition XLDataValidation.hpp:214
Definition XLDataValidation.hpp:174
bool empty() const
Definition XLDataValidation.hpp:223
XLDataValidations(const XMLNode &node)
Definition XLDataValidation.hpp:221
XLDataValidations()
Definition XLDataValidation.hpp:220
Iterator begin() const
Definition XLDataValidation.hpp:256
Iterator end() const
Definition XLDataValidation.hpp:264
Definition IZipArchive.hpp:18
XLIMEMode
IME (Input Method Editor) mode for data validation.
Definition XLDataValidation.hpp:40
XLDataValidationErrorStyle
Definition XLDataValidation.hpp:35
XLDataValidationType
Definition XLDataValidation.hpp:16
XLDataValidationOperator
Definition XLDataValidation.hpp:21
Definition XLDataValidation.hpp:60
std::string prompt
Definition XLDataValidation.hpp:70
std::string promptTitle
Definition XLDataValidation.hpp:69
std::string error
Definition XLDataValidation.hpp:72
std::string formula1
Definition XLDataValidation.hpp:73
std::string errorTitle
Definition XLDataValidation.hpp:71
std::string formula2
Definition XLDataValidation.hpp:74