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::Range;
use helper::address::*;

#[derive(Clone, Default, Debug)]
pub struct Address {
    sheet_name: String,
    range: Range,
}

impl Address {
    pub fn get_sheet_name(&self) -> &str {
        &self.sheet_name
    }

    pub fn set_sheet_name<S: Into<String>>(&mut self, value: S) -> &mut Address {
        self.sheet_name = value.into();
        self
    }

    pub fn get_range(&self) -> &Range {
        &self.range
    }

    pub fn get_range_mut(&mut self) -> &mut Range {
        &mut self.range
    }

    pub fn set_range(&mut self, value: Range) -> &mut Address {
        self.range = value;
        self
    }

    pub fn set_address<S: Into<String>>(&mut self, value: S) -> &mut Address {
        let org_value = value.into();
        let (sheet_name, range) = split_address(&org_value);
        self.range.set_range(range);
        if &sheet_name != "" {
            self.sheet_name = sheet_name;
        }
        self
    }

    pub fn get_address(&self) -> String {
        self.get_address_crate(false)
    }

    pub(crate) fn get_address_ptn2(&self) -> String {
        self.get_address_crate(true)
    }

    pub(crate) fn get_address_crate(&self, is_ptn2: bool) -> String {
        let range = self.range.get_range();
        if self.sheet_name.is_empty() {
            return range;
        }
        let mut with_space_char = "";
        let mut sheet_name = self.sheet_name.clone();
        if sheet_name.contains(char::is_whitespace) {
            with_space_char = "'";
        }
        if is_ptn2 {
            if sheet_name.contains("!") {
                with_space_char = "'";
            }
            if sheet_name.contains("'") {
                with_space_char = "'";
                sheet_name = sheet_name.replace("'", "''");
            }
            if sheet_name.contains(r#"""#) {
                with_space_char = "'";
            }
        }
        format!(
            "{}{}{}!{}",
            &with_space_char, sheet_name, &with_space_char, range
        )
    }

    pub(crate) fn adjustment_insert_coordinate(
        &mut self,
        sheet_name: &str,
        root_col_num: &u32,
        offset_col_num: &u32,
        root_row_num: &u32,
        offset_row_num: &u32,
    ) {
        if self.sheet_name == sheet_name {
            self.range.adjustment_insert_coordinate(
                root_col_num,
                offset_col_num,
                root_row_num,
                offset_row_num,
            );
        }
    }

    pub(crate) fn adjustment_remove_coordinate(
        &mut self,
        sheet_name: &str,
        root_col_num: &u32,
        offset_col_num: &u32,
        root_row_num: &u32,
        offset_row_num: &u32,
    ) {
        if self.sheet_name == sheet_name {
            self.range.adjustment_remove_coordinate(
                root_col_num,
                offset_col_num,
                root_row_num,
                offset_row_num,
            );
        }
    }

    pub(crate) fn is_remove(
        &self,
        sheet_name: &str,
        root_col_num: &u32,
        offset_col_num: &u32,
        root_row_num: &u32,
        offset_row_num: &u32,
    ) -> bool {
        self.sheet_name == sheet_name
            && self
                .range
                .is_remove(root_col_num, offset_col_num, root_row_num, offset_row_num)
    }
}