umya_spreadsheet/structs/
range.rs1use super::{
2 ColumnReference,
3 RowReference,
4};
5use crate::{
6 helper::coordinate::index_from_coordinate,
7 traits::{
8 AdjustmentCoordinate,
9 AdjustmentValue,
10 },
11};
12
13#[derive(Clone, Default, Debug)]
14pub struct Range {
15 start_col: Option<ColumnReference>,
16 start_row: Option<RowReference>,
17 end_col: Option<ColumnReference>,
18 end_row: Option<RowReference>,
19}
20
21impl Range {
22 pub fn set_range<S: Into<String>>(&mut self, value: S) -> &mut Self {
23 let org_value = value.into();
24 let (_name, rng) = org_value.split_once('!').unwrap_or(("", &org_value));
26 let coordinate_collection: Vec<&str> = rng.split(':').collect();
27
28 assert!(
29 matches!(coordinate_collection.len(), 1 | 2),
30 "Non-standard coordinate"
31 );
32
33 let (
34 row, col, is_lock_col, is_lock_row,
38 ) = index_from_coordinate(coordinate_collection[0]);
39 if let Some(v) = row {
40 let mut coordinate_start_col = ColumnReference::default();
41 coordinate_start_col.set_num(v);
42 coordinate_start_col.set_is_lock(is_lock_col.unwrap());
43 self.start_col = Some(coordinate_start_col);
44 }
45 if let Some(v) = col {
46 let mut coordinate_start_row = RowReference::default();
47 coordinate_start_row.set_num(v);
48 coordinate_start_row.set_is_lock(is_lock_row.unwrap());
49 self.start_row = Some(coordinate_start_row);
50 }
51
52 if coordinate_collection.len() == 2 {
53 let (
54 row, col, is_lock_col, is_lock_row,
58 ) = index_from_coordinate(coordinate_collection[1]);
59 if let Some(v) = row {
60 let mut coordinate_end_col = ColumnReference::default();
61 coordinate_end_col.set_num(v);
62 coordinate_end_col.set_is_lock(is_lock_col.unwrap());
63 self.end_col = Some(coordinate_end_col);
64 }
65 if let Some(v) = col {
66 let mut coordinate_end_row = RowReference::default();
67 coordinate_end_row.set_num(v);
68 coordinate_end_row.set_is_lock(is_lock_row.unwrap());
69 self.end_row = Some(coordinate_end_row);
70 }
71 }
72 self
73 }
74
75 #[inline]
76 #[must_use]
77 pub fn range(&self) -> String {
78 let mut result = self.coordinate_start();
79 if self.end_col.is_some() || self.end_row.is_some() {
80 result = format!("{}:{}", result, self.coordinate_end());
81 }
82 result
83 }
84
85 #[inline]
86 #[must_use]
87 #[deprecated(since = "3.0.0", note = "Use range()")]
88 pub fn get_range(&self) -> String {
89 self.range()
90 }
91
92 #[inline]
93 #[must_use]
94 pub fn coordinate_start_col(&self) -> Option<&ColumnReference> {
95 self.start_col.as_ref()
96 }
97
98 #[inline]
99 #[must_use]
100 #[deprecated(since = "3.0.0", note = "Use coordinate_start_col()")]
101 pub fn get_coordinate_start_col(&self) -> Option<&ColumnReference> {
102 self.coordinate_start_col()
103 }
104
105 #[inline]
106 pub fn coordinate_start_col_mut(&mut self) -> Option<&mut ColumnReference> {
107 self.start_col.as_mut()
108 }
109
110 #[inline]
111 #[deprecated(since = "3.0.0", note = "Use coordinate_start_col_mut()")]
112 pub fn get_coordinate_start_col_mut(&mut self) -> Option<&mut ColumnReference> {
113 self.coordinate_start_col_mut()
114 }
115
116 #[inline]
117 #[must_use]
118 pub fn coordinate_start_row(&self) -> Option<&RowReference> {
119 self.start_row.as_ref()
120 }
121
122 #[inline]
123 #[must_use]
124 #[deprecated(since = "3.0.0", note = "Use coordinate_start_row()")]
125 pub fn get_coordinate_start_row(&self) -> Option<&RowReference> {
126 self.coordinate_start_row()
127 }
128
129 #[inline]
130 pub fn coordinate_start_row_mut(&mut self) -> Option<&mut RowReference> {
131 self.start_row.as_mut()
132 }
133
134 #[inline]
135 #[deprecated(since = "3.0.0", note = "Use coordinate_start_row_mut()")]
136 pub fn get_coordinate_start_row_mut(&mut self) -> Option<&mut RowReference> {
137 self.coordinate_start_row_mut()
138 }
139
140 #[inline]
141 #[must_use]
142 pub fn coordinate_end_col(&self) -> Option<&ColumnReference> {
143 self.end_col.as_ref()
144 }
145
146 #[inline]
147 #[must_use]
148 #[deprecated(since = "3.0.0", note = "Use coordinate_end_col()")]
149 pub fn get_coordinate_end_col(&self) -> Option<&ColumnReference> {
150 self.coordinate_end_col()
151 }
152
153 #[inline]
154 pub fn coordinate_end_col_mut(&mut self) -> Option<&mut ColumnReference> {
155 self.end_col.as_mut()
156 }
157
158 #[inline]
159 #[deprecated(since = "3.0.0", note = "Use coordinate_end_col_mut()")]
160 pub fn get_coordinate_end_col_mut(&mut self) -> Option<&mut ColumnReference> {
161 self.coordinate_end_col_mut()
162 }
163
164 #[inline]
165 #[must_use]
166 pub fn coordinate_end_row(&self) -> Option<&RowReference> {
167 self.end_row.as_ref()
168 }
169
170 #[inline]
171 #[must_use]
172 #[deprecated(since = "3.0.0", note = "Use coordinate_end_row()")]
173 pub fn get_coordinate_end_row(&self) -> Option<&RowReference> {
174 self.coordinate_end_row()
175 }
176
177 #[inline]
178 pub fn coordinate_end_row_mut(&mut self) -> Option<&mut RowReference> {
179 self.end_row.as_mut()
180 }
181
182 #[inline]
183 #[deprecated(since = "3.0.0", note = "Use coordinate_end_row_mut()")]
184 pub fn get_coordinate_end_row_mut(&mut self) -> Option<&mut RowReference> {
185 self.coordinate_end_row_mut()
186 }
187
188 #[inline]
189 pub(crate) fn coordinate_start(&self) -> String {
190 let mut coordinate_str = String::new();
191 if let Some(v) = &self.start_col {
192 coordinate_str = v.coordinate();
193 }
194 if let Some(v) = &self.start_row {
195 coordinate_str = format!("{}{}", coordinate_str, v.coordinate());
196 }
197 coordinate_str
198 }
199
200 #[inline]
201 #[deprecated(since = "3.0.0", note = "Use sheet_name()")]
202 pub(crate) fn get_coordinate_start(&self) -> String {
203 self.coordinate_start()
204 }
205
206 #[inline]
207 pub(crate) fn coordinate_end(&self) -> String {
208 let mut coordinate_str = String::new();
209 if let Some(v) = &self.end_col {
210 coordinate_str = v.coordinate();
211 }
212 if let Some(v) = &self.end_row {
213 coordinate_str = format!("{}{}", coordinate_str, v.coordinate());
214 }
215 coordinate_str
216 }
217
218 #[inline]
219 #[deprecated(since = "3.0.0", note = "Use coordinate_end()")]
220 pub(crate) fn get_coordinate_end(&self) -> String {
221 self.coordinate_end()
222 }
223}
224impl AdjustmentCoordinate for Range {
225 #[inline]
226 fn adjustment_insert_coordinate(
227 &mut self,
228 root_col_num: u32,
229 offset_col_num: u32,
230 root_row_num: u32,
231 offset_row_num: u32,
232 ) {
233 if let Some(v) = &mut self.start_col {
234 v.adjustment_insert_value(root_col_num, offset_col_num);
235 }
236 if let Some(v) = &mut self.start_row {
237 v.adjustment_insert_value(root_row_num, offset_row_num);
238 }
239 if let Some(v) = &mut self.end_col {
240 v.adjustment_insert_value(root_col_num, offset_col_num);
241 }
242 if let Some(v) = &mut self.end_row {
243 v.adjustment_insert_value(root_row_num, offset_row_num);
244 }
245 }
246
247 #[inline]
248 fn adjustment_remove_coordinate(
249 &mut self,
250 root_col_num: u32,
251 offset_col_num: u32,
252 root_row_num: u32,
253 offset_row_num: u32,
254 ) {
255 if let Some(v) = &mut self.start_col {
256 v.adjustment_remove_value(root_col_num, offset_col_num);
257 }
258 if let Some(v) = &mut self.start_row {
259 v.adjustment_remove_value(root_row_num, offset_row_num);
260 }
261 if let Some(v) = &mut self.end_col {
262 v.adjustment_remove_value(root_col_num, offset_col_num);
263 }
264 if let Some(v) = &mut self.end_row {
265 v.adjustment_remove_value(root_row_num, offset_row_num);
266 }
267 }
268
269 #[inline]
270 fn is_remove_coordinate(
271 &self,
272 root_col_num: u32,
273 offset_col_num: u32,
274 root_row_num: u32,
275 offset_row_num: u32,
276 ) -> bool {
277 let start_col_result = match &self.start_col {
278 Some(v) => v.is_remove_value(root_col_num, offset_col_num),
279 None => false,
280 };
281 let start_row_result = match &self.start_row {
282 Some(v) => v.is_remove_value(root_row_num, offset_row_num),
283 None => false,
284 };
285 let end_col_result = match &self.end_col {
286 Some(v) => v.is_remove_value(root_col_num, offset_col_num),
287 None => false,
288 };
289 let end_row_result = match &self.end_row {
290 Some(v) => v.is_remove_value(root_row_num, offset_row_num),
291 None => false,
292 };
293 start_col_result && start_row_result && end_col_result && end_row_result
294 }
295}