spreadsheet_ods/
cell_.rs

1use crate::draw::{Annotation, DrawFrame};
2use crate::validation::ValidationRef;
3use crate::value_::Value;
4use crate::CellStyleRef;
5use get_size::GetSize;
6use get_size_derive::GetSize;
7use std::fmt::{Display, Formatter};
8
9/// A cell can span multiple rows/columns.
10#[derive(Debug, Clone, Copy, GetSize)]
11pub struct CellSpan {
12    pub(crate) row_span: u32,
13    pub(crate) col_span: u32,
14}
15
16impl Default for CellSpan {
17    fn default() -> Self {
18        Self {
19            row_span: 1,
20            col_span: 1,
21        }
22    }
23}
24
25impl Display for CellSpan {
26    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
27        write!(f, "(+{}+{})", self.row_span, self.col_span)
28    }
29}
30
31impl From<CellSpan> for (u32, u32) {
32    fn from(span: CellSpan) -> Self {
33        (span.row_span, span.col_span)
34    }
35}
36
37impl From<&CellSpan> for (u32, u32) {
38    fn from(span: &CellSpan) -> Self {
39        (span.row_span, span.col_span)
40    }
41}
42
43impl CellSpan {
44    /// Default span 1,1
45    pub fn new() -> Self {
46        Self::default()
47    }
48
49    /// Is this empty? Defined as row_span==1 and col_span==1.
50    #[inline]
51    pub fn is_empty(&self) -> bool {
52        self.row_span == 1 && self.col_span == 1
53    }
54
55    /// Sets the row span of this cell.
56    /// Cells below with values will be lost when writing.
57    #[inline]
58    pub fn set_row_span(&mut self, rows: u32) {
59        assert!(rows > 0);
60        self.row_span = rows;
61    }
62
63    /// Returns the row span.
64    #[inline]
65    pub fn row_span(&self) -> u32 {
66        self.row_span
67    }
68
69    /// Sets the column span of this cell.
70    /// Cells to the right with values will be lost when writing.
71    #[inline]
72    pub fn set_col_span(&mut self, cols: u32) {
73        assert!(cols > 0);
74        self.col_span = cols;
75    }
76
77    /// Returns the col span.
78    #[inline]
79    pub fn col_span(&self) -> u32 {
80        self.col_span
81    }
82}
83
84/// One Cell of the spreadsheet.
85#[derive(Debug, Clone, GetSize)]
86pub(crate) struct CellData {
87    pub(crate) value: Value,
88    // Unparsed formula string.
89    pub(crate) formula: Option<String>,
90    // Cell style name.
91    pub(crate) style: Option<CellStyleRef>,
92    // Cell repeated.
93    pub(crate) repeat: u32,
94    // Scarcely used extra data.
95    pub(crate) extra: Option<Box<CellDataExt>>,
96}
97
98/// Extra cell data.
99#[derive(Debug, Clone, Default, GetSize)]
100pub(crate) struct CellDataExt {
101    // Content validation name.
102    pub(crate) validation_name: Option<ValidationRef>,
103    // Row/Column span.
104    pub(crate) span: CellSpan,
105    // Matrix span.
106    pub(crate) matrix_span: CellSpan,
107    // Annotation
108    pub(crate) annotation: Option<Box<Annotation>>,
109    // Draw
110    pub(crate) draw_frames: Vec<DrawFrame>,
111}
112
113impl Default for CellData {
114    #[inline]
115    fn default() -> Self {
116        Self {
117            value: Default::default(),
118            formula: None,
119            style: None,
120            repeat: 1,
121            extra: None,
122        }
123    }
124}
125
126impl CellData {
127    /// Holds no value and no formula.
128    pub(crate) fn is_empty(&self) -> bool {
129        if self.value != Value::Empty {
130            return false;
131        }
132        if self.formula.is_some() {
133            return false;
134        }
135        // no style check
136        self.is_void_extra()
137    }
138
139    /// Holds no useful data at all.
140    pub(crate) fn is_void(&self, default_cellstyle: Option<&CellStyleRef>) -> bool {
141        if self.value != Value::Empty {
142            return false;
143        }
144        if self.formula.is_some() {
145            return false;
146        }
147        if self.style.is_some() && self.style.as_ref() != default_cellstyle {
148            return false;
149        }
150        self.is_void_extra()
151    }
152
153    fn is_void_extra(&self) -> bool {
154        if let Some(extra) = &self.extra {
155            if !extra.span.is_empty() {
156                return false;
157            }
158            if extra.validation_name.is_some() {
159                return false;
160            }
161            if extra.annotation.is_some() {
162                return false;
163            }
164            if !extra.draw_frames.is_empty() {
165                return false;
166            }
167            if !extra.matrix_span.is_empty() {
168                return false;
169            }
170        }
171        true
172    }
173
174    pub(crate) fn has_annotation(&self) -> bool {
175        if let Some(extra) = &self.extra {
176            extra.annotation.is_some()
177        } else {
178            false
179        }
180    }
181
182    pub(crate) fn has_draw_frames(&self) -> bool {
183        if let Some(extra) = &self.extra {
184            !extra.draw_frames.is_empty()
185        } else {
186            false
187        }
188    }
189
190    pub(crate) fn extra_mut(&mut self) -> &mut CellDataExt {
191        if self.extra.is_none() {
192            self.extra = Some(Box::default());
193        }
194        self.extra.as_mut().expect("celldataext")
195    }
196
197    pub(crate) fn cloned_cell_content(&self) -> CellContent {
198        let (validation_name, span, matrix_span, annotation, draw_frames) =
199            if let Some(extra) = &self.extra {
200                (
201                    extra.validation_name.clone(),
202                    extra.span,
203                    extra.matrix_span,
204                    extra.annotation.clone(),
205                    extra.draw_frames.clone(),
206                )
207            } else {
208                (
209                    None,
210                    Default::default(),
211                    Default::default(),
212                    None,
213                    Vec::new(),
214                )
215            };
216
217        CellContent {
218            value: self.value.clone(),
219            style: self.style.clone(),
220            formula: self.formula.clone(),
221            repeat: self.repeat,
222            validation_name,
223            span,
224            matrix_span,
225            annotation,
226            draw_frames,
227        }
228    }
229
230    pub(crate) fn into_cell_content(self) -> CellContent {
231        let (validation_name, span, matrix_span, annotation, draw_frames) =
232            if let Some(extra) = self.extra {
233                (
234                    extra.validation_name,
235                    extra.span,
236                    extra.matrix_span,
237                    extra.annotation,
238                    extra.draw_frames,
239                )
240            } else {
241                (
242                    None,
243                    Default::default(),
244                    Default::default(),
245                    None,
246                    Vec::new(),
247                )
248            };
249
250        CellContent {
251            value: self.value,
252            style: self.style,
253            formula: self.formula,
254            repeat: self.repeat,
255            validation_name,
256            span,
257            matrix_span,
258            annotation,
259            draw_frames,
260        }
261    }
262
263    pub(crate) fn cell_content_ref(&self) -> CellContentRef<'_> {
264        let (validation_name, span, matrix_span, annotation, draw_frames) =
265            if let Some(extra) = &self.extra {
266                (
267                    extra.validation_name.as_ref(),
268                    extra.span,
269                    extra.matrix_span,
270                    extra.annotation.as_ref(),
271                    Some(&extra.draw_frames),
272                )
273            } else {
274                (None, CellSpan::default(), CellSpan::default(), None, None)
275            };
276
277        CellContentRef {
278            value: &self.value,
279            style: self.style.as_ref(),
280            formula: self.formula.as_ref(),
281            repeat: self.repeat,
282            validation_name,
283            span,
284            matrix_span,
285            annotation: annotation.map(|v| v.as_ref()),
286            draw_frames,
287        }
288    }
289}
290
291/// Holds references to the combined content of a cell.
292/// A temporary to hold the data when iterating over a sheet.
293#[derive(Debug, Clone, Copy)]
294pub struct CellContentRef<'a> {
295    /// Reference to the cell value.
296    pub value: &'a Value,
297    /// Reference to the stylename.
298    pub style: Option<&'a CellStyleRef>,
299    /// Reference to the cell formula.
300    pub formula: Option<&'a String>,
301    /// Reference to the repeat count.
302    pub repeat: u32,
303    /// Reference to a cell validation.
304    pub validation_name: Option<&'a ValidationRef>,
305    /// Reference to the cellspan.
306    pub span: CellSpan,
307    /// Reference to a matrix cellspan.
308    pub matrix_span: CellSpan,
309    /// Reference to an annotation.
310    pub annotation: Option<&'a Annotation>,
311    /// Reference to draw-frames.
312    pub draw_frames: Option<&'a Vec<DrawFrame>>,
313}
314
315impl<'a> CellContentRef<'a> {
316    /// Returns the value.
317    #[inline]
318    pub fn value(&self) -> &'a Value {
319        self.value
320    }
321
322    /// Returns the formula.
323    #[inline]
324    pub fn formula(&self) -> Option<&'a String> {
325        self.formula
326    }
327
328    /// Returns the cell style.
329    #[inline]
330    pub fn style(&self) -> Option<&'a CellStyleRef> {
331        self.style
332    }
333
334    /// Returns the repeat count.
335    #[inline]
336    pub fn repeat(&self) -> u32 {
337        self.repeat
338    }
339
340    /// Returns the validation name.
341    #[inline]
342    pub fn validation(&self) -> Option<&'a ValidationRef> {
343        self.validation_name
344    }
345
346    /// Returns the row span.
347    #[inline]
348    pub fn row_span(&self) -> u32 {
349        self.span.row_span
350    }
351
352    /// Returns the col span.
353    #[inline]
354    pub fn col_span(&self) -> u32 {
355        self.span.col_span
356    }
357
358    /// Returns the row span for a matrix.
359    #[inline]
360    pub fn matrix_row_span(&self) -> u32 {
361        self.matrix_span.row_span
362    }
363
364    /// Returns the col span for a matrix.
365    #[inline]
366    pub fn matrix_col_span(&self) -> u32 {
367        self.matrix_span.col_span
368    }
369
370    /// Returns the validation name.
371    #[inline]
372    pub fn annotation(&self) -> Option<&'a Annotation> {
373        self.annotation
374    }
375
376    /// Returns draw frames.
377    #[inline]
378    pub fn draw_frames(&self) -> Option<&'a Vec<DrawFrame>> {
379        self.draw_frames
380    }
381
382    /// Creates a owned CellContent.
383    pub fn to_owned(&self) -> CellContent {
384        CellContent {
385            value: self.value.clone(),
386            style: self.style.cloned(),
387            formula: self.formula.cloned(),
388            repeat: self.repeat,
389            validation_name: self.validation_name.cloned(),
390            span: self.span,
391            matrix_span: self.matrix_span,
392            annotation: self.annotation.map(|v| Box::new(v.clone())),
393            draw_frames: self.draw_frames.cloned().unwrap_or_default(),
394        }
395    }
396}
397
398/// A copy of the relevant data for a spreadsheet cell.
399#[derive(Debug, Clone, Default)]
400pub struct CellContent {
401    /// Cell value.
402    pub value: Value,
403    /// Cell stylename.
404    pub style: Option<CellStyleRef>,
405    /// Cell formula.
406    pub formula: Option<String>,
407    /// Cell repeat count.
408    pub repeat: u32,
409    /// Reference to a validation rule.
410    pub validation_name: Option<ValidationRef>,
411    /// Cellspan.
412    pub span: CellSpan,
413    /// Matrix span.
414    pub matrix_span: CellSpan,
415    /// Annotation
416    pub annotation: Option<Box<Annotation>>,
417    /// DrawFrames
418    pub draw_frames: Vec<DrawFrame>,
419}
420
421impl CellContent {
422    /// Empty.
423    #[inline]
424    pub fn new() -> Self {
425        Default::default()
426    }
427
428    ///
429    pub(crate) fn into_celldata(mut self) -> CellData {
430        let extra = self.into_celldata_ext();
431        CellData {
432            value: self.value,
433            formula: self.formula,
434            style: self.style,
435            repeat: self.repeat,
436            extra,
437        }
438    }
439
440    /// Move stuff into a CellDataExt.
441    #[allow(clippy::wrong_self_convention)]
442    pub(crate) fn into_celldata_ext(&mut self) -> Option<Box<CellDataExt>> {
443        if self.validation_name.is_some()
444            || !self.span.is_empty()
445            || !self.matrix_span.is_empty()
446            || self.annotation.is_some()
447            || !self.draw_frames.is_empty()
448        {
449            Some(Box::new(CellDataExt {
450                validation_name: self.validation_name.take(),
451                span: self.span,
452                matrix_span: self.matrix_span,
453                annotation: self.annotation.take(),
454                draw_frames: std::mem::take(&mut self.draw_frames),
455            }))
456        } else {
457            None
458        }
459    }
460
461    /// Returns the value.
462    #[inline]
463    pub fn value(&self) -> &Value {
464        &self.value
465    }
466
467    /// Sets the value.
468    #[inline]
469    pub fn set_value<V: Into<Value>>(&mut self, value: V) {
470        self.value = value.into();
471    }
472
473    /// Returns the formula.
474    #[inline]
475    pub fn formula(&self) -> Option<&String> {
476        self.formula.as_ref()
477    }
478
479    /// Sets the formula.
480    #[inline]
481    pub fn set_formula<V: Into<String>>(&mut self, formula: V) {
482        self.formula = Some(formula.into());
483    }
484
485    /// Resets the formula.
486    #[inline]
487    pub fn clear_formula(&mut self) {
488        self.formula = None;
489    }
490
491    /// Returns the cell style.
492    #[inline]
493    pub fn style(&self) -> Option<&CellStyleRef> {
494        self.style.as_ref()
495    }
496
497    /// Sets the cell style.
498    #[inline]
499    pub fn set_style(&mut self, style: &CellStyleRef) {
500        self.style = Some(style.clone());
501    }
502
503    /// Removes the style.
504    #[inline]
505    pub fn clear_style(&mut self) {
506        self.style = None;
507    }
508
509    /// Sets the repeat count for the cell.
510    /// Value must be > 0.
511    #[inline]
512    pub fn set_repeat(&mut self, repeat: u32) {
513        assert!(repeat > 0);
514        self.repeat = repeat;
515    }
516
517    /// Returns the repeat count for the cell.
518    #[inline]
519    pub fn get_repeat(&mut self) -> u32 {
520        self.repeat
521    }
522
523    /// Returns the validation name.
524    #[inline]
525    pub fn validation(&self) -> Option<&ValidationRef> {
526        self.validation_name.as_ref()
527    }
528
529    /// Sets the validation name.
530    #[inline]
531    pub fn set_validation(&mut self, validation: &ValidationRef) {
532        self.validation_name = Some(validation.clone());
533    }
534
535    /// No validation.
536    #[inline]
537    pub fn clear_validation(&mut self) {
538        self.validation_name = None;
539    }
540
541    /// Sets the row span of this cell.
542    /// Cells below with values will be lost when writing.
543    #[inline]
544    pub fn set_row_span(&mut self, rows: u32) {
545        assert!(rows > 0);
546        self.span.row_span = rows;
547    }
548
549    /// Returns the row span.
550    #[inline]
551    pub fn row_span(&self) -> u32 {
552        self.span.row_span
553    }
554
555    /// Sets the column span of this cell.
556    /// Cells to the right with values will be lost when writing.
557    #[inline]
558    pub fn set_col_span(&mut self, cols: u32) {
559        assert!(cols > 0);
560        self.span.col_span = cols;
561    }
562
563    /// Returns the col span.
564    #[inline]
565    pub fn col_span(&self) -> u32 {
566        self.span.col_span
567    }
568
569    /// Sets the row span of this cell.
570    /// Cells below with values will be lost when writing.
571    #[inline]
572    pub fn set_matrix_row_span(&mut self, rows: u32) {
573        assert!(rows > 0);
574        self.matrix_span.row_span = rows;
575    }
576
577    /// Returns the row span.
578    #[inline]
579    pub fn matrix_row_span(&self) -> u32 {
580        self.matrix_span.row_span
581    }
582
583    /// Sets the column span of this cell.
584    /// Cells to the right with values will be lost when writing.
585    #[inline]
586    pub fn set_matrix_col_span(&mut self, cols: u32) {
587        assert!(cols > 0);
588        self.matrix_span.col_span = cols;
589    }
590
591    /// Returns the col span.
592    #[inline]
593    pub fn matrix_col_span(&self) -> u32 {
594        self.matrix_span.col_span
595    }
596
597    /// Annotation
598    #[inline]
599    pub fn set_annotation(&mut self, annotation: Annotation) {
600        self.annotation = Some(Box::new(annotation));
601    }
602
603    /// Annotation
604    #[inline]
605    pub fn clear_annotation(&mut self) {
606        self.annotation = None;
607    }
608
609    /// Returns the Annotation
610    #[inline]
611    pub fn annotation(&self) -> Option<&Annotation> {
612        self.annotation.as_ref().map(|v| v.as_ref())
613    }
614
615    /// Draw Frames
616    #[inline]
617    pub fn set_draw_frames(&mut self, draw_frames: Vec<DrawFrame>) {
618        self.draw_frames = draw_frames;
619    }
620
621    /// Draw Frames
622    #[inline]
623    pub fn draw_frames(&self) -> &Vec<DrawFrame> {
624        &self.draw_frames
625    }
626}