docparser-office 0.1.0

docparser Office 后端:DOCX / XLSX / PPTX 解析(zip + quick-xml 流式)
Documentation
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
//! XLSX 解析:ZIP + quick-xml 流式文本提取。
//!
//! 架构:`XlsxRawBackend` -> `ParseCapability`,支持共享字符串表、样式格式化、日期格式。

use chrono::NaiveDate;
use docparser_core::error::{DocError, Result};
use docparser_core::plugin::{ParseCapability, RawDoc, RawPage};
use quick_xml::events::Event;
use quick_xml::Reader;

use crate::zip_read_str;

/// XLSX raw 后端。
pub struct XlsxRawBackend;

impl ParseCapability for XlsxRawBackend {
    fn id(&self) -> &str {
        "office-raw"
    }
    fn formats(&self) -> &[&str] {
        &["xlsx"]
    }
    fn parse(&self, path: &str) -> Result<RawDoc> {
        let file = std::fs::File::open(path)
            .map_err(|e| DocError::corrupt(format!("cannot open {path}: {e}")))?;
        let mut zip = zip::ZipArchive::new(file)
            .map_err(|e| DocError::corrupt(format!("ZIP open failed: {e}")))?;

        // 读取 workbook.xml 获取 sheet 列表
        let wb_xml = zip_read_str(&mut zip, "xl/workbook.xml")?
            .ok_or_else(|| DocError::corrupt("missing xl/workbook.xml"))?;
        let sheet_names = parse_sheet_names(&wb_xml)?;

        // 读取 SharedStrings(惰性)
        let sst = SharedStrings::new(&mut zip)?;

        // 读取 Styles(惰性)
        let styles = Styles::new(&mut zip)?;

        // 解析每个 sheet
        let mut pages = Vec::new();
        for (idx, (_rid, name)) in sheet_names.iter().enumerate() {
            // 读取 sheet 的 XML
            let sheet_path = format!("xl/worksheets/sheet{}.xml", idx + 1);
            let xml = zip_read_str(&mut zip, &sheet_path)?.unwrap_or_default();
            let rows = parse_sheet_rows(&xml, &sst, &styles)?;
            let text = rows
                .iter()
                .map(|r| r.join(","))
                .collect::<Vec<_>>()
                .join("\n");
            pages.push(RawPage {
                name: Some(name.clone()),
                items: vec![docparser_core::plugin::RawTextItem { text, bbox: None }],
                ..Default::default()
            });
        }

        Ok(RawDoc {
            pages,
            ..Default::default()
        })
    }
}

/// 共享字符串表。
struct SharedStrings {
    items: Vec<String>,
}

impl SharedStrings {
    fn new(zip: &mut zip::ZipArchive<std::fs::File>) -> Result<Self> {
        let xml = match zip_read_str(zip, "xl/sharedStrings.xml")? {
            Some(s) => s,
            None => {
                return Ok(SharedStrings { items: Vec::new() });
            }
        };
        let items = parse_sst_items(&xml)?;
        Ok(SharedStrings { items })
    }

    fn get(&self, idx: usize) -> Option<&str> {
        self.items.get(idx).map(|s| s.as_str())
    }
}

/// 解析 `<sst>` 中的 `<si>` 条目。
fn parse_sst_items(xml: &str) -> Result<Vec<String>> {
    let mut reader = Reader::from_str(xml);
    // 不裁剪文本:空白由消费者 trim(实体两侧空格需保留,见 GeneralRef 分支)
    let mut buf = Vec::new();
    let mut items = Vec::new();
    let mut in_t = false;
    let mut text = String::new();

    loop {
        match reader.read_event_into(&mut buf) {
            Ok(Event::Start(ref e)) => match e.name().local_name().as_ref() {
                crate::tags::SI => {
                    text.clear();
                }
                crate::tags::T => {
                    in_t = true;
                }
                _ => {}
            },
            Ok(Event::End(ref e)) => match e.name().local_name().as_ref() {
                crate::tags::SI => {
                    items.push(text.trim().to_string());
                    text.clear();
                }
                crate::tags::T => {
                    in_t = false;
                }
                crate::tags::SST => {
                    break;
                }
                _ => {}
            },
            Ok(Event::Text(ref e)) => {
                if in_t {
                    // quick-xml 0.38+:Text 已是解码后的字符串,实体需显式 unescape
                    let raw = e.xml10_content();
                    if let Ok(t) = quick_xml::escape::unescape(&raw) {
                        text.push_str(&t);
                    }
                }
            }
            Ok(Event::GeneralRef(ref e)) => {
                if in_t {
                    crate::push_general_ref(&mut text, e);
                }
            }
            Ok(Event::CData(ref e)) => {
                if in_t {
                    text.push_str(e.as_ref());
                }
            }
            Ok(Event::Eof) => break,
            Err(e) => return Err(DocError::corrupt(format!("SST parse error: {e}"))),
            _ => {}
        }
        buf.clear();
    }
    Ok(items)
}

/// 单元格样式元数据。
#[derive(Default)]
struct Styles {
    num_fmts: std::collections::HashMap<usize, String>,
    xf_num_fmt_ids: Vec<usize>,
}

impl Styles {
    fn new(zip: &mut zip::ZipArchive<std::fs::File>) -> Result<Self> {
        let xml = match zip_read_str(zip, "xl/styles.xml")? {
            Some(s) => s,
            None => return Ok(Styles::default()),
        };
        let mut s = Styles::default();
        s.parse(&xml)?;
        Ok(s)
    }

    fn parse(&mut self, xml: &str) -> Result<()> {
        let mut reader = Reader::from_str(xml);
        // 不裁剪文本:空白由消费者 trim(实体两侧空格需保留,见 GeneralRef 分支)
        let mut buf = Vec::new();
        let mut _in_num_fmts = false;
        let mut _in_cell_xfs = false;
        let mut _num_fmt_id_val: Option<usize> = None;

        loop {
            match reader.read_event_into(&mut buf) {
                Ok(Event::Start(ref e)) => match e.name().local_name().as_ref() {
                    crate::tags::NUM_FMT => {
                        _num_fmt_id_val = None;
                        let mut fmt_id = 0usize;
                        let mut fmt_code = String::new();
                        for attr in e.attributes().filter_map(|a| a.ok()) {
                            match attr.key.as_ref() {
                                "numFmtId" => fmt_id = attr.value.parse().unwrap_or(0),
                                "formatCode" => fmt_code = attr.value.to_string(),
                                _ => {}
                            }
                        }
                        if fmt_id >= 164 {
                            self.num_fmts.insert(fmt_id, fmt_code);
                        }
                    }
                    crate::tags::XF => {
                        let mut num_fmt_id = 0usize;
                        for attr in e.attributes().filter_map(|a| a.ok()) {
                            if attr.key.as_ref() == "numFmtId" {
                                num_fmt_id = attr.value.parse().unwrap_or(0);
                            }
                        }
                        self.xf_num_fmt_ids.push(num_fmt_id);
                    }
                    _ => {}
                },
                Ok(Event::Empty(ref e)) => match e.name().local_name().as_ref() {
                    crate::tags::NUM_FMT => {
                        let mut fmt_id = 0usize;
                        let mut fmt_code = String::new();
                        for attr in e.attributes().filter_map(|a| a.ok()) {
                            match attr.key.as_ref() {
                                "numFmtId" => fmt_id = attr.value.parse().unwrap_or(0),
                                "formatCode" => fmt_code = attr.value.to_string(),
                                _ => {}
                            }
                        }
                        if fmt_id >= 164 {
                            self.num_fmts.insert(fmt_id, fmt_code);
                        }
                    }
                    crate::tags::XF => {
                        let mut num_fmt_id = 0usize;
                        for attr in e.attributes().filter_map(|a| a.ok()) {
                            if attr.key.as_ref() == "numFmtId" {
                                num_fmt_id = attr.value.parse().unwrap_or(0);
                            }
                        }
                        self.xf_num_fmt_ids.push(num_fmt_id);
                    }
                    _ => {}
                },
                Ok(Event::End(ref e)) => {
                    if e.name().local_name().as_ref() == crate::tags::CELL_XFS {
                        _in_cell_xfs = false;
                    }
                }
                Ok(Event::Eof) => break,
                Err(e) => return Err(DocError::corrupt(format!("Styles parse error: {e}"))),
                _ => {}
            }
            buf.clear();
        }
        Ok(())
    }

    /// 判断指定 xf 索引是否为日期格式
    fn is_date_format(&self, xf_idx: Option<usize>) -> bool {
        let num_fmt_id = xf_idx
            .and_then(|i| self.xf_num_fmt_ids.get(i))
            .copied()
            .unwrap_or(0);

        // 内置日期格式 ID: 14-22, 27-36, 45-47, 50-58, 71-81
        matches!(num_fmt_id,
            14..=22 | 27..=36 | 45..=47 | 50..=58 | 71..=81
        ) || self.num_fmts.contains_key(&num_fmt_id)
    }

    /// 获取指定 xf 索引的格式代码字符串(返回 OOXML 格式代码如 "yyyy-mm-dd")。
    fn code_for(&self, xf_idx: Option<usize>) -> Option<&str> {
        let num_fmt_id = xf_idx
            .and_then(|i| self.xf_num_fmt_ids.get(i))
            .copied()
            .unwrap_or(0);
        if num_fmt_id == 0 {
            return None;
        }
        if let Some(code) = self.num_fmts.get(&num_fmt_id) {
            return Some(code.as_str());
        }
        BUILTIN_FORMATS
            .iter()
            .find(|(id, _)| *id == num_fmt_id)
            .map(|(_, code)| *code)
    }
}

/// OOXML 内置日期格式映射(与 Python ooxml.py BUILTIN_FORMATS 对齐)。
const BUILTIN_FORMATS: &[(usize, &str)] = &[
    (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"),
    (45, "mm:ss"),
    (46, "[h]:mm:ss"),
    (47, "mmss.0"),
];

/// OLE Automation Date 序列号转 NaiveDate。Epoch = Dec 30, 1899。
fn ole_serial_to_date(serial: f64) -> Option<NaiveDate> {
    let epoch = NaiveDate::from_ymd_opt(1899, 12, 30)?;
    let mut days = serial as i64;
    // Lotus 123 bug:serial 60 (Feb 29, 1900) 不存在
    if serial > 0.0 && serial < 60.0 {
        days += 1;
    }
    epoch.checked_add_signed(chrono::Duration::days(days))
}

/// 将 OOXML 日期格式代码转换为 strftime 格式。
fn fmt_to_strftime(fmt: &str) -> String {
    let is_12h = fmt.contains("AM/PM");
    let s = fmt
        .replace("yyyy", "%Y")
        .replace("yy", "%y")
        .replace("mmm", "%b")
        .replace("dd", "%d");
    // 区分分钟 mm 与月份 mm(用占位符避免二次替换,与 Python ooxml.py 一致)
    let s = s.replace(":mm", ":M_M").replace("mm:", "M_M:");
    let s = s
        .replace("mm", "=*=")
        .replace("m", "=*=")
        .replace("=*=", "%m");
    let s = s.replace("M_M", "%M");
    let s = s.replace("ss", "%S").replace("AM/PM", "%p");
    if is_12h {
        s.replace("h", "%I")
    } else {
        s.replace("h", "%H")
    }
}

/// 格式化 Excel 日期(OLE 序列号 + 格式代码 → 字符串)。
fn format_excel_date(serial: f64, format_code: &str) -> String {
    if let Some(date) = ole_serial_to_date(serial) {
        let strfmt = fmt_to_strftime(format_code);
        date.format(&strfmt).to_string()
    } else {
        format!("num:{}", serial)
    }
}

/// 格式化数字:去科学记数法、去前导零,与 Python 的 `str()` 一致。
fn format_number(raw: &str) -> String {
    let trimmed = raw.trim();
    if trimmed.is_empty() {
        return String::new();
    }
    if let Ok(num) = trimmed.parse::<f64>() {
        if num.fract() == 0.0 && num.is_finite() {
            format!("{}", num as i64)
        } else {
            format!("{}", num)
        }
    } else {
        trimmed.to_string()
    }
}

/// 解析 workbook.xml 获取 sheet 名称列表。
fn parse_sheet_names(xml: &str) -> Result<Vec<(String, String)>> {
    let mut reader = Reader::from_str(xml);
    // 不裁剪文本:空白由消费者 trim(实体两侧空格需保留,见 GeneralRef 分支)
    let mut buf = Vec::new();
    let mut sheets = Vec::new();

    loop {
        match reader.read_event_into(&mut buf) {
            Ok(Event::Start(ref e)) | Ok(Event::Empty(ref e)) => {
                if e.name().local_name().as_ref() == "sheet" {
                    let mut rid = String::new();
                    let mut name = String::new();
                    for attr in e.attributes().filter_map(|a| a.ok()) {
                        // 属性按完整 QName 比较(`r:id` 与裸 `id` 区分,与迁移前一致)
                        match attr.key.as_ref() {
                            "r:id" => rid = attr.value.to_string(),
                            "name" => name = attr.value.to_string(),
                            // 某些 xlsx 使用短 id 属性
                            "id" if rid.is_empty() => rid = attr.value.to_string(),
                            _ => {}
                        }
                    }
                    if !name.is_empty() {
                        sheets.push((rid, name));
                    }
                }
            }
            Ok(Event::End(ref e)) => {
                if e.name().local_name().as_ref() == "workbook" {
                    break;
                }
            }
            Ok(Event::Eof) => break,
            Err(e) => return Err(DocError::corrupt(format!("workbook parse error: {e}"))),
            _ => {}
        }
        buf.clear();
    }
    Ok(sheets)
}

/// 解析 sheet 中的行/列单元格。
fn parse_sheet_rows(xml: &str, sst: &SharedStrings, styles: &Styles) -> Result<Vec<Vec<String>>> {
    let mut reader = Reader::from_str(xml);
    // 不裁剪文本:空白由消费者 trim(实体两侧空格需保留,见 GeneralRef 分支)
    let mut buf = Vec::new();
    let mut rows: Vec<Vec<String>> = Vec::new();
    let mut current_row: Vec<(usize, String)> = Vec::new(); // (col_idx, text)
    let mut cell_ref = String::new();
    let mut cell_type = 's'; // s = shared string, n = number, b = boolean, e = error, str = inline
    let mut cell_style: Option<usize> = None;
    let mut cell_value = String::new();
    let mut in_v = false;
    let mut in_is = false;
    let mut in_is_t = false;

    // regex 用于解析列号:如 "A1" -> 0, "Z1" -> 25, "AA1" -> 26
    fn col_from_ref(r: &str) -> usize {
        let letters: String = r.chars().take_while(|c| c.is_ascii_alphabetic()).collect();
        let mut col = 0usize;
        for c in letters.chars() {
            col = col * 26 + (c.to_ascii_uppercase() as u8 - b'A' + 1) as usize;
        }
        if col > 0 {
            col - 1
        } else {
            0
        }
    }

    loop {
        match reader.read_event_into(&mut buf) {
            Ok(Event::Start(ref e)) => {
                match e.name().local_name().as_ref() {
                    crate::tags::C => {
                        cell_type = 's';
                        cell_style = None;
                        cell_value.clear();
                        cell_ref.clear();
                        for attr in e.attributes().filter_map(|a| a.ok()) {
                            match attr.key.as_ref() {
                                "r" => cell_ref = attr.value.to_string(),
                                "t" => {
                                    // t: s=shared string, n=number, b=boolean, e=error, str=inlineStr
                                    cell_type = match attr.value.as_ref() {
                                        "s" => 's',                 // shared string
                                        "n" => 'n',                 // number
                                        "b" => 'b',                 // boolean
                                        "e" => 'e',                 // error
                                        "str" | "inlineStr" => 'i', // inline string
                                        _ => 's',
                                    };
                                }
                                "s" => cell_style = attr.value.parse().ok(),
                                _ => {}
                            }
                        }
                    }
                    crate::tags::V => {
                        in_v = true;
                    }
                    crate::tags::IS => {
                        in_is = true;
                    }
                    crate::tags::T if in_is => in_is_t = true,
                    _ => {}
                }
            }
            Ok(Event::End(ref e)) => {
                match e.name().local_name().as_ref() {
                    crate::tags::C => {
                        // 处理单元格值
                        let text = match cell_type {
                            's' => {
                                let idx: usize = cell_value.trim().parse().unwrap_or(0);
                                sst.get(idx).unwrap_or("").to_string()
                            }
                            'i' => {
                                // inline string: <is><t>...</t></is>,文本已在 cell_value 中
                                cell_value.trim().to_string()
                            }
                            'b' => {
                                // 匹配 Python: True/False 而非 TRUE/FALSE
                                if cell_value.trim() == "1" {
                                    "True".to_string()
                                } else {
                                    "False".to_string()
                                }
                            }
                            'e' => {
                                // 匹配 Python: 前缀 "ERROR:"
                                format!("ERROR:{}", cell_value.trim())
                            }
                            // 'n' or default: 纯数字或日期
                            _ => {
                                let val = cell_value.trim();
                                if val.is_empty() {
                                    String::new()
                                } else if styles.is_date_format(cell_style) {
                                    // 日期:按格式代码格式化
                                    if let Ok(num) = val.parse::<f64>() {
                                        if let Some(code) = styles.code_for(cell_style) {
                                            format_excel_date(num, code)
                                        } else {
                                            format!("num:{}", val)
                                        }
                                    } else {
                                        format!("num:{}", val)
                                    }
                                } else {
                                    // 数值:去科学记数法、去前导零
                                    format_number(val)
                                }
                            }
                        };

                        let col = col_from_ref(&cell_ref);
                        current_row.push((col, text));
                        in_v = false;
                        in_is = false;
                        in_is_t = false;
                    }
                    crate::tags::ROW => {
                        // 排序并输出行
                        current_row.sort_by_key(|(col, _)| *col);
                        let row_str: Vec<String> =
                            current_row.iter().map(|(_, t)| t.clone()).collect();
                        rows.push(row_str);
                        current_row.clear();
                    }
                    crate::tags::V => {
                        in_v = false;
                    }
                    crate::tags::IS => {
                        in_is = false;
                    }
                    crate::tags::T if in_is_t => in_is_t = false,
                    _ => {}
                }
            }
            Ok(Event::Text(ref e)) => {
                if in_v || in_is_t {
                    let raw = e.xml10_content();
                    if let Ok(t) = quick_xml::escape::unescape(&raw) {
                        cell_value.push_str(&t);
                    }
                }
            }
            Ok(Event::GeneralRef(ref e)) => {
                if in_v || in_is_t {
                    crate::push_general_ref(&mut cell_value, e);
                }
            }
            Ok(Event::CData(ref e)) => {
                if in_v || in_is_t {
                    cell_value.push_str(e.as_ref());
                }
            }
            Ok(Event::Eof) => break,
            Err(e) => return Err(DocError::corrupt(format!("sheet parse error: {e}"))),
            _ => {}
        }
        buf.clear();
    }
    Ok(rows)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_sst_empty() {
        let sst = SharedStrings { items: vec![] };
        assert_eq!(sst.get(0), None);
    }

    #[test]
    fn test_sst_parse() {
        let xml = r#"<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="2">
          <si><t>Hello</t></si>
          <si><t>World</t></si>
        </sst>"#;
        let items = parse_sst_items(xml).unwrap();
        assert_eq!(items.len(), 2);
        assert_eq!(items[0], "Hello");
        assert_eq!(items[1], "World");
    }

    #[test]
    fn test_sheet_names() {
        let xml = r#"<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
          xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
          <sheets>
            <sheet name="Sheet1" r:id="rId1"/>
            <sheet name="Sheet2" r:id="rId2"/>
          </sheets>
        </workbook>"#;
        let sheets = parse_sheet_names(xml).unwrap();
        assert_eq!(sheets.len(), 2);
        assert_eq!(sheets[0].1, "Sheet1");
    }

    /// reader 迁移 smoke:最小 xlsx(workbook + sharedStrings + sheet),走完整 ZIP → 事件流路径。
    ///
    /// 覆盖 `local_name()` 元素匹配、`attr.value`(Cow<str>)取值与共享字符串实体解码。
    #[test]
    fn smoke_parse_minimal_xlsx_zip() {
        use std::io::Write;

        const WORKBOOK: &str = concat!(
            r#"<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" "#,
            r#"xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">"#,
            r#"<sheets><sheet name="S1" sheetId="1" r:id="rId1"/></sheets></workbook>"#
        );
        const SST: &str = concat!(
            r#"<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">"#,
            r#"<si><t>甲</t></si><si><t>乙 &amp; 丙</t></si></sst>"#
        );
        const SHEET: &str = concat!(
            r#"<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">"#,
            r#"<sheetData><row r="1">"#,
            r#"<c r="A1" t="s"><v>0</v></c><c r="B1" t="s"><v>1</v></c>"#,
            r#"</row></sheetData></worksheet>"#
        );

        let dir = std::env::temp_dir().join("docparser_office_xlsx_smoke");
        std::fs::create_dir_all(&dir).unwrap();
        let path = dir.join("min.xlsx");
        {
            let f = std::fs::File::create(&path).unwrap();
            let mut zw = zip::ZipWriter::new(f);
            let opts = zip::write::SimpleFileOptions::default();
            for (name, body) in [
                ("xl/workbook.xml", WORKBOOK),
                ("xl/sharedStrings.xml", SST),
                ("xl/worksheets/sheet1.xml", SHEET),
            ] {
                zw.start_file(name, opts).unwrap();
                zw.write_all(body.as_bytes()).unwrap();
            }
            zw.finish().unwrap();
        }

        let raw = XlsxRawBackend.parse(path.to_str().unwrap()).unwrap();
        std::fs::remove_file(&path).ok();

        assert_eq!(raw.pages.len(), 1);
        assert_eq!(raw.pages[0].name.as_deref(), Some("S1"));
        assert_eq!(raw.pages[0].items.len(), 1);
        assert_eq!(raw.pages[0].items[0].text, "甲,乙 & 丙");
    }

    #[test]
    fn test_col_ref() {
        fn col(r: &str) -> usize {
            let letters: String = r.chars().take_while(|c| c.is_ascii_alphabetic()).collect();
            let mut col = 0usize;
            for c in letters.chars() {
                col = col * 26 + (c.to_ascii_uppercase() as u8 - b'A' + 1) as usize;
            }
            if col > 0 {
                col - 1
            } else {
                0
            }
        }
        assert_eq!(col("A1"), 0);
        assert_eq!(col("Z1"), 25);
        assert_eq!(col("AA1"), 26);
        assert_eq!(col("AB1"), 27);
    }
}