OpenXLSX 1.12.1
Modern C++ Excel Library (Fork of OpenXLSX by Kenneth Troldal Balslev)
Loading...
Searching...
No Matches
XLStyles_CellFormats.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_XLSTYLES_CELLFORMATS_HPP
12#define OPENXLSX_XLSTYLES_CELLFORMATS_HPP
13
14#ifdef _MSC_VER
15# pragma warning(push)
16# pragma warning(disable : 4251)
17# pragma warning(disable : 4275)
18#endif
19
20#include "XLStyles_Common.hpp"
21#include "XLXmlFile.hpp"
22#include "XLXmlParser.hpp"
23
24#include <memory>
25#include <string>
26#include <string_view>
27#include <unordered_map>
28#include <vector>
29
30
31namespace OpenXLSX
32{
33 class OPENXLSX_EXPORT XLAlignment
34 {
35 friend class XLStyles;
36 public: // ---------- Public Member Functions ---------- //
41
46 explicit XLAlignment(const XMLNode& node);
47
52 XLAlignment(const XLAlignment& other);
53
58 XLAlignment(XLAlignment&& other) noexcept = default;
59
64
70 XLAlignment& operator=(const XLAlignment& other);
71
77 XLAlignment& operator=(XLAlignment&& other) noexcept = default;
78
84
90
96 uint16_t textRotation() const;
97
102 bool wrapText() const;
103
108 uint32_t indent() const;
109
115 int32_t relativeIndent() const;
116
121 bool justifyLastLine() const;
122
127 bool shrinkToFit() const;
128
133 uint32_t readingOrder() const;
134
140 bool setHorizontal(XLAlignmentStyle newStyle);
141 bool setVertical(XLAlignmentStyle newStyle);
151 bool setTextRotation(uint16_t newRotation);
152 bool setWrapText(bool set = true);
153 bool setIndent(uint32_t newIndent);
154 bool setRelativeIndent(int32_t newIndent);
155 bool setJustifyLastLine(bool set = true);
156 bool setShrinkToFit(bool set = true);
157 bool setReadingOrder(
158 uint32_t newReadingOrder); // can be used with XLReadingOrderContextual, XLReadingOrderLeftToRight, XLReadingOrderRightToLeft
159
164 std::string summary() const;
165
166 private: // ---------- Private Member Variables ---------- //
167 std::unique_ptr<XMLNode> m_alignmentNode;
168 };
169
173 class OPENXLSX_EXPORT XLCellFormat
174 {
175 friend class XLCellFormats; // for access to m_cellFormatNode in XLCellFormats::create
176 friend class XLStyles;
177 public: // ---------- Public Member Functions ---------- //
181 XLCellFormat();
182
188 explicit XLCellFormat(const XMLNode& node, bool permitXfId);
189
194 XLCellFormat(const XLCellFormat& other);
195
200 XLCellFormat(XLCellFormat&& other) noexcept = default;
201
206
212 XLCellFormat& operator=(const XLCellFormat& other);
213
219 XLCellFormat& operator=(XLCellFormat&& other) noexcept = default;
220
224 XMLNode node() const { return *m_cellFormatNode; }
225
230 uint32_t numberFormatId() const;
231
236 XLStyleIndex fontIndex() const;
237
242 XLStyleIndex fillIndex() const;
243
248 XLStyleIndex borderIndex() const;
249
256 XLStyleIndex xfId() const;
257
262 bool applyNumberFormat() const;
263
268 bool applyFont() const;
269
274 bool applyFill() const;
275
280 bool applyBorder() const;
281
286 bool applyAlignment() const;
287
292 bool applyProtection() const;
293
300 bool quotePrefix() const;
301
308 bool pivotButton() const;
309
314 bool locked() const;
315
320 bool hidden() const;
321
327 XLAlignment alignment(bool createIfMissing = false) const;
328
332 XLUnsupportedElement extLst() const { return XLUnsupportedElement{}; } // <xf><extLst>...</extLst></xf>
333
339 XLCellFormat& setNumberFormatId(uint32_t newNumFmtId);
340 XLCellFormat& setFontIndex(XLStyleIndex newFontIndex);
341 XLCellFormat& setFillIndex(XLStyleIndex newFillIndex);
342 XLCellFormat& setBorderIndex(XLStyleIndex newBorderIndex);
343 XLCellFormat& setXfId(XLStyleIndex newXfId); // NOTE: throws when invoked from cellStyleFormats
344 XLCellFormat& setApplyNumberFormat(bool set = true);
345 XLCellFormat& setApplyFont(bool set = true);
346 XLCellFormat& setApplyFill(bool set = true);
347 XLCellFormat& setApplyBorder(bool set = true);
348 XLCellFormat& setApplyAlignment(bool set = true);
349 XLCellFormat& setApplyProtection(bool set = true);
350 XLCellFormat& setQuotePrefix(bool set = true);
351 XLCellFormat& setPivotButton(bool set = true);
352 XLCellFormat& setLocked(bool set = true);
353 XLCellFormat& setHidden(bool set = true);
357 XLCellFormat& setExtLst(XLUnsupportedElement const& newExtLst);
358
363 std::string summary() const;
364
365 private: // ---------- Private Member Variables ---------- //
366 std::unique_ptr<XMLNode> m_cellFormatNode;
367 bool m_permitXfId{false};
368 inline static const std::vector<std::string_view> m_nodeOrder = {"alignment", "protection"};
369 };
370
374 class OPENXLSX_EXPORT XLCellFormats
375 {
376 public: // ---------- Public Member Functions ---------- //
381
387 explicit XLCellFormats(const XMLNode& node, bool permitXfId = false);
388
393 XLCellFormats(const XLCellFormats& other);
394
400
405
412
418 XLCellFormats& operator=(XLCellFormats&& other) noexcept = default;
419
424 size_t count() const;
425
432
439
447 XLStyleIndex create(XLCellFormat copyFrom = XLCellFormat{}, std::string_view styleEntriesPrefix = XLDefaultStyleEntriesPrefix);
448
454 XLStyleIndex findOrCreate(XLCellFormat copyFrom, std::string_view styleEntriesPrefix = XLDefaultStyleEntriesPrefix);
455
460 void rebuild(const std::vector<XMLNode>& nodes);
461
462 private: // ---------- Private Member Variables ---------- //
463 std::unique_ptr<XMLNode> m_cellFormatsNode;
464 std::vector<XLCellFormat> m_cellFormats;
465 bool m_permitXfId{false};
466 mutable std::unordered_map<std::string, XLStyleIndex> m_fingerprintCache;
467 };
468
469} // namespace OpenXLSX
470
471#ifdef _MSC_VER
472# pragma warning(pop)
473#endif
474
475#endif // OPENXLSX_XLSTYLES_CELLFORMATS_HPP
Definition XLStyles_CellFormats.hpp:34
bool setShrinkToFit(bool set=true)
Definition XLStyles_CellFormats.cpp:112
uint32_t indent() const
Get the indent setting.
Definition XLStyles_CellFormats.cpp:88
bool setRelativeIndent(int32_t newIndent)
Definition XLStyles_CellFormats.cpp:108
XLAlignment()
Definition XLStyles_CellFormats.cpp:66
bool setReadingOrder(uint32_t newReadingOrder)
Definition XLStyles_CellFormats.cpp:114
XLAlignment & operator=(XLAlignment &&other) noexcept=default
Move assignment operator.
uint32_t readingOrder() const
Get the reading order setting.
Definition XLStyles_CellFormats.cpp:96
bool setTextRotation(uint16_t newRotation)
Definition XLStyles_CellFormats.cpp:102
bool wrapText() const
Check whether text wrapping is enabled.
Definition XLStyles_CellFormats.cpp:86
bool shrinkToFit() const
Check whether shrink to fit is enabled.
Definition XLStyles_CellFormats.cpp:94
XLAlignment(XLAlignment &&other) noexcept=default
Move Constructor.
friend class XLStyles
Definition XLStyles_CellFormats.hpp:35
bool setVertical(XLAlignmentStyle newStyle)
Definition XLStyles_CellFormats.cpp:100
bool setIndent(uint32_t newIndent)
Definition XLStyles_CellFormats.cpp:106
bool setWrapText(bool set=true)
Definition XLStyles_CellFormats.cpp:104
bool setHorizontal(XLAlignmentStyle newStyle)
Setter functions for style parameters.
Definition XLStyles_CellFormats.cpp:98
XLAlignmentStyle horizontal() const
Get the horizontal alignment.
Definition XLStyles_CellFormats.cpp:80
int32_t relativeIndent() const
Get the relative indent setting.
Definition XLStyles_CellFormats.cpp:90
XLAlignmentStyle vertical() const
Get the vertical alignment.
Definition XLStyles_CellFormats.cpp:82
bool setJustifyLastLine(bool set=true)
Definition XLStyles_CellFormats.cpp:110
uint16_t textRotation() const
Get text rotation.
Definition XLStyles_CellFormats.cpp:84
bool justifyLastLine() const
Check whether justification of last line is enabled.
Definition XLStyles_CellFormats.cpp:92
std::string summary() const
Return a string summary of the alignment properties.
Definition XLStyles_CellFormats.cpp:117
XLAlignment & operator=(const XLAlignment &other)
Copy assignment operator.
Definition XLStyles_CellFormats.cpp:74
An encapsulation of a cell format item.
Definition XLStyles_CellFormats.hpp:174
XLUnsupportedElement extLst() const
Unsupported getter.
Definition XLStyles_CellFormats.hpp:332
XLCellFormat(XLCellFormat &&other) noexcept=default
Move Constructor.
XLCellFormat & operator=(XLCellFormat &&other) noexcept=default
Move assignment operator.
XMLNode node() const
Get the underlying XMLNode of the cell format item.
Definition XLStyles_CellFormats.hpp:224
friend class XLCellFormats
Definition XLStyles_CellFormats.hpp:175
friend class XLStyles
Definition XLStyles_CellFormats.hpp:176
XLCellFormat()
Definition XLStyles_CellFormats.cpp:132
XLCellFormat & operator=(const XLCellFormat &other)
Copy assignment operator.
Definition XLStyles_CellFormats.cpp:146
XLCellFormat operator[](XLStyleIndex index) const
Operator overload: allow [] as shortcut access to cellFormatByIndex.
Definition XLStyles_CellFormats.hpp:438
size_t count() const
Get the count of cell style format descriptions.
Definition XLStyles_CellFormats.cpp:378
XLCellFormats()
Definition XLStyles_CellFormats.cpp:332
XLCellFormats & operator=(XLCellFormats &&other) noexcept=default
Move assignment operator.
XLCellFormat cellFormatByIndex(XLStyleIndex index) const
Get the cell style format description identified by index.
Definition XLStyles_CellFormats.cpp:380
XLCellFormats & operator=(const XLCellFormats &other)
Copy assignment operator.
Definition XLStyles_CellFormats.cpp:367
The XLUnsupportedElement class provides a stub implementation that can be used as function parameter ...
Definition XLXmlFile.hpp:42
Definition IXLCellProvider.hpp:24
XLAlignmentStyle
Definition XLStyles_Common.hpp:175
OpenXLSX_xml_node XMLNode
Definition XLXmlParser.hpp:64
size_t XLStyleIndex
Definition XLStyles_Common.hpp:36