umya_spreadsheet/structs/
style.rs

1use crate::structs::Alignment;
2use crate::structs::Borders;
3use crate::structs::Color;
4use crate::structs::Fill;
5use crate::structs::Font;
6use crate::structs::NumberingFormat;
7use crate::structs::PatternValues;
8use crate::structs::Protection;
9use crate::structs::UInt32Value;
10use md5::Digest;
11
12use crate::BooleanValue;
13
14/// # Examples
15/// ## add border
16/// ![Result Image](https://github.com/MathNya/umya-spreadsheet/raw/master/images/style/style_border.png)
17/// ```rust
18/// use umya_spreadsheet::*;
19/// let mut book = new_file();
20/// let mut style = book.get_sheet_by_name_mut("Sheet1").unwrap().get_style_mut("D2");
21///
22/// // add bottom border
23/// style.get_borders_mut().get_bottom_mut().set_border_style(Border::BORDER_MEDIUM);
24/// // add top border
25/// style.get_borders_mut().get_top_mut().set_border_style(Border::BORDER_MEDIUM);
26/// // add left border
27/// style.get_borders_mut().get_left_mut().set_border_style(Border::BORDER_MEDIUM);
28/// // add right border
29/// style.get_borders_mut().get_right_mut().set_border_style(Border::BORDER_MEDIUM);
30/// ```
31///
32/// ## change cell color
33/// ![Result Image](https://github.com/MathNya/umya-spreadsheet/raw/master/images/style/style_fill_color.png)
34/// ```rust
35/// use umya_spreadsheet::*;
36///
37/// let mut book = new_file();
38/// let mut style = book.get_sheet_by_name_mut("Sheet1").unwrap().get_style_mut("A1");
39///
40/// // fill color on red.
41/// style.set_background_color(Color::COLOR_RED);
42/// ```
43///
44/// ## change font color
45/// ![Result Image](https://github.com/MathNya/umya-spreadsheet/raw/master/images/style/style_font_color.png)
46/// ```rust
47/// use umya_spreadsheet::*;
48///
49/// let mut book = new_file();
50/// let mut style = book.get_sheet_by_name_mut("Sheet1").unwrap().get_style_mut("A1");
51///
52/// // font color on red.
53/// style.get_font_mut().get_color_mut().set_argb(Color::COLOR_RED);
54/// ```
55#[derive(Clone, Default, Debug, PartialEq, PartialOrd)]
56pub struct Style {
57    font: Option<Box<Font>>,
58    fill: Option<Box<Fill>>,
59    borders: Option<Box<Borders>>,
60    alignment: Option<Alignment>,
61    numbering_format: Option<Box<NumberingFormat>>,
62    format_id: UInt32Value,
63    protection: Option<Protection>,
64}
65impl Style {
66    #[inline]
67    pub fn get_font(&self) -> Option<&Font> {
68        self.font.as_deref()
69    }
70
71    #[inline]
72    pub fn get_font_mut(&mut self) -> &mut Font {
73        self.font.get_or_insert(Box::new(Font::get_default_value()))
74    }
75
76    #[inline]
77    pub fn set_font(&mut self, value: Font) -> &mut Self {
78        self.font = Some(Box::new(value));
79        self
80    }
81
82    #[inline]
83    pub fn remove_font(&mut self) -> &mut Self {
84        self.font = None;
85        self
86    }
87
88    #[inline]
89    pub(crate) fn set_font_crate(&mut self, value: Option<Font>) -> &mut Self {
90        self.font = value.map(Box::new);
91        self
92    }
93
94    #[inline]
95    pub fn get_fill(&self) -> Option<&Fill> {
96        self.fill.as_deref()
97    }
98
99    #[inline]
100    pub fn get_fill_mut(&mut self) -> &mut Fill {
101        self.fill.get_or_insert(Box::new(Fill::get_default_value()))
102    }
103
104    #[inline]
105    pub fn set_fill(&mut self, value: Fill) -> &mut Self {
106        self.fill = Some(Box::new(value));
107        self
108    }
109
110    #[inline]
111    pub fn get_background_color(&self) -> Option<&Color> {
112        self.get_fill()
113            .and_then(|fill| fill.get_pattern_fill()?.get_foreground_color())
114    }
115
116    #[inline]
117    pub fn set_background_color<S: Into<String>>(&mut self, color: S) -> &mut Self {
118        self.set_background_color_solid(color);
119        self
120    }
121
122    pub fn set_background_color_solid<S: Into<String>>(&mut self, color: S) -> &mut Self {
123        self.get_fill_mut()
124            .get_pattern_fill_mut()
125            .set_pattern_type(PatternValues::Solid)
126            .remove_background_color()
127            .get_foreground_color_mut()
128            .set_argb(color);
129        self
130    }
131
132    pub fn set_background_color_with_pattern<S: Into<String>>(
133        &mut self,
134        color1: S,
135        color2: S,
136        pattern: PatternValues,
137    ) -> &mut Self {
138        self.get_fill_mut()
139            .get_pattern_fill_mut()
140            .set_pattern_type(pattern)
141            .get_background_color_mut()
142            .set_argb(color1);
143        self.get_fill_mut()
144            .get_pattern_fill_mut()
145            .get_foreground_color_mut()
146            .set_argb(color2);
147        self
148    }
149
150    #[inline]
151    pub fn remove_fill(&mut self) -> &mut Self {
152        self.fill = None;
153        self
154    }
155
156    #[inline]
157    pub(crate) fn set_fill_crate(&mut self, value: Option<Fill>) -> &mut Self {
158        self.fill = value.map(Box::new);
159        self
160    }
161
162    #[inline]
163    pub fn get_borders(&self) -> Option<&Borders> {
164        self.borders.as_deref()
165    }
166
167    #[inline]
168    pub fn get_borders_mut(&mut self) -> &mut Borders {
169        self.borders
170            .get_or_insert(Box::new(Borders::get_default_value()))
171    }
172
173    #[inline]
174    pub fn set_borders(&mut self, value: Borders) -> &mut Self {
175        self.borders = Some(Box::new(value));
176        self
177    }
178
179    #[inline]
180    pub fn remove_borders(&mut self) -> &mut Self {
181        self.borders = None;
182        self
183    }
184
185    #[inline]
186    pub(crate) fn set_borders_crate(&mut self, value: Option<Borders>) -> &mut Self {
187        self.borders = value.map(Box::new);
188        self
189    }
190
191    #[inline]
192    pub fn get_alignment(&self) -> Option<&Alignment> {
193        self.alignment.as_ref()
194    }
195
196    #[inline]
197    pub fn get_alignment_mut(&mut self) -> &mut Alignment {
198        self.alignment.get_or_insert(Alignment::default())
199    }
200
201    #[inline]
202    pub fn set_alignment(&mut self, value: Alignment) -> &mut Self {
203        self.alignment = Some(value);
204        self
205    }
206
207    #[inline]
208    pub fn remove_alignment(&mut self) -> &mut Self {
209        self.alignment = None;
210        self
211    }
212
213    #[inline]
214    pub(crate) fn set_alignment_crate(&mut self, value: Option<Alignment>) -> &mut Self {
215        self.alignment = value;
216        self
217    }
218
219    #[inline]
220    pub fn get_numbering_format(&self) -> Option<&NumberingFormat> {
221        self.numbering_format.as_deref()
222    }
223
224    #[inline]
225    pub fn get_numbering_format_mut(&mut self) -> &mut NumberingFormat {
226        self.numbering_format
227            .get_or_insert(Box::new(NumberingFormat::default()))
228    }
229
230    #[inline]
231    pub fn set_numbering_format(&mut self, value: NumberingFormat) -> &mut Self {
232        self.numbering_format = Some(Box::new(value));
233        self
234    }
235
236    #[inline]
237    pub fn remove_numbering_format(&mut self) -> &mut Self {
238        self.numbering_format = None;
239        self
240    }
241
242    #[inline]
243    pub fn get_number_format(&self) -> Option<&NumberingFormat> {
244        self.get_numbering_format()
245    }
246
247    #[inline]
248    pub fn get_number_format_mut(&mut self) -> &mut NumberingFormat {
249        self.get_numbering_format_mut()
250    }
251
252    #[inline]
253    pub fn set_number_format(&mut self, value: NumberingFormat) -> &mut Self {
254        self.set_numbering_format(value)
255    }
256
257    #[inline]
258    pub fn remove_number_format(&mut self) -> &mut Self {
259        self.remove_numbering_format()
260    }
261
262    #[inline]
263    pub fn get_format_id(&self) -> &u32 {
264        self.format_id.get_value()
265    }
266
267    #[inline]
268    pub fn set_format_id(&mut self, value: u32) -> &mut Self {
269        self.format_id.set_value(value);
270        self
271    }
272
273    #[inline]
274    pub fn get_protection(&self) -> Option<&Protection> {
275        self.protection.as_ref()
276    }
277
278    #[inline]
279    pub fn get_protection_mut(&mut self) -> &mut Protection {
280        self.protection.get_or_insert(Protection::default())
281    }
282
283    #[inline]
284    pub fn set_protection(&mut self, value: Protection) -> &mut Self {
285        self.protection = Some(value);
286        self
287    }
288
289    #[inline]
290    pub fn remove_protection(&mut self) -> &mut Self {
291        self.protection = None;
292        self
293    }
294
295    #[inline]
296    pub(crate) fn set_protection_crate(&mut self, value: Option<Protection>) -> &mut Self {
297        self.protection = value;
298        self
299    }
300
301    #[inline]
302    pub(crate) fn is_empty(&self) -> bool {
303        !(self.font.is_some()
304            || self.fill.is_some()
305            || self.borders.is_some()
306            || self.alignment.is_some()
307            || self.numbering_format.is_some()
308            || self.protection.is_some())
309    }
310
311    // When opened in software such as Excel, it is visually blank.
312    #[inline]
313    pub(crate) fn is_visually_empty(&self) -> bool {
314        !(self.fill.as_ref().is_some_and(|x| !x.is_visually_empty())
315            || self
316                .borders
317                .as_ref()
318                .is_some_and(|x| !x.is_visually_empty()))
319    }
320
321    #[inline]
322    pub fn get_default_value() -> Self {
323        let mut def = Self::default();
324        def.set_font(Font::get_default_value());
325        def.set_borders(Borders::get_default_value());
326        def.set_fill(Fill::get_default_value());
327        def
328    }
329
330    #[inline]
331    pub(crate) fn get_default_value_2() -> Self {
332        let mut def = Self::default();
333        def.set_font(Font::get_default_value());
334        def.set_borders(Borders::get_default_value());
335        def.set_fill(Fill::get_default_value_2());
336        def
337    }
338}