spreadsheet_ods/
cell_.rs

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