OpenXLSX 1.12.1
Modern C++ Excel Library (Fork of OpenXLSX by Kenneth Troldal Balslev)
Loading...
Searching...
No Matches
XLSlicer.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_XLSLICER_HPP
12#define OPENXLSX_XLSLICER_HPP
13
14#include "OpenXLSX-Exports.hpp"
15#include "XLXmlFile.hpp"
16#include "XLXmlParser.hpp"
17#include <string>
18#include <string_view>
19#include <vector>
20#include <cstdint>
21
22namespace OpenXLSX
23{
24 class XLWorksheet;
25 class XLDocument;
26
27 // ─────────────────────────────────────────────────────────────────────────
28 // XLSlicerStyle — strongly-typed enum replacing magic strings
29 // ─────────────────────────────────────────────────────────────────────────
30 enum class XLSlicerStyle : uint8_t {
31 // Light styles
33 // Dark styles
35 // Other styles
37 // Custom (use setStyleRaw for arbitrary names)
38 Custom = 255
39 };
40
42 OPENXLSX_EXPORT std::string xlSlicerStyleToString(XLSlicerStyle style);
44 OPENXLSX_EXPORT XLSlicerStyle xlSlicerStyleFromString(std::string_view s);
45
46 // ─────────────────────────────────────────────────────────────────────────
47 // XLSlicer — three-source unified handle
48 //
49 // A slicer's full state lives across three XML parts:
50 // 1. xl/slicers/slicerN.xml → name, caption, style, showCaption …
51 // 2. xl/slicerCaches/slicerCacheN.xml → selectedItems, sortOrder …
52 // 3. xl/drawings/drawingM.xml → position anchor (cellRef, width, height)
53 //
54 // This class holds references to all three so every property is readable
55 // and writable from a single handle. All setters return *this for chaining.
56 // ─────────────────────────────────────────────────────────────────────────
57 class OPENXLSX_EXPORT XLSlicer
58 {
59 public:
60 // ── Constructors ────────────────────────────────────────────────────
61 XLSlicer() = default;
62
64 explicit XLSlicer(XLXmlData* slicerXml);
65
68 XMLNode slicerNode,
70 XMLNode anchorNode,
71 XLWorksheet* worksheet);
72
73 XLSlicer(const XLSlicer&) = default;
74 XLSlicer(XLSlicer&&) noexcept = default;
75 XLSlicer& operator=(const XLSlicer&) = default;
76 XLSlicer& operator=(XLSlicer&&) = default;
77 ~XLSlicer() = default;
78
79 // ── Validity ────────────────────────────────────────────────────────
80 [[nodiscard]] bool valid() const { return m_slicerXml != nullptr; }
81 explicit operator bool() const { return valid(); }
82
83 // ── Source 1: slicer XML properties ─────────────────────────────────
84
85 [[nodiscard]] std::string name() const;
86 [[nodiscard]] std::string caption() const;
87 [[nodiscard]] std::string cache() const;
88 [[nodiscard]] XLSlicerStyle style() const;
89 [[nodiscard]] std::string styleRaw() const;
90 [[nodiscard]] bool showCaption() const;
91 [[nodiscard]] int columnCount() const;
92 [[nodiscard]] bool lockedPosition() const;
93 [[nodiscard]] int rowHeight() const;
94
95 void setName(std::string_view name);
96 XLSlicer& setCaption(std::string_view caption);
97 XLSlicer& setStyle(XLSlicerStyle style);
98 XLSlicer& setStyleRaw(std::string_view rawStyleName);
99 XLSlicer& setShowCaption(bool show);
100 XLSlicer& setColumnCount(int cols);
101 XLSlicer& setLockedPosition(bool locked);
102 XLSlicer& setRowHeight(int emuHeight);
103
104 // ── Source 2: cache XML — filter items ──────────────────────────────
105
107 [[nodiscard]] std::vector<std::string> items() const;
109 [[nodiscard]] std::vector<std::string> selectedItems() const;
111 [[nodiscard]] bool isSortDescending() const;
112
114 XLSlicer& showOnly(const std::vector<std::string>& itemsToShow);
116 XLSlicer& showAll();
118 XLSlicer& hideItems(const std::vector<std::string>& itemsToHide);
120 XLSlicer& setSortDescending(bool desc);
121
122 // ── Source 3: drawing anchor — position & size ───────────────────────
123
125 [[nodiscard]] std::string cellRef() const;
127 [[nodiscard]] uint32_t width() const;
129 [[nodiscard]] uint32_t height() const;
130
132 XLSlicer& moveTo(std::string_view cellRef);
134 XLSlicer& resize(uint32_t widthPx, uint32_t heightPx);
135
136 // ── Internals used by XLSlicerCollection ────────────────────────────
137 XLXmlData* slicerXml() const { return m_slicerXml; }
138 XLXmlData* cacheXml() const { return m_cacheXml; }
139
140 private:
142 XMLNode slicerNode() const;
144 XMLNode cacheRoot() const;
145
146 XLXmlData* m_slicerXml{nullptr};
147 XMLNode m_slicerNode;
148 XLXmlData* m_cacheXml{nullptr};
149 XMLNode m_anchorNode;
150 };
151
152} // namespace OpenXLSX
153
154#endif // OPENXLSX_XLSLICER_HPP
This class encapsulates the concept of an excel file. It is different from the XLWorkbook,...
Definition XLDocument.hpp:97
Definition XLSlicer.hpp:58
XLXmlData * slicerXml() const
Definition XLSlicer.hpp:137
XLSlicer(const XLSlicer &)=default
bool valid() const
Definition XLSlicer.hpp:80
XLXmlData * cacheXml() const
Definition XLSlicer.hpp:138
XLSlicer(XLSlicer &&) noexcept=default
A class encapsulating an Excel worksheet. Access to XLWorksheet objects should be via the workbook ob...
Definition XLWorksheet.hpp:132
The XLXmlData class encapsulates the properties and behaviour of the .xml files in an ....
Definition XLXmlData.hpp:79
Definition IXLCellProvider.hpp:24
XLSlicerStyle
Definition XLSlicer.hpp:30
@ Dark3
Definition XLSlicer.hpp:34
@ Dark5
Definition XLSlicer.hpp:34
@ Dark1
Definition XLSlicer.hpp:34
@ Light1
Definition XLSlicer.hpp:32
@ Light3
Definition XLSlicer.hpp:32
@ Other2
Definition XLSlicer.hpp:36
@ Dark2
Definition XLSlicer.hpp:34
@ Dark4
Definition XLSlicer.hpp:34
@ Light5
Definition XLSlicer.hpp:32
@ Dark6
Definition XLSlicer.hpp:34
@ Light6
Definition XLSlicer.hpp:32
@ Light2
Definition XLSlicer.hpp:32
@ Light4
Definition XLSlicer.hpp:32
@ Other1
Definition XLSlicer.hpp:36
OPENXLSX_EXPORT XLSlicerStyle xlSlicerStyleFromString(std::string_view s)
Convert a raw style string to XLSlicerStyle enum (Custom if unrecognized).
Definition XLSlicer.cpp:47
OPENXLSX_EXPORT std::string xlSlicerStyleToString(XLSlicerStyle style)
Convert XLSlicerStyle enum to its Excel XML string representation.
Definition XLSlicer.cpp:26
OpenXLSX_xml_node XMLNode
Definition XLXmlParser.hpp:64
@ Custom
Definition XLChart.hpp:112