rustypyxl 0.7.1

Fast, dependency-light Rust library for reading and writing Excel (XLSX) files
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
//! Read-only pivot-table model. Pivot tables are preserved verbatim on save
//! (see [`crate::workbook::PivotArtifacts`]); this module parses those raw parts
//! into a structured view -- source range, cache fields, and the field
//! placements (row/column/data/page areas) -- matching openpyxl's read support.
//! It does not build or write pivot tables.

use crate::workbook::{resolve_rel_target, PivotArtifacts};
use quick_xml::events::Event;
use std::collections::HashMap;

/// A data field in a pivot table (the values area).
#[derive(Clone, Debug, PartialEq)]
pub struct PivotDataField {
    /// Display name, e.g. "Sum of Sales".
    pub name: String,
    /// The cache field it aggregates.
    pub source_field: String,
    /// Aggregation: sum, count, average, max, min, product, countNums, stdDev,
    /// stdDevp, var, varp. Excel's default when unspecified is "sum".
    pub subtotal: String,
}

/// A read-only view of one pivot table.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct PivotTableInfo {
    /// Pivot table name.
    pub name: String,
    /// The cache id it draws from, if declared.
    pub cache_id: Option<u32>,
    /// The cell range the pivot occupies on its sheet, e.g. "A3:D12".
    pub location: Option<String>,
    /// Source data sheet name.
    pub source_sheet: Option<String>,
    /// Source data range, e.g. "A1:D100".
    pub source_ref: Option<String>,
    /// The cache field names, in source-column order.
    pub cache_fields: Vec<String>,
    /// Fields placed in the row area (a data placeholder shows as "Values").
    pub row_fields: Vec<String>,
    /// Fields placed in the column area.
    pub col_fields: Vec<String>,
    /// Fields placed in the report-filter (page) area.
    pub page_fields: Vec<String>,
    /// Fields in the values area with their aggregation.
    pub data_fields: Vec<PivotDataField>,
}

/// Parse every pivot table in a workbook's preserved artifacts into a read-only
/// model. Returns an empty vector when there are no pivot tables.
pub fn parse_pivot_tables(artifacts: &PivotArtifacts) -> Vec<PivotTableInfo> {
    let by_path: HashMap<&str, &[u8]> = artifacts
        .parts
        .iter()
        .map(|(p, b)| (p.as_str(), b.as_slice()))
        .collect();

    let mut out = Vec::new();
    // Deterministic order: iterate the pivotTable parts by path.
    let mut table_paths: Vec<&str> = by_path
        .keys()
        .copied()
        .filter(|p| p.starts_with("xl/pivotTables/pivotTable") && p.ends_with(".xml"))
        .collect();
    table_paths.sort_unstable();

    for path in table_paths {
        let bytes = by_path[path];
        let raw = parse_pivot_table_xml(bytes);

        // Resolve this pivot table's cache definition through its .rels part.
        let rels_path = table_rels_path(path);
        let (source_sheet, source_ref, cache_fields) = by_path
            .get(rels_path.as_str())
            .and_then(|rels| first_cache_definition_target(rels))
            .map(|target| resolve_rel_target(path, &target))
            .and_then(|cache_path| by_path.get(cache_path.as_str()).copied())
            .map(parse_cache_definition_xml)
            .unwrap_or_default();

        out.push(combine(raw, source_sheet, source_ref, cache_fields));
    }
    out
}

/// The .rels path for a pivotTable part.
fn table_rels_path(table_path: &str) -> String {
    match table_path.rfind('/') {
        Some(idx) => format!(
            "{}/_rels/{}.rels",
            &table_path[..idx],
            &table_path[idx + 1..]
        ),
        None => format!("_rels/{}.rels", table_path),
    }
}

/// Read the pivotCacheDefinition target from a pivotTable .rels part.
fn first_cache_definition_target(rels_xml: &[u8]) -> Option<String> {
    let mut reader = quick_xml::Reader::from_reader(rels_xml);
    let mut buf = Vec::new();
    loop {
        match reader.read_event_into(&mut buf) {
            Ok(Event::Empty(e)) | Ok(Event::Start(e))
                if e.local_name().as_ref() == b"Relationship" =>
            {
                let (mut typ, mut target) = (None, None);
                for attr in e.attributes().flatten() {
                    let val = attr.unescape_value().ok().map(|v| v.into_owned());
                    match attr.key.local_name().as_ref() {
                        b"Type" => typ = val,
                        b"Target" => target = val,
                        _ => {}
                    }
                }
                if typ.is_some_and(|t| t.ends_with("pivotCacheDefinition")) {
                    return target;
                }
            }
            Ok(Event::Eof) | Err(_) => return None,
            _ => {}
        }
        buf.clear();
    }
}

/// Extract the source sheet/range and the ordered cache field names from a
/// pivotCacheDefinition part.
fn parse_cache_definition_xml(xml: &[u8]) -> (Option<String>, Option<String>, Vec<String>) {
    let mut reader = quick_xml::Reader::from_reader(xml);
    reader.config_mut().trim_text(true);
    let mut buf = Vec::new();

    let mut sheet = None;
    let mut reference = None;
    let mut fields = Vec::new();
    let mut in_cache_fields = false;

    loop {
        match reader.read_event_into(&mut buf) {
            Ok(Event::Start(e)) | Ok(Event::Empty(e)) => match e.local_name().as_ref() {
                b"worksheetSource" => {
                    for attr in e.attributes().flatten() {
                        let val = attr.unescape_value().ok().map(|v| v.into_owned());
                        match attr.key.local_name().as_ref() {
                            b"sheet" => sheet = val,
                            b"ref" => reference = val,
                            _ => {}
                        }
                    }
                }
                b"cacheFields" => in_cache_fields = true,
                b"cacheField" if in_cache_fields => {
                    let name = e
                        .attributes()
                        .flatten()
                        .find(|a| a.key.local_name().as_ref() == b"name")
                        .and_then(|a| a.unescape_value().ok().map(|v| v.into_owned()))
                        .unwrap_or_default();
                    fields.push(name);
                }
                _ => {}
            },
            Ok(Event::End(e)) if e.local_name().as_ref() == b"cacheFields" => {
                in_cache_fields = false;
            }
            Ok(Event::Eof) | Err(_) => break,
            _ => {}
        }
        buf.clear();
    }
    (sheet, reference, fields)
}

/// The placement of a pivot field, as read from the pivotTable part before cache
/// field names are joined in.
#[derive(Default)]
struct RawPivotTable {
    name: String,
    cache_id: Option<u32>,
    location: Option<String>,
    /// Row-area field indices (a `-2` is the data-values placeholder).
    row_x: Vec<i32>,
    col_x: Vec<i32>,
    /// Report-filter field indices.
    page_fld: Vec<i32>,
    /// (display name, source field index, subtotal) for each data field.
    data: Vec<(String, i32, String)>,
}

/// Which `<*Fields>` container the parser is currently inside.
enum Container {
    None,
    Row,
    Col,
    Page,
}

/// Parse a pivotTable part into raw field placements.
fn parse_pivot_table_xml(xml: &[u8]) -> RawPivotTable {
    let mut reader = quick_xml::Reader::from_reader(xml);
    reader.config_mut().trim_text(true);
    let mut buf = Vec::new();

    let mut raw = RawPivotTable::default();
    let mut container = Container::None;

    let attr = |e: &quick_xml::events::BytesStart, key: &[u8]| -> Option<String> {
        e.attributes()
            .flatten()
            .find(|a| a.key.local_name().as_ref() == key)
            .and_then(|a| a.unescape_value().ok().map(|v| v.into_owned()))
    };

    loop {
        match reader.read_event_into(&mut buf) {
            Ok(Event::Start(e)) | Ok(Event::Empty(e)) => match e.local_name().as_ref() {
                b"pivotTableDefinition" => {
                    raw.name = attr(&e, b"name").unwrap_or_default();
                    raw.cache_id = attr(&e, b"cacheId").and_then(|v| v.parse().ok());
                }
                b"location" => raw.location = attr(&e, b"ref"),
                b"rowFields" => container = Container::Row,
                b"colFields" => container = Container::Col,
                b"pageFields" => container = Container::Page,
                b"field" => {
                    if let Some(x) = attr(&e, b"x").and_then(|v| v.parse::<i32>().ok()) {
                        match container {
                            Container::Row => raw.row_x.push(x),
                            Container::Col => raw.col_x.push(x),
                            _ => {}
                        }
                    }
                }
                b"pageField" => {
                    if let Some(fld) = attr(&e, b"fld").and_then(|v| v.parse::<i32>().ok()) {
                        raw.page_fld.push(fld);
                    }
                }
                b"dataField" => {
                    let name = attr(&e, b"name").unwrap_or_default();
                    let fld = attr(&e, b"fld")
                        .and_then(|v| v.parse::<i32>().ok())
                        .unwrap_or(-1);
                    let subtotal = attr(&e, b"subtotal").unwrap_or_else(|| "sum".to_string());
                    raw.data.push((name, fld, subtotal));
                }
                _ => {}
            },
            Ok(Event::End(e)) => match e.local_name().as_ref() {
                b"rowFields" | b"colFields" | b"pageFields" => container = Container::None,
                _ => {}
            },
            Ok(Event::Eof) | Err(_) => break,
            _ => {}
        }
        buf.clear();
    }
    raw
}

// ---------- creation (phase 3) ----------

const NS_MAIN: &str = "http://schemas.openxmlformats.org/spreadsheetml/2006/main";
const NS_REL: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
const NS_PKG_REL: &str = "http://schemas.openxmlformats.org/package/2006/relationships";

/// A request to build a new pivot table's parts. Indices are 0-based positions
/// into `field_names`; `col_indices` may include `-2`, Excel's data-values
/// placeholder.
pub struct PivotBuildRequest<'a> {
    /// Cache id, shared between the workbook `<pivotCache>` and the pivotTable.
    pub cache_id: u32,
    /// Part-file number for the cache (pivotCacheDefinition{n}, records{n}).
    pub cache_num: u32,
    /// Part-file number for the pivotTable.
    pub table_num: u32,
    /// Pivot table name.
    pub name: &'a str,
    /// Source data sheet name.
    pub source_sheet: &'a str,
    /// Source data range, e.g. "A1:C100".
    pub source_ref: &'a str,
    /// Where the pivot is placed, e.g. "F1:J20".
    pub location_ref: &'a str,
    /// The source header field names, in column order.
    pub field_names: &'a [String],
    /// Row-area field indices.
    pub row_indices: &'a [i32],
    /// Column-area field indices (may include -2 for the data placeholder).
    pub col_indices: &'a [i32],
    /// (field index, display name, subtotal) for each data field.
    pub data_fields: &'a [(usize, String, String)],
    /// The workbook relationship id placeholder for the cache (phase-1 renumbers).
    pub cache_rel_id: &'a str,
    /// The sheet relationship id for the pivotTable.
    pub sheet_rel_id: &'a str,
}

/// The parts and wiring produced for a new pivot table.
pub struct BuiltPivot {
    /// The five package parts as (path, bytes).
    pub parts: Vec<(String, Vec<u8>)>,
    /// The `<pivotCache …/>` child to add to the workbook `<pivotCaches>`.
    pub caches_child: String,
    /// The workbook.xml.rels entry as (id, target).
    pub workbook_rel: (String, String),
    /// The source sheet's rels entry as (id, type, target).
    pub sheet_rel: (String, String, String),
}

fn escape(s: &str) -> String {
    s.replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
}

/// Generate the parts for a new pivot table. Excel rebuilds the cache from the
/// source on open (`refreshOnLoad`), so the records part is empty and pivot
/// field items are omitted.
pub fn build_pivot(req: PivotBuildRequest) -> BuiltPivot {
    let cache_def_path = format!("xl/pivotCache/pivotCacheDefinition{}.xml", req.cache_num);
    let cache_rec_path = format!("xl/pivotCache/pivotCacheRecords{}.xml", req.cache_num);
    let cache_rels_path = format!(
        "xl/pivotCache/_rels/pivotCacheDefinition{}.xml.rels",
        req.cache_num
    );
    let table_path = format!("xl/pivotTables/pivotTable{}.xml", req.table_num);
    let table_rels_path = format!("xl/pivotTables/_rels/pivotTable{}.xml.rels", req.table_num);

    // cacheFields
    let mut cache_fields = String::new();
    for name in req.field_names {
        cache_fields.push_str(&format!(
            r#"<cacheField name="{}" numFmtId="0"><sharedItems/></cacheField>"#,
            escape(name)
        ));
    }
    let cache_def = format!(
        concat!(
            r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>"#,
            r#"<pivotCacheDefinition xmlns="{m}" xmlns:r="{r}" r:id="rId1" refreshOnLoad="1" recordCount="0">"#,
            r#"<cacheSource type="worksheet"><worksheetSource ref="{sref}" sheet="{sheet}"/></cacheSource>"#,
            r#"<cacheFields count="{fcount}">{fields}</cacheFields>"#,
            r#"</pivotCacheDefinition>"#
        ),
        m = NS_MAIN,
        r = NS_REL,
        sref = escape(req.source_ref),
        sheet = escape(req.source_sheet),
        fcount = req.field_names.len(),
        fields = cache_fields
    );

    let cache_rec = format!(
        r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><pivotCacheRecords xmlns="{}" count="0"/>"#,
        NS_MAIN
    );

    let cache_rels = format!(
        concat!(
            r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>"#,
            r#"<Relationships xmlns="{p}">"#,
            r#"<Relationship Id="rId1" Type="{r}/pivotCacheRecords" Target="pivotCacheRecords{n}.xml"/>"#,
            r#"</Relationships>"#
        ),
        p = NS_PKG_REL,
        r = NS_REL,
        n = req.cache_num
    );

    // pivotFields: one per source field, tagged by placement.
    let row_set: std::collections::HashSet<i32> = req.row_indices.iter().copied().collect();
    let col_set: std::collections::HashSet<i32> = req.col_indices.iter().copied().collect();
    let data_set: std::collections::HashSet<usize> =
        req.data_fields.iter().map(|(i, _, _)| *i).collect();
    let mut pivot_fields = String::new();
    for i in 0..req.field_names.len() {
        let idx = i as i32;
        if row_set.contains(&idx) {
            pivot_fields.push_str(r#"<pivotField axis="axisRow" showAll="0"/>"#);
        } else if col_set.contains(&idx) {
            pivot_fields.push_str(r#"<pivotField axis="axisCol" showAll="0"/>"#);
        } else if data_set.contains(&i) {
            pivot_fields.push_str(r#"<pivotField dataField="1" showAll="0"/>"#);
        } else {
            pivot_fields.push_str(r#"<pivotField showAll="0"/>"#);
        }
    }

    let row_fields = if req.row_indices.is_empty() {
        String::new()
    } else {
        let inner: String = req
            .row_indices
            .iter()
            .map(|x| format!(r#"<field x="{}"/>"#, x))
            .collect();
        format!(
            r#"<rowFields count="{}">{}</rowFields>"#,
            req.row_indices.len(),
            inner
        )
    };
    let col_fields = if req.col_indices.is_empty() {
        String::new()
    } else {
        let inner: String = req
            .col_indices
            .iter()
            .map(|x| format!(r#"<field x="{}"/>"#, x))
            .collect();
        format!(
            r#"<colFields count="{}">{}</colFields>"#,
            req.col_indices.len(),
            inner
        )
    };
    let data_fields = if req.data_fields.is_empty() {
        String::new()
    } else {
        let inner: String = req
            .data_fields
            .iter()
            .map(|(fld, name, subtotal)| {
                format!(
                    r#"<dataField name="{}" fld="{}" subtotal="{}" baseField="0" baseItem="0"/>"#,
                    escape(name),
                    fld,
                    escape(subtotal)
                )
            })
            .collect();
        format!(
            r#"<dataFields count="{}">{}</dataFields>"#,
            req.data_fields.len(),
            inner
        )
    };

    let first_data_col = req.row_indices.len().max(1);
    let table = format!(
        concat!(
            r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>"#,
            r#"<pivotTableDefinition xmlns="{m}" name="{name}" cacheId="{cid}" applyNumberFormats="0" "#,
            r#"applyBorderFormats="0" applyFontFormats="0" applyPatternFormats="0" applyAlignmentFormats="0" "#,
            r#"applyWidthHeightFormats="1" dataCaption="Values" updatedVersion="6" minRefreshableVersion="3" "#,
            r#"useAutoFormatting="1" itemPrintTitles="1" createdVersion="6" indent="0" outline="1" "#,
            r#"outlineData="1" multipleFieldFilters="0">"#,
            r#"<location ref="{loc}" firstHeaderRow="1" firstDataRow="2" firstDataCol="{fdc}"/>"#,
            r#"<pivotFields count="{fcount}">{pfields}</pivotFields>"#,
            r#"{rowf}{colf}{dataf}"#,
            r#"<pivotTableStyleInfo name="PivotStyleLight16" showRowHeaders="1" showColHeaders="1" "#,
            r#"showRowStripes="0" showColStripes="0" showLastColumn="1"/>"#,
            r#"</pivotTableDefinition>"#
        ),
        m = NS_MAIN,
        name = escape(req.name),
        cid = req.cache_id,
        loc = escape(req.location_ref),
        fdc = first_data_col,
        fcount = req.field_names.len(),
        pfields = pivot_fields,
        rowf = row_fields,
        colf = col_fields,
        dataf = data_fields
    );

    let table_rels = format!(
        concat!(
            r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>"#,
            r#"<Relationships xmlns="{p}">"#,
            r#"<Relationship Id="rId1" Type="{r}/pivotCacheDefinition" Target="../pivotCache/pivotCacheDefinition{n}.xml"/>"#,
            r#"</Relationships>"#
        ),
        p = NS_PKG_REL,
        r = NS_REL,
        n = req.cache_num
    );

    BuiltPivot {
        parts: vec![
            (cache_def_path, cache_def.into_bytes()),
            (cache_rec_path, cache_rec.into_bytes()),
            (cache_rels_path, cache_rels.into_bytes()),
            (table_path, table.into_bytes()),
            (table_rels_path, table_rels.into_bytes()),
        ],
        caches_child: format!(
            r#"<pivotCache cacheId="{}" r:id="{}"/>"#,
            req.cache_id, req.cache_rel_id
        ),
        workbook_rel: (
            req.cache_rel_id.to_string(),
            format!("pivotCache/pivotCacheDefinition{}.xml", req.cache_num),
        ),
        sheet_rel: (
            req.sheet_rel_id.to_string(),
            format!("{}/pivotTable", NS_REL),
            format!("../pivotTables/pivotTable{}.xml", req.table_num),
        ),
    }
}

/// Map a field index to its cache field name; `-2` is Excel's data-values
/// placeholder, other out-of-range indices fall back to a positional label.
fn field_name(index: i32, fields: &[String]) -> String {
    if index == -2 {
        "Values".to_string()
    } else if index >= 0 && (index as usize) < fields.len() {
        fields[index as usize].clone()
    } else {
        format!("Field{}", index)
    }
}

/// Join raw field placements with the cache field names into the public model.
fn combine(
    raw: RawPivotTable,
    source_sheet: Option<String>,
    source_ref: Option<String>,
    cache_fields: Vec<String>,
) -> PivotTableInfo {
    let row_fields = raw
        .row_x
        .iter()
        .map(|x| field_name(*x, &cache_fields))
        .collect();
    let col_fields = raw
        .col_x
        .iter()
        .map(|x| field_name(*x, &cache_fields))
        .collect();
    let page_fields = raw
        .page_fld
        .iter()
        .map(|x| field_name(*x, &cache_fields))
        .collect();
    let data_fields = raw
        .data
        .into_iter()
        .map(|(name, fld, subtotal)| {
            let source_field = field_name(fld, &cache_fields);
            PivotDataField {
                name: if name.is_empty() {
                    format!("{} of {}", subtotal, source_field)
                } else {
                    name
                },
                source_field,
                subtotal,
            }
        })
        .collect();

    PivotTableInfo {
        name: raw.name,
        cache_id: raw.cache_id,
        location: raw.location,
        source_sheet,
        source_ref,
        cache_fields,
        row_fields,
        col_fields,
        page_fields,
        data_fields,
    }
}