Registry / RepositoryTypeScriptRustPython
Rollpie
ReadmeFiles
Versions
Info
Download
0.1.199.9 KB2026-09-230.1.098.9 KB2026-09-14
Version
0.1.1
Copyright
Rollpie, Irohabook
Publisher
math
Published
2026-09-23
Size
99.9 KB
Downloads
15
Checksum
e691f02f331393c7b5c3406746caa7ed2636c7af24b1cef422bc957ed16ecd74
Dependencies
None

style.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//! 書式
//!
//! セルの `s` は `xl/styles.xml` の `cellXfs` の何番目かを指し、そこから表示形式の番号 (numFmtId) が決まる。
//! folio が読むのはこの一本の道だけである。フォントや罫線は読まない。
//!
//! # 日付かどうか
//!
//! xlsx のセルに日付という型はない。日付は数で入っていて、表示形式が日付なら日付として読む。
//! だから [`Style::is_date`] を見ずに数だけ取ると、日付は 45000 のような数になる。

use crate::error::Error;
use crate::xml::{Event, Reader};
use std::collections::HashMap;


/// 決まった番号のうち、日付や時刻を表すもの
const DATE_FORMAT: [u32; 25] = [
    14, 15, 16, 17, 18, 19, 20, 21, 22, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 45, 46, 47, 50, 57,
    58,
];


/// styles.xml から読んだ、書式の対応
pub struct Style {
    /// cellXfs の各項目が指す表示形式の番号
    format: Vec<u32>,
    /// そのファイルが自前で決めた表示形式
    code: HashMap<u32, String>,
}


impl Style {
    /// 何も入っていない対応
    pub fn new() -> Style {
        Style {format: Vec::new(), code: HashMap::new()}
    }

    /// styles.xml を読む
    ///
    /// `<xf>` は `cellXfs` と `cellStyleXfs` の両方に出てくる。セルが指すのは `cellXfs` のほうだけである。
    pub fn read(reader: &mut Reader) -> Result<Style, Error> {
        let mut result = Style::new();
        let mut inside = false;

        while let Some(event) = reader.next()? {
            if event == Event::Close {
                if reader.name() == "cellXfs" {
                    inside = false;
                }

                continue;
            }

            if event != Event::Open && event != Event::Empty {
                continue;
            }

            match reader.name() {
                "cellXfs" => inside = event == Event::Open,

                "numFmt" => {
                    let id = number(reader.attribute("numFmtId"));
                    let code = reader.attribute("formatCode").unwrap_or("").to_string();
                    result.code.insert(id, code);
                }

                "xf" => {
                    if inside {
                        result.format.push(number(reader.attribute("numFmtId")));
                    }
                }

                _ => {}
            }
        }

        Ok(result)
    }

    /// 表示形式の文字列を足す
    ///
    /// xlsx では styles.xml から読むが、旧形式では FORMAT レコードから来る。
    pub fn put_format(&mut self, id: u32, code: &str) {
        self.code.insert(id, code.to_string());
    }

    /// 書式の並びに 1 つ足す。足した順が、セルの指す番号になる
    pub fn put_xf(&mut self, format: u32) {
        self.format.push(format);
    }

    /// cellXfs の位置から表示形式の番号を引く
    pub fn format_of(&self, style: u32) -> u32 {
        match self.format.get(style as usize) {
            Some(value) => *value,
            None => 0,
        }
    }

    /// cellXfs の位置から表示形式の文字列を引く
    pub fn code_of(&self, style: u32) -> Option<&str> {
        let id = self.format_of(style);

        if let Some(code) = self.code.get(&id) {
            return Some(code);
        }

        builtin(id)
    }

    /// その書式のセルを日付として読むべきか
    pub fn is_date(&self, style: u32) -> bool {
        let id = self.format_of(style);

        if DATE_FORMAT.contains(&id) {
            return true;
        }

        // 自前の表示形式は、文字列を見て決める
        match self.code.get(&id) {
            Some(code) => has_date(code),
            None => false,
        }
    }
}


impl Default for Style {
    fn default() -> Style {
        Style::new()
    }
}


/// 表示形式の文字列に、日付や時刻を出す指定が入っているか
///
/// 引用符と `\` の後ろ、`[]` の中は飾りなので見ない。
fn has_date(code: &str) -> bool {
    let mut quoted = false;
    let mut bracket = false;
    let mut escaped = false;

    for value in code.chars() {
        if escaped {
            escaped = false;
            continue;
        }

        match value {
            '\\' => escaped = true,
            '"' => quoted = !quoted,
            '[' if !quoted => bracket = true,
            ']' if !quoted => bracket = false,
            _ if quoted || bracket => {}
            'y' | 'Y' | 'd' | 'D' | 'h' | 'H' | 's' | 'S' | 'm' | 'M' => return true,
            _ => {}
        }
    }

    false
}


/// 数として読む。読めなければ 0
fn number(value: Option<&str>) -> u32 {
    match value {
        Some(text) => text.trim().parse().unwrap_or(0),
        None => 0,
    }
}


/// はじめから決まっている表示形式
pub fn builtin(id: u32) -> Option<&'static str> {
    let code = match id {
        0 => "General",
        1 => "0",
        2 => "0.00",
        3 => "#,##0",
        4 => "#,##0.00",
        9 => "0%",
        10 => "0.00%",
        11 => "0.00E+00",
        12 => "# ?/?",
        13 => "# ??/??",
        14 => "mm-dd-yy",
        15 => "d-mmm-yy",
        16 => "d-mmm",
        17 => "mmm-yy",
        18 => "h:mm AM/PM",
        19 => "h:mm:ss AM/PM",
        20 => "h:mm",
        21 => "h:mm:ss",
        22 => "m/d/yy h:mm",
        37 => "#,##0 ;(#,##0)",
        38 => "#,##0 ;[Red](#,##0)",
        39 => "#,##0.00;(#,##0.00)",
        40 => "#,##0.00;[Red](#,##0.00)",
        45 => "mm:ss",
        46 => "[h]:mm:ss",
        47 => "mmss.0",
        48 => "##0.0E+0",
        49 => "@",
        _ => return None,
    };

    Some(code)
}