umya_spreadsheet/structs/office2010/excel/
data_validation.rs1use crate::reader::driver::*;
3use crate::structs::office::excel::ReferenceSequence;
4use crate::structs::office2010::excel::DataValidationForumla1;
5use crate::structs::office2010::excel::DataValidationForumla2;
6use crate::structs::BooleanValue;
7use crate::structs::DataValidationOperatorValues;
8use crate::structs::DataValidationValues;
9use crate::structs::EnumValue;
10use crate::structs::StringValue;
11use crate::writer::driver::*;
12use quick_xml::events::{BytesStart, Event};
13use quick_xml::Reader;
14use quick_xml::Writer;
15use std::io::Cursor;
16use std::vec;
17
18#[derive(Default, Debug, Clone)]
19pub struct DataValidation {
20 r#type: EnumValue<DataValidationValues>,
21 operator: EnumValue<DataValidationOperatorValues>,
22 allow_blank: BooleanValue,
23 show_input_message: BooleanValue,
24 show_error_message: BooleanValue,
25 prompt_title: StringValue,
26 prompt: StringValue,
27 reference_sequence: ReferenceSequence,
28 formula1: Option<Box<DataValidationForumla1>>,
29 formula2: Option<Box<DataValidationForumla2>>,
30}
31impl DataValidation {
32 #[inline]
33 pub fn get_type(&self) -> &DataValidationValues {
34 self.r#type.get_value()
35 }
36
37 #[inline]
38 pub fn set_type(&mut self, value: DataValidationValues) -> &mut Self {
39 self.r#type.set_value(value);
40 self
41 }
42
43 #[inline]
44 pub fn get_operator(&self) -> &DataValidationOperatorValues {
45 self.operator.get_value()
46 }
47
48 #[inline]
49 pub fn set_operator(&mut self, value: DataValidationOperatorValues) -> &mut Self {
50 self.operator.set_value(value);
51 self
52 }
53
54 #[inline]
55 pub fn get_allow_blank(&self) -> &bool {
56 self.allow_blank.get_value()
57 }
58
59 #[inline]
60 pub fn set_allow_blank(&mut self, value: bool) -> &mut Self {
61 self.allow_blank.set_value(value);
62 self
63 }
64
65 #[inline]
66 pub fn get_show_input_message(&self) -> &bool {
67 self.show_input_message.get_value()
68 }
69
70 #[inline]
71 pub fn set_show_input_message(&mut self, value: bool) -> &mut Self {
72 self.show_input_message.set_value(value);
73 self
74 }
75
76 #[inline]
77 pub fn get_show_error_message(&self) -> &bool {
78 self.show_error_message.get_value()
79 }
80
81 #[inline]
82 pub fn set_show_error_message(&mut self, value: bool) -> &mut Self {
83 self.show_error_message.set_value(value);
84 self
85 }
86
87 #[inline]
88 pub fn get_prompt_title(&self) -> &str {
89 self.prompt_title.get_value_str()
90 }
91
92 #[inline]
93 pub fn set_prompt_title<S: Into<String>>(&mut self, value: S) -> &mut Self {
94 self.prompt_title.set_value(value);
95 self
96 }
97
98 #[inline]
99 pub fn get_prompt(&self) -> &str {
100 self.prompt.get_value_str()
101 }
102
103 #[inline]
104 pub fn set_prompt<S: Into<String>>(&mut self, value: S) -> &mut Self {
105 self.prompt.set_value(value);
106 self
107 }
108
109 #[inline]
110 pub fn get_reference_sequence(&self) -> &ReferenceSequence {
111 &self.reference_sequence
112 }
113
114 #[inline]
115 pub fn get_reference_sequence_mut(&mut self) -> &mut ReferenceSequence {
116 &mut self.reference_sequence
117 }
118
119 #[inline]
120 pub fn set_reference_sequence(&mut self, value: ReferenceSequence) -> &mut Self {
121 self.reference_sequence = value;
122 self
123 }
124
125 #[inline]
126 pub fn get_formula1(&self) -> Option<&DataValidationForumla1> {
127 self.formula1.as_deref()
128 }
129
130 #[inline]
131 pub fn get_formula1_mut(&mut self) -> Option<&mut DataValidationForumla1> {
132 self.formula1.as_deref_mut()
133 }
134
135 #[inline]
136 pub fn set_formula1(&mut self, value: DataValidationForumla1) -> &mut Self {
137 self.formula1 = Some(Box::new(value));
138 self
139 }
140
141 #[inline]
142 pub fn remove_formula1(&mut self) -> &mut Self {
143 self.formula1 = None;
144 self
145 }
146
147 #[inline]
148 pub fn get_formula2(&self) -> Option<&DataValidationForumla2> {
149 self.formula2.as_deref()
150 }
151
152 #[inline]
153 pub fn get_formula2_mut(&mut self) -> Option<&mut DataValidationForumla2> {
154 self.formula2.as_deref_mut()
155 }
156
157 #[inline]
158 pub fn set_formula2(&mut self, value: DataValidationForumla2) -> &mut Self {
159 self.formula2 = Some(Box::new(value));
160 self
161 }
162
163 #[inline]
164 pub fn remove_formula2(&mut self) -> &mut Self {
165 self.formula2 = None;
166 self
167 }
168
169 pub(crate) fn set_attributes<R: std::io::BufRead>(
170 &mut self,
171 reader: &mut Reader<R>,
172 e: &BytesStart,
173 empty_flg: bool,
174 ) {
175 if let Some(v) = get_attribute(e, b"type") {
176 self.r#type.set_value_string(v);
177 }
178
179 if let Some(v) = get_attribute(e, b"operator") {
180 self.operator.set_value_string(v);
181 }
182
183 if let Some(v) = get_attribute(e, b"allowBlank") {
184 self.allow_blank.set_value_string(v);
185 }
186
187 if let Some(v) = get_attribute(e, b"showInputMessage") {
188 self.show_input_message.set_value_string(v);
189 }
190
191 if let Some(v) = get_attribute(e, b"showErrorMessage") {
192 self.show_error_message.set_value_string(v);
193 }
194
195 if let Some(v) = get_attribute(e, b"promptTitle") {
196 self.prompt_title.set_value_string(v);
197 }
198
199 if let Some(v) = get_attribute(e, b"prompt") {
200 self.prompt.set_value_string(v);
201 }
202
203 if empty_flg {
204 return;
205 }
206
207 let mut buf = Vec::new();
208 loop {
209 match reader.read_event_into(&mut buf) {
210 Ok(Event::Start(ref e)) => match e.name().into_inner() {
211 b"x14:formula1" => {
212 let mut obj = DataValidationForumla1::default();
213 obj.set_attributes(reader, e);
214 self.formula1 = Some(Box::new(obj));
215 }
216 b"x14:formula2" => {
217 let mut obj = DataValidationForumla2::default();
218 obj.set_attributes(reader, e);
219 self.formula2 = Some(Box::new(obj));
220 }
221 b"xm:sqref" => {
222 let mut obj = ReferenceSequence::default();
223 obj.set_attributes(reader, e);
224 self.reference_sequence = obj;
225 }
226 _ => (),
227 },
228 Ok(Event::End(ref e)) => match e.name().into_inner() {
229 b"x14:dataValidation" => return,
230 _ => (),
231 },
232 Ok(Event::Eof) => {
233 panic!("Error: Could not find {} end element", "x14:dataValidation")
234 }
235 Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
236 _ => (),
237 }
238 buf.clear();
239 }
240 }
241
242 pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
243 let mut attributes: Vec<(&str, &str)> = Vec::new();
245
246 if self.r#type.has_value() {
247 attributes.push(("type", self.r#type.get_value_string()));
248 }
249
250 if self.allow_blank.has_value() {
251 attributes.push(("allowBlank", self.allow_blank.get_value_string()));
252 }
253
254 if self.show_input_message.has_value() {
255 attributes.push((
256 "showInputMessage",
257 self.show_input_message.get_value_string(),
258 ));
259 }
260
261 if self.operator.has_value() {
262 attributes.push(("operator", self.operator.get_value_string()));
263 }
264
265 if self.show_error_message.has_value() {
266 attributes.push((
267 "showErrorMessage",
268 self.show_error_message.get_value_string(),
269 ));
270 }
271
272 if self.prompt_title.has_value() {
273 attributes.push(("promptTitle", self.prompt_title.get_value_str()));
274 }
275
276 if self.prompt.has_value() {
277 attributes.push(("prompt", self.prompt.get_value_str()));
278 }
279
280 write_start_tag(writer, "x14:dataValidation", attributes, false);
281 match &self.formula1 {
282 Some(v) => v.write_to(writer),
283 None => {}
284 }
285 match &self.formula2 {
286 Some(v) => v.write_to(writer),
287 None => {}
288 }
289 self.reference_sequence.write_to(writer);
290 write_end_tag(writer, "x14:dataValidation");
291 }
292}