ferroxl 0.2.0

ferroxl: read and write Excel 2007 xlsx/xlsm files with an openpyxl-compatible API
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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
//! Writing worksheets to XML (`openpyxl/writer/worksheet.py`).
//!
//! The output is deliberately unindented and written with a streaming writer, matching what
//! the Python implementation produces via `XMLGenerator`. Element order follows the CT_Worksheet
//! schema, which Excel validates on load.

use std::collections::BTreeMap;

use crate::cell::cell::{Cell, CellValue, DataType};
use crate::cell::utils::{column_index_from_string, coordinate_from_string, get_column_letter};
use crate::exceptions::Result;
use crate::formatting::RULE_ATTRIBUTES;
use crate::worksheet::filters::SortCondition;
use crate::worksheet::{ColumnDimension, RowDimension, Worksheet};
use crate::writer::styles::{build_style_tables, StyleId, StyleTables};
use crate::xml::constants::{COMMENTS_NS, PKG_REL_NS, REL_NS, SHEET_MAIN_NS, VML_NS};
use crate::xml::functions::{fromstring, Element, XmlWriter};

/// Serialise a worksheet to its XML part.
///
/// `string_table` maps cell text to its shared-string index, and `style_tables` maps styles
/// to their `cellXfs` index.
///
/// This is the in-memory writer: it builds the whole part as a `String`. `save_dump` is the
/// streaming counterpart, for a sheet large enough that holding its XML matters. The two
/// produce identical bytes, which a test asserts.
pub fn write_worksheet(
    worksheet: &Worksheet,
    string_table: &crate::writer::strings::StringTable,
    style_tables: &StyleTables,
) -> Result<String> {
    let mut doc = XmlWriter::new();
    write_worksheet_head(&mut doc, worksheet, style_tables)?;
    write_sheet_rows(&mut doc, worksheet, string_table, style_tables)?;
    write_worksheet_tail(&mut doc, worksheet)?;
    Ok(doc.into_string())
}

/// The head of a worksheet part as a string, for the streaming writer.
///
/// A thin wrapper over `write_worksheet_head`. The head is bounded by the sheet's
/// configuration rather than by its cell count, so buffering it costs nothing that matters.
pub fn write_worksheet_head_to_string(
    worksheet: &Worksheet,
    style_tables: &StyleTables,
) -> Result<String> {
    let mut doc = XmlWriter::new();
    write_worksheet_head(&mut doc, worksheet, style_tables)?;
    Ok(doc.into_string())
}

/// The tail of a worksheet part as a string, from `</sheetData>` to the end.
pub fn write_worksheet_tail_to_string(worksheet: &Worksheet) -> Result<String> {
    let mut doc = XmlWriter::new();
    write_worksheet_tail(&mut doc, worksheet)?;
    Ok(doc.into_string())
}

/// Write everything up to and including the opening `<sheetData>`.
///
/// The part is split there and after the rows so the streaming writer can reuse both halves
/// unchanged. Neither grows with the size of the sheet's data: the head holds the
/// dimensions, the view and the column definitions, and the tail holds the merges,
/// validations and conditional formats.
fn write_worksheet_head(
    doc: &mut XmlWriter,
    worksheet: &Worksheet,
    style_tables: &StyleTables,
) -> Result<()> {
    doc.start_tag(
        "worksheet",
        [
            attr("xmlns", SHEET_MAIN_NS),
            attr(
                format!("xmlns:{}", crate::xml::constants::REL_PREFIX),
                REL_NS,
            ),
        ],
    );

    let vba_root = match &worksheet.xml_source {
        Some(source) => fromstring(source).ok(),
        None => None,
    };

    // sheetPr: a VBA workbook carries the sheet's code name, which VBA macros reference.
    match vba_root
        .as_ref()
        .and_then(|root| root.find(format!("{{{SHEET_MAIN_NS}}}sheetPr")))
        .and_then(|el| el.get("codeName"))
    {
        Some(code_name) => doc.start_tag("sheetPr", [("codeName", code_name)]),
        None if vba_root.is_some() => doc.start_tag("sheetPr", [("codeName", worksheet.title())]),
        None => doc.start_tag("sheetPr", [] as [(&str, &str); 0]),
    };
    doc.tag(
        "outlinePr",
        [
            ("summaryBelow", bool_str(worksheet.show_summary_below)),
            ("summaryRight", bool_str(worksheet.show_summary_right)),
        ],
        None,
    );
    if worksheet.page_setup.fit_to_page_enabled() {
        doc.tag("pageSetUpPr", [("fitToPage", "1")], None);
    }
    doc.end_tag("sheetPr");

    let dimension = worksheet.calculate_dimension()?;
    doc.tag("dimension", [("ref", &dimension)], None);
    write_sheet_views(doc, worksheet);
    doc.tag("sheetFormatPr", [("defaultRowHeight", "15")], None);
    write_cols(doc, worksheet, style_tables)?;
    doc.start_tag("sheetData", [] as [(&str, &str); 0]);
    Ok(())
}

/// Write everything after `</sheetData>`, and close the worksheet element.
///
/// `vba_root` is the sheet's original XML, kept from the head because a VBA workbook needs
/// the same parse for the code name and for the legacy drawing reference. Parsing the part
/// twice to avoid passing one value between two functions would cost more than it saves.
fn write_worksheet_tail(doc: &mut XmlWriter, worksheet: &Worksheet) -> Result<()> {
    doc.end_tag("sheetData");
    let vba_root = match &worksheet.xml_source {
        Some(source) => fromstring(source).ok(),
        None => None,
    };
    if worksheet.protection.enabled {
        let mut attributes = vec![("objects", "1"), ("scenarios", "1"), ("sheet", "1")];
        if !worksheet.protection.password().is_empty() {
            attributes.push(("password", worksheet.protection.password()));
        }
        doc.tag("sheetProtection", attributes, None);
    }

    write_auto_filter(doc, worksheet);
    write_merge_cells(doc, worksheet);
    write_data_validations(doc, worksheet);
    write_hyperlinks(doc, worksheet);
    write_conditional_formatting(doc, worksheet);

    let options = worksheet.page_setup.option_attributes();
    if !options.is_empty() {
        doc.tag("printOptions", attribute_refs(&options), None);
    }
    let margins = worksheet.page_margins.margin_attributes();
    if !margins.is_empty() {
        doc.tag("pageMargins", attribute_refs(&margins), None);
    }
    let setup = worksheet.page_setup.setup_attributes();
    if !setup.is_empty() {
        doc.tag("pageSetup", attribute_refs(&setup), None);
    }
    if worksheet.header_footer.has_header() || worksheet.header_footer.has_footer() {
        doc.start_tag("headerFooter", [] as [(&str, &str); 0]);
        if worksheet.header_footer.has_header() {
            doc.tag(
                "oddHeader",
                [] as [(&str, &str); 0],
                Some(&worksheet.header_footer.header_string()),
            );
        }
        if worksheet.header_footer.has_footer() {
            doc.tag(
                "oddFooter",
                [] as [(&str, &str); 0],
                Some(&worksheet.header_footer.footer_string()),
            );
        }
        doc.end_tag("headerFooter");
    }
    if !worksheet.charts.is_empty() || !worksheet.images.is_empty() {
        doc.tag(
            "drawing",
            [(format!("{}:id", crate::xml::constants::REL_PREFIX), "rId1")],
            None,
        );
    }
    let table_ids: Vec<String> = worksheet.table_relationship_ids();
    if let Some(parts) = crate::writer::table::write_table_parts(&table_ids) {
        doc.raw(&parts);
    }
    if let Some(root) = &vba_root {
        if let Some(legacy) = root.find(format!("{{{SHEET_MAIN_NS}}}legacyDrawing")) {
            if let Some(r_id) = legacy.get(format!("{{{REL_NS}}}id")) {
                doc.tag(
                    "legacyDrawing",
                    [(format!("{}:id", crate::xml::constants::REL_PREFIX), r_id)],
                    None,
                );
            }
        }
    }
    if !worksheet.page_breaks.is_empty() {
        let count = worksheet.page_breaks.len().to_string();
        doc.start_tag(
            "rowBreaks",
            [("count", &count), ("manualBreakCount", &count)],
        );
        for brk in &worksheet.page_breaks {
            doc.tag(
                "brk",
                [
                    attr("id", brk.to_string()),
                    attr("man", "true"),
                    attr("max", "16383"),
                    attr("min", "0"),
                ],
                None,
            );
        }
        doc.end_tag("rowBreaks");
    }
    if worksheet.comment_count() > 0 {
        doc.tag(
            "legacyDrawing",
            [(
                format!("{}:id", crate::xml::constants::REL_PREFIX),
                "commentsvml",
            )],
            None,
        );
    }
    doc.end_tag("worksheet");
    doc.end_document();
    Ok(())
}

fn bool_str(value: bool) -> &'static str {
    if value {
        "1"
    } else {
        "0"
    }
}

/// An owned attribute, so a literal and a computed value can share one array.
fn attr(key: impl Into<String>, value: impl Into<String>) -> (String, String) {
    (key.into(), value.into())
}

fn attribute_refs(attributes: &[(String, String)]) -> Vec<(&str, &str)> {
    attributes
        .iter()
        .map(|(k, v)| (k.as_str(), v.as_str()))
        .collect()
}

/// Borrow a `BTreeMap`'s entries as attribute pairs.
fn map_attributes(attributes: &std::collections::BTreeMap<String, String>) -> Vec<(&str, &str)> {
    attributes
        .iter()
        .map(|(k, v)| (k.as_str(), v.as_str()))
        .collect()
}

fn write_sheet_views(doc: &mut XmlWriter, worksheet: &Worksheet) {
    doc.start_tag("sheetViews", [] as [(&str, &str); 0]);
    doc.start_tag("sheetView", [("workbookViewId", "0")]);
    let mut selection: Vec<(String, String)> = Vec::new();
    if let Some(top_left) = worksheet.freeze_panes.clone() {
        if let Ok((column_letters, row)) = coordinate_from_string(&top_left) {
            if let Ok(column) = column_index_from_string(&column_letters) {
                let mut pane = "topRight".to_string();
                let mut attributes: Vec<(String, String)> = Vec::new();
                if column > 1 {
                    attributes.push(("xSplit".to_string(), (column - 1).to_string()));
                }
                if row > 1 {
                    attributes.push(("ySplit".to_string(), (row - 1).to_string()));
                    pane = "bottomLeft".to_string();
                    if column > 1 {
                        pane = "bottomRight".to_string();
                    }
                }
                attributes.push(("topLeftCell".to_string(), top_left.clone()));
                attributes.push(("activePane".to_string(), pane.clone()));
                attributes.push(("state".to_string(), "frozen".to_string()));
                doc.tag("pane", attribute_refs(&attributes), None);
                selection.push(("pane".to_string(), pane.clone()));
                if row > 1 && column > 1 {
                    doc.tag("selection", [("pane", "topRight")], None);
                    doc.tag("selection", [("pane", "bottomLeft")], None);
                }
            }
        }
    }
    selection.push(("activeCell".to_string(), worksheet.active_cell.clone()));
    selection.push(("sqref".to_string(), worksheet.selected_cell.clone()));
    doc.tag("selection", attribute_refs(&selection), None);
    doc.end_tag("sheetView");
    doc.end_tag("sheetViews");
}

fn write_cols(
    doc: &mut XmlWriter,
    worksheet: &Worksheet,
    style_tables: &StyleTables,
) -> Result<()> {
    if worksheet.column_dimensions.is_empty() {
        return Ok(());
    }
    // `<col>` elements come out in ascending column order, which is how Excel and openpyxl
    // both write them. The map is keyed by letters and `"Z"` sorts before `"AA"`, so the
    // entries have to be ordered by column number rather than by key. An unparseable key
    // sorts last and is reported by the `column_index_from_string` call below, which is
    // where the error belongs.
    let mut ordered: Vec<(&String, &ColumnDimension)> =
        worksheet.column_dimensions.iter().collect();
    ordered.sort_by_key(|(_, dimension)| dimension.column_index().unwrap_or(u32::from(u16::MAX)));
    doc.start_tag("cols", [] as [(&str, &str); 0]);
    for (letters, dimension) in ordered {
        let col_index = column_index_from_string(letters)?;
        let mut attributes: Vec<(String, String)> = vec![
            ("min".to_string(), col_index.to_string()),
            ("max".to_string(), col_index.to_string()),
        ];
        if dimension.width != -1.0 {
            attributes.push(("customWidth".to_string(), "1".to_string()));
        }
        if !dimension.visible {
            attributes.push(("hidden".to_string(), "true".to_string()));
        }
        if dimension.outline_level > 0 {
            attributes.push((
                "outlineLevel".to_string(),
                dimension.outline_level.to_string(),
            ));
        }
        if dimension.collapsed {
            attributes.push(("collapsed".to_string(), "true".to_string()));
        }
        if dimension.auto_size {
            attributes.push(("bestFit".to_string(), "true".to_string()));
        }
        let style = worksheet.get_style(letters);
        if worksheet.has_style(letters) {
            if let Some(id) = style_tables.id_for(&style) {
                attributes.push(("style".to_string(), id.0.to_string()));
            }
        }
        if dimension.width > 0.0 {
            attributes.push(("width".to_string(), format_number(dimension.width)));
        } else {
            attributes.push(("width".to_string(), "9.10".to_string()));
        }
        doc.tag("col", attribute_refs(&attributes), None);
    }
    doc.end_tag("cols");
    Ok(())
}

/// The `t` attribute a formula cell's cached value needs, if any.
///
/// `None` means no attribute, which is the numeric case: that is what a reader assumes without
/// being told. The other three are spelled differently from their non-formula counterparts --
/// notably a formula's text result is `str`, not the shared-string `s` a literal uses, because a
/// computed string was never put in the string table.
fn cached_value_type(value: &CellValue) -> Option<&'static str> {
    match value {
        CellValue::Bool(_) => Some("b"),
        CellValue::Text(_) => Some("str"),
        CellValue::Error(_) => Some("e"),
        _ => None,
    }
}

/// Format a float for XML: integral values lose the decimal point, as Python's `str` does.
fn format_number(value: f64) -> String {
    if value == value.trunc() {
        format!("{}", value as i64)
    } else {
        format!("{value}")
    }
}

/// Group a sheet's cells into rows, in column order within each row.
///
/// A worksheet stores cells by coordinate, and `"A10"` sorts before `"A2"`, so iteration
/// order is not row order. The rows come out ascending and the cells within each row
/// left to right, which is the order the schema wants.
pub fn group_rows(worksheet: &Worksheet) -> Vec<(u32, Vec<&Cell>)> {
    let mut by_row: BTreeMap<u32, Vec<&Cell>> = BTreeMap::new();
    for cell in worksheet.cells() {
        by_row.entry(cell.row).or_default().push(cell);
    }
    for cells in by_row.values_mut() {
        cells.sort_by_key(|cell| column_index_from_string(&cell.column).unwrap_or(0));
    }
    by_row.into_iter().collect()
}

fn write_sheet_rows(
    doc: &mut XmlWriter,
    worksheet: &Worksheet,
    string_table: &crate::writer::strings::StringTable,
    style_tables: &StyleTables,
) -> Result<()> {
    let max_column = worksheet.highest_column();
    let mut by_row: BTreeMap<u32, Vec<&Cell>> = group_rows(worksheet).into_iter().collect();
    // A row holding no cells still has to be written when it says something of its own: a
    // height, a hidden flag, an outline level or a style. `<row>` is the only element any
    // of those can live in, so a row present solely in `row_dimensions` would lose all of
    // them on save. openpyxl keeps such a row, and so must this.
    for (&index, dimension) in &worksheet.row_dimensions {
        if !by_row.contains_key(&index) && row_says_something(worksheet, index, dimension) {
            by_row.insert(index, Vec::new());
        }
    }
    for (row_index, cells) in by_row {
        write_row(
            doc,
            row_index,
            &cells,
            worksheet,
            string_table,
            style_tables,
            max_column,
        )?;
    }
    Ok(())
}

/// Whether a row dimension carries anything worth writing.
///
/// A row dimension with nothing set is not worth a `<row>` element of its own: it would say
/// nothing, and openpyxl does not write one. Only a difference from the defaults reaches
/// the file.
fn row_says_something(worksheet: &Worksheet, index: u32, dimension: &RowDimension) -> bool {
    dimension.height > 0.0
        || !dimension.visible
        || dimension.outline_level > 0
        || dimension.collapsed
        || worksheet.has_style(&index.to_string())
}

/// Write one `<row>` element and the cells in it.
pub fn write_row(
    doc: &mut XmlWriter,
    row_index: u32,
    cells: &[&Cell],
    worksheet: &Worksheet,
    string_table: &crate::writer::strings::StringTable,
    style_tables: &StyleTables,
    max_column: u32,
) -> Result<()> {
    let row_dimension = worksheet
        .row_dimensions
        .get(&row_index)
        .cloned()
        .unwrap_or_else(|| RowDimension::new(row_index));
    let mut attributes: Vec<(String, String)> = vec![
        ("r".to_string(), row_index.to_string()),
        ("spans".to_string(), format!("1:{max_column}")),
    ];
    if !row_dimension.visible {
        attributes.push(("hidden".to_string(), "1".to_string()));
    }
    if row_dimension.height > 0.0 {
        attributes.push(("ht".to_string(), format_number(row_dimension.height)));
        attributes.push(("customHeight".to_string(), "1".to_string()));
    }
    let row_key = row_index.to_string();
    if worksheet.has_style(&row_key) {
        if let Some(id) = style_tables.id_for(&worksheet.get_style(&row_key)) {
            attributes.push(("s".to_string(), id.0.to_string()));
            attributes.push(("customFormat".to_string(), "1".to_string()));
        }
    }
    doc.start_tag("row", attribute_refs(&attributes));

    for cell in cells {
        let coordinate = cell.coordinate();
        let value = cell.internal_value();
        let mut cell_attributes: Vec<(String, String)> =
            vec![("r".to_string(), coordinate.clone())];
        if cell.data_type != DataType::Formula {
            cell_attributes.push(("t".to_string(), cell.data_type.as_str().to_string()));
        } else if let Some(kind) = worksheet
            .cached_value(&coordinate)
            .and_then(cached_value_type)
        {
            // A formula's `<v>` holds a number unless told otherwise, so a text, boolean or
            // error result has to say which it is. Without this the reader would read the
            // cached value as a number and get something else entirely.
            cell_attributes.push(("t".to_string(), kind.to_string()));
        }
        if worksheet.has_style(&coordinate) {
            if let Some(id) = style_tables.id_for(&worksheet.get_style(&coordinate)) {
                cell_attributes.push(("s".to_string(), id.0.to_string()));
            }
        }

        if value.is_empty() {
            doc.tag("c", attribute_refs(&cell_attributes), None);
            continue;
        }

        doc.start_tag("c", attribute_refs(&cell_attributes));
        match cell.data_type {
            DataType::SharedString => {
                let text = value.as_text().unwrap_or_default().to_string();
                let index = string_table.get(&text).copied().unwrap_or(0);
                doc.tag("v", [] as [(&str, &str); 0], Some(&index.to_string()));
            }
            DataType::Formula => {
                if let Some(attributes) = &cell.formula_attributes {
                    let mut formula_attributes: Vec<(String, String)> = Vec::new();
                    if let Some(formula_type) = &attributes.formula_type {
                        formula_attributes.push(("t".to_string(), formula_type.clone()));
                    }
                    if let Some(si) = &attributes.si {
                        formula_attributes.push(("si".to_string(), si.clone()));
                    }
                    // A shared formula with no range is written as an empty `<f/>`:
                    // the followers inherit it from the group's master cell.
                    let bodyless = attributes.formula_type.as_deref() == Some("shared")
                        && attributes.reference.is_none();
                    if bodyless {
                        doc.tag("f", attribute_refs(&formula_attributes), None);
                    } else {
                        let body = value.as_text().unwrap_or_default();
                        let body = body.strip_prefix('=').unwrap_or(body).to_string();
                        let body = crate::formula::eval::prefix_future_functions(&body);
                        doc.tag("f", attribute_refs(&formula_attributes), Some(&body));
                    }
                } else {
                    let body = value.as_text().unwrap_or_default();
                    let body = body.strip_prefix('=').unwrap_or(body).to_string();
                    let body = crate::formula::eval::prefix_future_functions(&body);
                    doc.tag("f", [] as [(&str, &str); 0], Some(&body));
                }
                // The value half of a formula cell. Excel writes an empty `<v/>` when it has nothing cached,
                // and so did this until `recalculate` gave the field somewhere to go. The `t`
                // attribute on the `<c>` is what tells a reader which of the four storages the
                // text in `<v>` belongs to, so it has to be set here rather than on the cell.
                match worksheet.cached_value(&coordinate) {
                    None => {
                        doc.tag("v", [] as [(&str, &str); 0], None);
                    }
                    Some(CellValue::Number(number)) => {
                        doc.tag(
                            "v",
                            [] as [(&str, &str); 0],
                            Some(&crate::xml::functions::repr_float(*number)),
                        );
                    }
                    Some(CellValue::Bool(flag)) => {
                        doc.tag(
                            "v",
                            [] as [(&str, &str); 0],
                            Some(if *flag { "1" } else { "0" }),
                        );
                    }
                    Some(CellValue::Text(text)) => {
                        doc.tag("v", [] as [(&str, &str); 0], Some(text));
                    }
                    Some(CellValue::Error(code)) => {
                        doc.tag("v", [] as [(&str, &str); 0], Some(code));
                    }
                    Some(_) => {
                        // A blank or a date has no formula-cell representation this writer
                        // produces, so nothing is written rather than something misleading.
                        doc.tag("v", [] as [(&str, &str); 0], None);
                    }
                }
            }
            // An inline string keeps its text in `<is><t>`, not in `<v>`. Writing it into `<v>` is what
            // left a loaded workbook's text unreadable: a reader that follows the `t`
            // attribute looks in `<is>` and finds nothing, so the cell reads back empty.
            DataType::InlineString => {
                let text = value.as_text().unwrap_or_default().to_string();
                doc.start_tag("is", [] as [(&str, &str); 0]);
                doc.tag("t", [] as [(&str, &str); 0], Some(&text));
                doc.end_tag("is");
            }
            DataType::Numeric => match value {
                CellValue::Number(number) => {
                    doc.tag(
                        "v",
                        [] as [(&str, &str); 0],
                        Some(&crate::xml::functions::repr_float(*number)),
                    );
                }
                _ => {
                    doc.tag("v", [] as [(&str, &str); 0], value.as_text());
                }
            },
            DataType::Bool => {
                let text = match value {
                    CellValue::Bool(flag) => if *flag { "1" } else { "0" }.to_string(),
                    _ => "0".to_string(),
                };
                doc.tag("v", [] as [(&str, &str); 0], Some(&text));
            }
            _ => {
                let text = value.as_text().unwrap_or_default().to_string();
                doc.tag("v", [] as [(&str, &str); 0], Some(&text));
            }
        }
        doc.end_tag("c");
    }
    doc.end_tag("row");
    Ok(())
}

fn write_auto_filter(doc: &mut XmlWriter, worksheet: &Worksheet) {
    let filter = &worksheet.auto_filter;
    if filter.filter_columns().is_empty() && filter.sort_conditions().is_empty() {
        if let Some(reference) = filter.reference() {
            doc.tag("autoFilter", [("ref", reference)], None);
        }
        return;
    }
    let reference = filter.reference().unwrap_or_default().to_string();
    doc.start_tag("autoFilter", [("ref", &reference)]);
    for (col_id, column) in filter.filter_columns() {
        doc.start_tag("filterColumn", [("colId", &col_id.to_string())]);
        doc.start_tag("filters", attribute_refs(&column.filter_attributes()));
        for value in &column.vals {
            doc.tag("filter", [("val", value)], None);
        }
        doc.end_tag("filters");
        doc.end_tag("filterColumn");
    }
    if !filter.sort_conditions().is_empty() {
        doc.start_tag("sortState", [("ref", &reference)]);
        for condition in filter.sort_conditions() {
            // The element name is misspelled in the Python writer and Excel accepts it.
            doc.tag(
                "sortCondtion",
                attribute_refs(&sort_attributes(condition)),
                None,
            );
        }
        doc.end_tag("sortState");
    }
    doc.end_tag("autoFilter");
}

fn sort_attributes(condition: &SortCondition) -> Vec<(String, String)> {
    condition.attributes()
}

fn write_merge_cells(doc: &mut XmlWriter, worksheet: &Worksheet) {
    let merged = worksheet.merged_cells();
    if merged.is_empty() {
        return;
    }
    doc.start_tag("mergeCells", [("count", &merged.len().to_string())]);
    for range in merged {
        doc.tag("mergeCell", [("ref", range)], None);
    }
    doc.end_tag("mergeCells");
}

fn write_data_validations(doc: &mut XmlWriter, worksheet: &Worksheet) {
    let required: Vec<_> = worksheet
        .data_validations
        .iter()
        .filter(|validation| !validation.cells.is_empty() || !validation.ranges.is_empty())
        .collect();
    if required.is_empty() {
        return;
    }
    doc.start_tag("dataValidations", [("count", &required.len().to_string())]);
    for validation in required {
        doc.start_tag("dataValidation", map_attributes(&validation.attributes()));
        if !validation.formula1.is_empty() {
            doc.tag(
                "formula1",
                [] as [(&str, &str); 0],
                Some(&validation.formula1),
            );
        }
        if !validation.formula2.is_empty() {
            doc.tag(
                "formula2",
                [] as [(&str, &str); 0],
                Some(&validation.formula2),
            );
        }
        doc.end_tag("dataValidation");
    }
    doc.end_tag("dataValidations");
}

fn write_hyperlinks(doc: &mut XmlWriter, worksheet: &Worksheet) {
    let linked: Vec<&Cell> = worksheet.cells_with_hyperlinks().collect();
    if linked.is_empty() {
        return;
    }
    doc.start_tag("hyperlinks", [] as [(&str, &str); 0]);
    for cell in linked {
        let mut attributes: Vec<(String, String)> = vec![
            ("display".to_string(), cell.hyperlink().to_string()),
            ("ref".to_string(), cell.coordinate()),
            (
                format!("{}:id", crate::xml::constants::REL_PREFIX),
                cell.hyperlink_rel_id.clone().unwrap_or_default(),
            ),
        ];
        attributes.retain(|(_, value)| !value.is_empty());
        doc.tag("hyperlink", attribute_refs(&attributes), None);
    }
    doc.end_tag("hyperlinks");
}

fn write_conditional_formatting(doc: &mut XmlWriter, worksheet: &Worksheet) {
    for (range, rules) in &worksheet.conditional_formatting.cf_rules {
        if rules.is_empty() {
            continue;
        }
        doc.start_tag("conditionalFormatting", [("sqref", range)]);
        for rule in rules {
            let mut attributes: Vec<(String, String)> =
                vec![("type".to_string(), rule.rule_type.clone())];
            for name in RULE_ATTRIBUTES {
                if let Some(value) = rule.attributes.get(name) {
                    attributes.push((name.to_string(), value.clone()));
                }
            }
            doc.start_tag("cfRule", attribute_refs(&attributes));
            for formula in &rule.formula {
                doc.tag("formula", [] as [(&str, &str); 0], Some(formula));
            }
            if let Some(scale) = &rule.color_scale {
                doc.start_tag("colorScale", [] as [(&str, &str); 0]);
                for cfvo in &scale.cfvo {
                    doc.tag("cfvo", attribute_refs(&cfvo.attributes()), None);
                }
                for color in &scale.color {
                    write_scale_color(doc, &color.index);
                }
                doc.end_tag("colorScale");
            }
            if let Some(icon_set) = &rule.icon_set {
                let mut attributes: Vec<(String, String)> = Vec::new();
                if let Some(name) = &icon_set.icon_set {
                    attributes.push(("iconSet".to_string(), name.clone()));
                }
                if let Some(value) = &icon_set.show_value {
                    attributes.push(("showValue".to_string(), value.clone()));
                }
                if let Some(value) = &icon_set.reverse {
                    attributes.push(("reverse".to_string(), value.clone()));
                }
                if let Some(value) = &icon_set.percent {
                    attributes.push(("percent".to_string(), value.clone()));
                }
                doc.start_tag("iconSet", attribute_refs(&attributes));
                for cfvo in &icon_set.cfvo {
                    doc.tag("cfvo", attribute_refs(&cfvo.attributes()), None);
                }
                doc.end_tag("iconSet");
            }
            if let Some(bar) = &rule.data_bar {
                // The thresholds come before the colour: the schema fixes that order.
                let mut attributes: Vec<(String, String)> = Vec::new();
                let mut put = |name: &str, value: Option<String>| {
                    if let Some(value) = value {
                        attributes.push((name.to_string(), value));
                    }
                };
                put(
                    "showValue",
                    bar.show_value
                        .map(|v| if v { "1".into() } else { "0".into() }),
                );
                put("minLength", bar.min_length.map(|v| v.to_string()));
                put("maxLength", bar.max_length.map(|v| v.to_string()));
                doc.start_tag("dataBar", attribute_refs(&attributes));
                for cfvo in &bar.cfvo {
                    doc.tag("cfvo", attribute_refs(&cfvo.attributes()), None);
                }
                if !bar.color.is_empty() {
                    write_scale_color(doc, &bar.color);
                }
                doc.end_tag("dataBar");
            }
            doc.end_tag("cfRule");
        }
        doc.end_tag("conditionalFormatting");
    }
}

fn write_scale_color(doc: &mut XmlWriter, color: &str) {
    let parts: Vec<&str> = color.split(':').collect();
    if parts.first() == Some(&"theme") && parts.len() >= 2 {
        let mut attributes: Vec<(String, String)> =
            vec![("theme".to_string(), parts[1].to_string())];
        if let Some(tint) = parts.get(2) {
            if !tint.is_empty() {
                attributes.push(("tint".to_string(), (*tint).to_string()));
            }
        }
        doc.tag("color", attribute_refs(&attributes), None);
    } else {
        doc.tag("color", [("rgb", color)], None);
    }
}

/// Write the relationships part for a worksheet.
pub fn write_worksheet_rels(
    worksheet: &Worksheet,
    drawing_id: u32,
    comments_id: u32,
    tables: &[(String, u32)],
) -> String {
    let mut root = Element::new(format!("{{{PKG_REL_NS}}}Relationships"));
    for relationship in &worksheet.relationships {
        let mut node = Element::new(format!("{{{PKG_REL_NS}}}Relationship"));
        if let Some(id) = &relationship.id {
            node.set("Id", id.clone());
        }
        node.set("Type", relationship.type_uri());
        if let Some(target) = &relationship.target {
            node.set("Target", target.clone());
        }
        if let Some(mode) = &relationship.target_mode {
            node.set("TargetMode", mode.clone());
        }
        root.append(node);
    }
    if !worksheet.charts.is_empty() || !worksheet.images.is_empty() {
        let mut node = Element::new(format!("{{{PKG_REL_NS}}}Relationship"));
        node.set("Id", "rId1");
        node.set("Type", format!("{REL_NS}/drawing"));
        node.set("Target", format!("../drawings/drawing{drawing_id}.xml"));
        root.append(node);
    }
    // One relationship per table part. The id is what the sheet's `<tableParts>` refers to,
    // so it has to be the same string in both places.
    for (id, table_id) in tables {
        let mut node = Element::new(format!("{{{PKG_REL_NS}}}Relationship"));
        node.set("Id", id.clone());
        node.set("Type", crate::writer::workbook::TABLE_REL_TYPE.to_string());
        node.set("Target", format!("../tables/table{table_id}.xml"));
        root.append(node);
    }
    if worksheet.comment_count() > 0 {
        let mut node = Element::new(format!("{{{PKG_REL_NS}}}Relationship"));
        node.set("Id", "comments");
        node.set("Type", COMMENTS_NS);
        node.set("Target", format!("../comments{comments_id}.xml"));
        root.append(node);

        let mut node = Element::new(format!("{{{PKG_REL_NS}}}Relationship"));
        node.set("Id", "commentsvml");
        node.set("Type", VML_NS);
        node.set(
            "Target",
            format!("../drawings/commentsDrawing{comments_id}.vml"),
        );
        root.append(node);
    }
    root.to_pretty_string()
}

/// Build the style tables for a workbook, exposed for callers assembling parts directly.
pub fn style_tables_for(workbook: &crate::workbook::Workbook) -> StyleTables {
    build_style_tables(workbook)
}

/// The style id at a table position, exposed for callers assembling parts directly.
pub fn style_id_at(tables: &StyleTables, position: usize) -> Option<StyleId> {
    tables.id_at(position)
}

/// Helper: the column letters for a cell's column, for tests and callers.
pub fn cell_column_letters(cell: &Cell) -> Result<String> {
    let (letters, _) = coordinate_from_string(&cell.coordinate())?;
    get_column_letter(column_index_from_string(&letters)?)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cell::cell::CellContext;
    use crate::styles::style::Style;
    use crate::writer::strings::create_string_table;
    use crate::xml::functions::fromstring;

    fn write_one(worksheet: &Worksheet) -> String {
        // Build the shared tables from this worksheet alone so the test does not need a
        // whole workbook.
        let tables = tables_from_sheets(std::slice::from_ref(worksheet));
        let string_table = create_string_table(std::slice::from_ref(worksheet));
        write_worksheet(worksheet, &string_table, &tables).unwrap()
    }

    fn tables_from_sheets(sheets: &[Worksheet]) -> StyleTables {
        let mut workbook = crate::workbook::Workbook::new();
        workbook.worksheets.clear();
        workbook.worksheets.extend_from_slice(sheets);
        build_style_tables(&workbook)
    }

    fn new_sheet() -> Worksheet {
        let mut sheet = Worksheet::new("Sheet1").unwrap();
        sheet.context = CellContext::default();
        sheet
    }

    /// The `r` of every `<row>` in `xml`, in the order they were written.
    fn row_numbers(xml: &str) -> Vec<u32> {
        numbers_after(xml, "<row r=\"")
    }

    /// The `min` of every `<col>` in `xml`, in the order they were written.
    fn col_numbers(xml: &str) -> Vec<u32> {
        numbers_after(xml, "<col min=\"")
    }

    /// The first number following each occurrence of `marker`, in order.
    fn numbers_after(xml: &str, marker: &str) -> Vec<u32> {
        xml.match_indices(marker)
            .map(|(at, matched)| {
                xml[at + matched.len()..]
                    .split('"')
                    .next()
                    .expect("a quoted attribute value")
                    .parse()
                    .expect("a number")
            })
            .collect()
    }

    #[test]
    fn writes_a_minimal_worksheet() {
        let sheet = new_sheet();
        let xml = write_one(&sheet);
        assert!(xml.starts_with("<worksheet"));
        assert!(xml.contains("xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\""));
        assert!(xml.ends_with("</worksheet>"));
        assert!(xml.contains("<dimension ref=\"A1:A1\"></dimension>"));
        assert!(xml.contains("<sheetFormatPr defaultRowHeight=\"15\"></sheetFormatPr>"));
    }

    #[test]
    fn writes_cell_values_by_type() {
        let mut sheet = new_sheet();
        sheet.set("A1", "hello").unwrap();
        sheet.set("B1", 42.0).unwrap();
        sheet.set("C1", true).unwrap();
        let xml = write_one(&sheet);
        let root = fromstring(xml.as_bytes()).unwrap();
        let row = root
            .find("sheetData")
            .and_then(|data| data.find("row"))
            .expect("row");
        let cells = row.find_all("c");
        assert_eq!(cells.len(), 3);
        assert_eq!(cells[0].get("t"), Some("s"));
        assert_eq!(cells[0].find_text("v", ""), "0");
        assert_eq!(cells[1].get("t"), Some("n"));
        assert_eq!(cells[1].find_text("v", ""), "42");
        assert_eq!(cells[2].get("t"), Some("b"));
        assert_eq!(cells[2].find_text("v", ""), "1");
    }

    #[test]
    fn modern_formulas_are_prefixed_with_xlfn() {
        let mut sheet = new_sheet();
        sheet.set("A1", "=SEQUENCE(5)").unwrap();
        let xml = write_one(&sheet);
        assert!(xml.contains("<f>_xlfn.SEQUENCE(5)</f>"), "got {xml}");
    }

    #[test]
    fn formula_cells_have_no_type_and_an_empty_v() {
        let mut sheet = new_sheet();
        sheet.set("A1", "=SUM(B1:B2)").unwrap();
        let xml = write_one(&sheet);
        let root = fromstring(xml.as_bytes()).unwrap();
        let cell = root
            .find("sheetData")
            .and_then(|d| d.find("row"))
            .and_then(|r| r.find("c"))
            .expect("cell");
        assert_eq!(cell.get("t"), None);
        assert_eq!(cell.find_text("f", ""), "SUM(B1:B2)");
        assert!(cell.find("v").is_some());
    }

    #[test]
    fn shared_formulas_without_a_range_write_an_empty_f() {
        let mut sheet = new_sheet();
        sheet.set("A1", "=B1*2").unwrap();
        let cell = sheet.cell_mut("A1").unwrap();
        cell.formula_attributes = Some(crate::cell::cell::FormulaAttributes {
            formula_type: Some("shared".to_string()),
            si: Some("0".to_string()),
            reference: None,
        });
        let xml = write_one(&sheet);
        assert!(xml.contains("si=\"0\""));
        assert!(xml.contains("<f t=\"shared\" si=\"0\"></f>"));
    }

    #[test]
    fn merged_cells_and_dimension() {
        let mut sheet = new_sheet();
        sheet.set("A1", "x").unwrap();
        sheet.merge_cells("A1:B2").unwrap();
        sheet.set("C5", 1.0).unwrap();
        let xml = write_one(&sheet);
        assert!(xml.contains("<mergeCells count=\"1\">"));
        assert!(xml.contains("<mergeCell ref=\"A1:B2\"></mergeCell>"));
        assert!(xml.contains("ref=\"A1:C5\""));
    }

    #[test]
    fn freeze_panes_produce_a_pane_element() {
        let mut sheet = new_sheet();
        sheet.set_freeze_panes("B2");
        let xml = write_one(&sheet);
        assert!(xml.contains("<pane xSplit=\"1\" ySplit=\"1\" topLeftCell=\"B2\" activePane=\"bottomRight\" state=\"frozen\"></pane>"));
        assert!(xml.contains("pane=\"bottomRight\""));
    }

    #[test]
    fn column_dimensions_are_written() {
        let mut sheet = new_sheet();
        let mut dimension = crate::worksheet::ColumnDimension::new("A");
        dimension.width = 20.0;
        sheet.column_dimensions.insert("A".to_string(), dimension);
        let xml = write_one(&sheet);
        assert!(xml.contains("<col min=\"1\" max=\"1\" customWidth=\"1\" width=\"20\"></col>"));
    }

    #[test]
    fn column_dimensions_are_written_in_ascending_order() {
        // `"Z"` and `"AA"` sort the wrong way round as letters, so the order has to come
        // from the column number. A `<col>` list that runs backwards is not what Excel
        // writes and not what openpyxl reads back as canonical.
        let mut sheet = new_sheet();
        for letters in ["B", "Z", "AA", "C"] {
            let mut dimension = ColumnDimension::new(letters);
            dimension.width = 20.0;
            sheet
                .column_dimensions
                .insert(letters.to_string(), dimension);
        }
        let xml = write_one(&sheet);
        assert_eq!(col_numbers(&xml), vec![2, 3, 26, 27]);
    }

    #[test]
    fn a_row_with_no_cells_keeps_its_height() {
        // A height set on a row that holds nothing still has to reach the file. `<row>` is
        // the only element a row height can live in, so a row that exists only in
        // `row_dimensions` used to be dropped along with its height.
        let mut sheet = new_sheet();
        sheet.set("A1", "header").unwrap();
        sheet
            .row_dimensions
            .insert(9, RowDimension::new(9).with_height(30.0));
        let xml = write_one(&sheet);
        assert!(xml.contains("<row r=\"9\" spans=\"1:1\" ht=\"30\" customHeight=\"1\"></row>"));
    }

    #[test]
    fn a_row_dimension_with_nothing_set_is_not_written() {
        // The other half of the rule: an empty row with no height, no hidden flag, no
        // outline level and no style says nothing, so it gets no element of its own.
        let mut sheet = new_sheet();
        sheet.set("A1", "header").unwrap();
        sheet.row_dimensions.insert(9, RowDimension::new(9));
        let xml = write_one(&sheet);
        assert!(!xml.contains("<row r=\"9\""));
    }

    #[test]
    fn rows_are_written_in_ascending_order() {
        // Both sources of a row -- one holding cells, one only a dimension -- have to come
        // out merged and in order, because `<row>` is a sequence.
        let mut sheet = new_sheet();
        sheet.set("A5", "five").unwrap();
        sheet.set("A1", "one").unwrap();
        sheet
            .row_dimensions
            .insert(3, RowDimension::new(3).with_height(30.0));
        let xml = write_one(&sheet);
        assert_eq!(row_numbers(&xml), vec![1, 3, 5]);
    }

    #[test]
    fn auto_filter_variants() {
        let mut sheet = new_sheet();
        sheet.set("A1", "x").unwrap();
        sheet.auto_filter.set_reference("A1:C3");
        let xml = write_one(&sheet);
        assert!(xml.contains("<autoFilter ref=\"A1:C3\"></autoFilter>"));

        let mut sheet = new_sheet();
        sheet.auto_filter.set_reference("A1:C3");
        sheet
            .auto_filter
            .add_filter_column(0, vec!["a".to_string()], true);
        sheet.auto_filter.add_sort_condition("A2:A3", true);
        let xml = write_one(&sheet);
        assert!(xml.contains("<filterColumn colId=\"0\">"));
        assert!(xml.contains("<filters blank=\"1\">"));
        assert!(xml.contains("<filter val=\"a\"></filter>"));
        assert!(xml.contains("<sortCondtion ref=\"A2:A3\" descending=\"1\"></sortCondtion>"));
    }

    #[test]
    fn data_validations_are_written() {
        let mut sheet = new_sheet();
        let mut validation = crate::datavalidation::DataValidation::new(
            crate::datavalidation::ValidationType::Whole,
            Some(crate::datavalidation::ValidationOperator::Between),
            Some("1"),
            Some("10"),
            true,
        );
        validation.add_cell("A1");
        validation.add_cell("A2");
        sheet.add_data_validation(validation);
        let xml = write_one(&sheet);
        assert!(xml.contains("<dataValidations count=\"1\">"));
        assert!(xml.contains("type=\"whole\""));
        assert!(xml.contains("sqref=\"A1:A2\""));
        assert!(xml.contains("<formula1>1</formula1>"));
        assert!(xml.contains("<formula2>10</formula2>"));
    }

    #[test]
    fn hyperlinks_are_written_with_relationship_ids() {
        let mut sheet = new_sheet();
        sheet.set_hyperlink("A1", "http://example.com").unwrap();
        let xml = write_one(&sheet);
        assert!(xml.contains("<hyperlinks>"));
        assert!(xml.contains("r:id=\"rId1\""));
        assert!(xml.contains("ref=\"A1\""));
    }

    #[test]
    fn page_setup_and_margins() {
        let mut sheet = new_sheet();
        sheet
            .set_printer_settings("9", Worksheet::ORIENTATION_LANDSCAPE)
            .unwrap();
        sheet.page_margins = crate::worksheet::PageMargins::new().with_defaults();
        let xml = write_one(&sheet);
        assert!(xml.contains("<pageMargins left=\"0.70\""));
        assert!(xml.contains("<pageSetup orientation=\"landscape\" paperSize=\"9\"></pageSetup>"));
    }

    #[test]
    fn header_footer_round_trip() {
        let mut sheet = new_sheet();
        sheet.header_footer.set_header("&L&report");
        let xml = write_one(&sheet);
        assert!(xml.contains("<oddHeader>&amp;L"));
    }

    #[test]
    fn protection_is_written_when_enabled() {
        let mut sheet = new_sheet();
        sheet.protection.enable();
        let xml = write_one(&sheet);
        assert!(xml.contains(
            "<sheetProtection objects=\"1\" scenarios=\"1\" sheet=\"1\"></sheetProtection>"
        ));
    }

    #[test]
    fn conditional_formatting_is_written() {
        let mut sheet = new_sheet();
        let rule = crate::formatting::rules::CellIsRule::new(Some(">"), Some("5"), true).to_rule();
        sheet.conditional_formatting.add("A1:A4", rule);
        let xml = write_one(&sheet);
        assert!(xml.contains("<conditionalFormatting sqref=\"A1:A4\">"));
        assert!(xml.contains("type=\"cellIs\""));
        assert!(xml.contains("operator=\"greaterThan\""));
        assert!(xml.contains("stopIfTrue=\"1\""));
    }

    #[test]
    fn colour_scales_split_theme_and_tint() {
        let mut sheet = new_sheet();
        let rule = crate::formatting::rules::ColorScaleRule::new(
            Some("min"),
            None,
            Some(crate::styles::Color::theme("9", None)),
            None,
            None,
            None,
            Some("max"),
            None,
            Some(crate::styles::Color::new("FF4F81BD")),
        )
        .to_rule();
        sheet.conditional_formatting.add("A1:A4", rule);
        let xml = write_one(&sheet);
        assert!(xml.contains("<colorScale>"));
        assert!(xml.contains("theme=\"9\""));
        assert!(xml.contains("<color rgb=\"FF4F81BD\"></color>"));
        // The theme slot with an empty tint must not emit a tint attribute.
        assert!(!xml.contains("tint="));
    }

    #[test]
    fn styled_cells_carry_the_style_index() {
        let mut sheet = new_sheet();
        sheet.set("A1", "x").unwrap();
        let mut style = Style::new();
        style.font.bold = true;
        sheet.set_style("A1", style).unwrap();
        // The worksheet carries only the index into the shared style tables; the font itself
        // lives in `xl/styles.xml`.
        let root = fromstring(write_one(&sheet).as_bytes()).unwrap();
        let cell = root
            .find("sheetData")
            .and_then(|d| d.find("row"))
            .and_then(|r| r.find("c"))
            .unwrap();
        let style_id = cell
            .get("s")
            .and_then(|id| id.parse::<usize>().ok())
            .unwrap();
        assert_eq!(style_id, 1, "the bold style is not the default");
    }

    #[test]
    fn row_ordering_is_by_row_then_column() {
        let mut sheet = new_sheet();
        sheet.set("B2", 1.0).unwrap();
        sheet.set("A2", 2.0).unwrap();
        sheet.set("A1", 3.0).unwrap();
        let xml = write_one(&sheet);
        let root = fromstring(xml.as_bytes()).unwrap();
        let rows = root
            .find("sheetData")
            .unwrap()
            .find_all("row")
            .iter()
            .map(|row| row.get("r").unwrap_or("").to_string())
            .collect::<Vec<_>>();
        assert_eq!(rows, vec!["1", "2"]);
        let second_row = root.find("sheetData").unwrap().find_all("row")[1].clone();
        let refs: Vec<String> = second_row
            .find_all("c")
            .iter()
            .map(|c| c.get("r").unwrap_or("").to_string())
            .collect();
        assert_eq!(refs, vec!["A2", "B2"]);
    }

    #[test]
    fn relationships_include_drawings_and_comments() {
        let mut sheet = new_sheet();
        sheet.set("A1", "x").unwrap();
        sheet
            .set_comment("A1", Some(crate::comments::Comment::new("note", "me")))
            .unwrap();
        let rels = write_worksheet_rels(&sheet, 1, 1, &[]);
        assert!(rels.contains("Id=\"comments\""));
        assert!(rels.contains("../comments1.xml"));
        assert!(rels.contains("commentsDrawing1.vml"));
    }

    #[test]
    fn empty_cells_are_written_as_self_closing() {
        let mut sheet = new_sheet();
        sheet.set("A1", CellValue::None).unwrap();
        let xml = write_one(&sheet);
        assert!(xml.contains("<c r=\"A1\" t=\"s\"></c>"));
    }

    #[test]
    fn float_formatting_drops_trailing_zeros() {
        assert_eq!(format_number(20.0), "20");
        assert_eq!(format_number(9.1), "9.1");
    }
}