Skip to main content

excel_cli/excel/
cell.rs

1#[derive(Clone)]
2pub struct Cell {
3    pub value: String,
4    pub formula: Option<String>,
5    pub is_formula: bool,
6    pub cell_type: CellType,
7    pub original_type: Option<DataTypeInfo>,
8}
9
10#[derive(Clone, PartialEq)]
11pub enum CellType {
12    Text,
13    Number,
14    Date,
15    Boolean,
16    Empty,
17}
18
19#[derive(Clone, PartialEq)]
20pub enum DataTypeInfo {
21    Empty,
22    String,
23    Float(f64),
24    Int(i64),
25    Bool(bool),
26    DateTime(f64),
27    Duration(f64),
28    DateTimeIso(String),
29    DurationIso(String),
30    Error,
31}
32
33impl Cell {
34    pub fn new(value: String, is_formula: bool) -> Self {
35        let cell_type = if value.is_empty() {
36            CellType::Empty
37        } else if is_formula {
38            CellType::Text
39        } else if value.parse::<f64>().is_ok() {
40            CellType::Number
41        } else if (value.contains('/') && value.split('/').count() == 3)
42            || (value.contains('-') && value.split('-').count() == 3)
43        {
44            CellType::Date
45        } else if value == "true" || value == "false" {
46            CellType::Boolean
47        } else {
48            CellType::Text
49        };
50
51        Self::new_with_type(value, is_formula, cell_type, None)
52    }
53
54    pub fn new_with_type(
55        value: String,
56        is_formula: bool,
57        cell_type: CellType,
58        original_type: Option<DataTypeInfo>,
59    ) -> Self {
60        Self {
61            value,
62            formula: None,
63            is_formula,
64            cell_type,
65            original_type,
66        }
67    }
68
69    pub fn empty() -> Self {
70        Self {
71            value: String::new(),
72            formula: None,
73            is_formula: false,
74            cell_type: CellType::Empty,
75            original_type: Some(DataTypeInfo::Empty),
76        }
77    }
78}