umya_spreadsheet/structs/
address.rs1use super::Range;
2use crate::helper::address::*;
3use crate::helper::coordinate::*;
4use crate::traits::AdjustmentCoordinate;
5use crate::traits::AdjustmentCoordinateWithSheet;
6use fancy_regex::Regex;
7
8#[derive(Clone, Default, Debug)]
9pub struct Address {
10 sheet_name: Box<str>,
11 range: Range,
12}
13
14impl Address {
15 #[inline]
16 pub fn get_sheet_name(&self) -> &str {
17 &self.sheet_name
18 }
19
20 #[inline]
21 pub fn set_sheet_name<S: Into<String>>(&mut self, value: S) -> &mut Self {
22 self.sheet_name = value.into().into_boxed_str();
23 self
24 }
25
26 #[inline]
27 pub fn get_range(&self) -> &Range {
28 &self.range
29 }
30
31 #[inline]
32 pub fn get_range_mut(&mut self) -> &mut Range {
33 &mut self.range
34 }
35
36 #[inline]
37 pub fn set_range(&mut self, value: Range) -> &mut Self {
38 self.range = value;
39 self
40 }
41
42 pub fn set_address<S: Into<String>>(&mut self, value: S) -> &mut Self {
43 let org_value = value.into();
44 let (sheet_name, range) = split_address(&org_value);
45 self.range.set_range(range);
46 if sheet_name != "" {
47 self.sheet_name = sheet_name.into();
48 }
49 self
50 }
51
52 #[inline]
53 pub fn get_address(&self) -> String {
54 self.get_address_crate(false)
55 }
56
57 #[inline]
58 pub(crate) fn get_address_ptn2(&self) -> String {
59 self.get_address_crate(true)
60 }
61
62 pub(crate) fn get_address_crate(&self, is_ptn2: bool) -> String {
63 let range = self.range.get_range();
64 if self.sheet_name.is_empty() {
65 return range;
66 }
67 let mut with_space_char = "";
68 let mut sheet_name = self.sheet_name.clone();
69 if sheet_name.contains(char::is_whitespace) {
70 with_space_char = "'";
71 }
72 if is_ptn2 {
73 if sheet_name.contains("!") {
74 with_space_char = "'";
75 }
76 if sheet_name.contains("'") {
77 with_space_char = "'";
78 sheet_name = sheet_name.replace("'", "''").into_boxed_str();
79 }
80 if sheet_name.contains(r#"""#) {
81 with_space_char = "'";
82 }
83 if with_space_char == "" {
84 lazy_static! {
85 static ref RE: Regex = Regex::new(r"[^0-9a-zA-Z]").unwrap();
86 }
87 if RE.is_match(&sheet_name).unwrap_or(false) {
88 with_space_char = "'";
89 }
90 }
91 if with_space_char == "" {
92 if (None, None, None, None) != index_from_coordinate(&sheet_name) {
93 with_space_char = "'";
94 }
95 }
96 }
97 format!(
98 "{}{}{}!{}",
99 &with_space_char, sheet_name, &with_space_char, range
100 )
101 }
102}
103impl AdjustmentCoordinateWithSheet for Address {
104 #[inline]
105 fn adjustment_insert_coordinate_with_sheet(
106 &mut self,
107 sheet_name: &str,
108 root_col_num: &u32,
109 offset_col_num: &u32,
110 root_row_num: &u32,
111 offset_row_num: &u32,
112 ) {
113 if &*self.sheet_name == sheet_name {
114 self.range.adjustment_insert_coordinate(
115 root_col_num,
116 offset_col_num,
117 root_row_num,
118 offset_row_num,
119 );
120 }
121 }
122
123 #[inline]
124 fn adjustment_remove_coordinate_with_sheet(
125 &mut self,
126 sheet_name: &str,
127 root_col_num: &u32,
128 offset_col_num: &u32,
129 root_row_num: &u32,
130 offset_row_num: &u32,
131 ) {
132 if &*self.sheet_name == sheet_name {
133 self.range.adjustment_remove_coordinate(
134 root_col_num,
135 offset_col_num,
136 root_row_num,
137 offset_row_num,
138 );
139 }
140 }
141
142 #[inline]
143 fn is_remove_coordinate_with_sheet(
144 &self,
145 sheet_name: &str,
146 root_col_num: &u32,
147 offset_col_num: &u32,
148 root_row_num: &u32,
149 offset_row_num: &u32,
150 ) -> bool {
151 &*self.sheet_name == sheet_name
152 && self.range.is_remove_coordinate(
153 root_col_num,
154 offset_col_num,
155 root_row_num,
156 offset_row_num,
157 )
158 }
159}