OpenXLSX 1.9.1
Loading...
Searching...
No Matches
XLSheetBase.hpp
Go to the documentation of this file.
1#ifndef OPENXLSX_XLSHEETBASE_HPP
2#define OPENXLSX_XLSHEETBASE_HPP
3
4#include "OpenXLSX-Exports.hpp"
5#include "XLColor.hpp"
6#include "XLCommandQuery.hpp"
7#include "XLDocument.hpp"
8#include "XLXmlFile.hpp"
9#include <cstdint>
10#include <mutex>
11#include <shared_mutex>
12#include <string>
13#include <type_traits>
14
15namespace OpenXLSX
16{
17 class XLWorksheet;
18 class XLChartsheet;
19
20 constexpr const bool XLEmptyHiddenCells = true; // readability constant for XLWorksheet::mergeCells parameter emptyHiddenCells
21
25 enum class XLSheetState : uint8_t { Visible, Hidden, VeryHidden };
26
30 enum class XLPaneState : uint8_t { Split, Frozen, FrozenSplit };
31
35 enum class XLPane : uint8_t { BottomRight, TopRight, BottomLeft, TopLeft };
36
37 OPENXLSX_EXPORT std::string XLPaneStateToString(XLPaneState state);
38 OPENXLSX_EXPORT XLPaneState XLPaneStateFromString(std::string const& stateString);
39 OPENXLSX_EXPORT std::string XLPaneToString(XLPane pane);
40 OPENXLSX_EXPORT XLPane XLPaneFromString(std::string const& paneString);
41
42 OPENXLSX_EXPORT void setTabColor(const XMLDocument& xmlDocument, const XLColor& color);
43 OPENXLSX_EXPORT void setTabSelected(const XMLDocument& xmlDocument, bool selected);
44 OPENXLSX_EXPORT bool tabIsSelected(const XMLDocument& xmlDocument);
45
52 template<typename T, typename = std::enable_if_t<std::is_same_v<T, XLWorksheet> or std::is_same_v<T, XLChartsheet>>>
53 class OPENXLSX_EXPORT XLSheetBase : public XLXmlFile
54 {
55 public:
59 XLSheetBase() : XLXmlFile(nullptr) {};
60
68
74 XLSheetBase(const XLSheetBase& other) = default;
75
80 XLSheetBase(XLSheetBase&& other) noexcept = default;
81
86 ~XLSheetBase() = default;
87
93 XLSheetBase& operator=(const XLSheetBase&) = default;
94
100 XLSheetBase& operator=(XLSheetBase&& other) noexcept = default;
101
107 {
108 XLQuery query(XLQueryType::QuerySheetVisibility);
109 query.setParam("sheetID", relationshipID());
110 const auto state = parentDoc().execQuery(query).template result<std::string>();
111 auto result = XLSheetState::Visible;
112
113 if (state == "visible" or state.empty()) { result = XLSheetState::Visible; }
114 else if (state == "hidden") {
115 result = XLSheetState::Hidden;
116 }
117 else if (state == "veryHidden") {
118 result = XLSheetState::VeryHidden;
119 }
120
121 return result;
122 }
123
129 {
130 std::unique_lock<std::shared_mutex> lock(parentDoc().mutex());
131 auto stateString = std::string();
132 switch (state) {
133 case XLSheetState::Visible:
134 stateString = "visible";
135 break;
136
137 case XLSheetState::Hidden:
138 stateString = "hidden";
139 break;
140
141 case XLSheetState::VeryHidden:
142 stateString = "veryHidden";
143 break;
144 }
145
146 parentDoc().execCommand(XLCommand(XLCommandType::SetSheetVisibility)
147 .setParam("sheetID", relationshipID())
148 .setParam("sheetVisibility", stateString));
149 }
150
156 XLColor color() const { return static_cast<const T&>(*this).getColor_impl(); }
157
162 void setColor(const XLColor& color) { static_cast<T&>(*this).setColor_impl(color); }
163
169 uint16_t index() const
170 {
171 XLQuery query(XLQueryType::QuerySheetIndex);
172 query.setParam("sheetID", relationshipID());
173 return static_cast<uint16_t>(std::stoi(parentDoc().execQuery(query).template result<std::string>()));
174 }
175
180 void setIndex(uint16_t index)
181 {
182 std::unique_lock<std::shared_mutex> lock(parentDoc().mutex());
183 parentDoc().execCommand(
184 XLCommand(XLCommandType::SetSheetIndex).setParam("sheetID", relationshipID()).setParam("sheetIndex", index));
185 }
186
191 std::string name() const
192 {
193 XLQuery query(XLQueryType::QuerySheetName);
194 query.setParam("sheetID", relationshipID());
195 return parentDoc().execQuery(query).template result<std::string>();
196 }
197
202 void setName(const std::string& sheetName)
203 {
204 std::unique_lock<std::shared_mutex> lock(parentDoc().mutex());
205 parentDoc().execCommand(XLCommand(XLCommandType::SetSheetName)
206 .setParam("sheetID", relationshipID())
207 .setParam("sheetName", name())
208 .setParam("newName", sheetName));
209 }
210
215 bool isSelected() const { return static_cast<const T&>(*this).isSelected_impl(); }
216
221 void setSelected(bool selected) { static_cast<T&>(*this).setSelected_impl(selected); }
222
227 bool isActive() const { return static_cast<const T&>(*this).isActive_impl(); }
228
232 bool setActive() { return static_cast<T&>(*this).setActive_impl(); }
233
240 void clone(const std::string& newName)
241 {
242 parentDoc().execCommand(
243 XLCommand(XLCommandType::CloneSheet).setParam("sheetID", relationshipID()).setParam("cloneName", newName));
244 }
245
246 private: // ---------- Private Member Variables ---------- //
247 };
248} // namespace OpenXLSX
249
250#endif
XLXmlData * xmlData
Definition XLDocument.cpp:1422
Definition XLColor.hpp:22
Definition XLCommandQuery.hpp:43
Definition XLCommandQuery.hpp:106
XLQuery & setParam(const std::string &param, T value)
Definition XLCommandQuery.hpp:122
The XLSheetBase class is the base class for the XLWorksheet and XLChartsheet classes....
Definition XLSheetBase.hpp:54
bool setActive()
Definition XLSheetBase.hpp:232
bool isSelected() const
Definition XLSheetBase.hpp:215
std::string name() const
Method to retrieve the name of the sheet.
Definition XLSheetBase.hpp:191
bool isActive() const
Definition XLSheetBase.hpp:227
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:169
XLSheetBase(const XLSheetBase &other)=default
The copy constructor.
XLColor color() const
Definition XLSheetBase.hpp:156
void setIndex(uint16_t index)
Definition XLSheetBase.hpp:180
void clone(const std::string &newName)
Method for cloning the sheet.
Definition XLSheetBase.hpp:240
void setColor(const XLColor &color)
Definition XLSheetBase.hpp:162
~XLSheetBase()=default
The destructor.
void setVisibility(XLSheetState state)
Definition XLSheetBase.hpp:128
void setName(const std::string &sheetName)
Method for renaming the sheet.
Definition XLSheetBase.hpp:202
XLSheetState visibility() const
Definition XLSheetBase.hpp:106
XLSheetBase & operator=(const XLSheetBase &)=default
Assignment operator.
XLSheetBase & operator=(XLSheetBase &&other) noexcept=default
XLSheetBase()
Constructor.
Definition XLSheetBase.hpp:59
XLSheetBase(XLXmlData *xmlData)
The constructor. There are no default constructor, so all parameters must be provided for constructin...
Definition XLSheetBase.hpp:67
void setSelected(bool selected)
Definition XLSheetBase.hpp:221
XLSheetBase(XLSheetBase &&other) noexcept=default
The XLXmlData class encapsulates the properties and behaviour of the .xml files in an ....
Definition XLXmlData.hpp:29
The XLXmlFile class provides an interface for derived classes to use. It functions as an ancestor to ...
Definition XLXmlFile.hpp:42
Definition IZipArchive.hpp:18
OPENXLSX_EXPORT void setTabSelected(const XMLDocument &xmlDocument, bool selected)
Set the tab selected property to desired value.
Definition XLSheet.cpp:37
XLPaneState
The XLPaneState is an enumeration of the possible states of a pane, e.g. Frozen or Split.
Definition XLSheetBase.hpp:30
constexpr const bool XLEmptyHiddenCells
Definition XLSheetBase.hpp:20
OPENXLSX_EXPORT XLPaneState XLPaneStateFromString(std::string const &stateString)
inverse of XLPaneStateToString
Definition XLSheet.cpp:83
OpenXLSX_xml_document XMLDocument
Definition XLXmlParser.hpp:65
OPENXLSX_EXPORT XLPane XLPaneFromString(std::string const &paneString)
inverse of XLPaneToString
Definition XLSheet.cpp:116
XLPane
The XLPane is an enumeration of the possible pane identifiers.
Definition XLSheetBase.hpp:35
XLSheetState
The XLSheetState is an enumeration of the possible (visibility) states, e.g. Visible or Hidden.
Definition XLSheetBase.hpp:25
OPENXLSX_EXPORT bool tabIsSelected(const XMLDocument &xmlDocument)
Function for checking if the tab is selected.
Definition XLSheet.cpp:51
OPENXLSX_EXPORT std::string XLPaneStateToString(XLPaneState state)
get the correct XLPaneState from the OOXML pane state attribute string
Definition XLSheet.cpp:65
OPENXLSX_EXPORT void setTabColor(const XMLDocument &xmlDocument, const XLColor &color)
Function for setting tab color.
Definition XLSheet.cpp:19
OPENXLSX_EXPORT std::string XLPaneToString(XLPane pane)
get the correct XLPane from the OOXML pane identifier attribute string
Definition XLSheet.cpp:96