umya_spreadsheet/structs/
sheet_protection.rs

1// sheetProtection
2use super::BooleanValue;
3use super::StringValue;
4use super::UInt32Value;
5use crate::helper::crypt::*;
6use crate::reader::driver::*;
7use crate::writer::driver::*;
8use md5::Digest;
9use quick_xml::events::{BytesStart, Event};
10use quick_xml::Reader;
11use quick_xml::Writer;
12use std::io::Cursor;
13
14#[derive(Default, Debug, Clone, PartialEq, PartialOrd)]
15pub struct SheetProtection {
16    algorithm_name: StringValue,
17    hash_value: StringValue,
18    salt_value: StringValue,
19    spin_count: UInt32Value,
20    password: StringValue,
21    sheet: BooleanValue,
22    objects: BooleanValue,
23    delete_rows: BooleanValue,
24    insert_columns: BooleanValue,
25    delete_columns: BooleanValue,
26    insert_hyperlinks: BooleanValue,
27    auto_filter: BooleanValue,
28    scenarios: BooleanValue,
29    format_cells: BooleanValue,
30    format_columns: BooleanValue,
31    insert_rows: BooleanValue,
32    format_rows: BooleanValue,
33    pivot_tables: BooleanValue,
34    select_locked_cells: BooleanValue,
35    select_unlocked_cells: BooleanValue,
36    sort: BooleanValue,
37}
38impl SheetProtection {
39    #[inline]
40    pub fn get_algorithm_name(&self) -> &str {
41        self.algorithm_name.get_value_str()
42    }
43
44    #[inline]
45    pub fn set_algorithm_name<S: Into<String>>(&mut self, value: S) -> &mut Self {
46        self.algorithm_name.set_value(value);
47        self
48    }
49
50    #[inline]
51    pub fn get_hash_value(&self) -> &str {
52        self.hash_value.get_value_str()
53    }
54
55    #[inline]
56    pub fn set_hash_value<S: Into<String>>(&mut self, value: S) -> &mut Self {
57        self.hash_value.set_value(value);
58        self
59    }
60
61    #[inline]
62    pub fn get_salt_value(&self) -> &str {
63        self.salt_value.get_value_str()
64    }
65
66    #[inline]
67    pub fn set_salt_value<S: Into<String>>(&mut self, value: S) -> &mut Self {
68        self.salt_value.set_value(value);
69        self
70    }
71
72    #[inline]
73    pub fn get_spin_count(&self) -> &u32 {
74        self.spin_count.get_value()
75    }
76
77    #[inline]
78    pub fn set_spin_count(&mut self, value: u32) -> &mut Self {
79        self.spin_count.set_value(value);
80        self
81    }
82
83    #[inline]
84    pub fn get_password_raw(&self) -> &str {
85        self.password.get_value_str()
86    }
87
88    #[inline]
89    pub fn set_password_raw<S: Into<String>>(&mut self, value: S) -> &mut Self {
90        self.password.set_value(value);
91        self
92    }
93
94    #[inline]
95    pub fn remove_password_raw(&mut self) -> &mut Self {
96        self.password.remove_value();
97        self
98    }
99
100    #[inline]
101    pub fn get_sheet(&self) -> &bool {
102        self.sheet.get_value()
103    }
104
105    #[inline]
106    pub fn set_sheet(&mut self, value: bool) -> &mut Self {
107        self.sheet.set_value(value);
108        self
109    }
110
111    #[inline]
112    pub fn get_objects(&self) -> &bool {
113        self.objects.get_value()
114    }
115
116    #[inline]
117    pub fn set_objects(&mut self, value: bool) -> &mut Self {
118        self.objects.set_value(value);
119        self
120    }
121
122    #[inline]
123    pub fn get_delete_rows(&self) -> &bool {
124        self.delete_rows.get_value()
125    }
126
127    #[inline]
128    pub fn set_delete_rows(&mut self, value: bool) -> &mut Self {
129        self.delete_rows.set_value(value);
130        self
131    }
132
133    #[inline]
134    pub fn get_insert_columns(&self) -> &bool {
135        self.insert_columns.get_value()
136    }
137
138    #[inline]
139    pub fn set_insert_columns(&mut self, value: bool) -> &mut Self {
140        self.insert_columns.set_value(value);
141        self
142    }
143
144    #[inline]
145    pub fn get_delete_columns(&self) -> &bool {
146        self.delete_columns.get_value()
147    }
148
149    #[inline]
150    pub fn set_delete_columns(&mut self, value: bool) -> &mut Self {
151        self.delete_columns.set_value(value);
152        self
153    }
154
155    #[inline]
156    pub fn get_insert_hyperlinks(&self) -> &bool {
157        self.insert_hyperlinks.get_value()
158    }
159
160    #[inline]
161    pub fn set_insert_hyperlinks(&mut self, value: bool) -> &mut Self {
162        self.insert_hyperlinks.set_value(value);
163        self
164    }
165
166    #[inline]
167    pub fn get_auto_filter(&self) -> &bool {
168        self.auto_filter.get_value()
169    }
170
171    #[inline]
172    pub fn set_auto_filter(&mut self, value: bool) -> &mut Self {
173        self.auto_filter.set_value(value);
174        self
175    }
176
177    #[inline]
178    pub fn get_scenarios(&self) -> &bool {
179        self.scenarios.get_value()
180    }
181
182    #[inline]
183    pub fn set_scenarios(&mut self, value: bool) -> &mut Self {
184        self.scenarios.set_value(value);
185        self
186    }
187
188    #[inline]
189    pub fn get_format_cells(&self) -> &bool {
190        self.format_cells.get_value()
191    }
192
193    #[inline]
194    pub fn set_format_cells(&mut self, value: bool) -> &mut Self {
195        self.format_cells.set_value(value);
196        self
197    }
198
199    #[inline]
200    pub fn get_format_columns(&self) -> &bool {
201        self.format_columns.get_value()
202    }
203
204    #[inline]
205    pub fn set_format_columns(&mut self, value: bool) -> &mut Self {
206        self.format_columns.set_value(value);
207        self
208    }
209
210    #[inline]
211    pub fn get_insert_rows(&self) -> &bool {
212        self.insert_rows.get_value()
213    }
214
215    #[inline]
216    pub fn set_insert_rows(&mut self, value: bool) -> &mut Self {
217        self.insert_rows.set_value(value);
218        self
219    }
220
221    #[inline]
222    pub fn get_format_rows(&self) -> &bool {
223        self.format_rows.get_value()
224    }
225
226    #[inline]
227    pub fn set_format_rows(&mut self, value: bool) -> &mut Self {
228        self.format_rows.set_value(value);
229        self
230    }
231
232    #[inline]
233    pub fn get_pivot_tables(&self) -> &bool {
234        self.pivot_tables.get_value()
235    }
236
237    #[inline]
238    pub fn set_pivot_tables(&mut self, value: bool) -> &mut Self {
239        self.pivot_tables.set_value(value);
240        self
241    }
242
243    #[inline]
244    pub fn get_select_locked_cells(&self) -> &bool {
245        self.select_locked_cells.get_value()
246    }
247
248    #[inline]
249    pub fn set_select_locked_cells(&mut self, value: bool) -> &mut Self {
250        self.select_locked_cells.set_value(value);
251        self
252    }
253
254    #[inline]
255    pub fn get_select_unlocked_cells(&self) -> &bool {
256        self.select_unlocked_cells.get_value()
257    }
258
259    #[inline]
260    pub fn set_select_unlocked_cells(&mut self, value: bool) -> &mut Self {
261        self.select_unlocked_cells.set_value(value);
262        self
263    }
264
265    #[inline]
266    pub fn get_sort(&self) -> &bool {
267        self.sort.get_value()
268    }
269
270    #[inline]
271    pub fn set_sort(&mut self, value: bool) -> &mut Self {
272        self.sort.set_value(value);
273        self
274    }
275
276    #[inline]
277    pub fn set_password(&mut self, password: &str) -> &mut Self {
278        encrypt_sheet_protection(password, self);
279        self
280    }
281
282    pub(crate) fn set_attributes<R: std::io::BufRead>(
283        &mut self,
284        _reader: &mut Reader<R>,
285        e: &BytesStart,
286    ) {
287        set_string_from_xml!(self, e, algorithm_name, "algorithmName");
288        set_string_from_xml!(self, e, hash_value, "hashValue");
289        set_string_from_xml!(self, e, salt_value, "saltValue");
290        set_string_from_xml!(self, e, spin_count, "spinCount");
291        set_string_from_xml!(self, e, password, "password");
292        set_string_from_xml!(self, e, sheet, "sheet");
293        set_string_from_xml!(self, e, objects, "objects");
294        set_string_from_xml!(self, e, delete_rows, "deleteRows");
295        set_string_from_xml!(self, e, insert_columns, "insertColumns");
296        set_string_from_xml!(self, e, delete_columns, "deleteColumns");
297        set_string_from_xml!(self, e, insert_hyperlinks, "insertHyperlinks");
298        set_string_from_xml!(self, e, auto_filter, "autoFilter");
299        set_string_from_xml!(self, e, scenarios, "scenarios");
300        set_string_from_xml!(self, e, format_cells, "formatCells");
301        set_string_from_xml!(self, e, format_columns, "formatColumns");
302        set_string_from_xml!(self, e, insert_rows, "insertRows");
303        set_string_from_xml!(self, e, format_rows, "formatRows");
304        set_string_from_xml!(self, e, pivot_tables, "pivotTables");
305        set_string_from_xml!(self, e, select_locked_cells, "selectLockedCells");
306        set_string_from_xml!(self, e, select_unlocked_cells, "selectUnlockedCells");
307        set_string_from_xml!(self, e, sort, "sort");
308    }
309
310    pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
311        // sheetProtection
312        let mut attributes: Vec<(&str, &str)> = Vec::new();
313        if self.algorithm_name.has_value() {
314            attributes.push(("algorithmName", self.algorithm_name.get_value_str()));
315        }
316        if self.hash_value.has_value() {
317            attributes.push(("hashValue", self.hash_value.get_value_str()));
318        }
319        if self.salt_value.has_value() {
320            attributes.push(("saltValue", self.salt_value.get_value_str()));
321        }
322        let spin_count = self.spin_count.get_value_string();
323        if self.spin_count.has_value() {
324            attributes.push(("spinCount", &spin_count));
325        }
326        if self.password.has_value() {
327            attributes.push(("password", self.password.get_value_str()));
328        }
329        if self.sheet.has_value() {
330            attributes.push(("sheet", self.sheet.get_value_string()));
331        }
332        if self.objects.has_value() {
333            attributes.push(("objects", self.objects.get_value_string()));
334        }
335        if self.delete_rows.has_value() {
336            attributes.push(("deleteRows", self.delete_rows.get_value_string()));
337        }
338        if self.insert_columns.has_value() {
339            attributes.push(("insertColumns", self.insert_columns.get_value_string()));
340        }
341        if self.delete_columns.has_value() {
342            attributes.push(("deleteColumns", self.delete_columns.get_value_string()));
343        }
344        if self.insert_hyperlinks.has_value() {
345            attributes.push((
346                "insertHyperlinks",
347                self.insert_hyperlinks.get_value_string(),
348            ));
349        }
350        if self.auto_filter.has_value() {
351            attributes.push(("autoFilter", self.auto_filter.get_value_string()));
352        }
353        if self.scenarios.has_value() {
354            attributes.push(("scenarios", self.scenarios.get_value_string()));
355        }
356        if self.format_cells.has_value() {
357            attributes.push(("formatCells", self.format_cells.get_value_string()));
358        }
359        if self.format_columns.has_value() {
360            attributes.push(("formatColumns", self.format_columns.get_value_string()));
361        }
362        if self.insert_rows.has_value() {
363            attributes.push(("insertRows", self.insert_rows.get_value_string()));
364        }
365        if self.format_rows.has_value() {
366            attributes.push(("formatRows", self.format_rows.get_value_string()));
367        }
368        if self.pivot_tables.has_value() {
369            attributes.push(("pivotTables", self.pivot_tables.get_value_string()));
370        }
371        if self.select_locked_cells.has_value() {
372            attributes.push((
373                "selectLockedCells",
374                self.select_locked_cells.get_value_string(),
375            ));
376        }
377        if self.select_unlocked_cells.has_value() {
378            attributes.push((
379                "selectUnlockedCells",
380                self.select_unlocked_cells.get_value_string(),
381            ));
382        }
383        if self.sort.has_value() {
384            attributes.push(("sort", self.sort.get_value_string()));
385        }
386
387        write_start_tag(writer, "sheetProtection", attributes, true);
388    }
389}