Skip to main content

umya_spreadsheet/structs/
table.rs

1use super::{
2    BooleanValue,
3    EnumValue,
4    StringValue,
5    TotalsRowFunctionValues,
6    UInt32Value,
7    coordinate::Coordinate,
8};
9use crate::helper::coordinate::CellCoordinates;
10
11// use reader::driver::*;
12
13#[derive(Clone, Default, Debug)]
14pub struct Table {
15    name:             Box<str>,
16    area:             (Coordinate, Coordinate),
17    display_name:     Box<str>,
18    columns:          Vec<TableColumn>,
19    style_info:       Option<Box<TableStyleInfo>>,
20    totals_row_shown: BooleanValue,
21    totals_row_count: UInt32Value,
22}
23impl Table {
24    #[inline]
25    pub fn new<T>(name: &str, area: (T, T)) -> Self
26    where
27        T: Into<CellCoordinates>,
28    {
29        let coord_beg = Self::cell_coord_to_coord(area.0);
30        let coord_end = Self::cell_coord_to_coord(area.1);
31        let name: Box<str> = name.into();
32        Self {
33            area:             (coord_beg, coord_end),
34            name:             name.clone(),
35            display_name:     name,
36            columns:          Vec::<TableColumn>::default(),
37            style_info:       None,
38            totals_row_shown: BooleanValue::default(),
39            totals_row_count: UInt32Value::default(),
40        }
41    }
42
43    #[inline]
44    #[must_use]
45    pub fn is_ok(&self) -> bool {
46        !(self.name.is_empty()
47            || self.display_name.is_empty()
48            || self.area.0.col_num() == 0
49            || self.area.0.row_num() == 0
50            || self.area.1.col_num() == 0
51            || self.area.1.row_num() == 0
52            || self.area.0.col_num() > self.area.1.col_num()
53            || self.area.0.row_num() > self.area.1.row_num())
54    }
55
56    #[inline]
57    #[must_use]
58    pub fn name(&self) -> &str {
59        &self.name
60    }
61
62    #[inline]
63    #[must_use]
64    #[deprecated(since = "3.0.0", note = "Use name()")]
65    pub fn get_name(&self) -> &str {
66        self.name()
67    }
68
69    #[inline]
70    pub fn set_name(&mut self, name: &str) {
71        self.name = name.into();
72        if self.display_name.is_empty() {
73            self.display_name = name.into();
74        }
75    }
76
77    #[inline]
78    #[must_use]
79    pub fn display_name(&self) -> &str {
80        &self.display_name
81    }
82
83    #[inline]
84    #[must_use]
85    #[deprecated(since = "3.0.0", note = "Use display_name()")]
86    pub fn get_display_name(&self) -> &str {
87        self.display_name()
88    }
89
90    #[inline]
91    pub fn set_display_name(&mut self, display_name: &str) {
92        self.display_name = display_name.into();
93    }
94
95    #[inline]
96    #[must_use]
97    pub fn area(&self) -> &(Coordinate, Coordinate) {
98        &self.area
99    }
100
101    #[inline]
102    #[must_use]
103    #[deprecated(since = "3.0.0", note = "Use area()")]
104    pub fn get_area(&self) -> &(Coordinate, Coordinate) {
105        self.area()
106    }
107
108    #[inline]
109    pub fn set_area<T>(&mut self, area: (T, T))
110    where
111        T: Into<CellCoordinates>,
112    {
113        let coord_beg = Self::cell_coord_to_coord(area.0);
114        let coord_end = Self::cell_coord_to_coord(area.1);
115        self.area = (coord_beg, coord_end);
116    }
117
118    #[inline]
119    pub fn add_column(&mut self, col: TableColumn) {
120        self.columns.push(col);
121    }
122
123    #[inline]
124    #[must_use]
125    pub fn columns(&self) -> &[TableColumn] {
126        &self.columns
127    }
128
129    #[inline]
130    #[must_use]
131    pub fn columns_mut(&mut self) -> &mut [TableColumn] {
132        &mut self.columns
133    }
134
135    #[inline]
136    #[must_use]
137    #[deprecated(since = "3.0.0", note = "Use columns()")]
138    pub fn get_columns(&self) -> &[TableColumn] {
139        self.columns()
140    }
141
142    #[inline]
143    pub(crate) fn has_style_info(&self) -> bool {
144        self.style_info.is_some()
145    }
146
147    #[inline]
148    #[must_use]
149    pub fn style_info(&self) -> Option<&TableStyleInfo> {
150        self.style_info.as_deref()
151    }
152
153    #[inline]
154    #[must_use]
155    #[deprecated(since = "3.0.0", note = "Use style_info()")]
156    pub fn get_style_info(&self) -> Option<&TableStyleInfo> {
157        self.style_info()
158    }
159
160    #[inline]
161    pub fn set_style_info(&mut self, style_info: Option<TableStyleInfo>) {
162        self.style_info = style_info.map(Box::new);
163    }
164
165    #[inline]
166    pub(crate) fn has_totals_row_shown(&self) -> bool {
167        self.totals_row_shown.has_value()
168    }
169
170    #[inline]
171    #[must_use]
172    pub fn totals_row_shown(&self) -> bool {
173        self.totals_row_shown.value()
174    }
175
176    #[inline]
177    #[must_use]
178    #[deprecated(since = "3.0.0", note = "Use totals_row_shown()")]
179    pub fn get_totals_row_shown(&self) -> bool {
180        self.totals_row_shown()
181    }
182
183    #[inline]
184    pub(crate) fn totals_row_shown_str(&self) -> &str {
185        self.totals_row_shown.value_string()
186    }
187
188    #[inline]
189    #[deprecated(since = "3.0.0", note = "Use totals_row_shown_str()")]
190    pub(crate) fn get_totals_row_shown_str(&self) -> &str {
191        self.totals_row_shown_str()
192    }
193
194    #[inline]
195    pub fn set_totals_row_shown(&mut self, value: bool) {
196        self.totals_row_shown.set_value(value);
197    }
198
199    #[inline]
200    pub(crate) fn set_totals_row_shown_str(&mut self, value: &str) {
201        self.totals_row_shown.set_value_string(value);
202    }
203
204    #[inline]
205    pub(crate) fn has_totals_row_count(&self) -> bool {
206        self.totals_row_count.has_value()
207    }
208
209    #[inline]
210    #[must_use]
211    pub fn totals_row_count(&self) -> u32 {
212        self.totals_row_count.value()
213    }
214
215    #[inline]
216    #[must_use]
217    #[deprecated(since = "3.0.0", note = "Use totals_row_count()")]
218    pub fn get_totals_row_count(&self) -> u32 {
219        self.totals_row_count()
220    }
221
222    #[inline]
223    pub(crate) fn totals_row_count_str(&self) -> String {
224        self.totals_row_count.value_string()
225    }
226
227    #[inline]
228    #[deprecated(since = "3.0.0", note = "Use totals_row_count_str()")]
229    pub(crate) fn get_totals_row_count_str(&self) -> String {
230        self.totals_row_count_str()
231    }
232
233    #[inline]
234    pub fn set_totals_row_count(&mut self, value: u32) {
235        self.totals_row_count.set_value(value);
236    }
237
238    #[inline]
239    pub(crate) fn set_totals_row_count_str(&mut self, value: &str) {
240        self.totals_row_count.set_value_string(value);
241    }
242
243    #[inline]
244    fn cell_coord_to_coord<T>(cc: T) -> Coordinate
245    where
246        T: Into<CellCoordinates>,
247    {
248        let cell_coord: CellCoordinates = cc.into();
249        let mut coord: Coordinate = Coordinate::default();
250        coord.set_col_num(cell_coord.col);
251        coord.set_row_num(cell_coord.row);
252        coord
253    }
254}
255
256#[derive(Clone, Default, Debug)]
257pub struct TableColumn {
258    name:                      String,
259    totals_row_label:          StringValue,
260    totals_row_function:       EnumValue<TotalsRowFunctionValues>,
261    calculated_column_formula: Option<String>,
262}
263impl TableColumn {
264    #[inline]
265    #[must_use]
266    pub fn new(name: &str) -> Self {
267        Self {
268            name:                      name.to_string(),
269            totals_row_label:          StringValue::default(),
270            totals_row_function:       EnumValue::default(),
271            calculated_column_formula: None,
272        }
273    }
274
275    #[inline]
276    #[must_use]
277    pub fn name(&self) -> &str {
278        self.name.as_str()
279    }
280
281    #[inline]
282    #[must_use]
283    #[deprecated(since = "3.0.0", note = "Use name()")]
284    pub fn get_name(&self) -> &str {
285        self.name()
286    }
287
288    #[inline]
289    pub fn set_name(&mut self, name: String) {
290        self.name = name;
291    }
292
293    #[inline]
294    #[allow(dead_code)]
295    pub(crate) fn has_totals_row_label(&self) -> bool {
296        self.totals_row_label.has_value()
297    }
298
299    #[inline]
300    #[must_use]
301    pub fn totals_row_label(&self) -> Option<&str> {
302        self.totals_row_label.value()
303    }
304
305    #[inline]
306    #[must_use]
307    #[deprecated(since = "3.0.0", note = "Use totals_row_label()")]
308    pub fn get_totals_row_label(&self) -> Option<&str> {
309        self.totals_row_label()
310    }
311
312    #[inline]
313    pub(crate) fn totals_row_label_str(&self) -> &str {
314        self.totals_row_label.value_str()
315    }
316
317    #[inline]
318    #[deprecated(since = "3.0.0", note = "Use totals_row_label_str()")]
319    pub(crate) fn get_totals_row_label_str(&self) -> &str {
320        self.totals_row_label_str()
321    }
322
323    #[inline]
324    pub fn set_totals_row_label(&mut self, value: &str) {
325        self.totals_row_label.set_value(value);
326    }
327
328    #[inline]
329    pub(crate) fn set_totals_row_label_str(&mut self, value: &str) {
330        self.totals_row_label.set_value_string(value);
331    }
332
333    #[inline]
334    #[allow(dead_code)]
335    pub(crate) fn has_totals_row_function(&self) -> bool {
336        self.totals_row_function.has_value()
337    }
338
339    #[inline]
340    #[must_use]
341    pub fn totals_row_function(&self) -> &TotalsRowFunctionValues {
342        self.totals_row_function.value()
343    }
344
345    #[inline]
346    #[must_use]
347    #[deprecated(since = "3.0.0", note = "Use totals_row_function()")]
348    pub fn get_totals_row_function(&self) -> &TotalsRowFunctionValues {
349        self.totals_row_function()
350    }
351
352    #[inline]
353    pub(crate) fn totals_row_function_str(&self) -> &str {
354        self.totals_row_function.value_string()
355    }
356
357    #[inline]
358    #[deprecated(since = "3.0.0", note = "Use totals_row_function_str()")]
359    pub(crate) fn get_totals_row_function_str(&self) -> &str {
360        self.totals_row_function_str()
361    }
362
363    #[inline]
364    pub fn set_totals_row_function(&mut self, value: TotalsRowFunctionValues) {
365        self.totals_row_function.set_value(value);
366    }
367
368    #[inline]
369    pub(crate) fn set_totals_row_function_str(&mut self, value: &str) {
370        self.totals_row_function.set_value_string(value);
371    }
372
373    #[inline]
374    #[must_use]
375    pub fn calculated_column_formula(&self) -> Option<&String> {
376        self.calculated_column_formula.as_ref()
377    }
378
379    #[inline]
380    #[must_use]
381    #[deprecated(since = "3.0.0", note = "Use calculated_column_formula()")]
382    pub fn get_calculated_column_formula(&self) -> Option<&String> {
383        self.calculated_column_formula()
384    }
385
386    #[inline]
387    pub(crate) fn set_calculated_column_formula(&mut self, value: String) {
388        self.calculated_column_formula = Some(value);
389    }
390}
391
392#[derive(Clone, Debug)]
393pub struct TableStyleInfo {
394    name:             String,
395    show_first_col:   ShowColumn,
396    show_last_col:    ShowColumn,
397    show_row_stripes: ShowStripes,
398    show_col_stripes: ShowStripes,
399}
400
401#[derive(Debug, Clone, Copy)]
402pub enum ShowColumn {
403    Show,
404    Hide,
405}
406
407#[derive(Debug, Clone, Copy)]
408pub enum ShowStripes {
409    Show,
410    Hide,
411}
412
413impl Default for TableStyleInfo {
414    fn default() -> Self {
415        Self {
416            name:             String::new(), // Default name can be an empty string
417            show_first_col:   ShowColumn::Hide, // Default to Hide
418            show_last_col:    ShowColumn::Hide, // Default to Hide
419            show_row_stripes: ShowStripes::Hide, // Default to Hide
420            show_col_stripes: ShowStripes::Hide, // Default to Hide
421        }
422    }
423}
424
425impl TableStyleInfo {
426    #[inline]
427    #[must_use]
428    pub fn new(
429        name: &str,
430        show_first_col: ShowColumn,
431        show_last_col: ShowColumn,
432        show_row_stripes: ShowStripes,
433        show_col_stripes: ShowStripes,
434    ) -> Self {
435        Self {
436            name: name.to_string(),
437            show_first_col,
438            show_last_col,
439            show_row_stripes,
440            show_col_stripes,
441        }
442    }
443
444    #[inline]
445    #[must_use]
446    pub fn name(&self) -> &str {
447        self.name.as_str()
448    }
449
450    #[inline]
451    #[must_use]
452    #[deprecated(since = "3.0.0", note = "Use name()")]
453    pub fn get_name(&self) -> &str {
454        self.name()
455    }
456
457    #[inline]
458    #[must_use]
459    pub fn is_show_first_col(&self) -> bool {
460        matches!(self.show_first_col, ShowColumn::Show)
461    }
462
463    #[inline]
464    #[must_use]
465    pub fn is_show_last_col(&self) -> bool {
466        matches!(self.show_last_col, ShowColumn::Show)
467    }
468
469    #[inline]
470    #[must_use]
471    pub fn is_show_row_stripes(&self) -> bool {
472        matches!(self.show_row_stripes, ShowStripes::Show)
473    }
474
475    #[inline]
476    #[must_use]
477    pub fn is_show_col_stripes(&self) -> bool {
478        matches!(self.show_col_stripes, ShowStripes::Show)
479    }
480}