ferroxl 0.1.6

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
//! The workbook (`openpyxl/workbook/workbook.py`).
//!
//! `Workbook` is the top-level container: it owns the worksheets, the document properties
//! and security settings, and the defined names. Saving is delegated to
//! [`crate::writer::excel::save_workbook`].

use chrono::{NaiveDateTime, NaiveTime};

use crate::cell::cell::CellContext;
use crate::date_time::{BaseDate, CALENDAR_MAC_1904, CALENDAR_WINDOWS_1900};
use crate::exceptions::{Error, Result};
use crate::formatting::StyleProperties;
use crate::namedrange::{DefinedName, NamedRange};
use crate::styles::style::Style;
use crate::worksheet::Worksheet;

/// High-level document properties.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DocumentProperties {
    /// The document creator.
    pub creator: String,
    /// Who last modified the document.
    pub last_modified_by: String,
    /// Creation timestamp.
    pub created: NaiveDateTime,
    /// Modification timestamp.
    pub modified: NaiveDateTime,
    /// Document title.
    pub title: String,
    /// Document subject.
    pub subject: String,
    /// Document description.
    pub description: String,
    /// Document keywords.
    pub keywords: String,
    /// Document category.
    pub category: String,
    /// The company recorded in the extended properties.
    pub company: String,
    /// The workbook's date system.
    pub excel_base_date: BaseDate,
}

impl Default for DocumentProperties {
    fn default() -> Self {
        let now = NaiveDateTime::default();
        DocumentProperties {
            creator: "Unknown".to_string(),
            last_modified_by: "Unknown".to_string(),
            created: now,
            modified: now,
            title: "Untitled".to_string(),
            subject: String::new(),
            description: String::new(),
            keywords: String::new(),
            category: String::new(),
            company: "Microsoft Corporation".to_string(),
            excel_base_date: BaseDate::Windows1900,
        }
    }
}

impl DocumentProperties {
    /// A new property set with openpyxl's defaults.
    pub fn new() -> Self {
        DocumentProperties::default()
    }
}

/// Security information about the document.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct DocumentSecurity {
    /// Whether revision tracking is locked.
    pub lock_revision: bool,
    /// Whether workbook structure is locked.
    pub lock_structure: bool,
    /// Whether the workbook window is locked.
    pub lock_windows: bool,
    /// The revision password.
    pub revision_password: String,
    /// The workbook password.
    pub workbook_password: String,
}

impl DocumentSecurity {
    /// An unlocked document.
    pub fn new() -> Self {
        DocumentSecurity::default()
    }
}

/// `<calcPr>`: how and when Excel recalculates.
///
/// The one setting here that changes what a reader sees rather than how a file looks is
/// `full_calc_on_load`. ferroxl writes formulas without cached results, so a workbook opened
/// by anything other than Excel shows no values at all until something recalculates it. That
/// makes the flag load-bearing rather than cosmetic, and it is why it is settable here rather
/// than being the fixed literal it was.
// iterate_delta is an f64, so this cannot be Eq or Hash without the manual bit
// comparison GradientStop needs. It is compared as a value instead.
#[derive(Debug, Clone, PartialEq)]
pub struct CalcProperties {
    /// The engine version the file was last calculated by. Excel uses it to decide whether to
    /// recalculate; openpyxl writes 124519.
    pub calc_id: u32,
    /// `manual`, `auto` or `autoNoTable`.
    pub calc_mode: Option<String>,
    /// Recalculate the whole workbook when it is opened.
    ///
    /// `Some(true)` by default, which is what makes a workbook written by ferroxl show values
    /// when Excel opens it.
    pub full_calc_on_load: Option<bool>,
    /// Whether references are written `A1` or `R1C1`.
    pub ref_mode: Option<String>,
    /// Whether circular references are iterated rather than refused.
    pub iterate: Option<bool>,
    /// How many times to iterate before giving up.
    pub iterate_count: Option<u32>,
    /// The change between iterations that counts as converged.
    pub iterate_delta: Option<f64>,
    /// Whether to use full precision rather than Excel's 15 digits.
    pub full_precision: Option<bool>,
    /// Whether the last calculation finished. `Some(false)` marks a workbook as needing one.
    pub calc_completed: Option<bool>,
    /// Whether to recalculate when saving.
    pub calc_on_save: Option<bool>,
    /// Whether concurrent calculation is enabled.
    pub concurrent_calc: Option<bool>,
    /// How many calculation threads to allow while calculating manually.
    pub concurrent_manual_count: Option<u32>,
    /// Whether to force a full calculation.
    pub force_full_calc: Option<bool>,
}

impl Default for CalcProperties {
    /// Excel's own defaults, and openpyxl's: automatic, recalculating on load.
    fn default() -> Self {
        CalcProperties {
            calc_id: 124_519,
            calc_mode: Some("auto".to_string()),
            full_calc_on_load: Some(true),
            ref_mode: None,
            iterate: None,
            iterate_count: None,
            iterate_delta: None,
            full_precision: None,
            calc_completed: None,
            calc_on_save: None,
            concurrent_calc: None,
            concurrent_manual_count: None,
            force_full_calc: None,
        }
    }
}

impl CalcProperties {
    /// Manual calculation: nothing recalculates until asked.
    pub fn manual() -> Self {
        CalcProperties {
            calc_mode: Some("manual".to_string()),
            ..CalcProperties::default()
        }
    }

    /// The attributes for `<calcPr>`, in the order openpyxl declares them.
    pub fn attributes(&self) -> Vec<(String, String)> {
        let mut attrs = vec![("calcId".to_string(), self.calc_id.to_string())];
        let mut put = |name: &str, value: Option<String>| {
            if let Some(value) = value {
                attrs.push((name.to_string(), value));
            }
        };
        put("calcMode", self.calc_mode.clone());
        put("fullCalcOnLoad", self.full_calc_on_load.map(flag));
        put("refMode", self.ref_mode.clone());
        put("iterate", self.iterate.map(flag));
        put("iterateCount", self.iterate_count.map(|v| v.to_string()));
        put(
            "iterateDelta",
            self.iterate_delta.map(crate::xml::functions::safe_string),
        );
        put("fullPrecision", self.full_precision.map(flag));
        put("calcCompleted", self.calc_completed.map(flag));
        put("calcOnSave", self.calc_on_save.map(flag));
        put("concurrentCalc", self.concurrent_calc.map(flag));
        put(
            "concurrentManualCount",
            self.concurrent_manual_count.map(|v| v.to_string()),
        );
        put("forceFullCalc", self.force_full_calc.map(flag));
        attrs
    }
}

/// A boolean as OOXML writes it: `1` and `0`.
fn flag(value: bool) -> String {
    if value {
        "1".to_string()
    } else {
        "0".to_string()
    }
}

/// The container for all other parts of the document.
#[derive(Debug, Clone)]
pub struct Workbook {
    /// The worksheets, in order.
    pub worksheets: Vec<Worksheet>,
    /// Index of the active worksheet.
    pub active_sheet_index: usize,
    /// The defined names.
    pub named_ranges: Vec<DefinedName>,
    /// Document properties.
    pub properties: DocumentProperties,
    /// Document security.
    pub security: DocumentSecurity,
    /// How and when Excel recalculates this workbook.
    pub calculation: CalcProperties,
    /// The workbook-level default style.
    pub style: Style,
    /// Whether to use the streaming writer.
    pub optimized_write: bool,
    /// Whether the workbook was loaded for read-only streaming.
    pub optimized_read: bool,
    /// Whether cell types are guessed rather than read from the file.
    pub guess_types: bool,
    /// Whether formula cells report their cached value instead of the formula.
    pub data_only: bool,
    /// The text encoding used when reading and writing.
    pub encoding: String,
    /// The raw theme bytes read from a source workbook, re-emitted verbatim.
    pub loaded_theme: Option<Vec<u8>>,
    /// The workbook's raw bytes, kept for VBA projects.
    pub vba_archive: Option<Vec<u8>>,
    /// Style properties read from a source workbook.
    pub style_properties: Option<StyleProperties>,
    /// Drawings belonging to any sheet.
    pub drawings: Vec<usize>,
    /// Workbook-level relationships.
    pub relationships: Vec<crate::worksheet::relationship::Relationship>,
}

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

impl Workbook {
    /// A new workbook with one empty sheet, as openpyxl creates.
    pub fn new() -> Self {
        let context = CellContext::default();
        let mut workbook = Workbook {
            worksheets: Vec::new(),
            active_sheet_index: 0,
            named_ranges: Vec::new(),
            properties: DocumentProperties::new(),
            security: DocumentSecurity::new(),
            calculation: CalcProperties::default(),
            style: Style::new(),
            optimized_write: false,
            optimized_read: false,
            guess_types: false,
            data_only: false,
            encoding: "utf-8".to_string(),
            loaded_theme: None,
            vba_archive: None,
            style_properties: None,
            drawings: Vec::new(),
            relationships: Vec::new(),
        };
        if let Ok(mut sheet) = Worksheet::with_title("", 0) {
            sheet.context = context;
            workbook.worksheets.push(sheet);
        }
        workbook
    }

    /// A new workbook with no sheets.
    ///
    /// The streaming reader uses this, because the sheets are discovered from the archive.
    pub fn empty() -> Self {
        let mut workbook = Workbook::new();
        workbook.worksheets.clear();
        workbook
    }

    /// Create a worksheet at an optional index and return its index.
    pub fn create_sheet(&mut self, title: Option<&str>) -> Result<usize> {
        self.create_sheet_at(title, None)
    }

    /// Create a worksheet, optionally inserting it at `index`.
    pub fn create_sheet_at(&mut self, title: Option<&str>, index: Option<usize>) -> Result<usize> {
        if self.optimized_read {
            return Err(Error::ReadOnlyWorkbook(
                "Cannot create new sheet in a read-only workbook".into(),
            ));
        }
        let existing: Vec<String> = self.get_sheet_names();
        let position = self.worksheets.len();
        let mut sheet = Worksheet::with_title(title.unwrap_or(""), position)?;
        sheet.context = self.cell_context();
        let proposed = sheet.title().to_string();
        sheet.set_title(&proposed, &existing)?;
        let insert_at = index
            .unwrap_or(self.worksheets.len())
            .min(self.worksheets.len());
        self.worksheets.insert(insert_at, sheet);
        self.reindex_sheets();
        Ok(insert_at)
    }

    /// Insert an existing worksheet at an optional index.
    pub fn add_sheet(&mut self, worksheet: Worksheet, index: Option<usize>) -> Result<()> {
        if self.optimized_read {
            return Err(Error::ReadOnlyWorkbook(
                "Cannot add sheet to a read-only workbook".into(),
            ));
        }
        let existing: Vec<String> = self.get_sheet_names();
        let mut worksheet = worksheet;
        let proposed = worksheet.title().to_string();
        worksheet.set_title(&proposed, &existing)?;
        let insert_at = index
            .unwrap_or(self.worksheets.len())
            .min(self.worksheets.len());
        self.worksheets.insert(insert_at, worksheet);
        self.reindex_sheets();
        Ok(())
    }

    /// Remove a worksheet by index.
    pub fn remove_sheet(&mut self, index: usize) -> Result<()> {
        if self.optimized_read {
            return Err(Error::ReadOnlyWorkbook(
                "Cannot remove sheet from a read-only workbook".into(),
            ));
        }
        if index >= self.worksheets.len() {
            return Err(Error::Key(format!(
                "Worksheet index {index} does not exist"
            )));
        }
        self.worksheets.remove(index);
        self.reindex_sheets();
        Ok(())
    }

    /// The active worksheet index.
    pub fn active(&self) -> usize {
        self.active_sheet_index
    }

    /// Set the active worksheet index.
    pub fn set_active(&mut self, index: usize) -> Result<()> {
        if index >= self.worksheets.len() {
            return Err(Error::Key(format!(
                "Worksheet index {index} does not exist"
            )));
        }
        self.active_sheet_index = index;
        Ok(())
    }

    /// The active worksheet.
    pub fn active_sheet(&self) -> Result<&Worksheet> {
        self.worksheets
            .get(self.active_sheet_index)
            .ok_or_else(|| Error::Key("No active worksheet".to_string()))
    }

    /// The active worksheet, mutably.
    pub fn active_sheet_mut(&mut self) -> Result<&mut Worksheet> {
        let index = self.active_sheet_index;
        self.worksheets
            .get_mut(index)
            .ok_or_else(|| Error::Key("No active worksheet".to_string()))
    }

    /// Reassign worksheet indices after the sheet list changes.
    ///
    /// Called internally after any add or remove; exposed because the reader builds the
    /// sheet list directly.
    pub fn reindex_sheets(&mut self) {
        for (index, sheet) in self.worksheets.iter_mut().enumerate() {
            sheet.index = index;
        }
        if self.active_sheet_index >= self.worksheets.len() {
            self.active_sheet_index = self.worksheets.len().saturating_sub(1);
        }
    }

    /// Look a worksheet up by title.
    /// Replace the calculation properties.
    ///
    /// [`CalcProperties::default`] sets `full_calc_on_load`, which is the one setting that
    /// changes what a reader sees rather than how the file looks: ferroxl writes formulas
    /// without cached results, so without it anything other than Excel opens the workbook and
    /// finds no values at all.
    pub fn set_calculation_properties(&mut self, calculation: CalcProperties) -> &mut Self {
        self.calculation = calculation;
        self
    }

    /// The sheet with this title, if there is one.
    pub fn get_sheet_by_name(&self, name: &str) -> Option<&Worksheet> {
        self.worksheets.iter().find(|sheet| sheet.title() == name)
    }

    /// Look a worksheet up by title, mutably.
    pub fn get_sheet_by_name_mut(&mut self, name: &str) -> Option<&mut Worksheet> {
        self.worksheets
            .iter_mut()
            .find(|sheet| sheet.title() == name)
    }

    /// The index of a worksheet by title.
    pub fn get_index(&self, name: &str) -> Option<usize> {
        self.worksheets
            .iter()
            .position(|sheet| sheet.title() == name)
    }

    /// The titles of all worksheets, in order.
    pub fn get_sheet_names(&self) -> Vec<String> {
        self.worksheets
            .iter()
            .map(|sheet| sheet.title().to_string())
            .collect()
    }

    /// Whether a worksheet with the given title exists.
    pub fn contains(&self, name: &str) -> bool {
        self.worksheets.iter().any(|sheet| sheet.title() == name)
    }

    /// The workbook's date system.
    pub fn excel_base_date(&self) -> BaseDate {
        self.properties.excel_base_date
    }

    /// The cell context new cells should use.
    pub fn cell_context(&self) -> CellContext {
        CellContext {
            base_date: self.properties.excel_base_date,
            guess_types: self.guess_types,
        }
    }

    /// Mark the workbook as read-only for streaming.
    pub fn set_optimized_read(&mut self) {
        self.optimized_read = true;
    }

    /// Write the workbook to `path` as an Office Open XML package.
    ///
    /// This is the Rust spelling of `Workbook.save` in the Python original. The workbook
    /// is consumed because the writer takes ownership of it, which keeps a single
    /// definition of what "saving" does.
    pub fn save(self, path: impl AsRef<std::path::Path>) -> Result<()> {
        crate::writer::save_workbook(self, path)
    }

    /// Serialise the workbook to the package bytes without touching the filesystem.
    ///
    /// Useful for a web service that streams the download straight into a response.
    pub fn to_bytes(self) -> Result<Vec<u8>> {
        crate::writer::save_virtual_workbook(self)
    }

    /// The serial offset for the current date system.
    pub fn base_date_offset(&self) -> f64 {
        match self.properties.excel_base_date {
            BaseDate::Windows1900 => CALENDAR_WINDOWS_1900,
            BaseDate::Mac1904 => CALENDAR_MAC_1904,
        }
    }

    /// Register a defined name.
    pub fn add_named_range(&mut self, named_range: NamedRange) {
        self.named_ranges.push(DefinedName::Range(named_range));
    }

    /// Register a value-bearing defined name.
    pub fn add_named_value(&mut self, name: &str, value: &str, scope: Option<usize>) {
        self.named_ranges.push(DefinedName::Value(
            crate::namedrange::NamedRangeContainingValue::new(name, value, scope),
        ));
    }

    /// All defined names.
    pub fn get_named_ranges(&self) -> &[DefinedName] {
        &self.named_ranges
    }

    /// Look a defined name up by name.
    pub fn get_named_range(&self, name: &str) -> Option<&DefinedName> {
        self.named_ranges.iter().find(|entry| entry.name() == name)
    }

    /// Remove a defined name.
    pub fn remove_named_range(&mut self, name: &str) -> Result<()> {
        let before = self.named_ranges.len();
        self.named_ranges.retain(|entry| entry.name() != name);
        if self.named_ranges.len() == before {
            return Err(Error::Key(format!("No such named range: {name}")));
        }
        Ok(())
    }

    /// Create a named range covering `range` on the sheet at `sheet_index`.
    pub fn create_named_range(
        &mut self,
        name: &str,
        sheet_index: usize,
        range: &str,
        scope: Option<usize>,
    ) -> Result<()> {
        if sheet_index >= self.worksheets.len() {
            return Err(Error::Key(format!(
                "Worksheet index {sheet_index} does not exist"
            )));
        }
        self.add_named_range(NamedRange::new(
            name,
            vec![(sheet_index, range.to_string())],
            scope,
        ));
        Ok(())
    }

    /// Register a print-title named range for a sheet.
    pub fn add_print_title(
        &mut self,
        sheet_index: usize,
        n: u32,
        rows_or_cols: &str,
    ) -> Result<()> {
        let range = Worksheet::print_title_range(n, rows_or_cols)?;
        self.create_named_range("_xlnm.Print_Titles", sheet_index, &range, Some(sheet_index))
    }

    /// Append rows to a sheet, one entry per row.
    pub fn append_rows(
        &mut self,
        sheet_index: usize,
        rows: &[Vec<crate::cell::cell::CellValue>],
    ) -> Result<()> {
        let sheet = self
            .worksheets
            .get_mut(sheet_index)
            .ok_or_else(|| Error::Key(format!("Worksheet index {sheet_index} does not exist")))?;
        let mut row_number = sheet.highest_row();
        for row in rows {
            row_number += 1;
            sheet.write_row(row_number, 1, row)?;
        }
        Ok(())
    }

    /// The core properties' creation time as a time-only value, for tests.
    pub fn created_time(&self) -> NaiveTime {
        self.properties.created.time()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cell::cell::CellValue;
    use chrono::NaiveDate;

    #[test]
    fn new_workbook_has_one_sheet() {
        let wb = Workbook::new();
        assert_eq!(wb.worksheets.len(), 1);
        assert_eq!(wb.get_sheet_names(), vec!["Sheet1".to_string()]);
        assert_eq!(wb.active(), 0);
    }

    #[test]
    fn empty_workbook_has_no_sheets() {
        let wb = Workbook::empty();
        assert!(wb.worksheets.is_empty());
    }

    #[test]
    fn create_sheet_titles_uniquely() {
        let mut wb = Workbook::new();
        wb.create_sheet(None).unwrap();
        wb.create_sheet(None).unwrap();
        assert_eq!(wb.get_sheet_names(), vec!["Sheet1", "Sheet2", "Sheet3"]);
        wb.create_sheet(Some("Data")).unwrap();
        assert!(wb.contains("Data"));
        assert_eq!(wb.get_index("Data"), Some(3));
    }

    #[test]
    fn create_sheet_at_index() {
        let mut wb = Workbook::new();
        wb.create_sheet_at(Some("First"), Some(0)).unwrap();
        assert_eq!(wb.get_sheet_names()[0], "First");
        // Indices stay contiguous after insertion.
        for (index, sheet) in wb.worksheets.iter().enumerate() {
            assert_eq!(sheet.index, index);
        }
    }

    #[test]
    fn remove_sheet_reindexes() {
        let mut wb = Workbook::new();
        wb.create_sheet(Some("A")).unwrap();
        wb.create_sheet(Some("B")).unwrap();
        wb.remove_sheet(0).unwrap();
        assert_eq!(wb.get_sheet_names(), vec!["A", "B"]);
        assert_eq!(wb.worksheets[0].index, 0);
        assert!(wb.remove_sheet(9).is_err());
    }

    #[test]
    fn read_only_workbook_rejects_mutation() {
        let mut wb = Workbook::new();
        wb.set_optimized_read();
        assert!(wb.create_sheet(None).is_err());
        assert!(wb.remove_sheet(0).is_err());
        assert!(wb.add_sheet(Worksheet::new("X").unwrap(), None).is_err());
    }

    #[test]
    fn active_sheet_accessors() {
        let mut wb = Workbook::new();
        wb.create_sheet(Some("Second")).unwrap();
        wb.set_active(1).unwrap();
        assert_eq!(wb.active(), 1);
        assert_eq!(wb.active_sheet().unwrap().title(), "Second");
        wb.active_sheet_mut().unwrap().set_freeze_panes("B2");
        assert_eq!(wb.worksheets[1].freeze_panes.as_deref(), Some("B2"));
        assert!(wb.set_active(99).is_err());
    }

    #[test]
    fn named_ranges_round_trip() {
        let mut wb = Workbook::new();
        wb.create_named_range("MyRef", 0, "$A$1", None).unwrap();
        let found = wb.get_named_range("MyRef").unwrap();
        assert_eq!(found.name(), "MyRef");
        assert_eq!(found.scope(), None);
        match found {
            DefinedName::Range(r) => assert_eq!(r.destinations, vec![(0, "$A$1".to_string())]),
            other => panic!("expected a range, got {other:?}"),
        }
        assert!(wb.remove_named_range("MyRef").is_ok());
        assert!(wb.remove_named_range("MyRef").is_err());
    }

    #[test]
    fn value_names_are_supported() {
        let mut wb = Workbook::new();
        wb.add_named_value("MyValue", "9.99", None);
        assert_eq!(wb.get_named_range("MyValue").unwrap().name(), "MyValue");
        match wb.get_named_range("MyValue").unwrap() {
            DefinedName::Value(v) => assert_eq!(v.value, "9.99"),
            other => panic!("expected a value, got {other:?}"),
        }
    }

    #[test]
    fn print_titles_register_scoped_names() {
        let mut wb = Workbook::new();
        wb.create_sheet(Some("S2")).unwrap();
        wb.add_print_title(0, 2, "rows").unwrap();
        wb.add_print_title(1, 3, "cols").unwrap();
        let ranges = wb.get_named_ranges();
        assert_eq!(ranges.len(), 2);
        match &ranges[0] {
            DefinedName::Range(r) => {
                assert_eq!(r.name, "_xlnm.Print_Titles");
                assert_eq!(r.destinations[0].1, "$1:$2");
                assert_eq!(r.scope, Some(0));
            }
            other => panic!("expected a range, got {other:?}"),
        }
        match &ranges[1] {
            DefinedName::Range(r) => assert_eq!(r.destinations[0].1, "$A:$C"),
            other => panic!("expected a range, got {other:?}"),
        }
    }

    #[test]
    fn appending_rows_writes_them() {
        let mut wb = Workbook::new();
        wb.append_rows(
            0,
            &[
                vec![CellValue::text("a"), CellValue::Number(1.0)],
                vec![CellValue::text("b"), CellValue::Number(2.0)],
            ],
        )
        .unwrap();
        let sheet = &wb.worksheets[0];
        assert_eq!(sheet.cell_value("A2"), Some(CellValue::text("a")));
        assert_eq!(sheet.cell_value("B3"), Some(CellValue::Number(2.0)));
        assert!(wb.append_rows(9, &[]).is_err());
    }

    #[test]
    fn base_date_propagates_to_cells() {
        let mut wb = Workbook::new();
        wb.properties.excel_base_date = BaseDate::Mac1904;
        wb.get_sheet_by_name_mut("Sheet1").unwrap().context = wb.cell_context();
        wb.worksheets[0]
            .set(
                "A1",
                CellValue::Date(NaiveDate::from_ymd_opt(1904, 1, 1).unwrap()),
            )
            .unwrap();
        // The stored serial is the Mac calendar's zero, and reading it back gives the date.
        assert_eq!(
            *wb.worksheets[0].get_cell("A1").unwrap().internal_value(),
            CellValue::Number(0.0)
        );
        assert_eq!(
            wb.worksheets[0].cell_value("A1"),
            Some(CellValue::Date(
                NaiveDate::from_ymd_opt(1904, 1, 1).unwrap()
            ))
        );
        assert_eq!(wb.base_date_offset(), CALENDAR_MAC_1904);
    }

    #[test]
    fn guess_types_propagates() {
        let mut wb = Workbook::new();
        wb.guess_types = true;
        assert!(wb.cell_context().guess_types);
        // The context is read before the sheet is borrowed mutably.
        let context = wb.cell_context();
        let sheet = wb.get_sheet_by_name_mut("Sheet1").unwrap();
        sheet.context = context;
        sheet.set("A1", CellValue::text("50%")).unwrap();
        assert_eq!(sheet.cell_value("A1"), Some(CellValue::Number(0.5)));
    }

    #[test]
    fn default_properties() {
        let props = DocumentProperties::new();
        assert_eq!(props.creator, "Unknown");
        assert_eq!(props.title, "Untitled");
        assert_eq!(props.company, "Microsoft Corporation");
        assert_eq!(props.excel_base_date, BaseDate::Windows1900);
        assert!(!DocumentSecurity::new().lock_structure);
    }

    #[test]
    fn the_defaults_match_excel_and_openpyxl() {
        let calc = CalcProperties::default();
        assert_eq!(calc.calc_id, 124_519);
        assert_eq!(calc.calc_mode.as_deref(), Some("auto"));
        // The one that matters: ferroxl writes formulas with no cached result, so without
        // this a workbook opens in Excel showing blanks.
        assert_eq!(calc.full_calc_on_load, Some(true));
    }

    #[test]
    fn manual_calculation_is_expressible() {
        let calc = CalcProperties::manual();
        assert_eq!(calc.calc_mode.as_deref(), Some("manual"));
        // Turning calculation off without turning recalculation-on-load off is how a workbook
        // ends up showing nothing at all, so the default has to survive the switch.
        assert_eq!(calc.full_calc_on_load, Some(true));
    }

    #[test]
    fn the_attributes_come_out_in_declaration_order() {
        let calc = CalcProperties {
            calc_mode: Some("manual".to_string()),
            iterate: Some(true),
            iterate_count: Some(100),
            iterate_delta: Some(0.001),
            force_full_calc: Some(true),
            ..CalcProperties::default()
        };
        let attrs = calc.attributes();
        let names: Vec<&str> = attrs.iter().map(|(k, _)| k.as_str()).collect();
        assert_eq!(
            names,
            [
                "calcId",
                "calcMode",
                "fullCalcOnLoad",
                "iterate",
                "iterateCount",
                "iterateDelta",
                "forceFullCalc",
            ]
        );
        assert!(attrs.contains(&("iterate".to_string(), "1".to_string())));
    }

    #[test]
    fn a_workbook_carries_its_calculation_properties() {
        let mut workbook = Workbook::new();
        assert_eq!(workbook.calculation.calc_mode.as_deref(), Some("auto"));
        workbook.set_calculation_properties(CalcProperties::manual());
        assert_eq!(workbook.calculation.calc_mode.as_deref(), Some("manual"));
    }

    #[test]
    fn the_calc_properties_survive_a_round_trip() {
        let mut workbook = Workbook::new();
        workbook.set_calculation_properties(CalcProperties {
            calc_mode: Some("manual".to_string()),
            full_calc_on_load: Some(false),
            iterate: Some(true),
            iterate_count: Some(100),
            ..CalcProperties::default()
        });
        let bytes = workbook.to_bytes().expect("saved");
        let loaded = crate::reader::excel::load_workbook_from_bytes(bytes, Default::default())
            .expect("loaded");
        assert_eq!(loaded.calculation.calc_mode.as_deref(), Some("manual"));
        assert_eq!(loaded.calculation.full_calc_on_load, Some(false));
        assert_eq!(loaded.calculation.iterate, Some(true));
        assert_eq!(loaded.calculation.iterate_count, Some(100));
    }
}