spreadsheet_ods/
workbook_.rs

1//!
2//! Workbook
3//!
4
5use get_size::GetSize;
6use get_size_derive::GetSize;
7use std::borrow::Borrow;
8use std::fmt;
9use std::fmt::Formatter;
10use std::hash::Hash;
11
12use icu_locid::{locale, Locale};
13
14use crate::config::Config;
15use crate::defaultstyles::{DefaultFormat, DefaultStyle};
16use crate::ds::detach::{Detach, Detached};
17use crate::format::ValueFormatTrait;
18use crate::io::read::default_settings;
19use crate::io::NamespaceMap;
20use crate::manifest::Manifest;
21use crate::metadata::Metadata;
22use crate::sheet_::Sheet;
23use crate::style::{
24    ColStyle, ColStyleRef, FontFaceDecl, GraphicStyle, GraphicStyleRef, MasterPage, MasterPageRef,
25    PageStyle, PageStyleRef, ParagraphStyle, ParagraphStyleRef, RowStyle, RowStyleRef, RubyStyle,
26    RubyStyleRef, TableStyle, TableStyleRef, TextStyle, TextStyleRef,
27};
28use crate::validation::{Validation, ValidationRef};
29use crate::value_::ValueType;
30use crate::xlink::{XLinkActuate, XLinkType};
31use crate::xmltree::{XmlContent, XmlTag};
32use crate::{
33    locale, CellStyle, CellStyleRef, HashMap, ValueFormatBoolean, ValueFormatCurrency,
34    ValueFormatDateTime, ValueFormatNumber, ValueFormatPercentage, ValueFormatRef, ValueFormatText,
35    ValueFormatTimeDuration,
36};
37
38/// Book is the main structure for the Spreadsheet.
39#[derive(Clone, GetSize)]
40pub struct WorkBook {
41    /// The data.
42    pub(crate) sheets: Vec<Detach<Sheet>>,
43
44    /// ODS Version
45    pub(crate) version: String,
46
47    /// FontDecl hold the style:font-face elements
48    pub(crate) fonts: HashMap<String, FontFaceDecl>,
49
50    /// Auto-Styles. Maps the prefix to a number.
51    pub(crate) autonum: HashMap<String, u32>,
52
53    /// Scripts
54    pub(crate) scripts: Vec<Script>,
55    pub(crate) event_listener: HashMap<String, EventListener>,
56
57    /// Styles hold the style:style elements.
58    pub(crate) tablestyles: HashMap<TableStyleRef, TableStyle>,
59    pub(crate) rowstyles: HashMap<RowStyleRef, RowStyle>,
60    pub(crate) colstyles: HashMap<ColStyleRef, ColStyle>,
61    pub(crate) cellstyles: HashMap<CellStyleRef, CellStyle>,
62    pub(crate) paragraphstyles: HashMap<ParagraphStyleRef, ParagraphStyle>,
63    pub(crate) textstyles: HashMap<TextStyleRef, TextStyle>,
64    pub(crate) rubystyles: HashMap<RubyStyleRef, RubyStyle>,
65    pub(crate) graphicstyles: HashMap<GraphicStyleRef, GraphicStyle>,
66
67    /// Value-styles are actual formatting instructions for various datatypes.
68    /// Represents the various number:xxx-style elements.
69    pub(crate) formats_boolean: HashMap<String, ValueFormatBoolean>,
70    pub(crate) formats_number: HashMap<String, ValueFormatNumber>,
71    pub(crate) formats_percentage: HashMap<String, ValueFormatPercentage>,
72    pub(crate) formats_currency: HashMap<String, ValueFormatCurrency>,
73    pub(crate) formats_text: HashMap<String, ValueFormatText>,
74    pub(crate) formats_datetime: HashMap<String, ValueFormatDateTime>,
75    pub(crate) formats_timeduration: HashMap<String, ValueFormatTimeDuration>,
76
77    /// Default-styles per Type.
78    /// This is only used when writing the ods file.
79    pub(crate) def_styles: HashMap<ValueType, CellStyleRef>,
80
81    /// Page-layout data.
82    pub(crate) pagestyles: HashMap<PageStyleRef, PageStyle>,
83    pub(crate) masterpages: HashMap<MasterPageRef, MasterPage>,
84
85    /// Validations.
86    pub(crate) validations: HashMap<ValidationRef, Validation>,
87
88    /// Configuration data. Internal cache for all values.
89    /// Mapped into WorkBookConfig, SheetConfig.
90    pub(crate) config: Detach<Config>,
91    /// User modifiable config.
92    pub(crate) workbook_config: WorkBookConfig,
93    /// Keeps all the namespaces.
94    pub(crate) xmlns: HashMap<String, NamespaceMap>,
95
96    /// All extra files contained in the zip manifest are copied here.
97    pub(crate) manifest: HashMap<String, Manifest>,
98
99    /// Metadata
100    pub(crate) metadata: Metadata,
101
102    /// other stuff ...
103    pub(crate) extra: Vec<XmlTag>,
104}
105
106impl fmt::Debug for WorkBook {
107    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
108        writeln!(f, "{:?}", self.version)?;
109        for s in self.sheets.iter() {
110            writeln!(f, "{:?}", s)?;
111        }
112        for s in self.fonts.values() {
113            writeln!(f, "{:?}", s)?;
114        }
115        for s in self.tablestyles.values() {
116            writeln!(f, "{:?}", s)?;
117        }
118        for s in self.rowstyles.values() {
119            writeln!(f, "{:?}", s)?;
120        }
121        for s in self.colstyles.values() {
122            writeln!(f, "{:?}", s)?;
123        }
124        for s in self.cellstyles.values() {
125            writeln!(f, "{:?}", s)?;
126        }
127        for s in self.paragraphstyles.values() {
128            writeln!(f, "{:?}", s)?;
129        }
130        for s in self.textstyles.values() {
131            writeln!(f, "{:?}", s)?;
132        }
133        for s in self.rubystyles.values() {
134            writeln!(f, "{:?}", s)?;
135        }
136        for s in self.graphicstyles.values() {
137            writeln!(f, "{:?}", s)?;
138        }
139        for s in self.formats_boolean.values() {
140            writeln!(f, "{:?}", s)?;
141        }
142        for s in self.formats_number.values() {
143            writeln!(f, "{:?}", s)?;
144        }
145        for s in self.formats_percentage.values() {
146            writeln!(f, "{:?}", s)?;
147        }
148        for s in self.formats_currency.values() {
149            writeln!(f, "{:?}", s)?;
150        }
151        for s in self.formats_text.values() {
152            writeln!(f, "{:?}", s)?;
153        }
154        for s in self.formats_datetime.values() {
155            writeln!(f, "{:?}", s)?;
156        }
157        for s in self.formats_timeduration.values() {
158            writeln!(f, "{:?}", s)?;
159        }
160        for (t, s) in &self.def_styles {
161            writeln!(f, "{:?} -> {:?}", t, s)?;
162        }
163        for s in self.pagestyles.values() {
164            writeln!(f, "{:?}", s)?;
165        }
166        for s in self.masterpages.values() {
167            writeln!(f, "{:?}", s)?;
168        }
169        for s in self.validations.values() {
170            writeln!(f, "{:?}", s)?;
171        }
172        writeln!(f, "{:?}", &self.workbook_config)?;
173        for v in self.manifest.values() {
174            writeln!(f, "extras {:?}", v)?;
175        }
176        writeln!(f, "{:?}", &self.metadata)?;
177        for xtr in &self.extra {
178            writeln!(f, "extras {:?}", xtr)?;
179        }
180        Ok(())
181    }
182}
183
184/// Autogenerate a stylename. Runs a counter with the prefix and
185/// checks for existence.
186fn auto_style_name2<K, V>(
187    autonum: &mut HashMap<String, u32>,
188    prefix: &str,
189    styles: &HashMap<K, V>,
190) -> String
191where
192    K: Borrow<str> + Hash + Eq,
193{
194    let mut cnt = if let Some(n) = autonum.get(prefix) {
195        n + 1
196    } else {
197        0
198    };
199
200    let style_name = loop {
201        let style_name = format!("{}{}", prefix, cnt);
202        if !styles.contains_key(&style_name) {
203            break style_name;
204        }
205        cnt += 1;
206    };
207
208    autonum.insert(prefix.to_string(), cnt);
209
210    style_name
211}
212
213/// Autogenerate a stylename. Runs a counter with the prefix and
214/// checks for existence.
215fn auto_style_name<T>(
216    autonum: &mut HashMap<String, u32>,
217    prefix: &str,
218    styles: &HashMap<String, T>,
219) -> String {
220    let mut cnt = if let Some(n) = autonum.get(prefix) {
221        n + 1
222    } else {
223        0
224    };
225
226    let style_name = loop {
227        let style_name = format!("{}{}", prefix, cnt);
228        if !styles.contains_key(&style_name) {
229            break style_name;
230        }
231        cnt += 1;
232    };
233
234    autonum.insert(prefix.to_string(), cnt);
235
236    style_name
237}
238
239impl Default for WorkBook {
240    fn default() -> Self {
241        WorkBook::new(locale!("en"))
242    }
243}
244
245impl WorkBook {
246    /// Creates a new, completely empty workbook.
247    ///
248    /// WorkBook::locale_settings can be used to initialize default styles.
249    pub fn new_empty() -> Self {
250        WorkBook {
251            sheets: Default::default(),
252            version: "1.3".to_string(),
253            fonts: Default::default(),
254            autonum: Default::default(),
255            scripts: Default::default(),
256            event_listener: Default::default(),
257            tablestyles: Default::default(),
258            rowstyles: Default::default(),
259            colstyles: Default::default(),
260            cellstyles: Default::default(),
261            paragraphstyles: Default::default(),
262            textstyles: Default::default(),
263            rubystyles: Default::default(),
264            graphicstyles: Default::default(),
265            formats_boolean: Default::default(),
266            formats_number: Default::default(),
267            formats_percentage: Default::default(),
268            formats_currency: Default::default(),
269            formats_text: Default::default(),
270            formats_datetime: Default::default(),
271            formats_timeduration: Default::default(),
272            def_styles: Default::default(),
273            pagestyles: Default::default(),
274            masterpages: Default::default(),
275            validations: Default::default(),
276            config: default_settings(),
277            workbook_config: Default::default(),
278            extra: vec![],
279            manifest: Default::default(),
280            metadata: Default::default(),
281            xmlns: Default::default(),
282        }
283    }
284
285    /// Creates a new workbook, and initializes default styles according
286    /// to the given locale.
287    ///
288    /// If the locale is not supported no ValueFormat's are set and all
289    /// depends on the application opening the spreadsheet.
290    ///
291    /// The available locales can be activated via feature-flags.
292    pub fn new(locale: Locale) -> Self {
293        let mut wb = WorkBook::new_empty();
294        wb.locale_settings(locale);
295        wb
296    }
297
298    /// Creates a set of default formats and styles for every value-type.
299    ///
300    /// If the locale is not supported no ValueFormat's are set and all
301    /// depends on the application opening the spreadsheet.
302    ///
303    /// The available locales can be activated via feature-flags.
304    pub fn locale_settings(&mut self, locale: Locale) {
305        if let Some(lf) = locale::localized_format(locale) {
306            self.add_boolean_format(lf.boolean_format());
307            self.add_number_format(lf.number_format());
308            self.add_percentage_format(lf.percentage_format());
309            self.add_currency_format(lf.currency_format());
310            self.add_datetime_format(lf.date_format());
311            self.add_datetime_format(lf.datetime_format());
312            self.add_datetime_format(lf.time_of_day_format());
313            self.add_timeduration_format(lf.time_interval_format());
314        }
315
316        self.add_cellstyle(CellStyle::new(DefaultStyle::bool(), &DefaultFormat::bool()));
317        self.add_cellstyle(CellStyle::new(
318            DefaultStyle::number(),
319            &DefaultFormat::number(),
320        ));
321        self.add_cellstyle(CellStyle::new(
322            DefaultStyle::percent(),
323            &DefaultFormat::percent(),
324        ));
325        self.add_cellstyle(CellStyle::new(
326            DefaultStyle::currency(),
327            &DefaultFormat::currency(),
328        ));
329        self.add_cellstyle(CellStyle::new(DefaultStyle::date(), &DefaultFormat::date()));
330        self.add_cellstyle(CellStyle::new(
331            DefaultStyle::datetime(),
332            &DefaultFormat::datetime(),
333        ));
334        self.add_cellstyle(CellStyle::new(
335            DefaultStyle::time_of_day(),
336            &DefaultFormat::time_of_day(),
337        ));
338        self.add_cellstyle(CellStyle::new(
339            DefaultStyle::time_interval(),
340            &DefaultFormat::time_interval(),
341        ));
342
343        self.add_def_style(ValueType::Boolean, DefaultStyle::bool());
344        self.add_def_style(ValueType::Number, DefaultStyle::number());
345        self.add_def_style(ValueType::Percentage, DefaultStyle::percent());
346        self.add_def_style(ValueType::Currency, DefaultStyle::currency());
347        self.add_def_style(ValueType::DateTime, DefaultStyle::date());
348        self.add_def_style(ValueType::TimeDuration, DefaultStyle::time_interval());
349    }
350
351    /// ODS version. Defaults to 1.3.
352    pub fn version(&self) -> &String {
353        &self.version
354    }
355
356    /// ODS version. Defaults to 1.3.
357    /// It's not advised to set another value.
358    pub fn set_version(&mut self, version: String) {
359        self.version = version;
360    }
361
362    /// Configuration flags.
363    pub fn config(&self) -> &WorkBookConfig {
364        &self.workbook_config
365    }
366
367    /// Configuration flags.
368    pub fn config_mut(&mut self) -> &mut WorkBookConfig {
369        &mut self.workbook_config
370    }
371
372    /// Number of sheets.
373    pub fn num_sheets(&self) -> usize {
374        self.sheets.len()
375    }
376
377    /// Finds the sheet index by the sheet-name.
378    pub fn sheet_idx<S: AsRef<str>>(&self, name: S) -> Option<usize> {
379        for (idx, sheet) in self.sheets.iter().enumerate() {
380            if sheet.name() == name.as_ref() {
381                return Some(idx);
382            }
383        }
384        None
385    }
386
387    /// Detaches a sheet.
388    /// Useful if you have to make mutating calls to the workbook and
389    /// the sheet intermixed.
390    ///
391    /// Warning
392    ///
393    /// The sheet has to be re-attached before saving the workbook.
394    ///
395    /// Panics
396    ///
397    /// Panics if the sheet has already been detached.
398    /// Panics if n is out of bounds.
399    pub fn detach_sheet(&mut self, n: usize) -> Detached<usize, Sheet> {
400        self.sheets[n].detach(n)
401    }
402
403    /// Reattaches the sheet in the place it was before.
404    ///
405    /// Panics
406    ///
407    /// Panics if n is out of bounds.
408    pub fn attach_sheet(&mut self, sheet: Detached<usize, Sheet>) {
409        self.sheets[Detached::key(&sheet)].attach(sheet)
410    }
411
412    /// Returns a certain sheet.
413    ///
414    /// Panics
415    ///
416    /// Panics if n is out of bounds.
417    pub fn sheet(&self, n: usize) -> &Sheet {
418        self.sheets[n].as_ref()
419    }
420
421    /// Returns a certain sheet.
422    ///
423    /// Panics
424    ///
425    /// Panics if n does not exist.
426    pub fn sheet_mut(&mut self, n: usize) -> &mut Sheet {
427        self.sheets[n].as_mut()
428    }
429
430    /// Returns iterator over sheets.
431    pub fn iter_sheets(&self) -> impl Iterator<Item = &Sheet> {
432        self.sheets.iter().map(|sheet| &**sheet)
433    }
434
435    /// Inserts the sheet at the given position.
436    pub fn insert_sheet(&mut self, i: usize, sheet: Sheet) {
437        self.sheets.insert(i, sheet.into());
438    }
439
440    /// Appends a sheet.
441    pub fn push_sheet(&mut self, sheet: Sheet) {
442        self.sheets.push(sheet.into());
443    }
444
445    /// Removes a sheet from the table.
446    ///
447    /// Panics
448    ///
449    /// Panics if the sheet was detached.
450    pub fn remove_sheet(&mut self, n: usize) -> Sheet {
451        self.sheets.remove(n).take()
452    }
453
454    /// Scripts.
455    pub fn add_script(&mut self, v: Script) {
456        self.scripts.push(v);
457    }
458
459    /// Scripts.
460    pub fn iter_scripts(&self) -> impl Iterator<Item = &Script> {
461        self.scripts.iter()
462    }
463
464    /// Scripts
465    pub fn scripts(&self) -> &Vec<Script> {
466        &self.scripts
467    }
468
469    /// Scripts
470    pub fn scripts_mut(&mut self) -> &mut Vec<Script> {
471        &mut self.scripts
472    }
473
474    /// Event-Listener
475    pub fn add_event_listener(&mut self, e: EventListener) {
476        self.event_listener.insert(e.event_name.clone(), e);
477    }
478
479    /// Event-Listener
480    pub fn remove_event_listener(&mut self, event_name: &str) -> Option<EventListener> {
481        self.event_listener.remove(event_name)
482    }
483
484    /// Event-Listener
485    pub fn iter_event_listeners(&self) -> impl Iterator<Item = &EventListener> {
486        self.event_listener.values()
487    }
488
489    /// Event-Listener
490    pub fn event_listener(&self, event_name: &str) -> Option<&EventListener> {
491        self.event_listener.get(event_name)
492    }
493
494    /// Event-Listener
495    pub fn event_listener_mut(&mut self, event_name: &str) -> Option<&mut EventListener> {
496        self.event_listener.get_mut(event_name)
497    }
498
499    /// Adds a default-style for all new values.
500    /// This information is only used when writing the data to the ODS file.
501    pub fn add_def_style(&mut self, value_type: ValueType, style: CellStyleRef) {
502        self.def_styles.insert(value_type, style);
503    }
504
505    /// Returns the default style name.
506    pub fn def_style(&self, value_type: ValueType) -> Option<&CellStyleRef> {
507        self.def_styles.get(&value_type)
508    }
509
510    /// Adds a font.
511    pub fn add_font(&mut self, font: FontFaceDecl) {
512        self.fonts.insert(font.name().to_string(), font);
513    }
514
515    /// Removes a font.
516    pub fn remove_font(&mut self, name: &str) -> Option<FontFaceDecl> {
517        self.fonts.remove(name)
518    }
519
520    /// Iterates the fonts.
521    pub fn iter_fonts(&self) -> impl Iterator<Item = &FontFaceDecl> {
522        self.fonts.values()
523    }
524
525    /// Returns the FontDecl.
526    pub fn font(&self, name: &str) -> Option<&FontFaceDecl> {
527        self.fonts.get(name)
528    }
529
530    /// Returns a mutable FontDecl.
531    pub fn font_mut(&mut self, name: &str) -> Option<&mut FontFaceDecl> {
532        self.fonts.get_mut(name)
533    }
534
535    /// Adds a style.
536    /// Unnamed styles will be assigned an automatic name.
537    pub fn add_tablestyle(&mut self, mut style: TableStyle) -> TableStyleRef {
538        if style.name().is_empty() {
539            style.set_name(auto_style_name2(&mut self.autonum, "ta", &self.tablestyles));
540        }
541        let sref = style.style_ref();
542        self.tablestyles.insert(style.style_ref(), style);
543        sref
544    }
545
546    /// Removes a style.
547    pub fn remove_tablestyle<S: AsRef<str>>(&mut self, name: S) -> Option<TableStyle> {
548        self.tablestyles.remove(name.as_ref())
549    }
550
551    /// Iterates the table-styles.
552    pub fn iter_table_styles(&self) -> impl Iterator<Item = &TableStyle> {
553        self.tablestyles.values()
554    }
555
556    /// Returns the style.
557    pub fn tablestyle<S: AsRef<str>>(&self, name: S) -> Option<&TableStyle> {
558        self.tablestyles.get(name.as_ref())
559    }
560
561    /// Returns the mutable style.
562    pub fn tablestyle_mut<S: AsRef<str>>(&mut self, name: S) -> Option<&mut TableStyle> {
563        self.tablestyles.get_mut(name.as_ref())
564    }
565
566    /// Adds a style.
567    /// Unnamed styles will be assigned an automatic name.
568    pub fn add_rowstyle(&mut self, mut style: RowStyle) -> RowStyleRef {
569        if style.name().is_empty() {
570            style.set_name(auto_style_name2(&mut self.autonum, "ro", &self.rowstyles));
571        }
572        let sref = style.style_ref();
573        self.rowstyles.insert(style.style_ref(), style);
574        sref
575    }
576
577    /// Removes a style.
578    pub fn remove_rowstyle<S: AsRef<str>>(&mut self, name: S) -> Option<RowStyle> {
579        self.rowstyles.remove(name.as_ref())
580    }
581
582    /// Returns the style.
583    pub fn rowstyle<S: AsRef<str>>(&self, name: S) -> Option<&RowStyle> {
584        self.rowstyles.get(name.as_ref())
585    }
586
587    /// Returns the mutable style.
588    pub fn rowstyle_mut<S: AsRef<str>>(&mut self, name: S) -> Option<&mut RowStyle> {
589        self.rowstyles.get_mut(name.as_ref())
590    }
591
592    /// Returns iterator over styles.
593    pub fn iter_rowstyles(&self) -> impl Iterator<Item = &RowStyle> {
594        self.rowstyles.values()
595    }
596
597    /// Adds a style.
598    /// Unnamed styles will be assigned an automatic name.
599    pub fn add_colstyle(&mut self, mut style: ColStyle) -> ColStyleRef {
600        if style.name().is_empty() {
601            style.set_name(auto_style_name2(&mut self.autonum, "co", &self.colstyles));
602        }
603        let sref = style.style_ref();
604        self.colstyles.insert(style.style_ref(), style);
605        sref
606    }
607
608    /// Removes a style.
609    pub fn remove_colstyle<S: AsRef<str>>(&mut self, name: S) -> Option<ColStyle> {
610        self.colstyles.remove(name.as_ref())
611    }
612
613    /// Returns the style.
614    pub fn colstyle<S: AsRef<str>>(&self, name: S) -> Option<&ColStyle> {
615        self.colstyles.get(name.as_ref())
616    }
617
618    /// Returns the mutable style.
619    pub fn colstyle_mut<S: AsRef<str>>(&mut self, name: S) -> Option<&mut ColStyle> {
620        self.colstyles.get_mut(name.as_ref())
621    }
622
623    /// Returns iterator over styles.
624    pub fn iter_colstyles(&self) -> impl Iterator<Item = &ColStyle> {
625        self.colstyles.values()
626    }
627
628    /// Adds a style.
629    /// Unnamed styles will be assigned an automatic name.
630    pub fn add_cellstyle(&mut self, mut style: CellStyle) -> CellStyleRef {
631        if style.name().is_empty() {
632            style.set_name(auto_style_name2(&mut self.autonum, "ce", &self.cellstyles));
633        }
634        let sref = style.style_ref();
635        self.cellstyles.insert(style.style_ref(), style);
636        sref
637    }
638
639    /// Removes a style.
640    pub fn remove_cellstyle<S: AsRef<str>>(&mut self, name: S) -> Option<CellStyle> {
641        self.cellstyles.remove(name.as_ref())
642    }
643
644    /// Returns iterator over styles.
645    pub fn iter_cellstyles(&self) -> impl Iterator<Item = &CellStyle> {
646        self.cellstyles.values()
647    }
648
649    /// Returns the style.
650    pub fn cellstyle<S: AsRef<str>>(&self, name: S) -> Option<&CellStyle> {
651        self.cellstyles.get(name.as_ref())
652    }
653
654    /// Returns the mutable style.
655    pub fn cellstyle_mut<S: AsRef<str>>(&mut self, name: S) -> Option<&mut CellStyle> {
656        self.cellstyles.get_mut(name.as_ref())
657    }
658
659    /// Adds a style.
660    /// Unnamed styles will be assigned an automatic name.
661    pub fn add_paragraphstyle(&mut self, mut style: ParagraphStyle) -> ParagraphStyleRef {
662        if style.name().is_empty() {
663            style.set_name(auto_style_name2(
664                &mut self.autonum,
665                "para",
666                &self.paragraphstyles,
667            ));
668        }
669        let sref = style.style_ref();
670        self.paragraphstyles.insert(style.style_ref(), style);
671        sref
672    }
673
674    /// Removes a style.
675    pub fn remove_paragraphstyle<S: AsRef<str>>(&mut self, name: S) -> Option<ParagraphStyle> {
676        self.paragraphstyles.remove(name.as_ref())
677    }
678
679    /// Returns iterator over styles.
680    pub fn iter_paragraphstyles(&self) -> impl Iterator<Item = &ParagraphStyle> {
681        self.paragraphstyles.values()
682    }
683
684    /// Returns the style.
685    pub fn paragraphstyle<S: AsRef<str>>(&self, name: S) -> Option<&ParagraphStyle> {
686        self.paragraphstyles.get(name.as_ref())
687    }
688
689    /// Returns the mutable style.
690    pub fn paragraphstyle_mut<S: AsRef<str>>(&mut self, name: S) -> Option<&mut ParagraphStyle> {
691        self.paragraphstyles.get_mut(name.as_ref())
692    }
693
694    /// Adds a style.
695    /// Unnamed styles will be assigned an automatic name.
696    pub fn add_textstyle(&mut self, mut style: TextStyle) -> TextStyleRef {
697        if style.name().is_empty() {
698            style.set_name(auto_style_name2(&mut self.autonum, "txt", &self.textstyles));
699        }
700        let sref = style.style_ref();
701        self.textstyles.insert(style.style_ref(), style);
702        sref
703    }
704
705    /// Removes a style.
706    pub fn remove_textstyle<S: AsRef<str>>(&mut self, name: S) -> Option<TextStyle> {
707        self.textstyles.remove(name.as_ref())
708    }
709
710    /// Returns iterator over styles.
711    pub fn iter_textstyles(&self) -> impl Iterator<Item = &TextStyle> {
712        self.textstyles.values()
713    }
714
715    /// Returns the style.
716    pub fn textstyle<S: AsRef<str>>(&self, name: S) -> Option<&TextStyle> {
717        self.textstyles.get(name.as_ref())
718    }
719
720    /// Returns the mutable style.
721    pub fn textstyle_mut<S: AsRef<str>>(&mut self, name: S) -> Option<&mut TextStyle> {
722        self.textstyles.get_mut(name.as_ref())
723    }
724
725    /// Adds a style.
726    /// Unnamed styles will be assigned an automatic name.
727    pub fn add_rubystyle(&mut self, mut style: RubyStyle) -> RubyStyleRef {
728        if style.name().is_empty() {
729            style.set_name(auto_style_name2(
730                &mut self.autonum,
731                "ruby",
732                &self.rubystyles,
733            ));
734        }
735        let sref = style.style_ref();
736        self.rubystyles.insert(style.style_ref(), style);
737        sref
738    }
739
740    /// Removes a style.
741    pub fn remove_rubystyle<S: AsRef<str>>(&mut self, name: S) -> Option<RubyStyle> {
742        self.rubystyles.remove(name.as_ref())
743    }
744
745    /// Returns iterator over styles.
746    pub fn iter_rubystyles(&self) -> impl Iterator<Item = &RubyStyle> {
747        self.rubystyles.values()
748    }
749
750    /// Returns the style.
751    pub fn rubystyle<S: AsRef<str>>(&self, name: S) -> Option<&RubyStyle> {
752        self.rubystyles.get(name.as_ref())
753    }
754
755    /// Returns the mutable style.
756    pub fn rubystyle_mut<S: AsRef<str>>(&mut self, name: S) -> Option<&mut RubyStyle> {
757        self.rubystyles.get_mut(name.as_ref())
758    }
759
760    /// Adds a style.
761    /// Unnamed styles will be assigned an automatic name.
762    pub fn add_graphicstyle(&mut self, mut style: GraphicStyle) -> GraphicStyleRef {
763        if style.name().is_empty() {
764            style.set_name(auto_style_name2(
765                &mut self.autonum,
766                "gr",
767                &self.graphicstyles,
768            ));
769        }
770        let sref = style.style_ref();
771        self.graphicstyles.insert(style.style_ref(), style);
772        sref
773    }
774
775    /// Removes a style.
776    pub fn remove_graphicstyle<S: AsRef<str>>(&mut self, name: S) -> Option<GraphicStyle> {
777        self.graphicstyles.remove(name.as_ref())
778    }
779
780    /// Returns iterator over styles.
781    pub fn iter_graphicstyles(&self) -> impl Iterator<Item = &GraphicStyle> {
782        self.graphicstyles.values()
783    }
784
785    /// Returns the style.
786    pub fn graphicstyle<S: AsRef<str>>(&self, name: S) -> Option<&GraphicStyle> {
787        self.graphicstyles.get(name.as_ref())
788    }
789
790    /// Returns the mutable style.
791    pub fn graphicstyle_mut<S: AsRef<str>>(&mut self, name: S) -> Option<&mut GraphicStyle> {
792        self.graphicstyles.get_mut(name.as_ref())
793    }
794
795    /// Adds a value format.
796    /// Unnamed formats will be assigned an automatic name.
797    pub fn add_boolean_format(&mut self, mut vstyle: ValueFormatBoolean) -> ValueFormatRef {
798        if vstyle.name().is_empty() {
799            vstyle.set_name(
800                auto_style_name(&mut self.autonum, "val_boolean", &self.formats_boolean).as_str(),
801            );
802        }
803        let sref = vstyle.format_ref();
804        self.formats_boolean
805            .insert(vstyle.name().to_string(), vstyle);
806        sref
807    }
808
809    /// Removes the format.
810    pub fn remove_boolean_format(&mut self, name: &str) -> Option<ValueFormatBoolean> {
811        self.formats_boolean.remove(name)
812    }
813
814    /// Returns iterator over formats.
815    pub fn iter_boolean_formats(&self) -> impl Iterator<Item = &ValueFormatBoolean> {
816        self.formats_boolean.values()
817    }
818
819    /// Returns the format.
820    pub fn boolean_format(&self, name: &str) -> Option<&ValueFormatBoolean> {
821        self.formats_boolean.get(name)
822    }
823
824    /// Returns the mutable format.
825    pub fn boolean_format_mut(&mut self, name: &str) -> Option<&mut ValueFormatBoolean> {
826        self.formats_boolean.get_mut(name)
827    }
828
829    /// Adds a value format.
830    /// Unnamed formats will be assigned an automatic name.
831    pub fn add_number_format(&mut self, mut vstyle: ValueFormatNumber) -> ValueFormatRef {
832        if vstyle.name().is_empty() {
833            vstyle.set_name(
834                auto_style_name(&mut self.autonum, "val_number", &self.formats_number).as_str(),
835            );
836        }
837        let sref = vstyle.format_ref();
838        self.formats_number
839            .insert(vstyle.name().to_string(), vstyle);
840        sref
841    }
842
843    /// Removes the format.
844    pub fn remove_number_format(&mut self, name: &str) -> Option<ValueFormatNumber> {
845        self.formats_number.remove(name)
846    }
847
848    /// Returns iterator over formats.
849    pub fn iter_number_formats(&self) -> impl Iterator<Item = &ValueFormatNumber> {
850        self.formats_number.values()
851    }
852
853    /// Returns the format.
854    pub fn number_format(&self, name: &str) -> Option<&ValueFormatBoolean> {
855        self.formats_boolean.get(name)
856    }
857
858    /// Returns the mutable format.
859    pub fn number_format_mut(&mut self, name: &str) -> Option<&mut ValueFormatBoolean> {
860        self.formats_boolean.get_mut(name)
861    }
862
863    /// Adds a value format.
864    /// Unnamed formats will be assigned an automatic name.
865    pub fn add_percentage_format(&mut self, mut vstyle: ValueFormatPercentage) -> ValueFormatRef {
866        if vstyle.name().is_empty() {
867            vstyle.set_name(
868                auto_style_name(
869                    &mut self.autonum,
870                    "val_percentage",
871                    &self.formats_percentage,
872                )
873                .as_str(),
874            );
875        }
876        let sref = vstyle.format_ref();
877        self.formats_percentage
878            .insert(vstyle.name().to_string(), vstyle);
879        sref
880    }
881
882    /// Removes the format.
883    pub fn remove_percentage_format(&mut self, name: &str) -> Option<ValueFormatPercentage> {
884        self.formats_percentage.remove(name)
885    }
886
887    /// Returns iterator over formats.
888    pub fn iter_percentage_formats(&self) -> impl Iterator<Item = &ValueFormatPercentage> {
889        self.formats_percentage.values()
890    }
891
892    /// Returns the format.
893    pub fn percentage_format(&self, name: &str) -> Option<&ValueFormatPercentage> {
894        self.formats_percentage.get(name)
895    }
896
897    /// Returns the mutable format.
898    pub fn percentage_format_mut(&mut self, name: &str) -> Option<&mut ValueFormatPercentage> {
899        self.formats_percentage.get_mut(name)
900    }
901
902    /// Adds a value format.
903    /// Unnamed formats will be assigned an automatic name.
904    pub fn add_currency_format(&mut self, mut vstyle: ValueFormatCurrency) -> ValueFormatRef {
905        if vstyle.name().is_empty() {
906            vstyle.set_name(
907                auto_style_name(&mut self.autonum, "val_currency", &self.formats_currency).as_str(),
908            );
909        }
910        let sref = vstyle.format_ref();
911        self.formats_currency
912            .insert(vstyle.name().to_string(), vstyle);
913        sref
914    }
915
916    /// Removes the format.
917    pub fn remove_currency_format(&mut self, name: &str) -> Option<ValueFormatCurrency> {
918        self.formats_currency.remove(name)
919    }
920
921    /// Returns iterator over formats.
922    pub fn iter_currency_formats(&self) -> impl Iterator<Item = &ValueFormatCurrency> {
923        self.formats_currency.values()
924    }
925
926    /// Returns the format.
927    pub fn currency_format(&self, name: &str) -> Option<&ValueFormatCurrency> {
928        self.formats_currency.get(name)
929    }
930
931    /// Returns the mutable format.
932    pub fn currency_format_mut(&mut self, name: &str) -> Option<&mut ValueFormatCurrency> {
933        self.formats_currency.get_mut(name)
934    }
935
936    /// Adds a value format.
937    /// Unnamed formats will be assigned an automatic name.
938    pub fn add_text_format(&mut self, mut vstyle: ValueFormatText) -> ValueFormatRef {
939        if vstyle.name().is_empty() {
940            vstyle.set_name(
941                auto_style_name(&mut self.autonum, "val_text", &self.formats_text).as_str(),
942            );
943        }
944        let sref = vstyle.format_ref();
945        self.formats_text.insert(vstyle.name().to_string(), vstyle);
946        sref
947    }
948
949    /// Removes the format.
950    pub fn remove_text_format(&mut self, name: &str) -> Option<ValueFormatText> {
951        self.formats_text.remove(name)
952    }
953
954    /// Returns iterator over formats.
955    pub fn iter_text_formats(&self) -> impl Iterator<Item = &ValueFormatText> {
956        self.formats_text.values()
957    }
958
959    /// Returns the format.
960    pub fn text_format(&self, name: &str) -> Option<&ValueFormatText> {
961        self.formats_text.get(name)
962    }
963
964    /// Returns the mutable format.
965    pub fn text_format_mut(&mut self, name: &str) -> Option<&mut ValueFormatText> {
966        self.formats_text.get_mut(name)
967    }
968
969    /// Adds a value format.
970    /// Unnamed formats will be assigned an automatic name.
971    pub fn add_datetime_format(&mut self, mut vstyle: ValueFormatDateTime) -> ValueFormatRef {
972        if vstyle.name().is_empty() {
973            vstyle.set_name(
974                auto_style_name(&mut self.autonum, "val_datetime", &self.formats_datetime).as_str(),
975            );
976        }
977        let sref = vstyle.format_ref();
978        self.formats_datetime
979            .insert(vstyle.name().to_string(), vstyle);
980        sref
981    }
982
983    /// Removes the format.
984    pub fn remove_datetime_format(&mut self, name: &str) -> Option<ValueFormatDateTime> {
985        self.formats_datetime.remove(name)
986    }
987
988    /// Returns iterator over formats.
989    pub fn iter_datetime_formats(&self) -> impl Iterator<Item = &ValueFormatDateTime> {
990        self.formats_datetime.values()
991    }
992
993    /// Returns the format.
994    pub fn datetime_format(&self, name: &str) -> Option<&ValueFormatDateTime> {
995        self.formats_datetime.get(name)
996    }
997
998    /// Returns the mutable format.
999    pub fn datetime_format_mut(&mut self, name: &str) -> Option<&mut ValueFormatDateTime> {
1000        self.formats_datetime.get_mut(name)
1001    }
1002
1003    /// Adds a value format.
1004    /// Unnamed formats will be assigned an automatic name.
1005    pub fn add_timeduration_format(
1006        &mut self,
1007        mut vstyle: ValueFormatTimeDuration,
1008    ) -> ValueFormatRef {
1009        if vstyle.name().is_empty() {
1010            vstyle.set_name(
1011                auto_style_name(
1012                    &mut self.autonum,
1013                    "val_timeduration",
1014                    &self.formats_timeduration,
1015                )
1016                .as_str(),
1017            );
1018        }
1019        let sref = vstyle.format_ref();
1020        self.formats_timeduration
1021            .insert(vstyle.name().to_string(), vstyle);
1022        sref
1023    }
1024
1025    /// Removes the format.
1026    pub fn remove_timeduration_format(&mut self, name: &str) -> Option<ValueFormatTimeDuration> {
1027        self.formats_timeduration.remove(name)
1028    }
1029
1030    /// Returns iterator over formats.
1031    pub fn iter_timeduration_formats(&self) -> impl Iterator<Item = &ValueFormatTimeDuration> {
1032        self.formats_timeduration.values()
1033    }
1034
1035    /// Returns the format.
1036    pub fn timeduration_format(&self, name: &str) -> Option<&ValueFormatTimeDuration> {
1037        self.formats_timeduration.get(name)
1038    }
1039
1040    /// Returns the mutable format.
1041    pub fn timeduration_format_mut(&mut self, name: &str) -> Option<&mut ValueFormatTimeDuration> {
1042        self.formats_timeduration.get_mut(name)
1043    }
1044
1045    /// Adds a value PageStyle.
1046    /// Unnamed formats will be assigned an automatic name.
1047    pub fn add_pagestyle(&mut self, mut pstyle: PageStyle) -> PageStyleRef {
1048        if pstyle.name().is_empty() {
1049            pstyle.set_name(auto_style_name2(
1050                &mut self.autonum,
1051                "page",
1052                &self.pagestyles,
1053            ));
1054        }
1055        let sref = pstyle.style_ref();
1056        self.pagestyles.insert(pstyle.style_ref(), pstyle);
1057        sref
1058    }
1059
1060    /// Removes the PageStyle.
1061    pub fn remove_pagestyle<S: AsRef<str>>(&mut self, name: S) -> Option<PageStyle> {
1062        self.pagestyles.remove(name.as_ref())
1063    }
1064
1065    /// Returns iterator over formats.
1066    pub fn iter_pagestyles(&self) -> impl Iterator<Item = &PageStyle> {
1067        self.pagestyles.values()
1068    }
1069
1070    /// Returns the PageStyle.
1071    pub fn pagestyle<S: AsRef<str>>(&self, name: S) -> Option<&PageStyle> {
1072        self.pagestyles.get(name.as_ref())
1073    }
1074
1075    /// Returns the mutable PageStyle.
1076    pub fn pagestyle_mut<S: AsRef<str>>(&mut self, name: S) -> Option<&mut PageStyle> {
1077        self.pagestyles.get_mut(name.as_ref())
1078    }
1079
1080    /// Adds a value MasterPage.
1081    /// Unnamed formats will be assigned an automatic name.
1082    pub fn add_masterpage(&mut self, mut mpage: MasterPage) -> MasterPageRef {
1083        if mpage.name().is_empty() {
1084            mpage.set_name(auto_style_name2(&mut self.autonum, "mp", &self.masterpages));
1085        }
1086        let sref = mpage.masterpage_ref();
1087        self.masterpages.insert(mpage.masterpage_ref(), mpage);
1088        sref
1089    }
1090
1091    /// Removes the MasterPage.
1092    pub fn remove_masterpage<S: AsRef<str>>(&mut self, name: S) -> Option<MasterPage> {
1093        self.masterpages.remove(name.as_ref())
1094    }
1095
1096    /// Returns iterator over formats.
1097    pub fn iter_masterpages(&self) -> impl Iterator<Item = &MasterPage> {
1098        self.masterpages.values()
1099    }
1100
1101    /// Returns the MasterPage.
1102    pub fn masterpage<S: AsRef<str>>(&self, name: S) -> Option<&MasterPage> {
1103        self.masterpages.get(name.as_ref())
1104    }
1105
1106    /// Returns the mutable MasterPage.
1107    pub fn masterpage_mut<S: AsRef<str>>(&mut self, name: S) -> Option<&mut MasterPage> {
1108        self.masterpages.get_mut(name.as_ref())
1109    }
1110
1111    /// Adds a Validation.
1112    /// Nameless validations will be assigned a name.
1113    pub fn add_validation(&mut self, mut valid: Validation) -> ValidationRef {
1114        if valid.name().is_empty() {
1115            valid.set_name(auto_style_name2(
1116                &mut self.autonum,
1117                "val",
1118                &self.validations,
1119            ));
1120        }
1121        let vref = valid.validation_ref();
1122        self.validations.insert(valid.validation_ref(), valid);
1123        vref
1124    }
1125
1126    /// Removes a Validation.
1127    pub fn remove_validation<S: AsRef<str>>(&mut self, name: S) -> Option<Validation> {
1128        self.validations.remove(name.as_ref())
1129    }
1130
1131    /// Returns iterator over formats.
1132    pub fn iter_validations(&self) -> impl Iterator<Item = &Validation> {
1133        self.validations.values()
1134    }
1135
1136    /// Returns the Validation.
1137    pub fn validation<S: AsRef<str>>(&self, name: S) -> Option<&Validation> {
1138        self.validations.get(name.as_ref())
1139    }
1140
1141    /// Returns a mutable Validation.
1142    pub fn validation_mut<S: AsRef<str>>(&mut self, name: S) -> Option<&mut Validation> {
1143        self.validations.get_mut(name.as_ref())
1144    }
1145
1146    /// Adds a manifest entry, replaces an existing one with the same name.
1147    pub fn add_manifest(&mut self, manifest: Manifest) {
1148        self.manifest.insert(manifest.full_path.clone(), manifest);
1149    }
1150
1151    /// Removes a manifest entry.
1152    pub fn remove_manifest(&mut self, path: &str) -> Option<Manifest> {
1153        self.manifest.remove(path)
1154    }
1155
1156    /// Iterates the manifest.
1157    pub fn iter_manifest(&self) -> impl Iterator<Item = &Manifest> {
1158        self.manifest.values()
1159    }
1160
1161    /// Returns the manifest entry for the path
1162    pub fn manifest(&self, path: &str) -> Option<&Manifest> {
1163        self.manifest.get(path)
1164    }
1165
1166    /// Returns the manifest entry for the path
1167    pub fn manifest_mut(&mut self, path: &str) -> Option<&mut Manifest> {
1168        self.manifest.get_mut(path)
1169    }
1170
1171    /// Gives access to meta-data.
1172    pub fn metadata(&self) -> &Metadata {
1173        &self.metadata
1174    }
1175
1176    /// Gives access to meta-data.
1177    pub fn metadata_mut(&mut self) -> &mut Metadata {
1178        &mut self.metadata
1179    }
1180}
1181
1182/// Subset of the Workbook wide configurations.
1183#[derive(Clone, Debug, GetSize)]
1184pub struct WorkBookConfig {
1185    /// Which table is active when opening.    
1186    pub active_table: String,
1187    /// Show grid in general. Per sheet definition take priority.
1188    pub show_grid: bool,
1189    /// Show page-breaks.
1190    pub show_page_breaks: bool,
1191    /// Are the sheet-tabs shown or not.
1192    pub has_sheet_tabs: bool,
1193}
1194
1195impl Default for WorkBookConfig {
1196    fn default() -> Self {
1197        Self {
1198            active_table: "".to_string(),
1199            show_grid: true,
1200            show_page_breaks: false,
1201            has_sheet_tabs: true,
1202        }
1203    }
1204}
1205
1206/// Script.
1207#[derive(Debug, Default, Clone, GetSize)]
1208pub struct Script {
1209    pub(crate) script_lang: String,
1210    pub(crate) script: Vec<XmlContent>,
1211}
1212
1213impl Script {
1214    /// Script
1215    pub fn new() -> Self {
1216        Self {
1217            script_lang: "".to_string(),
1218            script: Default::default(),
1219        }
1220    }
1221
1222    /// Script language
1223    pub fn script_lang(&self) -> &str {
1224        &self.script_lang
1225    }
1226
1227    /// Script language
1228    pub fn set_script_lang(&mut self, script_lang: String) {
1229        self.script_lang = script_lang
1230    }
1231
1232    /// Script
1233    pub fn script(&self) -> &Vec<XmlContent> {
1234        &self.script
1235    }
1236
1237    /// Script
1238    pub fn set_script(&mut self, script: Vec<XmlContent>) {
1239        self.script = script
1240    }
1241}
1242
1243/// Event-Listener.
1244#[derive(Debug, Clone, GetSize)]
1245pub struct EventListener {
1246    pub(crate) event_name: String,
1247    pub(crate) script_lang: String,
1248    pub(crate) macro_name: String,
1249    pub(crate) actuate: XLinkActuate,
1250    pub(crate) href: String,
1251    pub(crate) link_type: XLinkType,
1252}
1253
1254impl EventListener {
1255    /// EventListener
1256    pub fn new() -> Self {
1257        Self {
1258            event_name: Default::default(),
1259            script_lang: Default::default(),
1260            macro_name: Default::default(),
1261            actuate: XLinkActuate::OnLoad,
1262            href: Default::default(),
1263            link_type: Default::default(),
1264        }
1265    }
1266
1267    /// Name
1268    pub fn event_name(&self) -> &str {
1269        &self.event_name
1270    }
1271
1272    /// Name
1273    pub fn set_event_name(&mut self, name: String) {
1274        self.event_name = name;
1275    }
1276
1277    /// Script language
1278    pub fn script_lang(&self) -> &str {
1279        &self.script_lang
1280    }
1281
1282    /// Script language
1283    pub fn set_script_lang(&mut self, lang: String) {
1284        self.script_lang = lang
1285    }
1286
1287    /// Macro name
1288    pub fn macro_name(&self) -> &str {
1289        &self.macro_name
1290    }
1291
1292    /// Macro name
1293    pub fn set_macro_name(&mut self, name: String) {
1294        self.macro_name = name
1295    }
1296
1297    /// Actuate
1298    pub fn actuate(&self) -> XLinkActuate {
1299        self.actuate
1300    }
1301
1302    /// Actuate
1303    pub fn set_actuate(&mut self, actuate: XLinkActuate) {
1304        self.actuate = actuate;
1305    }
1306
1307    /// HRef
1308    pub fn href(&self) -> &str {
1309        &self.href
1310    }
1311
1312    /// HRef
1313    pub fn set_href(&mut self, href: String) {
1314        self.href = href;
1315    }
1316
1317    /// Link type
1318    pub fn link_type(&self) -> XLinkType {
1319        self.link_type
1320    }
1321
1322    /// Link type
1323    pub fn set_link_type(&mut self, link_type: XLinkType) {
1324        self.link_type = link_type
1325    }
1326}
1327
1328impl Default for EventListener {
1329    fn default() -> Self {
1330        Self {
1331            event_name: Default::default(),
1332            script_lang: Default::default(),
1333            macro_name: Default::default(),
1334            actuate: XLinkActuate::OnRequest,
1335            href: Default::default(),
1336            link_type: Default::default(),
1337        }
1338    }
1339}