OpenXLSX 1.12.1
Modern C++ Excel Library (Fork of OpenXLSX by Kenneth Troldal Balslev)
Loading...
Searching...
No Matches
XLComments.hpp
Go to the documentation of this file.
1/*
2 * OpenXLSX-NX - Modern High-Performance C++ Excel Library
3 *
4 * Original work Copyright (c) 2018-2025, Kenneth Troldal Balslev and OpenXLSX contributors
5 * Modified work Copyright (c) 2026, Curry Tang and OpenXLSX-NX contributors
6 *
7 * Distributed under the BSD 3-Clause License.
8 * See LICENSE.md in the project root or https://opensource.org/licenses/BSD-3-Clause
9 */
10
11#ifndef OPENXLSX_XLCOMMENTS_HPP
12#define OPENXLSX_XLCOMMENTS_HPP
13
14// ===== External Includes ===== //
15#include <cstdint> // uint8_t, uint16_t, uint32_t
16#include <ostream> // std::basic_ostream
17#include <unordered_map> // std::unordered_map
18
19// ===== OpenXLSX Includes ===== //
20#include "OpenXLSX-Exports.hpp"
21// #include "XLDocument.hpp"
22#include "XLDrawing.hpp" // XLVmlDrawing
23#include "XLException.hpp"
24#include "XLRichText.hpp"
25#include "XLXmlData.hpp"
26#include "XLXmlFile.hpp"
27
28namespace OpenXLSX
29{
33 class OPENXLSX_EXPORT XLComment
34 {
35 public:
36 XLComment() = delete; // do not allow default constructor (for now) - could still be constructed with an empty XMLNode
37
42 explicit XLComment(XMLNode node);
43
48 XLComment(const XLComment& other) = default;
49
54 XLComment(XLComment&& other) noexcept = default;
55 ~XLComment() = default;
56
62 XLComment& operator=(const XLComment& other) = default;
63
69 XLComment& operator=(XLComment&& other) noexcept = default;
70
75 bool valid() const;
76 std::string ref() const; // the cell reference of the comment
77 std::string text() const;
78 XLRichText richText() const;
79 uint16_t authorId() const;
80 XLComment& setText(const std::string& newText);
82 XLComment& setAuthorId(uint16_t newAuthorId);
83
84 // /**
85 // * @brief Return a string summary of the comment properties
86 // * @return string with info about the comment object
87 // */
88 // std::string summary() const;
89
90 private:
91 mutable XMLNode m_commentNode;
92 };
93
97 class OPENXLSX_EXPORT XLComments : public XLXmlFile
98 {
99 friend class XLWorksheet; // for access to XLXmlFile::getXmlPath
100 public:
104 XLComments();
105
110 explicit XLComments(XLXmlData* xmlData);
111
116 XLComments(const XLComments& other) = default;
117
122 XLComments(XLComments&& other) noexcept = default;
123
128 ~XLComments() = default;
129
135 XLComments& operator=(XLComments&& other) noexcept = default;
136
141 XLComments& operator=(const XLComments& other) = default;
142
148 bool setVmlDrawing(const XLVmlDrawing& vmlDrawing);
149
150 private:
151 XMLNode authorNode(uint16_t index) const;
152 XMLNode commentNode(size_t index) const;
153 XMLNode commentNode(const std::string& cellRef) const;
154 void setupVmlShape(const std::string& cellRef,
155 uint32_t destRow,
156 uint16_t destCol,
157 bool newCommentCreated,
158 uint16_t widthCols = 4,
159 uint16_t heightRows = 6);
160
161 public:
162 uint16_t authorCount() const;
163
164 std::string author(uint16_t index) const;
165
166 bool deleteAuthor(uint16_t index);
167
168 uint16_t addAuthor(const std::string& authorName);
169
174 size_t count() const;
175
176 uint16_t authorId(const std::string& cellRef) const;
177
178 bool deleteComment(const std::string& cellRef);
179
185 XLComment get(size_t index) const;
186
192 std::string get(std::string const& cellRef) const;
193
203 bool set(std::string const& cellRef,
204 std::string const& comment,
205 uint16_t authorId_ = 0,
206 uint16_t widthCols = 4,
207 uint16_t heightRows = 6);
208
218 bool set(std::string const& cellRef,
219 std::string const& comment,
220 std::string const& authorName,
221 uint16_t widthCols = 4,
222 uint16_t heightRows = 6);
223
233 bool setRichText(std::string const& cellRef,
234 const XLRichText& richText,
235 uint16_t authorId_ = 0,
236 uint16_t widthCols = 4,
237 uint16_t heightRows = 6);
238
248 bool setRichText(std::string const& cellRef,
249 const XLRichText& richText,
250 std::string const& authorName,
251 uint16_t widthCols = 4,
252 uint16_t heightRows = 6);
253
257 XLShape shape(std::string const& cellRef);
258
265 bool setVisible(std::string const& cellRef, bool visible);
266
270 void print(std::basic_ostream<char>& ostr) const;
271
272 private:
273 XMLNode m_authors{};
274 XMLNode m_commentList{};
275 XLVmlDrawing m_vmlDrawing;
276 mutable std::unordered_map<std::string, XMLNode> m_commentMap{};
277 mutable XMLNode m_hintNode{}; // the last comment XML Node accessed by index is stored here, if any - will be reset when comments
278 // are inserted or deleted
279 mutable size_t m_hintIndex{0}; // this has the index at which m_hintNode was accessed, only valid if not m_hintNode.empty()
280 inline static const std::vector<std::string_view> m_nodeOrder = {// comments XML node required child sequence
281 "authors",
282 "commentList"};
283 };
284} // namespace OpenXLSX
285
286#endif // OPENXLSX_XLCOMMENTS_HPP
An encapsulation of a comment element.
Definition XLComments.hpp:34
uint16_t authorId() const
Definition XLComments.cpp:127
std::string ref() const
Definition XLComments.cpp:124
std::string text() const
Definition XLComments.cpp:125
bool valid() const
Test if XLComment is linked to valid XML.
Definition XLComments.cpp:123
XLRichText richText() const
Definition XLComments.cpp:126
XLComment & setAuthorId(uint16_t newAuthorId)
Definition XLComments.cpp:198
XLComment(XLComment &&other) noexcept=default
Move Constructor.
XLComment & setRichText(const XLRichText &richText)
Definition XLComments.cpp:137
XLComment & setText(const std::string &newText)
Definition XLComments.cpp:128
XLComment(const XLComment &other)=default
Copy Constructor.
XLComment & operator=(XLComment &&other) noexcept=default
Move assignment operator.
XLComment & operator=(const XLComment &other)=default
Copy assignment operator.
void print(std::basic_ostream< char > &ostr) const
Print the XML contents of this XLComments instance using the underlying XMLNode print function.
Definition XLComments.cpp:671
bool setRichText(std::string const &cellRef, const XLRichText &richText, uint16_t authorId_=0, uint16_t widthCols=4, uint16_t heightRows=6)
set the rich text comment for the referenced cell
Definition XLComments.cpp:556
XLComments & operator=(const XLComments &other)=default
Assignment operator.
bool deleteComment(const std::string &cellRef)
Definition XLComments.cpp:364
friend class XLWorksheet
Definition XLComments.hpp:99
bool setVmlDrawing(const XLVmlDrawing &vmlDrawing)
associate the worksheet's VML drawing object with the comments so it can be modified from here
Definition XLComments.cpp:237
uint16_t authorId(const std::string &cellRef) const
Definition XLComments.cpp:359
bool deleteAuthor(uint16_t index)
Definition XLComments.cpp:307
XLComments()
Constructor.
Definition XLComments.cpp:204
uint16_t authorCount() const
Definition XLComments.cpp:288
XLComments & operator=(XLComments &&other) noexcept=default
std::string author(uint16_t index) const
Definition XLComments.cpp:298
bool setVisible(std::string const &cellRef, bool visible)
Set the visibility of the comment shape.
Definition XLComments.cpp:639
bool set(std::string const &cellRef, std::string const &comment, uint16_t authorId_=0, uint16_t widthCols=4, uint16_t heightRows=6)
set the comment for the referenced cell
Definition XLComments.cpp:480
XLShape shape(std::string const &cellRef)
get the XLShape object for this comment
Definition XLComments.cpp:627
~XLComments()=default
The destructor.
uint16_t addAuthor(const std::string &authorName)
Definition XLComments.cpp:325
XLComments(XLComments &&other) noexcept=default
size_t count() const
get the amount of comments
Definition XLComments.cpp:349
XLComments(const XLComments &other)=default
The copy constructor.
XLComment get(size_t index) const
get a comment by its index in the comment list
Definition XLComments.cpp:382
A class representing rich text in a cell.
Definition XLRichText.hpp:139
Definition XLDrawing.hpp:221
The XLVmlDrawing class is the base class for worksheet comments.
Definition XLDrawing.hpp:523
The XLXmlData class encapsulates the properties and behaviour of the .xml files in an ....
Definition XLXmlData.hpp:79
std::string xmlData(XLXmlSavingDeclaration savingDeclaration=XLXmlSavingDeclaration{}) const
Method for getting the XML data represented by the object.
Definition XLXmlFile.cpp:49
XLXmlFile()=default
Default constructor.
Definition IXLCellProvider.hpp:24
OpenXLSX_xml_node XMLNode
Definition XLXmlParser.hpp:64