xlnt - community edition
cell.hpp
1 // Copyright (c) 2014-2022 Thomas Fussell
2 // Copyright (c) 2010-2015 openpyxl
3 // Copyright (c) 2024-2025 xlnt-community
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE
22 //
23 // @license: http://www.opensource.org/licenses/mit-license.php
24 // @author: see AUTHORS file
25 
26 #pragma once
27 
28 #include <string>
29 #include <unordered_map>
30 
31 #include <xlnt/xlnt_config.hpp>
32 #include <xlnt/cell/cell_type.hpp>
33 #include <xlnt/cell/index_types.hpp>
34 #include <xlnt/cell/rich_text.hpp>
35 
36 namespace xlnt {
37 
38 enum class calendar;
39 
40 class alignment;
41 class base_format;
42 class border;
43 class cell_reference;
44 class comment;
45 class fill;
46 class font;
47 class format;
48 class number_format;
49 class protection;
50 class range;
51 class relationship;
52 class style;
53 class workbook;
54 class worksheet;
55 class xlsx_consumer;
56 class xlsx_producer;
57 class phonetic_pr;
58 
59 struct date;
60 struct datetime;
61 struct time;
62 struct timedelta;
63 
64 namespace detail {
65 
66 class xlsx_consumer;
67 class xlsx_producer;
68 
69 struct cell_impl;
70 
71 } // namespace detail
72 
83 class XLNT_API cell
84 {
85 public:
89  using type = cell_type;
90 
94  static const std::unordered_map<std::string, int> &error_codes();
95 
99  cell(const cell &) = default;
100 
101  // value
102 
106  bool has_value() const;
107 
113  template <typename T>
114  T value() const;
115 
120  void clear_value();
121 
125  void value(std::nullptr_t);
126 
130  void value(bool boolean_value);
131 
135  void value(int int_value);
136 
140  void value(unsigned int int_value);
141 
145  void value(long long int int_value);
146 
150  void value(unsigned long long int int_value);
151 
155  void value(float float_value);
156 
160  void value(double float_value);
161 
165  void value(const date &date_value);
166 
170  void value(const time &time_value);
171 
175  void value(const datetime &datetime_value);
176 
180  void value(const timedelta &timedelta_value);
181 
185  void value(const std::string &string_value);
186 
190  void value(const char *string_value);
191 
195  void value(const rich_text &text_value);
196 
207  void value(const cell other_cell);
208 
213  void value(const std::string &string_value, bool infer_type);
214 
218  type data_type() const;
219 
224  void data_type(type t);
225 
226  // properties
227 
232  bool garbage_collectible() const;
233 
237  bool is_date() const;
238 
239  // position
240 
244  cell_reference reference() const;
245 
249  column_t column() const;
250 
254  column_t::index_t column_index() const;
255 
259  row_t row() const;
260 
264  std::pair<int, int> anchor() const;
265 
266  // hyperlink
267 
271  class hyperlink hyperlink() const;
272 
277  void hyperlink(const std::string &url, const std::string &display = "");
278 
282  void hyperlink(xlnt::cell target, const std::string &display = "");
283 
287  void hyperlink(xlnt::range target, const std::string &display = "");
288 
292  bool has_hyperlink() const;
293 
294  // computed formatting
295 
301  class alignment computed_alignment() const;
302 
308  class border computed_border() const;
309 
315  class fill computed_fill() const;
316 
322  class font computed_font() const;
323 
329  class number_format computed_number_format() const;
330 
336  class protection computed_protection() const;
337 
338  // format
339 
343  bool has_format() const;
344 
349  const class format format() const;
350 
357  void format(const class format new_format);
358 
364  void clear_format();
365 
369  class number_format number_format() const;
370 
375  void number_format(const class number_format &format);
376 
380  class font font() const;
381 
386  void font(const class font &font_);
387 
391  class fill fill() const;
392 
397  void fill(const class fill &fill_);
398 
402  class border border() const;
403 
408  void border(const class border &border_);
409 
413  class alignment alignment() const;
414 
419  void alignment(const class alignment &alignment_);
420 
424  class protection protection() const;
425 
430  void protection(const class protection &protection_);
431 
432  // style
433 
437  bool has_style() const;
438 
442  class style style();
443 
447  const class style style() const;
448 
453  void style(const class style &new_style);
454 
460  void style(const std::string &style_name);
461 
467  void clear_style();
468 
469  // formula
470 
474  std::string formula() const;
475 
480  void formula(const std::string &formula);
481 
485  void clear_formula();
486 
490  bool has_formula() const;
491 
492  // printing
493 
498  std::string to_string() const;
499 
500  // merging
501 
506  bool is_merged() const;
507 
513  void merged(bool merged);
514 
515  // phonetics
516 
520  bool phonetics_visible() const;
521 
525  void show_phonetics(bool phonetics);
526 
530  std::string error() const;
531 
535  void error(const std::string &error);
536 
541  cell offset(int column, int row);
542 
546  class worksheet worksheet();
547 
551  const class worksheet worksheet() const;
552 
556  class workbook workbook();
557 
561  const class workbook workbook() const;
562 
566  calendar base_date() const;
567 
571  std::string check_string(const std::string &to_check);
572 
573  // comment
574 
578  bool has_comment() const;
579 
583  void clear_comment();
584 
588  class comment comment() const;
589 
594  void comment(const std::string &text,
595  const std::string &author = "Microsoft Office User");
596 
601  void comment(const std::string &comment_text,
602  const class font &comment_font,
603  const std::string &author = "Microsoft Office User");
604 
608  void comment(const class comment &new_comment);
609 
613  double width() const;
614 
618  double height() const;
619 
620  // comparisons
621 
627  bool compare(const cell &other, bool compare_by_reference) const;
628 
629  // operators
630 
635  cell &operator=(const cell &rhs);
636 
640  bool operator==(const cell &comparand) const;
641 
645  bool operator!=(const cell &comparand) const;
646 
647 private:
648  friend class style;
649  friend class worksheet;
650  friend class detail::xlsx_consumer;
651  friend class detail::xlsx_producer;
652  friend struct detail::cell_impl;
653 
657  void value_no_check(const rich_text &text);
658 
665  void copy_from_other_workbook(const cell &source);
666 
673  void copy_format_from_other_workbook(const class format &source_format);
674 
679  class format modifiable_format();
680 
684  cell() = delete;
685 
689  cell(detail::cell_impl *d);
690 
694  detail::cell_impl *d_ = nullptr;
695 };
696 
700 XLNT_API bool operator==(std::nullptr_t, const cell &cell);
701 
705 XLNT_API bool operator==(const cell &cell, std::nullptr_t);
706 
710 XLNT_API bool operator!=(std::nullptr_t, const cell &cell);
711 
715 XLNT_API bool operator!=(const cell &cell, std::nullptr_t);
716 
721 XLNT_API std::ostream &operator<<(std::ostream &stream, const xlnt::cell &cell);
722 
723 template <>
724 XLNT_API bool cell::value<bool>() const;
725 
726 template <>
727 XLNT_API int cell::value<int>() const;
728 
729 template <>
730 XLNT_API unsigned int cell::value<unsigned int>() const;
731 
732 template <>
733 XLNT_API long long int cell::value<long long int>() const;
734 
735 template <>
736 XLNT_API unsigned long long cell::value<unsigned long long int>() const;
737 
738 template <>
739 XLNT_API float cell::value<float>() const;
740 
741 template <>
742 XLNT_API double cell::value<double>() const;
743 
744 template <>
745 XLNT_API date cell::value<date>() const;
746 
747 template <>
748 XLNT_API time cell::value<time>() const;
749 
750 template <>
751 XLNT_API datetime cell::value<datetime>() const;
752 
753 template <>
754 XLNT_API timedelta cell::value<timedelta>() const;
755 
756 template <>
757 XLNT_API std::string cell::value<std::string>() const;
758 
759 template <>
760 XLNT_API rich_text cell::value<rich_text>() const;
761 
762 } // namespace xlnt
A comment can be applied to a cell to provide extra information about its contents.
Definition: comment.hpp:37
A range is a 2D collection of cells with defined extens that can be iterated upon.
Definition: range.hpp:53
Represents an association between a source Package or part, and a target object which can be a part o...
Definition: relationship.hpp:103
std::uint32_t index_t
Alias declaration for the internal numeric type of this column.
Definition: index_types.hpp:53
std::uint32_t row_t
All rows should be referred to by an instance of this type.
Definition: index_types.hpp:40
Enumerates the possible types a cell can be determined by it&#39;s current value.
Definition: cell.hpp:36
bool operator!=(std::nullptr_t, const cell &cell)
Returns true if this cell is initialized.
Describes the font style of a particular cell.
Definition: font.hpp:43
value is a known error code such as #VALUE!
A time is a specific time of the day specified in terms of an hour, minute, second, and microsecond (0-999999). It can also be initialized as a fraction of a day using time::from_number.
Definition: time.hpp:38
Describes the fill style of a particular cell.
Definition: fill.hpp:299
A worksheet is a 2D array of cells starting with cell A1 in the top-left corner and extending indefin...
Definition: worksheet.hpp:75
Represents a span of time between two datetimes. This is not fully supported yet throughout the libra...
Definition: timedelta.hpp:35
Describes the border style of a particular cell.
Definition: border.hpp:91
bool operator==(std::nullptr_t, const cell &cell)
Returns true if this cell is uninitialized.
An object used to refer to a cell. References have two parts, the column and the row. In Excel, the reference string A1 refers to the top-left-most cell. A cell_reference can be initialized from a string of this form or a 1-indexed ordered pair of the form column, row.
Definition: cell_reference.hpp:59
Describes a unit of data in a worksheet at a specific coordinate and its associated properties...
Definition: cell.hpp:83
Encapsulates zero or more formatted text runs where a text run is a string of text with the same defi...
Definition: rich_text.hpp:41
A datetime is a combination of a date and a time. IMPORTANT: The datetime could be in an empty/invali...
Definition: datetime.hpp:41
Describes the number formatting applied to text and numbers within a certain cell.
Definition: number_format.hpp:40
std::ostream & operator<<(std::ostream &stream, const xlnt::cell &cell)
Convenience function for writing cell to an ostream. Uses cell::to_string() internally.
Describes the formatting of a particular cell.
Definition: format.hpp:58
Alignment options that determine how text should be displayed within a cell.
Definition: alignment.hpp:63
Columns can be referred to as a string A,B,...Z,AA,AB,..,ZZ,AAA,...,ZZZ or as a 1-indexed index...
Definition: index_types.hpp:47
cell_type
Enumerates the possible types a cell can be determined by it&#39;s current value.
Definition: cell_type.hpp:39
Describes the protection style of a particular cell.
Definition: protection.hpp:36
Describes a style which has a name and can be applied to multiple individual formats. In Excel this is a "Cell Style".
Definition: style.hpp:55
A date is a specific day specified in terms of a year, month, and day. It can also be initialized as ...
Definition: date.hpp:37
workbook is the container for all other parts of the document.
Definition: workbook.hpp:96
calendar
An enumeration of possible base dates. Dates in Excel are stored as days since this base date...
Definition: calendar.hpp:35
Phonetic properties Element provides a collection of properties that affect display of East Asian Lan...
Definition: phonetic_pr.hpp:39