xlnt
cell.hpp
1 // Copyright (c) 2014-2022 Thomas Fussell
2 // Copyright (c) 2010-2015 openpyxl
3 // Copyright (c) 2024 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 <memory>
29 #include <string>
30 #include <unordered_map>
31 
32 #include <xlnt/xlnt_config.hpp>
33 #include <xlnt/cell/cell_type.hpp>
34 #include <xlnt/cell/index_types.hpp>
35 #include <xlnt/cell/rich_text.hpp>
36 
37 namespace xlnt {
38 
39 enum class calendar;
40 
41 class alignment;
42 class base_format;
43 class border;
44 class cell_reference;
45 class comment;
46 class fill;
47 class font;
48 class format;
49 class number_format;
50 class protection;
51 class range;
52 class relationship;
53 class style;
54 class workbook;
55 class worksheet;
56 class xlsx_consumer;
57 class xlsx_producer;
58 class phonetic_pr;
59 
60 struct date;
61 struct datetime;
62 struct time;
63 struct timedelta;
64 
65 namespace detail {
66 
67 class xlsx_consumer;
68 class xlsx_producer;
69 
70 struct cell_impl;
71 
72 } // namespace detail
73 
84 class XLNT_API cell
85 {
86 public:
90  using type = cell_type;
91 
95  static const std::unordered_map<std::string, int> &error_codes();
96 
100  cell(const cell &) = default;
101 
102  // value
103 
107  bool has_value() const;
108 
114  template <typename T>
115  T value() const;
116 
121  void clear_value();
122 
126  void value(std::nullptr_t);
127 
131  void value(bool boolean_value);
132 
136  void value(int int_value);
137 
141  void value(unsigned int int_value);
142 
146  void value(long long int int_value);
147 
151  void value(unsigned long long int int_value);
152 
156  void value(float float_value);
157 
161  void value(double float_value);
162 
166  void value(const date &date_value);
167 
171  void value(const time &time_value);
172 
176  void value(const datetime &datetime_value);
177 
181  void value(const timedelta &timedelta_value);
182 
186  void value(const std::string &string_value);
187 
191  void value(const char *string_value);
192 
196  void value(const rich_text &text_value);
197 
201  void value(const cell other_cell);
202 
207  void value(const std::string &string_value, bool infer_type);
208 
212  type data_type() const;
213 
218  void data_type(type t);
219 
220  // properties
221 
226  bool garbage_collectible() const;
227 
231  bool is_date() const;
232 
233  // position
234 
238  cell_reference reference() const;
239 
243  column_t column() const;
244 
248  column_t::index_t column_index() const;
249 
253  row_t row() const;
254 
258  std::pair<int, int> anchor() const;
259 
260  // hyperlink
261 
265  class hyperlink hyperlink() const;
266 
271  void hyperlink(const std::string &url, const std::string &display = "");
272 
276  void hyperlink(xlnt::cell target, const std::string &display = "");
277 
281  void hyperlink(xlnt::range target, const std::string &display = "");
282 
286  bool has_hyperlink() const;
287 
288  // computed formatting
289 
295  class alignment computed_alignment() const;
296 
302  class border computed_border() const;
303 
309  class fill computed_fill() const;
310 
316  class font computed_font() const;
317 
323  class number_format computed_number_format() const;
324 
330  class protection computed_protection() const;
331 
332  // format
333 
337  bool has_format() const;
338 
343  const class format format() const;
344 
348  void format(const class format new_format);
349 
355  void clear_format();
356 
360  class number_format number_format() const;
361 
366  void number_format(const class number_format &format);
367 
371  class font font() const;
372 
377  void font(const class font &font_);
378 
382  class fill fill() const;
383 
388  void fill(const class fill &fill_);
389 
393  class border border() const;
394 
399  void border(const class border &border_);
400 
404  class alignment alignment() const;
405 
410  void alignment(const class alignment &alignment_);
411 
415  class protection protection() const;
416 
421  void protection(const class protection &protection_);
422 
423  // style
424 
428  bool has_style() const;
429 
433  class style style();
434 
438  const class style style() const;
439 
444  void style(const class style &new_style);
445 
451  void style(const std::string &style_name);
452 
458  void clear_style();
459 
460  // formula
461 
465  std::string formula() const;
466 
471  void formula(const std::string &formula);
472 
476  void clear_formula();
477 
481  bool has_formula() const;
482 
483  // printing
484 
489  std::string to_string() const;
490 
491  // merging
492 
497  bool is_merged() const;
498 
504  void merged(bool merged);
505 
506  // phonetics
507 
511  bool phonetics_visible() const;
512 
516  void show_phonetics(bool phonetics);
517 
521  std::string error() const;
522 
526  void error(const std::string &error);
527 
532  cell offset(int column, int row);
533 
537  class worksheet worksheet();
538 
542  const class worksheet worksheet() const;
543 
547  class workbook &workbook();
548 
552  const class workbook &workbook() const;
553 
557  calendar base_date() const;
558 
562  std::string check_string(const std::string &to_check);
563 
564  // comment
565 
569  bool has_comment();
570 
574  void clear_comment();
575 
579  class comment comment();
580 
585  void comment(const std::string &text,
586  const std::string &author = "Microsoft Office User");
587 
592  void comment(const std::string &comment_text,
593  const class font &comment_font,
594  const std::string &author = "Microsoft Office User");
595 
599  void comment(const class comment &new_comment);
600 
604  double width() const;
605 
609  double height() const;
610 
611  // operators
612 
617  cell &operator=(const cell &rhs);
618 
622  bool operator==(const cell &comparand) const;
623 
627  bool operator!=(const cell &comparand) const;
628 
629 private:
630  friend class style;
631  friend class worksheet;
632  friend class detail::xlsx_consumer;
633  friend class detail::xlsx_producer;
634  friend struct detail::cell_impl;
635 
640  class format modifiable_format();
641 
645  cell() = delete;
646 
650  cell(detail::cell_impl *d);
651 
655  detail::cell_impl *d_;
656 };
657 
661 XLNT_API bool operator==(std::nullptr_t, const cell &cell);
662 
666 XLNT_API bool operator==(const cell &cell, std::nullptr_t);
667 
672 XLNT_API std::ostream &operator<<(std::ostream &stream, const xlnt::cell &cell);
673 
674 template <>
675 bool cell::value<bool>() const;
676 
677 template <>
678 int cell::value<int>() const;
679 
680 template <>
681 unsigned int cell::value<unsigned int>() const;
682 
683 template <>
684 long long int cell::value<long long int>() const;
685 
686 template <>
687 unsigned long long cell::value<unsigned long long int>() const;
688 
689 template <>
690 float cell::value<float>() const;
691 
692 template <>
693 double cell::value<double>() const;
694 
695 template <>
696 date cell::value<date>() const;
697 
698 template <>
699 time cell::value<time>() const;
700 
701 template <>
702 datetime cell::value<datetime>() const;
703 
704 template <>
705 timedelta cell::value<timedelta>() const;
706 
707 template <>
708 std::string cell::value<std::string>() const;
709 
710 template <>
711 rich_text cell::value<rich_text>() const;
712 
713 } // 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:55
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:54
std::uint32_t row_t
All rows should be referred to by an instance of this type.
Definition: index_types.hpp:41
Enumerates the possible types a cell can be determined by it&#39;s current value.
Definition: cell.hpp:37
Describes the font style of a particular cell.
Definition: font.hpp:41
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:77
Represents a span of time between two datetimes. This is not fully supported yet throughout the libra...
Definition: timedelta.hpp:37
Describes the border style of a particular cell.
Definition: border.hpp:94
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:60
Describes a unit of data in a worksheet at a specific coordinate and its associated properties...
Definition: cell.hpp:84
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:41
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:48
cell_type
Enumerates the possible types a cell can be determined by it&#39;s current value.
Definition: cell_type.hpp:39
bool operator!=(const std::string &reference_string, const range_reference &ref)
Returns true if the string representation of the range is not equivalent to ref.
Describes the protection style of a particular cell.
Definition: protection.hpp:38
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:56
A date is a specific day specified in terms of a year, month, and day. It can also be initialized as ...
Definition: date.hpp:39
workbook is the container for all other parts of the document.
Definition: workbook.hpp:92
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