Skip to main content

umya_spreadsheet/structs/
style.rs

1use crate::structs::{
2    Alignment,
3    Borders,
4    Color,
5    Fill,
6    Font,
7    NumberingFormat,
8    PatternValues,
9    Protection,
10    UInt32Value,
11};
12
13/// # Examples
14/// ## add border
15/// ![Result Image](https://github.com/MathNya/umya-spreadsheet/raw/master/images/style/style_border.png)
16/// ```rust
17/// use umya_spreadsheet::*;
18/// let mut book = new_file();
19/// let mut style = book
20///     .sheet_by_name_mut("Sheet1")
21///     .unwrap()
22///     .style_mut("D2");
23///
24/// // add bottom border
25/// style
26///     .borders_mut()
27///     .bottom_mut()
28///     .set_border_style(Border::BORDER_MEDIUM);
29/// // add top border
30/// style
31///     .borders_mut()
32///     .top_mut()
33///     .set_border_style(Border::BORDER_MEDIUM);
34/// // add left border
35/// style
36///     .borders_mut()
37///     .left_mut()
38///     .set_border_style(Border::BORDER_MEDIUM);
39/// // add right border
40/// style
41///     .borders_mut()
42///     .right_mut()
43///     .set_border_style(Border::BORDER_MEDIUM);
44/// ```
45///
46/// ## change cell color
47/// ![Result Image](https://github.com/MathNya/umya-spreadsheet/raw/master/images/style/style_fill_color.png)
48/// ```rust
49/// use umya_spreadsheet::*;
50///
51/// let mut book = new_file();
52/// let mut style = book
53///     .sheet_by_name_mut("Sheet1")
54///     .unwrap()
55///     .style_mut("A1");
56///
57/// // fill color on red.
58/// style.set_background_color(Color::COLOR_RED_STR);
59/// ```
60///
61/// ## change font color
62/// ![Result Image](https://github.com/MathNya/umya-spreadsheet/raw/master/images/style/style_font_color.png)
63/// ```rust
64/// use umya_spreadsheet::*;
65///
66/// let mut book = new_file();
67/// let mut style = book
68///     .sheet_by_name_mut("Sheet1")
69///     .unwrap()
70///     .style_mut("A1");
71///
72/// // font color on red.
73/// style
74///     .font_mut()
75///     .color_mut()
76///     .set_argb(Color::COLOR_RED);
77/// ```
78#[derive(Clone, Default, Debug, PartialEq, PartialOrd)]
79pub struct Style {
80    font:             Option<Box<Font>>,
81    fill:             Option<Box<Fill>>,
82    borders:          Option<Box<Borders>>,
83    alignment:        Option<Alignment>,
84    numbering_format: Option<Box<NumberingFormat>>,
85    format_id:        UInt32Value,
86    protection:       Option<Protection>,
87}
88impl Style {
89    #[inline]
90    #[must_use]
91    pub fn font(&self) -> Option<&Font> {
92        self.font.as_deref()
93    }
94
95    #[inline]
96    #[must_use]
97    #[deprecated(since = "3.0.0", note = "Use font()")]
98    pub fn get_font(&self) -> Option<&Font> {
99        self.font()
100    }
101
102    #[inline]
103    pub fn font_mut(&mut self) -> &mut Font {
104        self.font.get_or_insert(Box::new(Font::default_value()))
105    }
106
107    #[inline]
108    #[deprecated(since = "3.0.0", note = "Use font_mut()")]
109    pub fn get_font_mut(&mut self) -> &mut Font {
110        self.font_mut()
111    }
112
113    #[inline]
114    pub fn set_font(&mut self, value: Font) -> &mut Self {
115        self.font = Some(Box::new(value));
116        self
117    }
118
119    #[inline]
120    pub fn remove_font(&mut self) -> &mut Self {
121        self.font = None;
122        self
123    }
124
125    #[inline]
126    pub(crate) fn set_font_crate(&mut self, value: Option<Font>) -> &mut Self {
127        self.font = value.map(Box::new);
128        self
129    }
130
131    #[inline]
132    #[must_use]
133    pub fn fill(&self) -> Option<&Fill> {
134        self.fill.as_deref()
135    }
136
137    #[inline]
138    #[must_use]
139    #[deprecated(since = "3.0.0", note = "Use fill()")]
140    pub fn get_fill(&self) -> Option<&Fill> {
141        self.fill()
142    }
143
144    #[inline]
145    pub fn fill_mut(&mut self) -> &mut Fill {
146        self.fill.get_or_insert(Box::new(Fill::default_value()))
147    }
148
149    #[inline]
150    #[deprecated(since = "3.0.0", note = "Use fill_mut()")]
151    pub fn get_fill_mut(&mut self) -> &mut Fill {
152        self.fill_mut()
153    }
154
155    #[inline]
156    pub fn set_fill(&mut self, value: Fill) -> &mut Self {
157        self.fill = Some(Box::new(value));
158        self
159    }
160
161    #[inline]
162    #[must_use]
163    pub fn background_color(&self) -> Option<&Color> {
164        self.fill()
165            .and_then(|fill| fill.pattern_fill()?.foreground_color())
166    }
167
168    #[inline]
169    #[must_use]
170    #[deprecated(since = "3.0.0", note = "Use background_color()")]
171    pub fn get_background_color(&self) -> Option<&Color> {
172        self.background_color()
173    }
174    
175    #[inline]
176    pub fn set_background_color<S: AsRef<str>>(&mut self, color: S) -> &mut Self {
177        self.set_background_color_solid(color);
178        self
179    }
180
181    pub fn set_background_color_solid<S: AsRef<str>>(&mut self, color: S) -> &mut Self {
182        self.fill_mut()
183            .pattern_fill_mut()
184            .set_pattern_type(PatternValues::Solid)
185            .remove_background_color()
186            .foreground_color_mut()
187            .set_argb_str(color);
188        self
189    }
190
191    pub fn set_background_color_with_pattern<S: AsRef<str>>(
192        &mut self,
193        color1: S,
194        color2: S,
195        pattern: PatternValues,
196    ) -> &mut Self {
197        self.fill_mut()
198            .pattern_fill_mut()
199            .set_pattern_type(pattern)
200            .background_color_mut()
201            .set_argb_str(color1);
202        self.fill_mut()
203            .pattern_fill_mut()
204            .foreground_color_mut()
205            .set_argb_str(color2);
206        self
207    }
208
209    #[inline]
210    pub fn remove_fill(&mut self) -> &mut Self {
211        self.fill = None;
212        self
213    }
214
215    #[inline]
216    pub(crate) fn set_fill_crate(&mut self, value: Option<Fill>) -> &mut Self {
217        self.fill = value.map(Box::new);
218        self
219    }
220
221    #[inline]
222    #[must_use]
223    pub fn borders(&self) -> Option<&Borders> {
224        self.borders.as_deref()
225    }
226
227    #[inline]
228    #[must_use]
229    #[deprecated(since = "3.0.0", note = "Use borders()")]
230    pub fn get_borders(&self) -> Option<&Borders> {
231        self.borders()
232    }
233
234    #[inline]
235    pub fn borders_mut(&mut self) -> &mut Borders {
236        self.borders
237            .get_or_insert(Box::new(Borders::default_value()))
238    }
239
240    #[inline]
241    #[deprecated(since = "3.0.0", note = "Use borders_mut()")]
242    pub fn get_borders_mut(&mut self) -> &mut Borders {
243        self.borders_mut()
244    }
245
246    #[inline]
247    pub fn set_borders(&mut self, value: Borders) -> &mut Self {
248        self.borders = Some(Box::new(value));
249        self
250    }
251
252    #[inline]
253    pub fn remove_borders(&mut self) -> &mut Self {
254        self.borders = None;
255        self
256    }
257
258    #[inline]
259    pub(crate) fn set_borders_crate(&mut self, value: Option<Borders>) -> &mut Self {
260        self.borders = value.map(Box::new);
261        self
262    }
263
264    #[inline]
265    #[must_use]
266    pub fn alignment(&self) -> Option<&Alignment> {
267        self.alignment.as_ref()
268    }
269
270    #[inline]
271    #[must_use]
272    #[deprecated(since = "3.0.0", note = "Use alignment()")]
273    pub fn get_alignment(&self) -> Option<&Alignment> {
274        self.alignment()
275    }
276
277    #[inline]
278    pub fn alignment_mut(&mut self) -> &mut Alignment {
279        self.alignment.get_or_insert(Alignment::default())
280    }
281
282    #[inline]
283    #[deprecated(since = "3.0.0", note = "Use alignment_mut()")]
284    pub fn get_alignment_mut(&mut self) -> &mut Alignment {
285        self.alignment_mut()
286    }
287
288    #[inline]
289    pub fn set_alignment(&mut self, value: Alignment) -> &mut Self {
290        self.alignment = Some(value);
291        self
292    }
293
294    #[inline]
295    pub fn remove_alignment(&mut self) -> &mut Self {
296        self.alignment = None;
297        self
298    }
299
300    #[inline]
301    pub(crate) fn set_alignment_crate(&mut self, value: Option<Alignment>) -> &mut Self {
302        self.alignment = value;
303        self
304    }
305
306    #[inline]
307    #[must_use]
308    pub fn numbering_format(&self) -> Option<&NumberingFormat> {
309        self.numbering_format.as_deref()
310    }
311
312    #[inline]
313    #[must_use]
314    #[deprecated(since = "3.0.0", note = "Use numbering_format()")]
315    pub fn get_numbering_format(&self) -> Option<&NumberingFormat> {
316        self.numbering_format()
317    }
318
319    #[inline]
320    pub fn numbering_format_mut(&mut self) -> &mut NumberingFormat {
321        self.numbering_format
322            .get_or_insert(Box::new(NumberingFormat::default()))
323    }
324
325    #[inline]
326    #[deprecated(since = "3.0.0", note = "Use numbering_format_mut()")]
327    pub fn get_numbering_format_mut(&mut self) -> &mut NumberingFormat {
328        self.numbering_format_mut()
329    }
330
331    #[inline]
332    pub fn set_numbering_format(&mut self, value: NumberingFormat) -> &mut Self {
333        self.numbering_format = Some(Box::new(value));
334        self
335    }
336
337    #[inline]
338    pub fn remove_numbering_format(&mut self) -> &mut Self {
339        self.numbering_format = None;
340        self
341    }
342
343    #[inline]
344    #[must_use]
345    pub fn number_format(&self) -> Option<&NumberingFormat> {
346        self.numbering_format()
347    }
348
349    #[inline]
350    #[must_use]
351    #[deprecated(since = "3.0.0", note = "Use number_format()")]
352    pub fn get_number_format(&self) -> Option<&NumberingFormat> {
353        self.number_format()
354    }
355
356    #[inline]
357    pub fn number_format_mut(&mut self) -> &mut NumberingFormat {
358        self.numbering_format_mut()
359    }
360
361    #[inline]
362    #[deprecated(since = "3.0.0", note = "Use number_format_mut()")]
363    pub fn get_number_format_mut(&mut self) -> &mut NumberingFormat {
364        self.number_format_mut()
365    }
366
367    #[inline]
368    pub fn set_number_format(&mut self, value: NumberingFormat) -> &mut Self {
369        self.set_numbering_format(value)
370    }
371
372    #[inline]
373    pub fn remove_number_format(&mut self) -> &mut Self {
374        self.remove_numbering_format()
375    }
376
377    #[inline]
378    #[must_use]
379    pub fn format_id(&self) -> u32 {
380        self.format_id.value()
381    }
382
383    #[inline]
384    #[must_use]
385    #[deprecated(since = "3.0.0", note = "Use format_id()")]
386    pub fn get_format_id(&self) -> u32 {
387        self.format_id()
388    }
389
390    #[inline]
391    pub fn set_format_id(&mut self, value: u32) -> &mut Self {
392        self.format_id.set_value(value);
393        self
394    }
395
396    #[inline]
397    #[must_use]
398    pub fn protection(&self) -> Option<&Protection> {
399        self.protection.as_ref()
400    }
401
402    #[inline]
403    #[must_use]
404    #[deprecated(since = "3.0.0", note = "Use protection()")]
405    pub fn get_protection(&self) -> Option<&Protection> {
406        self.protection()
407    }
408
409    #[inline]
410    pub fn protection_mut(&mut self) -> &mut Protection {
411        self.protection.get_or_insert(Protection::default())
412    }
413
414    #[inline]
415    #[deprecated(since = "3.0.0", note = "Use protection_mut()")]
416    pub fn get_protection_mut(&mut self) -> &mut Protection {
417        self.protection_mut()
418    }
419
420    #[inline]
421    pub fn set_protection(&mut self, value: Protection) -> &mut Self {
422        self.protection = Some(value);
423        self
424    }
425
426    #[inline]
427    pub fn remove_protection(&mut self) -> &mut Self {
428        self.protection = None;
429        self
430    }
431
432    #[inline]
433    #[allow(dead_code)]
434    pub(crate) fn set_protection_crate(&mut self, value: Option<Protection>) -> &mut Self {
435        self.protection = value;
436        self
437    }
438
439    #[inline]
440    pub(crate) fn is_empty(&self) -> bool {
441        !(self.font.is_some()
442            || self.fill.is_some()
443            || self.borders.is_some()
444            || self.alignment.is_some()
445            || self.numbering_format.is_some()
446            || self.protection.is_some())
447    }
448
449    // When opened in software such as Excel, it is visually blank.
450    #[inline]
451    pub(crate) fn is_visually_empty(&self) -> bool {
452        !(self.fill.as_ref().is_some_and(|x| !x.is_visually_empty())
453            || self
454                .borders
455                .as_ref()
456                .is_some_and(|x| !x.is_visually_empty()))
457    }
458
459    #[inline]
460    #[must_use]
461    pub fn default_value() -> Self {
462        let mut def = Self::default();
463        def.set_font(Font::default_value());
464        def.set_borders(Borders::default_value());
465        def.set_fill(Fill::default_value());
466        def
467    }
468
469    #[inline]
470    #[must_use]
471    #[deprecated(since = "3.0.0", note = "Use default_value()")]
472    pub fn get_default_value() -> Self {
473        Self::default_value()
474    }
475
476    #[inline]
477    pub(crate) fn default_value_2() -> Self {
478        let mut def = Self::default();
479        def.set_font(Font::default_value());
480        def.set_borders(Borders::default_value());
481        def.set_fill(Fill::default_value_2());
482        def
483    }
484
485    #[inline]
486    #[deprecated(since = "3.0.0", note = "Use default_value_2()")]
487    pub(crate) fn get_default_value_2() -> Self {
488        Self::default_value_2()
489    }
490}