spreadsheet_ods/
defaultstyles.rs

1//!
2//! Creates default formats for a new Workbook.
3//!
4
5use crate::format::ValueFormatRef;
6use crate::style::CellStyle;
7use crate::{format, CellStyleRef, ValueType, WorkBook};
8use icu_locale_core::locale;
9
10///
11/// Allows access to the value-format names for the default formats
12/// as created by create_default_styles.
13///
14#[derive(Debug)]
15pub struct DefaultFormat {}
16
17impl DefaultFormat {
18    /// Default format.
19    #[allow(clippy::should_implement_trait)]
20    pub fn default() -> ValueFormatRef {
21        ValueFormatRef::from("")
22    }
23
24    /// Default boolean format.
25    pub fn bool() -> ValueFormatRef {
26        ValueFormatRef::from("bool1")
27    }
28
29    /// Default number format.
30    pub fn number() -> ValueFormatRef {
31        ValueFormatRef::from("num1")
32    }
33
34    /// Default percentage format.
35    pub fn percent() -> ValueFormatRef {
36        ValueFormatRef::from("percent1")
37    }
38
39    /// Default currency format.
40    pub fn currency() -> ValueFormatRef {
41        ValueFormatRef::from("currency1")
42    }
43
44    /// Default date format.
45    pub fn date() -> ValueFormatRef {
46        ValueFormatRef::from("date1")
47    }
48
49    /// Default datetime format.
50    pub fn datetime() -> ValueFormatRef {
51        ValueFormatRef::from("datetime1")
52    }
53
54    /// Default time format.
55    pub fn time_of_day() -> ValueFormatRef {
56        ValueFormatRef::from("time1")
57    }
58
59    /// Default time format.
60    pub fn time_interval() -> ValueFormatRef {
61        ValueFormatRef::from("interval1")
62    }
63}
64
65///
66/// Allows access to the names of the default styles as created by
67/// create_default_styles.
68///
69#[derive(Debug)]
70pub struct DefaultStyle {}
71
72impl DefaultStyle {
73    /// Default bool style.
74    pub fn bool() -> CellStyleRef {
75        CellStyleRef::from("default-bool")
76    }
77
78    /// Default number style.
79    pub fn number() -> CellStyleRef {
80        CellStyleRef::from("default-num")
81    }
82
83    /// Default percent style.
84    pub fn percent() -> CellStyleRef {
85        CellStyleRef::from("default-percent")
86    }
87
88    /// Default currency style.
89    pub fn currency() -> CellStyleRef {
90        CellStyleRef::from("default-currency")
91    }
92
93    /// Default date style.
94    pub fn date() -> CellStyleRef {
95        CellStyleRef::from("default-date")
96    }
97
98    /// Default datetime style.
99    pub fn datetime() -> CellStyleRef {
100        CellStyleRef::from("default-datetime")
101    }
102
103    /// Default time style.
104    pub fn time_of_day() -> CellStyleRef {
105        CellStyleRef::from("default-time")
106    }
107
108    /// Default time style.
109    pub fn time_interval() -> CellStyleRef {
110        CellStyleRef::from("default-interval")
111    }
112}
113
114/// Replaced with WorkBook::locale_settings() or WorkBook::new(l: Locale).
115#[deprecated]
116pub fn create_default_styles(book: &mut WorkBook) {
117    book.add_boolean_format(format::create_boolean_format(DefaultFormat::bool()));
118    book.add_number_format(format::create_number_format(
119        DefaultFormat::number(),
120        2,
121        false,
122    ));
123    book.add_percentage_format(format::create_percentage_format(
124        DefaultFormat::percent(),
125        2,
126    ));
127    book.add_currency_format(format::create_currency_prefix(
128        DefaultFormat::currency(),
129        locale!("de-AT"),
130        "€",
131    ));
132    book.add_datetime_format(format::create_date_dmy_format(DefaultFormat::date()));
133    book.add_datetime_format(format::create_datetime_format(DefaultFormat::datetime()));
134    book.add_timeduration_format(format::create_time_of_day_format(
135        DefaultFormat::time_of_day(),
136    ));
137    book.add_timeduration_format(format::create_time_interval_format(
138        DefaultFormat::time_interval(),
139    ));
140
141    book.add_cellstyle(CellStyle::new(DefaultStyle::bool(), &DefaultFormat::bool()));
142    book.add_cellstyle(CellStyle::new(
143        DefaultStyle::number(),
144        &DefaultFormat::number(),
145    ));
146    book.add_cellstyle(CellStyle::new(
147        DefaultStyle::percent(),
148        &DefaultFormat::percent(),
149    ));
150    book.add_cellstyle(CellStyle::new(
151        DefaultStyle::currency(),
152        &DefaultFormat::currency(),
153    ));
154    book.add_cellstyle(CellStyle::new(DefaultStyle::date(), &DefaultFormat::date()));
155    book.add_cellstyle(CellStyle::new(
156        DefaultStyle::datetime(),
157        &DefaultFormat::datetime(),
158    ));
159    book.add_cellstyle(CellStyle::new(
160        DefaultStyle::time_of_day(),
161        &DefaultFormat::time_of_day(),
162    ));
163    book.add_cellstyle(CellStyle::new(
164        DefaultStyle::time_interval(),
165        &DefaultFormat::time_interval(),
166    ));
167
168    book.add_def_style(ValueType::Boolean, DefaultStyle::bool());
169    book.add_def_style(ValueType::Number, DefaultStyle::number());
170    book.add_def_style(ValueType::Percentage, DefaultStyle::percent());
171    book.add_def_style(ValueType::Currency, DefaultStyle::currency());
172    book.add_def_style(ValueType::DateTime, DefaultStyle::date());
173    book.add_def_style(ValueType::TimeDuration, DefaultStyle::time_interval());
174}