OpenXLSX 1.12.1
Modern C++ Excel Library (Fork of OpenXLSX by Kenneth Troldal Balslev)
Loading...
Searching...
No Matches
XLMergeCells.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_XLMERGECELLS_HPP
12#define OPENXLSX_XLMERGECELLS_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 <deque>
21#include <limits>
22#include <memory>
23#include <ostream>
24#include <string>
25#include <string_view>
26#include <vector>
27
28// ===== OpenXLSX Includes ===== //
29#include "OpenXLSX-Exports.hpp"
30#include "XLCellReference.hpp"
31#include "XLXmlParser.hpp"
32
33namespace OpenXLSX
34{
35 typedef int32_t XLMergeIndex;
36 constexpr const XLMergeIndex XLMergeNotFound = -1;
37
38 constexpr size_t XLMaxMergeCells = (std::numeric_limits<XLMergeIndex>::max)();
39
47 class OPENXLSX_EXPORT XLMergeCells
48 {
49 public:
53 struct XLRect
54 {
55 uint32_t top;
56 uint16_t left;
57 uint32_t bottom;
58 uint16_t right;
59
63 [[nodiscard]] bool contains(uint32_t row, uint16_t col) const noexcept
64 { return row >= top && row <= bottom && col >= left && col <= right; }
65
70 [[nodiscard]] bool overlaps(const XLRect& other) const noexcept
71 { return top <= other.bottom && bottom >= other.top && left <= other.right && right >= other.left; }
72 };
73
75 {
76 std::string reference;
78 };
79
80 public:
82
87 explicit XLMergeCells(const XMLNode& rootNode, std::vector<std::string_view> const& nodeOrder);
88
89 ~XLMergeCells() = default;
90 XLMergeCells(const XLMergeCells& other) = default;
91 XLMergeCells(XLMergeCells&& other) noexcept = default;
92 XLMergeCells& operator=(const XLMergeCells& other) = default;
93 XLMergeCells& operator=(XLMergeCells&& other) noexcept = default;
94
98 [[nodiscard]] bool valid() const;
99
103 [[nodiscard]] XLMergeIndex findMerge(std::string_view reference) const;
104
105 [[nodiscard]] bool mergeExists(std::string_view reference) const;
106
110 [[nodiscard]] XLMergeIndex findMergeByCell(std::string_view cellRef) const;
111 [[nodiscard]] XLMergeIndex findMergeByCell(XLCellReference cellRef) const;
112
113 [[nodiscard]] size_t count() const;
114
118 [[nodiscard]] const char* merge(XLMergeIndex index) const;
119
120 [[nodiscard]] const char* operator[](XLMergeIndex index) const { return merge(index); }
121
126 XLMergeIndex appendMerge(const std::string& reference);
127
131 void deleteMerge(XLMergeIndex index);
132
136 void deleteAll();
137
146 void shiftRows(int32_t delta, uint32_t fromRow);
147
153 void shiftCols(int32_t delta, uint16_t fromCol);
154
155 void print(std::basic_ostream<char>& ostr) const;
156
157 private:
158 XMLNode m_rootNode;
159 std::vector<std::string_view> m_nodeOrder;
160 XMLNode m_mergeCellsNode;
161 std::deque<XLMergeEntry> m_mergeCache;
162 };
163} // namespace OpenXLSX
164
165#ifdef _MSC_VER
166# pragma warning(pop)
167#endif
168
169#endif // OPENXLSX_XLMERGECELLS_HPP
Definition XLCellReference.hpp:44
XLMergeCells(const XLMergeCells &other)=default
const char * operator[](XLMergeIndex index) const
Definition XLMergeCells.hpp:120
XLMergeCells(XLMergeCells &&other) noexcept=default
XLMergeCells & operator=(XLMergeCells &&other) noexcept=default
bool mergeExists(std::string_view reference) const
Definition XLMergeCells.cpp:130
size_t count() const
Definition XLMergeCells.cpp:154
const char * merge(XLMergeIndex index) const
Definition XLMergeCells.cpp:159
XLMergeCells & operator=(const XLMergeCells &other)=default
XLMergeIndex findMerge(std::string_view reference) const
Definition XLMergeCells.cpp:119
bool valid() const
Definition XLMergeCells.cpp:114
XLMergeIndex findMergeByCell(std::string_view cellRef) const
Definition XLMergeCells.cpp:135
Definition IXLCellProvider.hpp:24
constexpr const XLMergeIndex XLMergeNotFound
Definition XLMergeCells.hpp:36
OpenXLSX_xml_node XMLNode
Definition XLXmlParser.hpp:64
constexpr size_t XLMaxMergeCells
Definition XLMergeCells.hpp:38
int32_t XLMergeIndex
Definition XLMergeCells.hpp:35
Definition XLMergeCells.hpp:75
std::string reference
Definition XLMergeCells.hpp:76
XLRect rect
Definition XLMergeCells.hpp:77
Internal numerical bounds representation for constant-time coordinate tests.
Definition XLMergeCells.hpp:54
uint32_t top
Definition XLMergeCells.hpp:55
uint16_t left
Definition XLMergeCells.hpp:56
uint16_t right
Definition XLMergeCells.hpp:58
uint32_t bottom
Definition XLMergeCells.hpp:57
bool overlaps(const XLRect &other) const noexcept
Detect spatial intersection between two regions to prevent overlapping merges, which are illegal in t...
Definition XLMergeCells.hpp:70
bool contains(uint32_t row, uint16_t col) const noexcept
Determine if a cell coordinate is within the merged region.
Definition XLMergeCells.hpp:63