OpenXLSX 1.12.1
Modern C++ Excel Library (Fork of OpenXLSX by Kenneth Troldal Balslev)
Loading...
Searching...
No Matches
XLWorkbook.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_XLWORKBOOK_HPP
12#define OPENXLSX_XLWORKBOOK_HPP
13
14#ifdef _MSC_VER // conditionally enable MSVC specific pragmas to avoid other compilers warning about unknown pragmas
15# pragma warning(push)
16# pragma warning(disable : 4251)
17# pragma warning(disable : 4275)
18#endif // _MSC_VER
19
20// ===== External Includes ===== //
21#include <optional>
22#include <ostream> // std::basic_ostream
23#include <string>
24#include <string_view>
25#include <vector>
26
27// ===== OpenXLSX Includes ===== //
28#include "OpenXLSX-Exports.hpp"
29#include "XLXmlFile.hpp"
30
31namespace OpenXLSX
32{
33 class XLSheet;
34 class XLWorksheet;
35 class XLChartsheet;
36
41
45 class OPENXLSX_EXPORT XLDefinedName
46 {
47 public:
48 XLDefinedName() = default;
49 explicit XLDefinedName(const XMLNode& node);
50
51 std::string name() const;
52 void setName(std::string_view name);
53
54 std::string refersTo() const;
55 void setRefersTo(std::string_view formula);
56
57 std::optional<uint32_t> localSheetId() const;
58 void setLocalSheetId(uint32_t id);
59
60 bool hidden() const;
61 void setHidden(bool hidden);
62
63 std::string comment() const;
64 void setComment(std::string_view comment);
65
66 bool valid() const { return !m_node.empty(); }
67
68 private:
69 XMLNode m_node;
70 };
71
75 class OPENXLSX_EXPORT XLDefinedNames
76 {
77 public:
78 XLDefinedNames() = default;
79 explicit XLDefinedNames(const XMLNode& node);
80
81 XLDefinedName append(std::string_view name, std::string_view formula, std::optional<uint32_t> localSheetId = std::nullopt);
82 void remove(std::string_view name, std::optional<uint32_t> localSheetId = std::nullopt);
83 XLDefinedName get(std::string_view name, std::optional<uint32_t> localSheetId = std::nullopt) const;
84 std::vector<XLDefinedName> all() const;
85 bool exists(std::string_view name, std::optional<uint32_t> localSheetId = std::nullopt) const;
86 size_t count() const;
87
88 private:
89 XMLNode m_node;
90 };
91
96 class OPENXLSX_EXPORT XLWorkbook : public XLXmlFile
97 {
98 friend class XLSheet;
99 friend class XLDocument;
100
101 public: // ---------- Public Member Functions ---------- //
105 XLWorkbook() = default;
106
112 explicit XLWorkbook(XLXmlData* xmlData);
113
118 XLWorkbook(const XLWorkbook& other) = default;
119
124 XLWorkbook(XLWorkbook&& other) = default;
125
130
134 XLWorkbook& operator=(const XLWorkbook& other) = default;
135
139 XLWorkbook& operator=(XLWorkbook&& other) = default;
140
147 XLSheet sheet(uint16_t index);
148
154 XLSheet sheet(std::string_view sheetName);
155
161 XLWorksheet worksheet(std::string_view sheetName);
162
168 XLWorksheet worksheet(uint16_t index);
169
175 XLChartsheet chartsheet(std::string_view sheetName);
176
182 XLChartsheet chartsheet(uint16_t index);
183
189
195 void deleteSheet(std::string_view sheetName);
196
201 void addWorksheet(std::string_view sheetName);
202 void addChartsheet(std::string_view sheetName);
203
209 void cloneSheet(std::string_view existingName, std::string_view newName);
210
217 XLWorksheet cloneSheet(const XLWorksheet& sourceSheet, std::string_view newName);
218
224 void setSheetIndex(std::string_view sheetName, unsigned int index);
225
231 unsigned int indexOfSheet(std::string_view sheetName) const;
232
238 XLSheetType typeOfSheet(std::string_view sheetName) const;
239
245 XLSheetType typeOfSheet(unsigned int index) const;
246
250 unsigned int sheetCount() const;
251
255 unsigned int worksheetCount() const;
256
260 unsigned int chartsheetCount() const;
261
265 std::vector<std::string> sheetNames() const;
266
270 std::vector<std::string> worksheetNames() const;
271
275 std::vector<std::string> chartsheetNames() const;
276
280 bool sheetExists(std::string_view sheetName) const;
281
285 bool worksheetExists(std::string_view sheetName) const;
286
290 bool chartsheetExists(std::string_view sheetName) const;
291
295 void updateSheetReferences(std::string_view oldName, std::string_view newName);
296
300 void deleteNamedRanges();
301
306
311
317 void compactStyles();
318
325 void protect(bool lockStructure, bool lockWindows, std::string_view password = "");
326
330 void unprotect();
331
335 bool isProtected() const;
336
340 void print(std::basic_ostream<char>& ostr) const;
341
342 private: // ---------- Private Member Functions ---------- //
346 uint16_t createInternalSheetID();
347
351 std::string sheetID(std::string_view sheetName);
352
356 std::string sheetName(std::string_view sheetID) const;
357
361 std::string sheetVisibility(std::string_view sheetID) const;
362
366 void prepareSheetMetadata(std::string_view sheetName, uint16_t internalID, std::string_view sheetPath = "");
367
371 void setSheetName(std::string_view sheetRID, std::string_view newName);
372
376 void setSheetVisibility(std::string_view sheetRID, std::string_view state);
377
381 bool sheetIsActive(std::string_view sheetRID) const;
382
386 bool setSheetActive(std::string_view sheetRID);
387
391 bool isVisibleState(std::string_view state) const;
392
396 bool isVisible(XMLNode const& sheetNode) const;
397 };
398} // namespace OpenXLSX
399
400#ifdef _MSC_VER // conditionally enable MSVC specific pragmas to avoid other compilers warning about unknown pragmas
401# pragma warning(pop)
402#endif // _MSC_VER
403
404#endif // OPENXLSX_XLWORKBOOK_HPP
Class representing an Excel chartsheet.
Definition XLChartsheet.hpp:25
A class representing a single defined name in a workbook.
Definition XLWorkbook.hpp:46
bool valid() const
Definition XLWorkbook.hpp:66
void setRefersTo(std::string_view formula)
Definition XLWorkbook.cpp:63
std::string name() const
Definition XLWorkbook.cpp:59
void setLocalSheetId(uint32_t id)
Definition XLWorkbook.cpp:72
std::string refersTo() const
Definition XLWorkbook.cpp:62
std::string comment() const
Definition XLWorkbook.cpp:87
void setComment(std::string_view comment)
Definition XLWorkbook.cpp:88
bool hidden() const
Definition XLWorkbook.cpp:79
std::optional< uint32_t > localSheetId() const
Definition XLWorkbook.cpp:65
void setHidden(bool hidden)
Definition XLWorkbook.cpp:80
void setName(std::string_view name)
Definition XLWorkbook.cpp:60
A class representing the collection of defined names in a workbook.
Definition XLWorkbook.hpp:76
XLDefinedName get(std::string_view name, std::optional< uint32_t > localSheetId=std::nullopt) const
Definition XLWorkbook.cpp:125
XLDefinedName append(std::string_view name, std::string_view formula, std::optional< uint32_t > localSheetId=std::nullopt)
Definition XLWorkbook.cpp:97
size_t count() const
Definition XLWorkbook.cpp:150
bool exists(std::string_view name, std::optional< uint32_t > localSheetId=std::nullopt) const
Definition XLWorkbook.cpp:148
void remove(std::string_view name, std::optional< uint32_t > localSheetId=std::nullopt)
Definition XLWorkbook.cpp:106
std::vector< XLDefinedName > all() const
Definition XLWorkbook.cpp:141
The XLAbstractSheet is a generalized sheet class, which functions as superclass for specialized class...
Definition XLSheet.hpp:40
XLDefinedNames definedNames()
Get the defined names collection.
Definition XLWorkbook.cpp:210
void cloneSheet(std::string_view existingName, std::string_view newName)
Clone an existing sheet.
Definition XLWorkbook.cpp:285
XLWorkbook & operator=(XLWorkbook &&other)=default
Move assignment operator.
void protect(bool lockStructure, bool lockWindows, std::string_view password="")
Protect the workbook.
Definition XLWorkbook.cpp:779
void updateWorksheetDimensions()
Update the dimension (used range) for all worksheets in the workbook.
Definition XLWorkbook.cpp:737
bool chartsheetExists(std::string_view sheetName) const
Check if a chartsheet exists.
Definition XLWorkbook.cpp:706
XLWorkbook & operator=(const XLWorkbook &other)=default
Copy assignment operator.
~XLWorkbook()
Destructor.
XLChartsheet chartsheet(std::string_view sheetName)
Get the chartsheet with the given name.
Definition XLWorkbook.cpp:207
std::vector< std::string > worksheetNames() const
Get a list of all worksheet names.
Definition XLWorkbook.cpp:669
friend class XLSheet
Definition XLWorkbook.hpp:98
bool worksheetExists(std::string_view sheetName) const
Check if a worksheet exists.
Definition XLWorkbook.cpp:700
unsigned int indexOfSheet(std::string_view sheetName) const
Get the index of a sheet.
Definition XLWorkbook.cpp:591
void unprotect()
Unprotect the workbook.
Definition XLWorkbook.cpp:800
bool sheetExists(std::string_view sheetName) const
Check if a sheet exists.
Definition XLWorkbook.cpp:697
unsigned int chartsheetCount() const
Get the total number of chartsheets in the workbook.
Definition XLWorkbook.cpp:647
void print(std::basic_ostream< char > &ostr) const
Print the XML contents of the workbook.xml.
Definition XLWorkbook.cpp:804
bool isProtected() const
Check if the workbook is protected.
Definition XLWorkbook.cpp:802
void compactStyles()
Compact and deduplicate all cell formats across all worksheets in the workbook.
Definition XLWorkbook.cpp:858
void setFullCalculationOnLoad()
Set a flag to force full calculation upon loading the file in Excel.
Definition XLWorkbook.cpp:763
void updateSheetReferences(std::string_view oldName, std::string_view newName)
Update references to a sheet after renaming.
Definition XLWorkbook.cpp:712
unsigned int worksheetCount() const
Get the total number of worksheets in the workbook.
Definition XLWorkbook.cpp:634
XLWorksheet worksheet(std::string_view sheetName)
Get the worksheet with the given name.
Definition XLWorkbook.cpp:205
std::vector< std::string > sheetNames() const
Get a list of all sheet names.
Definition XLWorkbook.cpp:660
XLSheet sheet(uint16_t index)
Get the sheet (worksheet or chartsheet) at the given index.
Definition XLWorkbook.cpp:179
std::vector< std::string > chartsheetNames() const
Get a list of all chartsheet names.
Definition XLWorkbook.cpp:683
void deleteNamedRanges()
Delete all named ranges.
Definition XLWorkbook.cpp:218
friend class XLDocument
Definition XLWorkbook.hpp:99
void deleteSheet(std::string_view sheetName)
Delete sheet (worksheet or chartsheet) from the workbook.
Definition XLWorkbook.cpp:220
unsigned int sheetCount() const
Get the total number of sheets in the workbook.
Definition XLWorkbook.cpp:625
XLWorkbook()=default
Default constructor. Creates an empty ('null') XLWorkbook object.
void addChartsheet(std::string_view sheetName)
Definition XLWorkbook.cpp:272
XLWorkbook(const XLWorkbook &other)=default
Copy Constructor.
XLWorkbook(XLWorkbook &&other)=default
Move constructor.
void setSheetIndex(std::string_view sheetName, unsigned int index)
Move a sheet to a new index.
Definition XLWorkbook.cpp:517
void addWorksheet(std::string_view sheetName)
Add a new worksheet.
Definition XLWorkbook.cpp:258
XLSheetType typeOfSheet(std::string_view sheetName) const
Get the type of a sheet.
Definition XLWorkbook.cpp:604
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 xmlData(XLXmlSavingDeclaration savingDeclaration=XLXmlSavingDeclaration{}) const
Method for getting the XML data represented by the object.
Definition XLXmlFile.cpp:49
XLXmlFile()=default
Default constructor.
Definition XLFormulaUtils.hpp:37
Definition IXLCellProvider.hpp:24
XLSheetType
Enumeration of the different types of sheets.
Definition XLWorkbook.hpp:40
@ Worksheet
Definition XLContentTypes.hpp:42
@ Chartsheet
Definition XLContentTypes.hpp:43
@ Macrosheet
Definition XLRelationships.hpp:76
@ Dialogsheet
Definition XLRelationships.hpp:75
OpenXLSX_xml_node XMLNode
Definition XLXmlParser.hpp:64