xlnt - community edition
cell.hpp
1 // Copyright (c) 2014-2022 Thomas Fussell
2 // Copyright (c) 2010-2015 openpyxl
3 // Copyright (c) 2024-2026 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 
273  class hyperlink hyperlink() const;
274 
280  void hyperlink(const std::string &url, const std::string &display = "");
281 
285  void hyperlink(xlnt::cell target, const std::string &display = "");
286 
290  void hyperlink(xlnt::range target, const std::string &display = "");
291 
295  bool has_hyperlink() const;
296 
297  // computed formatting
298 
304  class alignment computed_alignment() const;
305 
311  class border computed_border() const;
312 
318  class fill computed_fill() const;
319 
325  class font computed_font() const;
326 
332  class number_format computed_number_format() const;
333 
339  class protection computed_protection() const;
340 
341  // format
342 
346  bool has_format() const;
347 
356  const class format format() const;
357 
366  void format(const class format new_format);
367 
372  void clear_format();
373 
378  class number_format number_format() const;
379 
384  void number_format(const class number_format &format);
385 
390  class font font() const;
391 
396  void font(const class font &font_);
397 
402  class fill fill() const;
403 
408  void fill(const class fill &fill_);
409 
414  class border border() const;
415 
420  void border(const class border &border_);
421 
426  class alignment alignment() const;
427 
432  void alignment(const class alignment &alignment_);
433 
438  class protection protection() const;
439 
444  void protection(const class protection &protection_);
445 
446  // style
447 
451  bool has_style() const;
452 
458  class style style();
459 
465  const class style style() const;
466 
471  void style(const class style &new_style);
472 
479  void style(const std::string &style_name);
480 
485  void clear_style();
486 
487  // formula
488 
494  std::string formula() const;
495 
500  void formula(const std::string &formula);
501 
505  void clear_formula();
506 
510  bool has_formula() const;
511 
512  // printing
513 
518  std::string to_string() const;
519 
520  // merging
521 
526  bool is_merged() const;
527 
533  void merged(bool merged);
534 
535  // phonetics
536 
540  bool phonetics_visible() const;
541 
545  void show_phonetics(bool phonetics);
546 
552  std::string error() const;
553 
559  void error(const std::string &error);
560 
565  cell offset(int column, int row);
566 
570  class worksheet worksheet();
571 
575  const class worksheet worksheet() const;
576 
580  class workbook workbook();
581 
585  const class workbook workbook() const;
586 
590  calendar base_date() const;
591 
595  std::string check_string(const std::string &to_check);
596 
597  // comment
598 
602  bool has_comment() const;
603 
607  void clear_comment();
608 
614  class comment comment() const;
615 
620  void comment(const std::string &text,
621  const std::string &author = "Microsoft Office User");
622 
627  void comment(const std::string &comment_text,
628  const class font &comment_font,
629  const std::string &author = "Microsoft Office User");
630 
634  void comment(const class comment &new_comment);
635 
639  double width() const;
640 
644  double height() const;
645 
646  // comparisons
647 
653  bool compare(const cell &other, bool compare_by_reference) const;
654 
655  // operators
656 
661  cell &operator=(const cell &rhs);
662 
666  bool operator==(const cell &comparand) const;
667 
671  bool operator!=(const cell &comparand) const;
672 
673 private:
674  friend class style;
675  friend class worksheet;
676  friend class detail::xlsx_consumer;
677  friend class detail::xlsx_producer;
678  friend struct detail::cell_impl;
679 
683  void value_no_check(const rich_text &text);
684 
691  void copy_from_other_workbook(const cell &source);
692 
699  void copy_format_from_other_workbook(const class format &source_format);
700 
707  class format modifiable_format();
708 
712  cell() = delete;
713 
717  cell(detail::cell_impl *d);
718 
722  detail::cell_impl *d_ = nullptr;
723 };
724 
728 XLNT_API bool operator==(std::nullptr_t, const cell &cell);
729 
733 XLNT_API bool operator==(const cell &cell, std::nullptr_t);
734 
738 XLNT_API bool operator!=(std::nullptr_t, const cell &cell);
739 
743 XLNT_API bool operator!=(const cell &cell, std::nullptr_t);
744 
749 XLNT_API std::ostream &operator<<(std::ostream &stream, const xlnt::cell &cell);
750 
751 template <>
752 XLNT_API bool cell::value<bool>() const;
753 
754 template <>
755 XLNT_API int cell::value<int>() const;
756 
757 template <>
758 XLNT_API unsigned int cell::value<unsigned int>() const;
759 
760 template <>
761 XLNT_API long long int cell::value<long long int>() const;
762 
763 template <>
764 XLNT_API unsigned long long cell::value<unsigned long long int>() const;
765 
766 template <>
767 XLNT_API float cell::value<float>() const;
768 
769 template <>
770 XLNT_API double cell::value<double>() const;
771 
772 template <>
773 XLNT_API date cell::value<date>() const;
774 
775 template <>
776 XLNT_API time cell::value<time>() const;
777 
778 template <>
779 XLNT_API datetime cell::value<datetime>() const;
780 
781 template <>
782 XLNT_API timedelta cell::value<timedelta>() const;
783 
784 template <>
785 XLNT_API std::string cell::value<std::string>() const;
786 
787 template <>
788 XLNT_API rich_text cell::value<rich_text>() const;
789 
790 } // 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:59
std::uint32_t row_t
All rows should be referred to by an instance of this type. By default, row references range from 1 t...
Definition: index_types.hpp:43
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. By default, cell references range from column A1–A1048576 (column A:A) to column XFD1–XFD1048576 (column XFD:XFD, column index 16384). The OOXML specification allows to extend this range, and XLNT allows both rows and columns between 1 and 4294967295. Please note that not all applications might support these extended ranges.
Definition: cell_reference.hpp:62
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,...,XFD or as a 1-indexed index (indi...
Definition: index_types.hpp:53
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