sheet-xlsx 0.1.4

XLSX codec: workbook, worksheets, shared strings, styles, calc chain
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
//! Package writing — `write_xlsx`.
//!
//! Emits the canonical minimal `SpreadsheetML` package: content types, root
//! relationships, `xl/workbook.xml`, per-sheet worksheet parts, the
//! shared-string table, a canonical minimal `styles.xml` (see [`crate::styles`]),
//! and a freshly derived `xl/calcChain.xml` whenever any cell carries a
//! formula ([`crate::calc_chain`]). All text is XML-escaped; output is
//! deterministic — same workbook in, byte-identical package out.

use crate::calc_chain::CalcChain;
use crate::error::XlsxError;
use crate::{SheetData, XlsxValue, XlsxWorkbook};
use quick_xml::events::BytesText;
use quick_xml::Writer;
use sheet_core::refs::cell_name;
use std::fmt::Write as FmtWrite;
use std::io::Write as IoWrite;
use std::string::String;
use std::vec::Vec;

/// `SpreadsheetML` main namespace.
const NS_MAIN: &str = "http://schemas.openxmlformats.org/spreadsheetml/2006/main";
/// Package relationships namespace.
const NS_RELS: &str = "http://schemas.openxmlformats.org/package/2006/relationships";
/// Office document relationship type base.
const NS_OFFICE_REL: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";

/// Serializes `workbook` into XLSX bytes.
///
/// # Errors
///
/// - [`XlsxError::NonFiniteNumber`] for `NaN`/infinite cell numbers.
/// - [`XlsxError::SharedStringRange`] when a cell's shared-string index
///   points past [`XlsxWorkbook::shared_strings`].
/// - [`XlsxError::Zip`] if container serialization fails.
pub fn write_xlsx(workbook: &XlsxWorkbook) -> Result<Vec<u8>, XlsxError> {
    validate(workbook)?;

    let cursor = std::io::Cursor::new(Vec::new());
    let mut zip = zip::ZipWriter::new(cursor);
    // Default compression is Deflated (the `deflate` feature is on).
    let options: zip::write::SimpleFileOptions = zip::write::SimpleFileOptions::default();

    let sheet_count = workbook.sheets.len();
    let has_formulas = workbook
        .sheets
        .iter()
        .any(|s| s.cells.values().any(|c| c.formula.is_some()));
    // rId allocation: 1..=N sheets, then styles, then sharedStrings, then calcChain.
    let rid_styles = sheet_count + 1;
    let rid_shared = sheet_count + 2;
    let rid_calc = sheet_count + 3;

    let mut add = |name: &str, bytes: &[u8]| -> Result<(), XlsxError> {
        zip.start_file(name, options).map_err(XlsxError::Zip)?;
        zip.write_all(bytes)
            .map_err(|e| XlsxError::Zip(zip::result::ZipError::Io(e)))
    };

    add(
        "[Content_Types].xml",
        &content_types(sheet_count, has_formulas),
    )?;
    add("_rels/.rels", &root_rels())?;
    add(
        "xl/workbook.xml",
        &workbook_xml(workbook, rid_styles, rid_shared, rid_calc, has_formulas),
    )?;
    add(
        "xl/_rels/workbook.xml.rels",
        &workbook_rels(sheet_count, rid_styles, rid_shared, rid_calc, has_formulas),
    )?;
    for (pos, sheet) in workbook.sheets.iter().enumerate() {
        add(&sheet_path(pos), &worksheet_xml(sheet, workbook)?)?;
    }
    add("xl/styles.xml", &styles_xml(workbook.styles))?;
    add("xl/sharedStrings.xml", &shared_strings_xml(workbook))?;
    if has_formulas {
        add(
            "xl/calcChain.xml",
            &calc_chain_xml(&CalcChain::from_workbook(workbook)),
        )?;
    }

    let finished = zip.finish().map_err(XlsxError::Zip)?;
    Ok(finished.into_inner())
}

/// Cross-checks the workbook before serialization.
fn validate(workbook: &XlsxWorkbook) -> Result<(), XlsxError> {
    for sheet in &workbook.sheets {
        for (coord, cell) in &sheet.cells {
            if let XlsxValue::Number(n) = cell.value {
                if !n.is_finite() {
                    return Err(XlsxError::NonFiniteNumber(n));
                }
            }
            if let XlsxValue::SharedString(index) = cell.value {
                if usize::try_from(index).is_ok_and(|i| i >= workbook.shared_strings.len()) {
                    return Err(XlsxError::SharedStringRange {
                        index,
                        len: workbook.shared_strings.len(),
                    });
                }
            }
            let _ = coord; // coordinates are total (u32, u32)
        }
    }
    Ok(())
}

/// `xl/worksheets/sheetN.xml` for a 0-based sheet position (package path).
fn sheet_path(pos: usize) -> String {
    format!("xl/worksheets/sheet{}.xml", pos + 1)
}

/// Worksheet target relative to the workbook part's directory (`xl/`).
fn sheet_rel_target(pos: usize) -> String {
    format!("worksheets/sheet{}.xml", pos + 1)
}

/// Serializes an XML element with text content into a fresh buffer.
fn element(name: &str, attrs: &[(&str, &str)], content: &str) -> Vec<u8> {
    let mut buf = Vec::new();
    let mut w = Writer::new(&mut buf);
    let mut el = w.create_element(name);
    for (k, v) in attrs {
        el = el.with_attribute((*k, *v));
    }
    let _ = el.write_text_content(BytesText::from_escaped(quick_xml::escape::escape(content)));
    buf
}

/// `[Content_Types].xml`.
fn content_types(sheet_count: usize, has_formulas: bool) -> Vec<u8> {
    let mut overrides = String::new();
    overrides.push_str(
        &element(
            "Override",
            &[
                ("PartName", "/xl/workbook.xml"),
                (
                    "ContentType",
                    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml",
                ),
            ],
            "",
        )
        .lossy_string(),
    );
    for pos in 0..sheet_count {
        overrides.push_str(
            &element(
                "Override",
                &[
                    ("PartName", &format!("/{}", sheet_path(pos))),
                    (
                        "ContentType",
                        "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml",
                    ),
                ],
                "",
            )
            .lossy_string(),
        );
    }
    overrides.push_str(
        &element(
            "Override",
            &[
                ("PartName", "/xl/styles.xml"),
                (
                    "ContentType",
                    "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml",
                ),
            ],
            "",
        )
        .lossy_string(),
    );
    overrides.push_str(
        &element(
            "Override",
            &[
                ("PartName", "/xl/sharedStrings.xml"),
                (
                    "ContentType",
                    "application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml",
                ),
            ],
            "",
        )
        .lossy_string(),
    );
    if has_formulas {
        overrides.push_str(
            &element(
                "Override",
                &[
                    ("PartName", "/xl/calcChain.xml"),
                    (
                        "ContentType",
                        "application/vnd.openxmlformats-officedocument.spreadsheetml.calcChain+xml",
                    ),
                ],
                "",
            )
            .lossy_string(),
        );
    }

    let mut out = String::from("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
    out.push_str("<Types xmlns=\"http://schemas.openxmlformats.org/package/2006/content-types\">");
    out.push_str(
        "<Default Extension=\"rels\" ContentType=\"application/vnd.openxmlformats-package.relationships+xml\"/>",
    );
    out.push_str("<Default Extension=\"xml\" ContentType=\"application/xml\"/>");
    out.push_str(&overrides);
    out.push_str("</Types>");
    out.into_bytes()
}

/// `_rels/.rels` — the package-level office-document pointer.
fn root_rels() -> Vec<u8> {
    let rel = element(
        "Relationship",
        &[
            ("Id", "rId1"),
            ("Type", &format!("{NS_OFFICE_REL}/{OFFICE_DOCUMENT}")),
            ("Target", "xl/workbook.xml"),
        ],
        "",
    )
    .lossy_string();
    let mut out = String::from("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
    let _ = write!(out, "<Relationships xmlns=\"{NS_RELS}\">");
    out.push_str(&rel);
    out.push_str("</Relationships>");
    out.into_bytes()
}

/// Relationship-type local names.
const OFFICE_DOCUMENT: &str = "officeDocument";
const WORKSHEET: &str = "worksheet";
const SHARED_STRINGS: &str = "sharedStrings";
const STYLES: &str = "styles";
const CALC_CHAIN: &str = "calcChain";

/// `xl/workbook.xml`.
fn workbook_xml(
    workbook: &XlsxWorkbook,
    rid_styles: usize,
    rid_shared: usize,
    rid_calc: usize,
    has_formulas: bool,
) -> Vec<u8> {
    let mut out = String::from("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
    let _ = write!(
        out,
        "<workbook xmlns=\"{NS_MAIN}\" xmlns:r=\"{NS_OFFICE_REL}\"><sheets>"
    );
    for (pos, sheet) in workbook.sheets.iter().enumerate() {
        let rid = pos + 1;
        let name_escaped = quick_xml::escape::escape(&sheet.name).into_owned();
        let _ = write!(
            out,
            "<sheet name=\"{name_escaped}\" sheetId=\"{}\" r:id=\"rId{rid}\"/>",
            pos + 1
        );
    }
    out.push_str("</sheets>");
    if has_formulas {
        let _ = (rid_calc, rid_shared, rid_styles); // rIds are allocated uniformly
        out.push_str("<calcPr calcId=\"0\"/>");
    }
    out.push_str("</workbook>");
    out.into_bytes()
}

/// `xl/_rels/workbook.xml.rels`.
fn workbook_rels(
    sheet_count: usize,
    rid_styles: usize,
    rid_shared: usize,
    rid_calc: usize,
    has_formulas: bool,
) -> Vec<u8> {
    let mut body = String::new();
    for pos in 0..sheet_count {
        let rid = pos + 1;
        body.push_str(
            &element(
                "Relationship",
                &[
                    ("Id", &format!("rId{rid}")),
                    ("Type", &format!("{NS_OFFICE_REL}/{WORKSHEET}")),
                    ("Target", &sheet_rel_target(pos)),
                ],
                "",
            )
            .lossy_string(),
        );
    }
    body.push_str(
        &element(
            "Relationship",
            &[
                ("Id", &format!("rId{rid_styles}")),
                ("Type", &format!("{NS_OFFICE_REL}/{STYLES}")),
                ("Target", "styles.xml"),
            ],
            "",
        )
        .lossy_string(),
    );
    body.push_str(
        &element(
            "Relationship",
            &[
                ("Id", &format!("rId{rid_shared}")),
                ("Type", &format!("{NS_OFFICE_REL}/{SHARED_STRINGS}")),
                ("Target", "sharedStrings.xml"),
            ],
            "",
        )
        .lossy_string(),
    );
    if has_formulas {
        body.push_str(
            &element(
                "Relationship",
                &[
                    ("Id", &format!("rId{rid_calc}")),
                    ("Type", &format!("{NS_OFFICE_REL}/{CALC_CHAIN}")),
                    ("Target", "calcChain.xml"),
                ],
                "",
            )
            .lossy_string(),
        );
    }
    let mut out = String::from("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
    let _ = write!(out, "<Relationships xmlns=\"{NS_RELS}\">");
    out.push_str(&body);
    out.push_str("</Relationships>");
    out.into_bytes()
}

/// Number formatting for `<v>`: shortest round-trip decimal, no exponent
/// padding, integral values without a fractional part.
fn format_number(n: f64) -> String {
    // The 1e15 bound guarantees the f64→i64 cast is lossless (2^53 ≈ 9e15),
    // so the truncation cannot happen by construction.
    #[allow(clippy::cast_possible_truncation)]
    if n.fract() == 0.0_f64 && n.abs() < 1e15 {
        format!("{}", n as i64)
    } else {
        format!("{n}")
    }
}

/// One worksheet part.
fn worksheet_xml(sheet: &SheetData, workbook: &XlsxWorkbook) -> Result<Vec<u8>, XlsxError> {
    let mut out = String::from("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
    let _ = write!(out, "<worksheet xmlns=\"{NS_MAIN}\">");
    if let Some(((min_row, min_col), (max_row, max_col))) = used_range_of(sheet) {
        let _ = write!(
            out,
            "<dimension ref=\"{}:{}\"/>",
            cell_name((min_row, min_col)),
            cell_name((max_row, max_col))
        );
    }
    out.push_str("<sheetData>");

    // Cells iterate row-major; group consecutive same-row cells.
    let mut row_open = false;
    let mut current_row: Option<u32> = None;
    for (&(row, col), cell) in &sheet.cells {
        if current_row != Some(row) {
            if row_open {
                out.push_str("</row>");
            }
            let _ = write!(out, "<row r=\"{}\">", row + 1);
            row_open = true;
            current_row = Some(row);
        }
        out.push_str(&cell_xml(row, col, cell, workbook)?);
    }
    if row_open {
        out.push_str("</row>");
    }
    out.push_str("</sheetData></worksheet>");
    Ok(out.into_bytes())
}

/// Bounding box of present cells (`sheet_core`-style).
fn used_range_of(sheet: &SheetData) -> Option<((u32, u32), (u32, u32))> {
    let mut min = (u32::MAX, u32::MAX);
    let mut max = (0u32, 0u32);
    for &(r, c) in sheet.cells.keys() {
        min = (min.0.min(r), min.1.min(c));
        max = (max.0.max(r), max.1.max(c));
    }
    sheet.cells.keys().next().map(|_| (min, max))
}

/// One `<c>` element (no row context).
fn cell_xml(
    row: u32,
    col: u32,
    cell: &crate::XlsxCell,
    workbook: &XlsxWorkbook,
) -> Result<String, XlsxError> {
    let reference = cell_name((row, col));
    let mut attrs = String::from("r=\"");
    attrs.push_str(&reference);
    attrs.push('"');
    if let Some(style) = cell.style_index {
        let _ = write!(attrs, " s=\"{style}\"");
    }

    let body = match &cell.value {
        XlsxValue::Number(n) => {
            if !n.is_finite() {
                return Err(XlsxError::NonFiniteNumber(*n));
            }
            format!("<v>{}</v>", format_number(*n))
        }
        XlsxValue::SharedString(index) => {
            if usize::try_from(*index).is_ok_and(|i| i >= workbook.shared_strings.len()) {
                return Err(XlsxError::SharedStringRange {
                    index: *index,
                    len: workbook.shared_strings.len(),
                });
            }
            attrs.push_str(" t=\"s\"");
            format!("<v>{index}</v>")
        }
        XlsxValue::InlineString(text) => {
            attrs.push_str(" t=\"inlineStr\"");
            let escaped = quick_xml::escape::escape(text);
            format!("<is><t xml:space=\"preserve\">{escaped}</t></is>")
        }
        XlsxValue::Boolean(b) => {
            attrs.push_str(" t=\"b\"");
            format!("<v>{}</v>", u8::from(*b))
        }
        XlsxValue::Error(text) => {
            attrs.push_str(" t=\"e\"");
            let escaped = quick_xml::escape::escape(text);
            format!("<v>{escaped}</v>")
        }
        XlsxValue::FormulaString(text) => {
            attrs.push_str(" t=\"str\"");
            let escaped = quick_xml::escape::escape(text);
            format!("<v>{escaped}</v>")
        }
    };

    let formula = match &cell.formula {
        Some(f) => {
            let escaped = quick_xml::escape::escape(f);
            format!("<f>{escaped}</f>")
        }
        None => String::new(),
    };

    Ok(format!("<c {attrs}>{formula}{body}</c>"))
}

/// Canonical minimal `styles.xml` with the modeled `cellXfs` count. A count
/// of zero is schema-valid (`count` is optional and unsigned); faithfulness
/// beats Excel's usual ≥1 convention here, so round-trips are exact.
fn styles_xml(styles: crate::Styles) -> Vec<u8> {
    let count = styles.cell_xfs;
    let mut xfs = String::new();
    for _ in 0..count {
        xfs.push_str("<xf fontId=\"0\" fillId=\"0\" borderId=\"0\" xfId=\"0\"/>");
    }
    let mut out = String::from("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
    let _ = write!(out, "<styleSheet xmlns=\"{NS_MAIN}\">");
    out.push_str("<fonts count=\"1\"><font><sz val=\"11\"/><name val=\"Calibri\"/></font></fonts>");
    out.push_str("<fills count=\"2\"><fill><patternFill patternType=\"none\"/></fill><fill><patternFill patternType=\"gray125\"/></fill></fills>");
    out.push_str("<borders count=\"1\"><border/></borders>");
    out.push_str(
        "<cellStyleXfs count=\"1\"><xf fontId=\"0\" fillId=\"0\" borderId=\"0\"/></cellStyleXfs>",
    );
    let _ = write!(out, "<cellXfs count=\"{count}\">{xfs}</cellXfs>");
    out.push_str("</styleSheet>");
    out.into_bytes()
}

/// `xl/sharedStrings.xml`.
fn shared_strings_xml(workbook: &XlsxWorkbook) -> Vec<u8> {
    let count = workbook.shared_strings.len();
    let mut items = String::new();
    for text in workbook.shared_strings.iter() {
        let escaped = quick_xml::escape::escape(text);
        let _ = write!(items, "<si><t xml:space=\"preserve\">{escaped}</t></si>");
    }
    let mut out = String::from("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
    let _ = write!(
        out,
        "<sst xmlns=\"{NS_MAIN}\" count=\"{count}\" uniqueCount=\"{count}\">"
    );
    out.push_str(&items);
    out.push_str("</sst>");
    out.into_bytes()
}

/// `xl/calcChain.xml` from the derived chain.
fn calc_chain_xml(chain: &CalcChain) -> Vec<u8> {
    let mut entries = String::new();
    for entry in &chain.entries {
        let _ = write!(
            entries,
            "<c r=\"{}\" i=\"{}\"/>",
            entry.reference, entry.sheet_index
        );
    }
    let mut out = String::from("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
    let _ = write!(out, "<calcChain xmlns=\"{NS_MAIN}\">");
    out.push_str(&entries);
    out.push_str("</calcChain>");
    out.into_bytes()
}

/// Helper: `Vec<u8>` → lossy `String` for embedding pre-rendered elements.
trait LossyString {
    fn lossy_string(&self) -> String;
}

impl LossyString for Vec<u8> {
    fn lossy_string(&self) -> String {
        String::from_utf8_lossy(self).into_owned()
    }
}

#[cfg(test)]
mod tests {
    #![allow(
        clippy::unwrap_used,
        clippy::expect_used,
        clippy::panic,
        clippy::indexing_slicing
    )]

    use super::{format_number, sheet_path};

    #[test]
    fn number_formatting() {
        assert_eq!(format_number(41.0), "41");
        assert_eq!(format_number(-7.0), "-7");
        assert_eq!(format_number(1.5), "1.5");
        assert_eq!(format_number(0.1), "0.1");
        assert_eq!(format_number(1e20), "100000000000000000000");
    }

    #[test]
    fn sheet_paths_are_one_based() {
        assert_eq!(sheet_path(0), "xl/worksheets/sheet1.xml");
        assert_eq!(sheet_path(9), "xl/worksheets/sheet10.xml");
    }
}