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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
// xdr:wsDr
use std::io::Cursor;

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

use super::{
    ConnectionShape,
    GraphicFrame,
    OneCellAnchor,
    Picture,
    Shape,
    TwoCellAnchor,
};
use crate::{
    helper::const_str::{
        DRAWINGML_MAIN_NS,
        SHEET_DRAWING_NS,
    },
    reader::driver::xml_read_loop,
    structs::{
        Chart,
        Image,
        OleObjects,
        raw::RawRelationships,
    },
    traits::{
        AdjustmentCoordinate,
        AdjustmentCoordinateWithSheet,
    },
    writer::driver::{
        write_end_tag,
        write_start_tag,
    },
};

#[derive(Clone, Default, Debug)]
pub struct WorksheetDrawing {
    image_collection:           Vec<Image>,
    chart_collection:           Vec<Chart>,
    one_cell_anchor_collection: Vec<OneCellAnchor>,
    two_cell_anchor_collection: Vec<TwoCellAnchor>,
}

impl WorksheetDrawing {
    #[inline]
    #[must_use]
    pub fn image_collection(&self) -> &[Image] {
        &self.image_collection
    }

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

    #[inline]
    pub fn image_collection_mut(&mut self) -> &mut Vec<Image> {
        &mut self.image_collection
    }

    #[inline]
    #[deprecated(since = "3.0.0", note = "Use image_collection_mut()")]
    pub fn get_image_collection_mut(&mut self) -> &mut Vec<Image> {
        self.image_collection_mut()
    }

    #[inline]
    pub fn add_image(&mut self, value: Image) -> &mut Self {
        self.image_collection.push(value);
        self
    }

    #[inline]
    #[must_use]
    pub fn image(&self, col: u32, row: u32) -> Option<&Image> {
        self.image_collection
            .iter()
            .find(|&image| image.col() == col - 1 && image.row() == row - 1)
    }

    #[inline]
    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use image()")]
    pub fn get_image(&self, col: &u32, row: &u32) -> Option<&Image> {
        self.image(*col, *row)
    }

    #[inline]
    pub fn image_mut(&mut self, col: u32, row: u32) -> Option<&mut Image> {
        self.image_collection
            .iter_mut()
            .find(|image| image.col() == col - 1 && image.row() == row - 1)
    }

    #[inline]
    #[deprecated(since = "3.0.0", note = "Use image_mut()")]
    pub fn get_image_mut(&mut self, col: &u32, row: &u32) -> Option<&mut Image> {
        self.image_mut(*col, *row)
    }

    #[inline]
    #[must_use]
    pub fn images(&self, col: u32, row: u32) -> Vec<&Image> {
        self.image_collection
            .iter()
            .filter(|image| image.col() == col - 1 && image.row() == row - 1)
            .collect()
    }

    #[inline]
    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use images()")]
    pub fn get_images(&self, col: &u32, row: &u32) -> Vec<&Image> {
        self.images(*col, *row)
    }

    #[inline]
    pub fn images_mut(&mut self, col: u32, row: u32) -> Vec<&mut Image> {
        self.image_collection
            .iter_mut()
            .filter(|image| image.col() == col - 1 && image.row() == row - 1)
            .collect()
    }

    #[inline]
    #[deprecated(since = "3.0.0", note = "Use images_mut()")]
    pub fn get_images_mut(&mut self, col: &u32, row: &u32) -> Vec<&mut Image> {
        self.images_mut(*col, *row)
    }

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

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

    #[inline]
    pub fn chart_collection_mut(&mut self) -> &mut Vec<Chart> {
        &mut self.chart_collection
    }

    #[inline]
    #[deprecated(since = "3.0.0", note = "Use chart_collection_mut()")]
    pub fn get_chart_collection_mut(&mut self) -> &mut Vec<Chart> {
        self.chart_collection_mut()
    }

    #[inline]
    pub fn add_chart_collection(&mut self, value: Chart) -> &mut Self {
        self.chart_collection.push(value);
        self
    }

    #[inline]
    #[must_use]
    pub fn chart(&self, col: u32, row: u32) -> Option<&Chart> {
        self.chart_collection
            .iter()
            .find(|&chart| chart.col() == col - 1 && chart.row() == row - 1)
    }

    #[inline]
    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use chart()")]
    pub fn get_chart(&self, col: &u32, row: &u32) -> Option<&Chart> {
        self.chart(*col, *row)
    }

    #[inline]
    pub fn chart_mut(&mut self, col: u32, row: u32) -> Option<&mut Chart> {
        self.chart_collection
            .iter_mut()
            .find(|chart| chart.col() == col - 1 && chart.row() == row - 1)
    }

    #[inline]
    #[deprecated(since = "3.0.0", note = "Use chart_mut()")]
    pub fn get_chart_mut(&mut self, col: &u32, row: &u32) -> Option<&mut Chart> {
        self.chart_mut(*col, *row)
    }

    #[must_use]
    pub fn charts(&self, col: u32, row: u32) -> Vec<&Chart> {
        self.chart_collection
            .iter()
            .filter(|chart| chart.col() == col - 1 && chart.row() == row - 1)
            .collect()
    }

    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use charts()")]
    pub fn get_charts(&self, col: &u32, row: &u32) -> Vec<&Chart> {
        self.charts(*col, *row)
    }

    pub fn charts_mut(&mut self, col: u32, row: u32) -> Vec<&mut Chart> {
        self.chart_collection
            .iter_mut()
            .filter(|chart| chart.col() == col - 1 && chart.row() == row - 1)
            .collect()
    }

    #[deprecated(since = "3.0.0", note = "Use charts_mut()")]
    pub fn get_charts_mut(&mut self, col: &u32, row: &u32) -> Vec<&mut Chart> {
        self.charts_mut(*col, *row)
    }

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

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

    #[inline]
    pub fn one_cell_anchor_collection_mut(&mut self) -> &mut Vec<OneCellAnchor> {
        &mut self.one_cell_anchor_collection
    }

    #[inline]
    #[deprecated(since = "3.0.0", note = "Use one_cell_anchor_collection_mut()")]
    pub fn get_one_cell_anchor_collection_mut(&mut self) -> &mut Vec<OneCellAnchor> {
        self.one_cell_anchor_collection_mut()
    }

    #[inline]
    pub fn add_one_cell_anchor_collection(&mut self, value: OneCellAnchor) -> &mut Self {
        self.one_cell_anchor_collection.push(value);
        self
    }

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

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

    #[inline]
    pub fn two_cell_anchor_collection_mut(&mut self) -> &mut Vec<TwoCellAnchor> {
        &mut self.two_cell_anchor_collection
    }

    #[inline]
    #[deprecated(since = "3.0.0", note = "Use two_cell_anchor_collection_mut()")]
    pub fn get_two_cell_anchor_collection_mut(&mut self) -> &mut Vec<TwoCellAnchor> {
        self.two_cell_anchor_collection_mut()
    }

    #[inline]
    pub fn add_two_cell_anchor_collection(&mut self, value: TwoCellAnchor) -> &mut Self {
        self.two_cell_anchor_collection.push(value);
        self
    }

    #[inline]
    #[must_use]
    pub fn has_drawing_object(&self) -> bool {
        !self.chart_collection.is_empty()
            || !self.image_collection.is_empty()
            || !self.one_cell_anchor_collection.is_empty()
            || !self.two_cell_anchor_collection.is_empty()
    }

    #[must_use]
    pub fn graphic_frame_collection(&self) -> Vec<&GraphicFrame> {
        self.two_cell_anchor_collection
            .iter()
            .filter_map(|two_cell_anchor| two_cell_anchor.graphic_frame())
            .collect()
    }

    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use graphic_frame_collection()")]
    pub fn get_graphic_frame_collection(&self) -> Vec<&GraphicFrame> {
        self.graphic_frame_collection()
    }

    pub fn graphic_frame_collection_mut(&mut self) -> Vec<&mut GraphicFrame> {
        self.two_cell_anchor_collection
            .iter_mut()
            .filter_map(|two_cell_anchor| two_cell_anchor.graphic_frame_mut())
            .collect()
    }

    #[deprecated(since = "3.0.0", note = "Use graphic_frame_collection_mut()")]
    pub fn get_graphic_frame_collection_mut(&mut self) -> Vec<&mut GraphicFrame> {
        self.graphic_frame_collection_mut()
    }

    #[must_use]
    pub fn shape_collection(&self) -> Vec<&Shape> {
        self.two_cell_anchor_collection
            .iter()
            .filter_map(|two_cell_anchor| two_cell_anchor.shape())
            .collect()
    }

    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use shape_collection()")]
    pub fn get_shape_collection(&self) -> Vec<&Shape> {
        self.shape_collection()
    }

    pub fn shape_collection_mut(&mut self) -> Vec<&mut Shape> {
        self.two_cell_anchor_collection
            .iter_mut()
            .filter_map(|two_cell_anchor| two_cell_anchor.shape_mut())
            .collect()
    }

    #[deprecated(since = "3.0.0", note = "Use shape_collection_mut()")]
    pub fn get_shape_collection_mut(&mut self) -> Vec<&mut Shape> {
        self.shape_collection_mut()
    }

    #[must_use]
    pub fn connection_shape_collection(&self) -> Vec<&ConnectionShape> {
        self.two_cell_anchor_collection
            .iter()
            .filter_map(|two_cell_anchor| two_cell_anchor.connection_shape())
            .collect()
    }

    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use connection_shape_collection()")]
    pub fn get_connection_shape_collection(&self) -> Vec<&ConnectionShape> {
        self.connection_shape_collection()
    }

    pub fn connection_shape_collection_mut(&mut self) -> Vec<&mut ConnectionShape> {
        self.two_cell_anchor_collection
            .iter_mut()
            .filter_map(|two_cell_anchor| two_cell_anchor.connection_shape_mut())
            .collect()
    }

    #[deprecated(since = "3.0.0", note = "Use connection_shape_collection_mut()")]
    pub fn get_connection_shape_collection_mut(&mut self) -> Vec<&mut ConnectionShape> {
        self.connection_shape_collection_mut()
    }

    #[must_use]
    pub fn picture_collection(&self) -> Vec<&Picture> {
        self.two_cell_anchor_collection
            .iter()
            .filter_map(|two_cell_anchor| two_cell_anchor.picture())
            .collect()
    }

    #[must_use]
    #[deprecated(since = "3.0.0", note = "Use picture_collection()")]
    pub fn get_picture_collection(&self) -> Vec<&Picture> {
        self.picture_collection()
    }

    pub fn one_cell_anchor_all_list(&mut self) -> Vec<&mut OneCellAnchor> {
        self.one_cell_anchor_collection
            .iter_mut()
            .chain(
                self.image_collection
                    .iter_mut()
                    .filter_map(|image| image.one_cell_anchor_mut()),
            )
            .collect()
    }

    #[deprecated(since = "3.0.0", note = "Use one_cell_anchor_all_list()")]
    pub fn get_one_cell_anchor_all_list(&mut self) -> Vec<&mut OneCellAnchor> {
        self.one_cell_anchor_all_list()
    }

    pub fn two_cell_anchor_all_list(&mut self) -> Vec<&mut TwoCellAnchor> {
        self.two_cell_anchor_collection
            .iter_mut()
            .chain(
                self.chart_collection
                    .iter_mut()
                    .map(Chart::two_cell_anchor_mut),
            )
            .chain(
                self.image_collection
                    .iter_mut()
                    .filter_map(|image| image.two_cell_anchor_mut()),
            )
            .collect()
    }

    #[deprecated(since = "3.0.0", note = "Use two_cell_anchor_all_list()")]
    pub fn get_two_cell_anchor_all_list(&mut self) -> Vec<&mut TwoCellAnchor> {
        self.two_cell_anchor_all_list()
    }

    pub fn picture_collection_mut(&mut self) -> Vec<&mut Picture> {
        self.two_cell_anchor_collection
            .iter_mut()
            .filter_map(|two_cell_anchor| two_cell_anchor.picture_mut())
            .collect()
    }

    #[deprecated(since = "3.0.0", note = "Use picture_collection_mut()")]
    pub fn get_picture_collection_mut(&mut self) -> Vec<&mut Picture> {
        self.picture_collection_mut()
    }

    pub(crate) fn set_attributes<R: std::io::BufRead>(
        &mut self,
        reader: &mut Reader<R>,
        _e: &BytesStart,
        drawing_relationships: Option<&RawRelationships>,
        ole_objects: &mut OleObjects,
    ) {
        let mut ole_index = 0;
        let mut is_alternate_content = false;

        xml_read_loop!(
            reader,
            Event::Start(ref e) => {
                match e.name().into_inner() {
                    b"mc:AlternateContent" => {
                        is_alternate_content = true;
                    }
                    b"xdr:oneCellAnchor" | b"oneCellAnchor" => {
                        if is_alternate_content {
                            continue;
                        }
                        let mut obj = OneCellAnchor::default();
                        obj.set_attributes(reader, e, drawing_relationships);
                        if obj.is_image() {
                            let mut image = Image::default();
                            image.set_one_cell_anchor(obj);
                            self.add_image(image);
                        } else {
                            self.add_one_cell_anchor_collection(obj);
                        }
                    }
                    b"xdr:twoCellAnchor" | b"twoCellAnchor" => {
                        let os = ole_objects.ole_object_mut();
                        if is_alternate_content && !os.is_empty() {
                            os[ole_index]
                                .two_cell_anchor_mut()
                                .set_is_alternate_content(true);
                            os[ole_index].two_cell_anchor_mut().set_attributes(
                                reader,
                                e,
                                drawing_relationships,
                            );
                            ole_index += 1;
                            continue;
                        }
                        let mut obj = TwoCellAnchor::default();
                        obj.set_attributes(reader, e, drawing_relationships);
                        if obj.is_support() {
                            if obj.is_chart() {
                                let mut chart = Chart::default();
                                chart.set_two_cell_anchor(obj);
                                self.add_chart_collection(chart);
                            } else if obj.is_image() {
                                let mut image = Image::default();
                                image.set_two_cell_anchor(obj);
                                self.add_image(image);
                            } else {
                                self.add_two_cell_anchor_collection(obj);
                            }
                        }
                    }
                    _ => (),
                }
            },

            Event::End(ref e) => {
                match e.name().into_inner() {
                    b"mc:AlternateContent" => {
                        is_alternate_content = false;
                    }
                    b"xdr:wsDr" | b"wsDr" => return,
                    _ => (),
                }
            },

            Event::Eof => panic!("Error: Could not find {} end element", "xdr:wsDr")
        );
    }

    pub(crate) fn write_to(
        &self,
        writer: &mut Writer<Cursor<Vec<u8>>>,
        ole_objects: &OleObjects,
        rel_list: &mut Vec<(String, String)>,
    ) {
        // xdr:wsDr
        write_start_tag(
            writer,
            "xdr:wsDr",
            vec![
                ("xmlns:xdr", SHEET_DRAWING_NS).into(),
                ("xmlns:a", DRAWINGML_MAIN_NS).into(),
            ],
            false,
        );

        // xdr:twoCellAnchor
        for chart in &self.chart_collection {
            chart.two_cell_anchor().write_to(writer, rel_list, 0);
        }
        for image in &self.image_collection {
            image.write_to(writer, rel_list);
        }
        for two_cell_anchor in &self.two_cell_anchor_collection {
            two_cell_anchor.write_to(writer, rel_list, 0);
        }

        // xdr:oneCellAnchor
        for one_cell_anchor in &self.one_cell_anchor_collection {
            one_cell_anchor.write_to(writer, rel_list);
        }

        // mc:AlternateContent
        //        let mut ole_id = 1000 + 25;
        for ole_object in ole_objects.ole_object() {
            ole_object.two_cell_anchor().write_to(writer, rel_list, 0);
            //            ole_id += 1;
        }

        write_end_tag(writer, "xdr:wsDr");
    }
}
impl AdjustmentCoordinate for WorksheetDrawing {
    fn adjustment_insert_coordinate(
        &mut self,
        root_col_num: u32,
        offset_col_num: u32,
        root_row_num: u32,
        offset_row_num: u32,
    ) {
        for anchor in &mut self.one_cell_anchor_collection {
            anchor.adjustment_insert_coordinate(
                root_col_num,
                offset_col_num,
                root_row_num,
                offset_row_num,
            );
        }
        for anchor in &mut self.two_cell_anchor_collection {
            anchor.adjustment_insert_coordinate(
                root_col_num,
                offset_col_num,
                root_row_num,
                offset_row_num,
            );
        }
        for chart in &mut self.chart_collection {
            chart.adjustment_insert_coordinate(
                root_col_num,
                offset_col_num,
                root_row_num,
                offset_row_num,
            );
        }
        for image in &mut self.image_collection {
            image.adjustment_insert_coordinate(
                root_col_num,
                offset_col_num,
                root_row_num,
                offset_row_num,
            );
        }
    }

    fn adjustment_remove_coordinate(
        &mut self,
        root_col_num: u32,
        offset_col_num: u32,
        root_row_num: u32,
        offset_row_num: u32,
    ) {
        self.one_cell_anchor_collection.retain(|k| {
            !(k.is_remove_coordinate(root_col_num, offset_col_num, root_row_num, offset_row_num))
        });
        for anchor in &mut self.one_cell_anchor_collection {
            anchor.adjustment_remove_coordinate(
                root_col_num,
                offset_col_num,
                root_row_num,
                offset_row_num,
            );
        }
        self.two_cell_anchor_collection.retain(|k| {
            !(k.is_remove_coordinate(root_col_num, offset_col_num, root_row_num, offset_row_num))
        });
        for anchor in &mut self.two_cell_anchor_collection {
            anchor.adjustment_remove_coordinate(
                root_col_num,
                offset_col_num,
                root_row_num,
                offset_row_num,
            );
        }
        self.chart_collection.retain(|k| {
            !(k.is_remove_coordinate(root_col_num, offset_col_num, root_row_num, offset_row_num))
        });
        for chart in &mut self.chart_collection {
            chart.adjustment_remove_coordinate(
                root_col_num,
                offset_col_num,
                root_row_num,
                offset_row_num,
            );
        }
        self.image_collection.retain(|k| {
            !(k.is_remove_coordinate(root_col_num, offset_col_num, root_row_num, offset_row_num))
        });
        for image in &mut self.image_collection {
            image.adjustment_remove_coordinate(
                root_col_num,
                offset_col_num,
                root_row_num,
                offset_row_num,
            );
        }
    }
}
impl AdjustmentCoordinateWithSheet for WorksheetDrawing {
    fn adjustment_insert_coordinate_with_sheet(
        &mut self,
        sheet_name: &str,
        root_col_num: u32,
        offset_col_num: u32,
        root_row_num: u32,
        offset_row_num: u32,
    ) {
        // chart
        for chart in &mut self.chart_collection {
            chart.adjustment_insert_coordinate_with_sheet(
                sheet_name,
                root_col_num,
                offset_col_num,
                root_row_num,
                offset_row_num,
            );
        }
    }

    fn adjustment_remove_coordinate_with_sheet(
        &mut self,
        sheet_name: &str,
        root_col_num: u32,
        offset_col_num: u32,
        root_row_num: u32,
        offset_row_num: u32,
    ) {
        // chart
        for chart in &mut self.chart_collection {
            chart.adjustment_remove_coordinate_with_sheet(
                sheet_name,
                root_col_num,
                offset_col_num,
                root_row_num,
                offset_row_num,
            );
        }
    }
}