umya-spreadsheet 2.3.3

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
// styleSheet
use super::BordersCrate;
use super::CellFormat;
use super::CellFormats;
use super::CellStyleFormats;
use super::CellStyles;
use super::Colors;
use super::DifferentialFormats;
use super::Fills;
use super::Fonts;
use super::NumberingFormats;
use super::Protection;
use super::Style;
use crate::helper::const_str::*;
use crate::reader::driver::*;
use crate::writer::driver::*;
use quick_xml::events::{BytesStart, Event};
use quick_xml::Reader;
use quick_xml::Writer;
use std::io::Cursor;
use thin_vec::ThinVec;

#[derive(Clone, Default, Debug)]
pub(crate) struct Stylesheet {
    numbering_formats: NumberingFormats,
    fonts: Fonts,
    fills: Fills,
    borders: BordersCrate,
    cell_style_formats: CellStyleFormats,
    cell_formats: CellFormats,
    cell_styles: CellStyles,
    differential_formats: DifferentialFormats,
    colors: Colors,
    maked_style_list: ThinVec<Style>,
}

impl Stylesheet {
    #[inline]
    pub(crate) fn _get_numbering_formats(&self) -> &NumberingFormats {
        &self.numbering_formats
    }

    #[inline]
    pub(crate) fn _get_numbering_formats_mut(&mut self) -> &mut NumberingFormats {
        &mut self.numbering_formats
    }

    #[inline]
    pub(crate) fn _set_numbering_formats(&mut self, value: NumberingFormats) -> &mut Self {
        self.numbering_formats = value;
        self
    }

    #[inline]
    pub(crate) fn _get_fonts(&self) -> &Fonts {
        &self.fonts
    }

    #[inline]
    pub(crate) fn get_fonts_mut(&mut self) -> &mut Fonts {
        &mut self.fonts
    }

    #[inline]
    pub(crate) fn _set_fonts(&mut self, value: Fonts) -> &mut Self {
        self.fonts = value;
        self
    }

    #[inline]
    pub(crate) fn _get_fills(&self) -> &Fills {
        &self.fills
    }

    #[inline]
    pub(crate) fn get_fills_mut(&mut self) -> &mut Fills {
        &mut self.fills
    }

    #[inline]
    pub(crate) fn _set_fills(&mut self, value: Fills) -> &mut Self {
        self.fills = value;
        self
    }

    #[inline]
    pub(crate) fn _get_borders(&self) -> &BordersCrate {
        &self.borders
    }

    #[inline]
    pub(crate) fn get_borders_mut(&mut self) -> &mut BordersCrate {
        &mut self.borders
    }

    #[inline]
    pub(crate) fn _set_borders(&mut self, value: BordersCrate) -> &mut Self {
        self.borders = value;
        self
    }

    #[inline]
    pub(crate) fn _get_cell_style_formats(&self) -> &CellStyleFormats {
        &self.cell_style_formats
    }

    #[inline]
    pub(crate) fn _get_cell_style_formats_mut(&mut self) -> &mut CellStyleFormats {
        &mut self.cell_style_formats
    }

    #[inline]
    pub(crate) fn _set_cell_style_formats(&mut self, value: CellStyleFormats) -> &mut Self {
        self.cell_style_formats = value;
        self
    }

    #[inline]
    pub(crate) fn _get_cell_formats(&self) -> &CellFormats {
        &self.cell_formats
    }

    #[inline]
    pub(crate) fn _get_cell_formats_mut(&mut self) -> &mut CellFormats {
        &mut self.cell_formats
    }

    #[inline]
    pub(crate) fn _set_cell_formats(&mut self, value: CellFormats) -> &mut Self {
        self.cell_formats = value;
        self
    }

    #[inline]
    pub(crate) fn _get_cell_styles(&self) -> &CellStyles {
        &self.cell_styles
    }

    #[inline]
    pub(crate) fn _get_cell_styles_mut(&mut self) -> &mut CellStyles {
        &mut self.cell_styles
    }

    #[inline]
    pub(crate) fn _set_cell_styles(&mut self, value: CellStyles) -> &mut Self {
        self.cell_styles = value;
        self
    }

    #[inline]
    pub(crate) fn get_differential_formats(&self) -> &DifferentialFormats {
        &self.differential_formats
    }

    #[inline]
    pub(crate) fn get_differential_formats_mut(&mut self) -> &mut DifferentialFormats {
        &mut self.differential_formats
    }

    #[inline]
    pub(crate) fn _set_differential_formats(&mut self, value: DifferentialFormats) -> &mut Self {
        self.differential_formats = value;
        self
    }

    #[inline]
    pub(crate) fn _get_colors(&self) -> &Colors {
        &self.colors
    }

    #[inline]
    pub(crate) fn _get_colors_mut(&mut self) -> &mut Colors {
        &mut self.colors
    }

    #[inline]
    pub(crate) fn _set_colors(&mut self, value: Colors) -> &mut Self {
        self.colors = value;
        self
    }

    #[inline]
    pub(crate) fn get_style(&self, id: usize) -> Style {
        self.maked_style_list.get(id).unwrap().clone()
    }

    pub(crate) fn make_style(&mut self) -> &mut Self {
        for cell_format in self.cell_formats.get_cell_format() {
            let def_cell_format = self
                .cell_style_formats
                .get_cell_format()
                .get(*cell_format.get_format_id() as usize)
                .cloned()
                .unwrap_or_default();

            let mut style = Style::default();
            self.get_style_by_cell_format(&mut style, &def_cell_format, cell_format);
            self.maked_style_list.push(style);
        }

        self
    }

    pub(crate) fn get_style_by_cell_format(
        &self,
        style: &mut Style,
        def_cell_format: &CellFormat,
        cell_format: &CellFormat,
    ) {
        // number_format
        let mut apply = true;
        if def_cell_format.has_apply_number_format() {
            apply = *def_cell_format.get_apply_number_format();
        }
        if cell_format.has_apply_number_format() {
            apply = *cell_format.get_apply_number_format();
        }
        if apply {
            let id = cell_format.get_number_format_id();
            if let Some(obj) = self.numbering_formats.get_numbering_format().get(id) {
                style.set_numbering_format(obj.clone());
            }
        }

        // font
        let mut apply = true;
        if def_cell_format.has_apply_font() {
            apply = *def_cell_format.get_apply_font();
        }
        if cell_format.has_apply_font() {
            apply = *cell_format.get_apply_font();
        }
        if apply {
            let id = *cell_format.get_font_id() as usize;
            let obj = self.fonts.get_font().get(id).unwrap();
            style.set_font(obj.clone());
        }

        // fill
        let mut apply = true;
        if def_cell_format.has_apply_fill() {
            apply = *def_cell_format.get_apply_fill();
        }
        if cell_format.has_apply_fill() {
            apply = *cell_format.get_apply_fill();
        }
        if apply {
            let id = *cell_format.get_fill_id() as usize;
            let obj = self.fills.get_fill().get(id).unwrap();
            style.set_fill(obj.clone());
        }

        // borders
        let mut apply = true;
        if def_cell_format.has_apply_border() {
            apply = *def_cell_format.get_apply_border();
        }
        if cell_format.has_apply_border() {
            apply = *cell_format.get_apply_border();
        }
        if apply {
            let id = *cell_format.get_border_id() as usize;
            let obj = self.borders.get_borders().get(id).unwrap();
            style.set_borders(obj.clone());
        }

        // format_id
        style.set_format_id(*cell_format.get_format_id());

        // alignment
        let mut apply = true;
        if def_cell_format.has_apply_alignment() {
            apply = *def_cell_format.get_apply_alignment();
        }
        if cell_format.has_apply_alignment() {
            apply = *cell_format.get_apply_alignment();
        }
        if apply {
            if let Some(v) = def_cell_format.get_alignment() {
                style.set_alignment(v.clone());
            }
            if let Some(v) = cell_format.get_alignment() {
                style.set_alignment(v.clone());
            }
        }

        // protection
        let mut apply = true;
        if def_cell_format.has_apply_protection() {
            apply = *def_cell_format.get_apply_protection();
        }
        if cell_format.has_apply_protection() {
            apply = *cell_format.get_apply_protection();
        }
        if !apply {
            return;
        }

        if let Some(v) = def_cell_format.get_protection() {
            style.set_protection(v.clone());
        }
        if let Some(v) = cell_format.get_protection() {
            style.set_protection(v.clone());
        }
    }

    pub(crate) fn set_style(&mut self, style: &Style) -> u32 {
        let mut index = 0;
        let def_style = Style::default();
        if style == &def_style {
            return index;
        }
        for maked_style in &self.maked_style_list {
            if style == maked_style {
                return index;
            }
            index += 1;
        }
        let mut cell_format = CellFormat::default();

        let number_format_id = self.numbering_formats.set_style(style);
        let font_id = self.fonts.set_style(style);
        let fill_id = self.fills.set_style(style);
        let border_id = self.borders.set_style(style);
        let format_id = 0;

        cell_format.set_number_format_id(number_format_id);
        cell_format.set_font_id(font_id);
        cell_format.set_fill_id(fill_id);
        cell_format.set_border_id(border_id);
        cell_format.set_format_id(format_id);

        if style.get_numbering_format().is_some() {
            cell_format.set_apply_number_format(true);
        }

        if style.get_font().is_some() {
            cell_format.set_apply_font(true);
        }

        if style.get_fill().is_some() {
            cell_format.set_apply_fill(true);
        }

        if style.get_borders().is_some() {
            cell_format.set_apply_border(true);
        }

        if let Some(v) = style.get_alignment() {
            cell_format.set_alignment(v.clone());
            cell_format.set_apply_alignment(true);
        }

        if let Some(v) = style.get_protection() {
            cell_format.set_protection(v.clone());
            cell_format.set_apply_protection(true);
        }

        self.maked_style_list.push(style.clone());
        self.cell_formats.set_cell_format(cell_format);
        index
    }

    pub(crate) fn set_defalut_value(&mut self) -> &mut Self {
        let style = Style::get_default_value();
        self.set_style(&style);
        let style = Style::get_default_value_2();
        self.set_style(&style);
        self
    }

    pub(crate) fn set_attributes<R: std::io::BufRead>(
        &mut self,
        reader: &mut Reader<R>,
        _e: &BytesStart,
    ) {
        self.numbering_formats.get_build_in_formats();

        xml_read_loop!(
            reader,
            Event::Start(ref e) => {
                match e.name().into_inner() {
                    b"numFmts" => {
                        self.numbering_formats.set_attributes(reader, e);
                    }
                    b"fonts" => {
                        self.fonts.set_attributes(reader, e);
                    }
                    b"fills" => {
                        self.fills.set_attributes(reader, e);
                    }
                    b"borders" => {
                        self.borders.set_attributes(reader, e);
                    }
                    b"cellStyleXfs" => {
                        self.cell_style_formats.set_attributes(reader, e);
                    }
                    b"cellXfs" => {
                        self.cell_formats.set_attributes(reader, e);
                    }
                    b"cellStyles" => {
                        self.cell_styles.set_attributes(reader, e);
                    }
                    b"dxfs" => {
                        self.differential_formats.set_attributes(reader, e);
                    }
                    b"colors" => {
                        self.colors.set_attributes(reader, e);
                    }
                    _ => (),
                }
            },
            Event::End(ref e) => {
                if e.name().into_inner() == b"styleSheet" {
                    return
                }
            },
            Event::Eof => panic!("Error: Could not find {} end element", "styleSheet")
        );
    }

    pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
        // styleSheet
        write_start_tag(
            writer,
            "styleSheet",
            vec![
                ("xmlns", SHEET_MAIN_NS),
                ("xmlns:mc", MC_NS),
                ("mc:Ignorable", "x14ac"),
                ("xmlns:x14ac", SHEETML_AC_NS),
            ],
            false,
        );

        // numFmts
        self.numbering_formats.write_to(writer);

        // fonts
        self.fonts.write_to(writer);

        // fills
        self.fills.write_to(writer);

        // borders
        self.borders.write_to(writer);

        // cellStyleXfs
        self.cell_style_formats.write_to(writer);

        // cellXfs
        self.cell_formats.write_to(writer);

        // cellStyles
        self.cell_styles.write_to(writer);

        // dxfs
        self.differential_formats.write_to(writer);

        // colors
        self.colors.write_to(writer);

        // tableStyles
        write_start_tag(
            writer,
            "tableStyles",
            vec![
                ("count", "0"),
                ("defaultTableStyle", "TableStyleMedium2"),
                ("defaultPivotStyle", "PivotStyleMedium9"),
            ],
            true,
        );

        // extLst
        write_start_tag(writer, "extLst", vec![], false);

        // ext
        write_start_tag(
            writer,
            "ext",
            vec![
                ("uri", "{EB79DEF2-80B8-43e5-95BD-54CBDDF9020C}"),
                ("xmlns:x14", SHEET_MS_MAIN_NS),
            ],
            false,
        );

        // x14:slicerStyles
        write_start_tag(
            writer,
            "x14:slicerStyles",
            vec![("defaultSlicerStyle", "SlicerStyleLight1")],
            true,
        );

        write_end_tag(writer, "ext");

        write_end_tag(writer, "extLst");

        write_end_tag(writer, "styleSheet");
    }
}