umya_spreadsheet/structs/
selection.rs1use super::Coordinate;
2use super::EnumValue;
3use super::PaneValues;
4use super::SequenceOfReferences;
5use crate::reader::driver::*;
6use crate::writer::driver::*;
7use quick_xml::events::BytesStart;
8use quick_xml::Reader;
9use quick_xml::Writer;
10use std::io::Cursor;
11
12#[derive(Clone, Default, Debug)]
13pub struct Selection {
14 pane: EnumValue<PaneValues>,
15 active_cell: Option<Coordinate>,
16 sequence_of_references: SequenceOfReferences,
17}
18
19impl Selection {
20 #[inline]
21 pub fn get_pane(&self) -> &PaneValues {
22 self.pane.get_value()
23 }
24
25 #[inline]
26 pub fn set_pane(&mut self, value: PaneValues) -> &mut Self {
27 self.pane.set_value(value);
28 self
29 }
30
31 #[inline]
32 pub fn get_active_cell(&self) -> Option<&Coordinate> {
33 self.active_cell.as_ref()
34 }
35
36 #[inline]
37 pub fn get_active_cell_mut(&mut self) -> Option<&mut Coordinate> {
38 self.active_cell.as_mut()
39 }
40
41 #[inline]
42 pub fn set_active_cell(&mut self, value: Coordinate) -> &mut Self {
43 self.active_cell = Some(value);
44 self
45 }
46
47 #[inline]
48 pub fn get_sequence_of_references(&self) -> &SequenceOfReferences {
49 &self.sequence_of_references
50 }
51
52 #[inline]
53 pub fn get_sequence_of_references_mut(&mut self) -> &mut SequenceOfReferences {
54 &mut self.sequence_of_references
55 }
56
57 #[inline]
58 pub fn set_sequence_of_references(&mut self, value: SequenceOfReferences) -> &mut Self {
59 self.sequence_of_references = value;
60 self
61 }
62
63 pub(crate) fn set_attributes<R: std::io::BufRead>(
64 &mut self,
65 _reader: &mut Reader<R>,
66 e: &BytesStart,
67 ) {
68 set_string_from_xml!(self, e, pane, "pane");
69
70 if let Some(v) = get_attribute(e, b"activeCell") {
71 let mut obj = Coordinate::default();
72 obj.set_coordinate(v);
73 self.set_active_cell(obj);
74 }
75
76 if let Some(v) = get_attribute(e, b"sqref") {
77 self.sequence_of_references.set_sqref(v);
78 }
79 }
80
81 pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
82 let mut attributes: Vec<(&str, &str)> = Vec::new();
84
85 let mut active_cell_id = 0;
86 if let Some(active_cell) = &self.active_cell {
87 for range in self.sequence_of_references.get_range_collection() {
88 let range_str = range.get_range();
89 if range_str.contains(&active_cell.to_string()) {
90 break;
91 }
92 active_cell_id += 1;
93 }
94 }
95
96 if self.pane.has_value() {
97 attributes.push(("pane", self.pane.get_value_string()));
98 }
99
100 let active_cell_str = match &self.active_cell {
101 Some(active_cell) => active_cell.to_string(),
102 None => String::new(),
103 };
104 if !active_cell_str.is_empty() {
105 attributes.push(("activeCell", &active_cell_str));
106 }
107
108 let active_cell_id_str = active_cell_id.to_string();
109 if active_cell_id > 0 {
110 attributes.push(("activeCellId", &active_cell_id_str));
111 }
112
113 let sqref = self.sequence_of_references.get_sqref();
114 if !sqref.is_empty() {
115 attributes.push(("sqref", &sqref));
116 }
117
118 write_start_tag(writer, "selection", attributes, true);
119 }
120}