umya-spreadsheet 3.0.0

umya-spreadsheet is a library written in pure Rust to read and write xlsx file.
Documentation
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
// dataValidation
use std::{
    io::Cursor,
    vec,
};

use quick_xml::{
    Reader,
    Writer,
    events::{
        BytesStart,
        Event,
    },
};

use super::{
    BooleanValue,
    DataValidationOperatorValues,
    DataValidationValues,
    EnumValue,
    SequenceOfReferences,
    StringValue,
};
use crate::{
    reader::driver::get_attribute,
    writer::driver::{
        write_end_tag,
        write_start_tag,
        write_text_node,
    },
};

#[derive(Default, Debug, Clone)]
pub struct DataValidation {
    r#type:                 EnumValue<DataValidationValues>,
    operator:               EnumValue<DataValidationOperatorValues>,
    allow_blank:            BooleanValue,
    show_input_message:     BooleanValue,
    show_error_message:     BooleanValue,
    prompt_title:           StringValue,
    prompt:                 StringValue,
    error_title:            StringValue,
    error_messsage:         StringValue,
    sequence_of_references: SequenceOfReferences,
    formula1:               StringValue,
    formula2:               StringValue,
}
impl DataValidation {
    #[inline]
    #[must_use]
    pub fn get_type(&self) -> &DataValidationValues {
        self.r#type.value()
    }

    #[inline]
    pub fn set_type(&mut self, value: DataValidationValues) -> &mut Self {
        self.r#type.set_value(value);
        self
    }

    #[inline]
    #[must_use]
    pub fn operator(&self) -> &DataValidationOperatorValues {
        self.operator.value()
    }

    #[inline]
    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use operator()")]
    pub fn get_operator(&self) -> &DataValidationOperatorValues {
        self.operator()
    }

    #[inline]
    pub fn set_operator(&mut self, value: DataValidationOperatorValues) -> &mut Self {
        self.operator.set_value(value);
        self
    }

    #[inline]
    #[must_use]
    pub fn allow_blank(&self) -> bool {
        self.allow_blank.value()
    }

    #[inline]
    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use allow_blank()")]
    pub fn get_allow_blank(&self) -> bool {
        self.allow_blank()
    }

    #[inline]
    pub fn set_allow_blank(&mut self, value: bool) -> &mut Self {
        self.allow_blank.set_value(value);
        self
    }

    #[inline]
    #[must_use]
    pub fn show_input_message(&self) -> bool {
        self.show_input_message.value()
    }

    #[inline]
    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use show_input_message()")]
    pub fn get_show_input_message(&self) -> bool {
        self.show_input_message()
    }

    #[inline]
    pub fn set_show_input_message(&mut self, value: bool) -> &mut Self {
        self.show_input_message.set_value(value);
        self
    }

    #[inline]
    #[must_use]
    pub fn show_error_message(&self) -> bool {
        self.show_error_message.value()
    }

    #[inline]
    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use show_error_message()")]
    pub fn get_show_error_message(&self) -> bool {
        self.show_error_message()
    }

    #[inline]
    pub fn set_show_error_message(&mut self, value: bool) -> &mut Self {
        self.show_error_message.set_value(value);
        self
    }

    #[inline]
    #[must_use]
    pub fn prompt_title(&self) -> &str {
        self.prompt_title.value_str()
    }

    #[inline]
    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use prompt_title()")]
    pub fn get_prompt_title(&self) -> &str {
        self.prompt_title()
    }

    #[inline]
    pub fn set_prompt_title<S: Into<String>>(&mut self, value: S) -> &mut Self {
        self.prompt_title.set_value(value);
        self
    }

    #[inline]
    #[must_use]
    pub fn error_title(&self) -> &str {
        self.error_title.value_str()
    }

    #[inline]
    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use error_title()")]
    pub fn get_error_title(&self) -> &str {
        self.error_title()
    }

    #[inline]
    pub fn set_error_title<S: Into<String>>(&mut self, value: S) -> &mut Self {
        self.error_title.set_value(value);
        self
    }

    #[inline]
    #[must_use]
    pub fn error_message(&self) -> &str {
        self.error_messsage.value_str()
    }

    #[inline]
    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use error_messsage()")]
    pub fn get_error_message(&self) -> &str {
        self.error_message()
    }

    #[inline]
    pub fn set_error_message<S: Into<String>>(&mut self, value: S) -> &mut Self {
        self.error_messsage.set_value(value);
        self
    }

    #[inline]
    #[must_use]
    pub fn prompt(&self) -> &str {
        self.prompt.value_str()
    }

    #[inline]
    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use prompt()")]
    pub fn get_prompt(&self) -> &str {
        self.prompt()
    }

    #[inline]
    pub fn set_prompt<S: Into<String>>(&mut self, value: S) -> &mut Self {
        self.prompt.set_value(value);
        self
    }

    #[inline]
    #[must_use]
    pub fn sequence_of_references(&self) -> &SequenceOfReferences {
        &self.sequence_of_references
    }

    #[inline]
    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use sequence_of_references()")]
    pub fn get_sequence_of_references(&self) -> &SequenceOfReferences {
        self.sequence_of_references()
    }

    #[inline]
    pub fn sequence_of_references_mut(&mut self) -> &mut SequenceOfReferences {
        &mut self.sequence_of_references
    }

    #[inline]
    #[deprecated(since = "3.0.0", note = "Use sequence_of_references_mut()")]
    pub fn get_sequence_of_references_mut(&mut self) -> &mut SequenceOfReferences {
        self.sequence_of_references_mut()
    }

    #[inline]
    pub fn set_sequence_of_references(&mut self, value: SequenceOfReferences) -> &mut Self {
        self.sequence_of_references = value;
        self
    }

    #[inline]
    #[must_use]
    pub fn formula1(&self) -> &str {
        self.formula1.value_str()
    }

    #[inline]
    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use formula1()")]
    pub fn get_formula1(&self) -> &str {
        self.formula1()
    }

    #[inline]
    pub fn set_formula1<S: Into<String>>(&mut self, value: S) -> &mut Self {
        self.formula1.set_value(value);
        self
    }

    #[inline]
    #[must_use]
    pub fn formula2(&self) -> &str {
        self.formula2.value_str()
    }

    #[inline]
    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use formula2()")]
    pub fn get_formula2(&self) -> &str {
        self.formula2()
    }

    #[inline]
    pub fn set_formula2<S: Into<String>>(&mut self, value: S) -> &mut Self {
        self.formula2.set_value(value);
        self
    }

    pub(crate) fn set_attributes<R: std::io::BufRead>(
        &mut self,
        reader: &mut Reader<R>,
        e: &BytesStart,
        empty_flg: bool,
    ) {
        if let Some(v) = get_attribute(e, b"type") {
            self.r#type.set_value_string(v);
        }

        if let Some(v) = get_attribute(e, b"operator") {
            self.operator.set_value_string(v);
        }

        if let Some(v) = get_attribute(e, b"allowBlank") {
            self.allow_blank.set_value_string(v);
        }

        if let Some(v) = get_attribute(e, b"showInputMessage") {
            self.show_input_message.set_value_string(v);
        }

        if let Some(v) = get_attribute(e, b"showErrorMessage") {
            self.show_error_message.set_value_string(v);
        }

        if let Some(v) = get_attribute(e, b"errorTitle") {
            self.error_title.set_value_string(v);
        }

        if let Some(v) = get_attribute(e, b"error") {
            self.error_messsage.set_value_string(v);
        }

        if let Some(v) = get_attribute(e, b"promptTitle") {
            self.prompt_title.set_value_string(v);
        }

        if let Some(v) = get_attribute(e, b"prompt") {
            self.prompt.set_value_string(v);
        }

        if let Some(v) = get_attribute(e, b"sqref") {
            self.sequence_of_references.set_sqref(v);
        }

        if empty_flg {
            return;
        }

        let mut value: String = String::new();
        let mut buf = Vec::new();
        loop {
            match reader.read_event_into(&mut buf) {
                Ok(Event::Text(e)) => {
                    value = e.unescape().unwrap().to_string();
                }
                Ok(Event::End(ref e)) => match e.name().into_inner() {
                    b"formula1" => {
                        self.formula1.set_value_string(std::mem::take(&mut value));
                    }
                    b"formula2" => {
                        self.formula2.set_value_string(std::mem::take(&mut value));
                    }
                    b"dataValidation" => return,
                    _ => {}
                },
                Ok(Event::Eof) => {
                    panic!("Error: Could not find {} end element", "dataValidation")
                }
                Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
                _ => {}
            }
            buf.clear();
        }
    }

    pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
        let is_inner = self.formula1.has_value() || self.formula2.has_value();

        // dataValidation
        let mut attributes: crate::structs::AttrCollection = Vec::new();

        if self.r#type.has_value() {
            attributes.push(("type", self.r#type.value_string()).into());
        }

        if self.allow_blank.has_value() {
            attributes.push(("allowBlank", self.allow_blank.value_string()).into());
        }

        if self.show_input_message.has_value() {
            attributes.push(
                (
                    "showInputMessage",
                    self.show_input_message.value_string(),
                )
                    .into(),
            );
        }

        if self.operator.has_value() {
            attributes.push(("operator", self.operator.value_string()).into());
        }

        if self.show_error_message.has_value() {
            attributes.push(
                (
                    "showErrorMessage",
                    self.show_error_message.value_string(),
                )
                    .into(),
            );
        }

        if self.error_title.has_value() {
            attributes.push(("errorTitle", self.error_title.value_str()).into());
        }

        if self.error_messsage.has_value() {
            attributes.push(("error", self.error_messsage.value_str()).into());
        }

        if self.prompt_title.has_value() {
            attributes.push(("promptTitle", self.prompt_title.value_str()).into());
        }

        if self.prompt.has_value() {
            attributes.push(("prompt", self.prompt.value_str()).into());
        }

        let sequence_of_references = &self.sequence_of_references.get_sqref();
        if !sequence_of_references.is_empty() {
            attributes.push(("sqref", sequence_of_references).into());
        }

        write_start_tag(writer, "dataValidation", attributes, !is_inner);
        if is_inner {
            if self.formula1.has_value() {
                write_start_tag(writer, "formula1", vec![], false);
                write_text_node(writer, self.formula1.value_str());
                write_end_tag(writer, "formula1");
            }
            if self.formula2.has_value() {
                write_start_tag(writer, "formula2", vec![], false);
                write_text_node(writer, self.formula2.value_str());
                write_end_tag(writer, "formula2");
            }
            write_end_tag(writer, "dataValidation");
        }
    }
}