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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
use std::io::Cursor;

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

use super::{
    BooleanValue,
    ColorScale,
    ConditionalFormatValues,
    ConditionalFormattingOperatorValues,
    DataBar,
    DifferentialFormats,
    EnumValue,
    Formula,
    IconSet,
    Int32Value,
    StringValue,
    Style,
    TimePeriodValues,
    UInt32Value,
};
use crate::{
    reader::driver::{
        get_attribute,
        set_string_from_xml,
        xml_read_loop,
    },
    writer::driver::{
        write_end_tag,
        write_start_tag,
    },
};

#[derive(Clone, Default, Debug)]
pub struct ConditionalFormattingRule {
    r#type:        EnumValue<ConditionalFormatValues>,
    operator:      EnumValue<ConditionalFormattingOperatorValues>,
    text:          StringValue,
    priority:      Int32Value,
    percent:       BooleanValue,
    bottom:        BooleanValue,
    rank:          UInt32Value,
    stop_if_true:  BooleanValue,
    std_dev:       Int32Value,
    above_average: BooleanValue,
    equal_average: BooleanValue,
    time_period:   EnumValue<TimePeriodValues>,
    style:         Option<Box<Style>>,
    color_scale:   Option<ColorScale>,
    data_bar:      Option<DataBar>,
    icon_set:      Option<IconSet>,
    formula:       Option<Box<Formula>>,
}

impl ConditionalFormattingRule {
    #[inline]
    #[must_use]
    pub fn get_type(&self) -> &ConditionalFormatValues {
        self.r#type.value()
    }

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

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

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

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

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

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

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

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

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

    #[inline]
    pub fn set_priority(&mut self, value: i32) -> &mut Self {
        self.priority.set_value(value);
        self
    }

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

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

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

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

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

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

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

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

    #[inline]
    pub fn set_rank(&mut self, value: u32) -> &mut Self {
        self.rank.set_value(value);
        self
    }

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

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

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

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

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

    #[inline]
    pub fn set_std_dev(&mut self, value: i32) -> &mut Self {
        self.std_dev.set_value(value);
        self
    }

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

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

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

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

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

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

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

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

    #[inline]
    pub fn set_time_period(&mut self, value: TimePeriodValues) -> &mut Self {
        self.time_period.set_value(value);
        self
    }

    #[inline]
    #[must_use]
    pub fn style(&self) -> Option<&Style> {
        self.style.as_deref()
    }

    #[inline]
    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use style()")]
    pub fn get_style(&self) -> Option<&Style> {
        self.style()
    }

    #[inline]
    pub fn set_style(&mut self, value: Style) -> &mut Self {
        self.style = Some(Box::new(value));
        self
    }

    #[inline]
    pub fn remove_style(&mut self) -> &mut Self {
        self.style = None;
        self
    }

    #[inline]
    #[must_use]
    pub fn color_scale(&self) -> Option<&ColorScale> {
        self.color_scale.as_ref()
    }

    #[inline]
    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use color_scale()")]
    pub fn get_color_scale(&self) -> Option<&ColorScale> {
        self.color_scale()
    }

    #[inline]
    pub fn set_color_scale(&mut self, value: ColorScale) -> &mut Self {
        self.color_scale = Some(value);
        self
    }

    #[inline]
    pub fn remove_color_scale(&mut self) -> &mut Self {
        self.color_scale = None;
        self
    }

    #[inline]
    #[must_use]
    pub fn data_bar(&self) -> Option<&DataBar> {
        self.data_bar.as_ref()
    }

    #[inline]
    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use data_bar()")]
    pub fn get_data_bar(&self) -> Option<&DataBar> {
        self.data_bar()
    }

    #[inline]
    pub fn set_data_bar(&mut self, value: DataBar) -> &mut Self {
        self.data_bar = Some(value);
        self
    }

    #[inline]
    pub fn remove_data_bar(&mut self) -> &mut Self {
        self.data_bar = None;
        self
    }

    #[inline]
    #[must_use]
    pub fn icon_set(&self) -> Option<&IconSet> {
        self.icon_set.as_ref()
    }

    #[inline]
    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use icon_set()")]
    pub fn get_icon_set(&self) -> Option<&IconSet> {
        self.icon_set()
    }

    #[inline]
    pub fn set_icon_set(&mut self, value: IconSet) -> &mut Self {
        self.icon_set = Some(value);
        self
    }

    #[inline]
    pub fn remove_icon_set(&mut self) -> &mut Self {
        self.icon_set = None;
        self
    }

    #[inline]
    #[must_use]
    pub fn formula(&self) -> Option<&Formula> {
        self.formula.as_deref()
    }

    #[inline]
    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use formula()")]
    pub fn get_formula(&self) -> Option<&Formula> {
        self.formula()
    }

    #[inline]
    pub fn set_formula(&mut self, value: Formula) -> &mut Self {
        self.formula = Some(Box::new(value));
        self
    }

    #[inline]
    pub fn remove_formula(&mut self) -> &mut Self {
        self.formula = None;
        self
    }

    pub(crate) fn set_attributes<R: std::io::BufRead>(
        &mut self,
        reader: &mut Reader<R>,
        e: &BytesStart,
        differential_formats: &DifferentialFormats,
        empty_flag: bool,
    ) {
        set_string_from_xml!(self, e, r#type, "type");
        set_string_from_xml!(self, e, operator, "operator");

        if let Some(v) = get_attribute(e, b"dxfId") {
            if let Ok(dxf_id) = v.parse::<usize>() {
                let style = differential_formats.style(dxf_id);
                self.set_style(style);
            }
        }

        set_string_from_xml!(self, e, priority, "priority");
        set_string_from_xml!(self, e, percent, "percent");
        set_string_from_xml!(self, e, bottom, "bottom");
        set_string_from_xml!(self, e, rank, "rank");
        set_string_from_xml!(self, e, stop_if_true, "stopIfTrue");
        set_string_from_xml!(self, e, std_dev, "stdDev");
        set_string_from_xml!(self, e, time_period, "timePeriod");
        set_string_from_xml!(self, e, above_average, "aboveAverage");
        set_string_from_xml!(self, e, equal_average, "equalAverage");

        if empty_flag {
            return;
        }

        xml_read_loop!(
            reader,
            Event::Start(ref e) => {
                match e.name().into_inner() {
                    b"colorScale" => {
                        let mut obj = ColorScale::default();
                        obj.set_attributes(reader, e);
                        self.color_scale = Some(obj);
                    }
                    b"dataBar" => {
                        let mut obj = DataBar::default();
                        obj.set_attributes(reader, e);
                        self.data_bar = Some(obj);
                    }
                    b"iconSet" => {
                        let mut obj = IconSet::default();
                        obj.set_attributes(reader, e);
                        self.icon_set = Some(obj);
                    }
                    b"formula" => {
                        let mut obj = Formula::default();
                        obj.set_attributes(reader, e);
                        self.formula = Some(Box::new(obj));
                    }
                    _ => (),
                }
            },
            Event::End(ref e) => {
                if e.name().into_inner() == b"cfRule" {
                    return
                }
            },
            Event::Eof => return
        );
    }

    pub(crate) fn write_to(
        &self,
        writer: &mut Writer<Cursor<Vec<u8>>>,
        differential_formats: &mut DifferentialFormats,
    ) {
        let is_inner = self.color_scale.is_some()
            || self.data_bar.is_some()
            || self.icon_set.is_some()
            || self.formula.is_some();

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

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

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

        let dxf_id_str: String;
        if let Some(v) = &self.style {
            let dxf_id = differential_formats.set_style(v);
            dxf_id_str = dxf_id.to_string();
            attributes.push(("dxfId", &dxf_id_str).into());
        }

        let priority = self.priority.value_string();
        if self.priority.has_value() {
            attributes.push(("priority", &priority).into());
        }

        let percent = self.percent.value_string();
        if self.percent.has_value() {
            attributes.push(("percent", percent).into());
        }

        let bottom = self.bottom.value_string();
        if self.bottom.has_value() {
            attributes.push(("bottom", bottom).into());
        }

        let rank = self.rank.value_string();
        if self.rank.has_value() {
            attributes.push(("rank", &rank).into());
        }

        let stop_if_true = self.stop_if_true.value_string();
        if self.stop_if_true.has_value() {
            attributes.push(("stopIfTrue", stop_if_true).into());
        }

        let std_dev = self.std_dev.value_string();
        if self.std_dev.has_value() {
            attributes.push(("stdDev", &std_dev).into());
        }

        let time_period = self.time_period.value_string();
        if self.time_period.has_value() {
            attributes.push(("timePeriod", time_period).into());
        }

        let above_average = self.above_average.value_string();
        if self.above_average.has_value() {
            attributes.push(("aboveAverage", above_average).into());
        }

        let equal_average = self.equal_average.value_string();
        if self.equal_average.has_value() {
            attributes.push(("equalAverage", equal_average).into());
        }

        write_start_tag(writer, "cfRule", attributes, !is_inner);

        if is_inner {
            // colorScale
            if let Some(v) = &self.color_scale {
                v.write_to(writer);
            }

            // dataBar
            if let Some(v) = &self.data_bar {
                v.write_to(writer);
            }

            // iconSet
            if let Some(v) = &self.icon_set {
                v.write_to(writer);
            }

            // formula
            if let Some(v) = &self.formula {
                v.write_to(writer);
            }

            write_end_tag(writer, "cfRule");
        }
    }
}