OpenXLSX 1.12.1
Modern C++ Excel Library (Fork of OpenXLSX by Kenneth Troldal Balslev)
Loading...
Searching...
No Matches
XLSheetBase.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_XLSHEETBASE_HPP
12#define OPENXLSX_XLSHEETBASE_HPP
13
14#include "OpenXLSX-Exports.hpp"
15#include "XLColor.hpp"
16#include "XLCommandQuery.hpp"
17#include "XLDocument.hpp"
18#include "XLXmlFile.hpp"
19#include <cstdint>
20#include <mutex>
21#include <shared_mutex>
22#include <string>
23#include <type_traits>
24
25namespace OpenXLSX
26{
27 class XLWorksheet;
28 class XLChartsheet;
29
30 constexpr const bool XLEmptyHiddenCells = true; // readability constant for XLWorksheet::mergeCells parameter emptyHiddenCells
31
35 enum class XLSheetState : uint8_t { Visible, Hidden, VeryHidden };
36
40 enum class XLPaneState : uint8_t { Split, Frozen, FrozenSplit };
41
45 enum class XLPane : uint8_t { BottomRight, TopRight, BottomLeft, TopLeft };
46
47 OPENXLSX_EXPORT std::string XLPaneStateToString(XLPaneState state);
48 OPENXLSX_EXPORT XLPaneState XLPaneStateFromString(std::string const& stateString);
49 OPENXLSX_EXPORT std::string XLPaneToString(XLPane pane);
50 OPENXLSX_EXPORT XLPane XLPaneFromString(std::string const& paneString);
51
52 OPENXLSX_EXPORT void setTabColor(const XMLDocument& xmlDocument, const XLColor& color);
53 OPENXLSX_EXPORT void setTabSelected(const XMLDocument& xmlDocument, bool selected);
54 OPENXLSX_EXPORT bool tabIsSelected(const XMLDocument& xmlDocument);
55
62 template<typename T, typename = std::enable_if_t<std::is_same_v<T, XLWorksheet> or std::is_same_v<T, XLChartsheet>>>
63 class OPENXLSX_EXPORT XLSheetBase : public XLXmlFile
64 {
65 public:
69 XLSheetBase() : XLXmlFile(nullptr) {};
70
78
84 XLSheetBase(const XLSheetBase& other) = default;
85
90 XLSheetBase(XLSheetBase&& other) noexcept = default;
91
96 ~XLSheetBase() = default;
97
104
110 XLSheetBase& operator=(XLSheetBase&& other) noexcept = default;
111
117 {
119 query.setParam("sheetID", relationshipID());
120 const auto state = parentDoc().execQuery(query).template result<std::string>();
121 auto result = XLSheetState::Visible;
122
123 if (state == "visible" or state.empty()) { result = XLSheetState::Visible; }
124 else if (state == "hidden") {
125 result = XLSheetState::Hidden;
126 }
127 else if (state == "veryHidden") {
129 }
130
131 return result;
132 }
133
139 {
140 std::unique_lock<std::shared_mutex> lock(parentDoc().mutex());
141 auto stateString = std::string();
142 switch (state) {
144 stateString = "visible";
145 break;
146
148 stateString = "hidden";
149 break;
150
152 stateString = "veryHidden";
153 break;
154 }
155
157 .setParam("sheetID", relationshipID())
158 .setParam("sheetVisibility", stateString));
159 }
160
166 XLColor color() const { return static_cast<const T&>(*this).getColor_impl(); }
167
172 void setColor(const XLColor& color) { static_cast<T&>(*this).setColor_impl(color); }
173
179 uint16_t index() const
180 {
182 query.setParam("sheetID", relationshipID());
183 return static_cast<uint16_t>(std::stoi(parentDoc().execQuery(query).template result<std::string>()));
184 }
185
190 void setIndex(uint16_t index)
191 {
192 std::unique_lock<std::shared_mutex> lock(parentDoc().mutex());
193 parentDoc().execCommand(
194 XLCommand(XLCommandType::SetSheetIndex).setParam("sheetID", relationshipID()).setParam("sheetIndex", index));
195 }
196
201 std::string name() const
202 {
204 query.setParam("sheetID", relationshipID());
205 return parentDoc().execQuery(query).template result<std::string>();
206 }
207
212 void setName(const std::string& sheetName)
213 {
214 std::unique_lock<std::shared_mutex> lock(parentDoc().mutex());
216 .setParam("sheetID", relationshipID())
217 .setParam("sheetName", name())
218 .setParam("newName", sheetName));
219 }
220
225 bool isSelected() const { return static_cast<const T&>(*this).isSelected_impl(); }
226
231 void setSelected(bool selected) { static_cast<T&>(*this).setSelected_impl(selected); }
232
237 bool isActive() const { return static_cast<const T&>(*this).isActive_impl(); }
238
242 bool setActive() { return static_cast<T&>(*this).setActive_impl(); }
243
250 void clone(const std::string& newName)
251 {
252 parentDoc().execCommand(
253 XLCommand(XLCommandType::CloneSheet).setParam("sheetID", relationshipID()).setParam("cloneName", newName));
254 }
255
256 private: // ---------- Private Member Variables ---------- //
257 };
258} // namespace OpenXLSX
259
260#endif
Class representing an Excel chartsheet.
Definition XLChartsheet.hpp:25
Definition XLColor.hpp:32
Definition XLCommandQuery.hpp:53
Definition XLCommandQuery.hpp:116
XLQuery & setParam(std::string_view param, T value)
Definition XLCommandQuery.hpp:132
bool setActive()
Definition XLSheetBase.hpp:242
bool isSelected() const
Definition XLSheetBase.hpp:225
std::string name() const
Method to retrieve the name of the sheet.
Definition XLSheetBase.hpp:201
bool isActive() const
Definition XLSheetBase.hpp:237
uint16_t index() const
look up sheet name via relationshipID, then attempt to match that to a sheet in XLWorkbook::sheet(uin...
Definition XLSheetBase.hpp:179
XLSheetBase(const XLSheetBase &other)=default
The copy constructor.
XLColor color() const
Definition XLSheetBase.hpp:166
void setIndex(uint16_t index)
Definition XLSheetBase.hpp:190
void clone(const std::string &newName)
Method for cloning the sheet.
Definition XLSheetBase.hpp:250
void setColor(const XLColor &color)
Definition XLSheetBase.hpp:172
~XLSheetBase()=default
The destructor.
void setVisibility(XLSheetState state)
Definition XLSheetBase.hpp:138
void setName(const std::string &sheetName)
Method for renaming the sheet.
Definition XLSheetBase.hpp:212
XLSheetState visibility() const
Definition XLSheetBase.hpp:116
XLSheetBase & operator=(const XLSheetBase &)=default
Assignment operator.
XLSheetBase & operator=(XLSheetBase &&other) noexcept=default
XLSheetBase()
Constructor.
Definition XLSheetBase.hpp:69
XLSheetBase(XLXmlData *xmlData)
The constructor. There are no default constructor, so all parameters must be provided for constructin...
Definition XLSheetBase.hpp:77
void setSelected(bool selected)
Definition XLSheetBase.hpp:231
XLSheetBase(XLSheetBase &&other) 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
std::string relationshipID() const
This function returns the relationship ID (the ID used in the XLRelationships objects) for the object...
Definition XLXmlFile.cpp:101
XLDocument & parentDoc()
This function provides access to the parent XLDocument object.
Definition XLXmlFile.cpp:61
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_EXPORT void setTabSelected(const XMLDocument &xmlDocument, bool selected)
Set the tab selected property to desired value.
Definition XLSheet.cpp:47
XLPaneState
The XLPaneState is an enumeration of the possible states of a pane, e.g. Frozen or Split.
Definition XLSheetBase.hpp:40
@ FrozenSplit
Definition XLSheetBase.hpp:40
@ Frozen
Definition XLSheetBase.hpp:40
@ Split
Definition XLSheetBase.hpp:40
constexpr const bool XLEmptyHiddenCells
Definition XLSheetBase.hpp:30
OPENXLSX_EXPORT XLPaneState XLPaneStateFromString(std::string const &stateString)
inverse of XLPaneStateToString
Definition XLSheet.cpp:93
OPENXLSX_EXPORT XLPane XLPaneFromString(std::string const &paneString)
inverse of XLPaneToString
Definition XLSheet.cpp:126
XLPane
The XLPane is an enumeration of the possible pane identifiers.
Definition XLSheetBase.hpp:45
@ BottomRight
Definition XLSheetBase.hpp:45
@ BottomLeft
Definition XLSheetBase.hpp:45
@ TopLeft
Definition XLSheetBase.hpp:45
OpenXLSX_xml_document XMLDocument
Definition XLXmlParser.hpp:65
XLSheetState
The XLSheetState is an enumeration of the possible (visibility) states, e.g. Visible or Hidden.
Definition XLSheetBase.hpp:35
@ Hidden
Definition XLSheetBase.hpp:35
@ VeryHidden
Definition XLSheetBase.hpp:35
@ Visible
Definition XLSheetBase.hpp:35
@ TopRight
Definition XLChart.hpp:70
@ Hidden
Definition XLChart.hpp:70
OPENXLSX_EXPORT bool tabIsSelected(const XMLDocument &xmlDocument)
Function for checking if the tab is selected.
Definition XLSheet.cpp:61
OPENXLSX_EXPORT std::string XLPaneStateToString(XLPaneState state)
get the correct XLPaneState from the OOXML pane state attribute string
Definition XLSheet.cpp:75
OPENXLSX_EXPORT void setTabColor(const XMLDocument &xmlDocument, const XLColor &color)
Function for setting tab color.
Definition XLSheet.cpp:29
OPENXLSX_EXPORT std::string XLPaneToString(XLPane pane)
get the correct XLPane from the OOXML pane identifier attribute string
Definition XLSheet.cpp:106
@ QuerySheetIndex
Definition XLCommandQuery.hpp:101
@ QuerySheetVisibility
Definition XLCommandQuery.hpp:102
@ QuerySheetName
Definition XLCommandQuery.hpp:100
@ CloneSheet
Definition XLCommandQuery.hpp:45
@ SetSheetIndex
Definition XLCommandQuery.hpp:34
@ SetSheetVisibility
Definition XLCommandQuery.hpp:33
@ SetSheetName
Definition XLCommandQuery.hpp:32