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
use super::Coordinate;
use super::EnumValue;
use super::PaneValues;
use super::SequenceOfReferences;
use quick_xml::events::BytesStart;
use quick_xml::Reader;
use quick_xml::Writer;
use reader::driver::*;
use std::io::Cursor;
use writer::driver::*;

#[derive(Clone, Default, Debug)]
pub struct Selection {
    pane: EnumValue<PaneValues>,
    active_cell: Option<Coordinate>,
    sequence_of_references: SequenceOfReferences,
}
impl Selection {
    pub fn get_pane(&self) -> &PaneValues {
        self.pane.get_value()
    }

    pub fn set_pane(&mut self, value: PaneValues) -> &mut Self {
        self.pane.set_value(value);
        self
    }

    pub fn get_active_cell(&self) -> &Option<Coordinate> {
        &self.active_cell
    }

    pub fn get_active_cell_mut(&mut self) -> &mut Option<Coordinate> {
        &mut self.active_cell
    }

    pub fn set_active_cell(&mut self, value: Coordinate) -> &mut Self {
        self.active_cell = Some(value);
        self
    }

    pub fn get_sequence_of_references(&self) -> &SequenceOfReferences {
        &self.sequence_of_references
    }

    pub fn get_sequence_of_references_mut(&mut self) -> &mut SequenceOfReferences {
        &mut self.sequence_of_references
    }

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

    pub(crate) fn set_attributes<R: std::io::BufRead>(
        &mut self,
        _reader: &mut Reader<R>,
        e: &BytesStart,
    ) {
        match get_attribute(e, b"pane") {
            Some(v) => {
                self.pane.set_value_string(v);
            }
            None => {}
        }

        match get_attribute(e, b"activeCell") {
            Some(v) => {
                let mut obj = Coordinate::default();
                obj.set_coordinate(v);
                self.set_active_cell(obj);
            }
            None => {}
        }

        match get_attribute(e, b"sqref") {
            Some(v) => {
                self.sequence_of_references.set_sqref(v);
            }
            None => {}
        }
    }

    pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
        // selection
        let mut attributes: Vec<(&str, &str)> = Vec::new();

        let mut active_cell_id = 0;
        match &self.active_cell {
            Some(active_cell) => {
                for range in self.sequence_of_references.get_range_collection() {
                    let range_str = range.get_range();
                    match range_str.find(active_cell.get_coordinate().as_str()) {
                        Some(_) => {
                            break;
                        }
                        None => {}
                    }
                    active_cell_id += 1;
                }
            }
            None => {}
        }

        if self.pane.has_value() {
            attributes.push(("pane", self.pane.get_value_string()));
        }

        let active_cell_str = match &self.active_cell {
            Some(active_cell) => active_cell.get_coordinate(),
            None => String::from(""),
        };
        if active_cell_str != "" {
            attributes.push(("activeCell", active_cell_str.as_str()));
        }

        let active_cell_id_str = active_cell_id.to_string();
        if active_cell_id > 0 {
            attributes.push(("activeCellId", active_cell_id_str.as_str()));
        }

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

        write_start_tag(writer, "selection", attributes, true);
    }
}