11#ifndef OPENXLSX_XLDOCUMENT_HPP
12#define OPENXLSX_XLDOCUMENT_HPP
16# pragma warning(disable : 4251)
17# pragma warning(disable : 4275)
28#include <shared_mutex>
31#include <unordered_map>
35#include "OpenXLSX-Exports.hpp"
118 {
return &m_data.emplace_back(
this,
path,
id, type); }
154 void open(std::string_view fileName);
169 void open(std::string_view fileName,
const std::string& password);
176 void create(std::string_view fileName,
bool forceOverwrite =
XLForceOverwrite);
182 [[deprecated]]
void create(
const std::string& fileName);
193 void addStreamedFile(std::string_view pathInZip, std::string_view tempFilePath);
201 void validatePackageInvariants()
const;
208 void saveAs(std::string_view fileName,
bool forceOverwrite =
XLForceOverwrite);
216 void saveAs(std::string_view fileName,
const std::string& password,
bool forceOverwrite =
XLForceOverwrite);
222 [[deprecated]]
void saveAs(
const std::string& fileName);
228 [[nodiscard]] std::string name()
const;
234 [[nodiscard]]
const std::string& path()
const;
240 [[nodiscard]]
const XLContentTypes& contentTypes()
const override;
250 bool hasMacro()
const;
264 [[nodiscard]] uint32_t nextTableId()
const override;
275 [[nodiscard]] std::string property(
XLProperty prop)
const;
282 void setProperty(
XLProperty prop, std::string_view value);
292 [[nodiscard]]
explicit operator bool()
const;
297 [[nodiscard]]
bool isOpen()
const;
303 void setCompressionLevel(
int level);
308 int compressionLevel()
const;
313 void setDefaultAuthor(
const std::string& author);
318 std::string defaultAuthor()
const;
324 [[nodiscard]]
const XLStyles& styles()
const;
329 void compactStyles();
335 [[nodiscard]]
bool hasSheetRelationships(uint16_t sheetXmlNo,
bool isChartsheet =
false)
const override;
336 [[nodiscard]]
bool hasSheetVmlDrawing(uint16_t sheetXmlNo)
const override;
337 [[nodiscard]]
bool hasSheetComments(uint16_t sheetXmlNo)
const override;
338 [[nodiscard]]
bool hasSheetThreadedComments(uint16_t sheetXmlNo)
const override;
339 [[nodiscard]]
bool hasSheetDrawing(uint16_t sheetXmlNo)
const override;
340 [[nodiscard]]
bool hasSheetTables(uint16_t sheetXmlNo)
const override;
342 XLRelationships sheetRelationships(uint16_t sheetXmlNo,
bool isChartsheet =
false)
override;
343 XLDrawing sheetDrawing(uint16_t sheetXmlNo)
override;
345 XLDrawing drawing(std::string_view path)
override;
346 XLVmlDrawing sheetVmlDrawing(uint16_t sheetXmlNo)
override;
347 XLComments sheetComments(uint16_t sheetXmlNo)
override;
349 XLTables sheetTables(uint16_t sheetXmlNo)
override;
361 void deleteManagedXmlPart(std::string_view path);
364 createTableSlicerCache(uint32_t tableId, uint32_t tableColumnId, std::string_view name, std::string_view sourceName) override;
365 std::string findOrCreateTableSlicerCache(uint32_t tableId,
366 uint32_t tableColumnId,
367 std::string_view name,
368 std::string_view sourceName) override;
370 std::string createPivotSlicerCache(uint32_t pivotCacheId,
372 std::string_view pivotTableName,
373 std::string_view name,
374 std::string_view sourceName) override;
375 std::string findOrCreatePivotSlicerCache(uint32_t pivotCacheId,
377 std::string_view pivotTableName,
378 std::string_view name,
379 std::string_view sourceName) override;
381 std::string createSlicer(std::string_view name,
382 std::string_view cacheName,
383 std::string_view caption,
384 std::string_view existingSlicerFile = {}) override;
386 void deleteSlicerFileAndOrphanCache(const std::string& name) override;
394 std::string addImage(std::string_view name, std::string_view data) override;
395 std::string addImage(std::string_view name, gsl::span<const uint8_t> data) override;
400 [[nodiscard]] std::string getImage(std::string_view path) const override;
402 XLRelationships drawingRelationships(std::string_view drawingPath) override;
421 [[nodiscard]]
XLXmlData* findXmlPart(std::string_view path,
bool doNotThrow =
false)
override;
422 [[nodiscard]]
const XLXmlData* findXmlPart(std::string_view path,
bool doNotThrow =
false)
const override;
442 bool validateSheetName(std::string_view sheetName,
bool throwOnInvalid =
false);
444 bool execCommand(
const XLCommand& command);
470 [[nodiscard]] int32_t stringCount()
const override;
471 [[nodiscard]] int32_t getStringIndex(std::string_view str)
const override;
472 [[nodiscard]]
bool stringExists(std::string_view str)
const override;
473 [[nodiscard]]
const char* getString(int32_t index)
const override;
474 [[nodiscard]] std::string_view getStringView(int32_t index)
const override;
479 bool hasPersons()
const;
490 void cleanupSharedStrings();
502 using XLCellChangeListener = std::function<void(std::string_view sheetName, uint32_t row, uint16_t col,
bool formulaChanged)>;
518 void notifyCellChanged(std::string_view sheetName, uint32_t row, uint16_t col,
bool formulaChanged);
540 std::shared_mutex&
mutex()
const {
return *m_docMutex; }
543 bool m_suppressWarnings{
true};
544 std::string m_filePath{};
545 std::string m_defaultAuthor{
"System Admin"};
547 XLXmlSavingDeclaration m_xmlSavingDeclaration;
549 mutable std::list<XLXmlData> m_data{};
550 mutable XLSharedStringsState m_sharedStringsState{};
551 mutable std::unique_ptr<std::shared_mutex> m_docMutex{std::make_unique<std::shared_mutex>()};
552 mutable XLSharedStrings m_sharedStrings{};
553 mutable std::map<void*, std::unordered_map<uint32_t, SharedFormula>> m_sharedFormulas{};
554 std::unordered_map<const void*, XLCellChangeListener> m_cellChangeListeners{};
555 std::map<std::string, std::string> m_unhandledEntries{};
557 bool m_formulaNeedsRecalculation{
false};
558 bool m_isEncryptedSession{
false};
559 std::string m_encryptionPassword{
""};
560 std::string m_tempDecryptedPath{
""};
562 XLRelationships m_docRelationships{};
563 XLRelationships m_wbkRelationships{};
564 XLContentTypes m_contentTypes{};
565 XLAppProperties m_appProperties{};
566 XLProperties m_coreProperties{};
567 XLCustomProperties m_customProperties{};
569 XLWorkbook m_workbook{};
570 XLPersons m_persons{};
571 IZipArchive m_archive{};
574 [[nodiscard]] OPENXLSX_EXPORT std::string
BinaryAsHexString(gsl::span<const std::byte> data);
575 [[nodiscard]] OPENXLSX_EXPORT uint16_t
ExcelPasswordHash(std::string_view password);
Narrow factory port for creating / resolving OPC package parts used by worksheets.
Narrow package-level ports for XML parts that must touch the OPC package without depending on the ful...
Read-only shared-string table (SST) port.
This class functions as a wrapper around any class that provides the necessary functionality for a zi...
Definition IZipArchive.hpp:38
The XLAppProperties class represents the extended/application properties (docProps/app....
Definition XLProperties.hpp:156
Definition XLChart.hpp:261
Definition XLCommandQuery.hpp:53
The purpose of this class is to load, store add and save item in the [Content_Types]....
Definition XLContentTypes.hpp:112
The XLCustomProperties class represents custom user-defined properties (docProps/custom....
Definition XLProperties.hpp:303
This class encapsulates the concept of an excel file. It is different from the XLWorkbook,...
Definition XLDocument.hpp:97
std::map< void *, std::unordered_map< uint32_t, SharedFormula > > & sharedFormulas(XLInternalAccess) const
Definition XLDocument.hpp:120
const XLSharedStrings & sharedStrings() const
Access the workbook's central Shared String Table (SST). The SST reduces file size by deduplicating t...
Definition XLDocument.hpp:460
bool hasXmlData(std::string_view path) const
Definition XLDocument.cpp:941
const XLXmlData * getXmlData(XLInternalAccess, std::string_view path, bool doNotThrow=false) const
Definition XLDocument.hpp:114
const XLProperties & coreProperties() const
Definition XLDocument.hpp:437
XLDocument(const IZipArchive &zipArchive=XLZipArchive())
Default constructor. Initializes an empty document package with a default ZIP archive.
Definition XLDocument.cpp:45
XLXmlData * getXmlData(XLInternalAccess, std::string_view path, bool doNotThrow=false)
Definition XLDocument.hpp:111
void registerCellChangeListener(const void *token, XLCellChangeListener listener)
Register a cell-change listener. token must be unique (e.g. this of the engine).
Definition XLDocument.cpp:950
const std::string & path() const
Access the full filesystem path of the current document. [[nodiscard]] prevents ignoring the current ...
Definition XLDocument.cpp:806
std::string extractXmlFromArchive(std::string_view path)
Fetch raw XML content for a specific package path. Used internally for lazily loading components on d...
Definition XLDocument.cpp:905
const XLAppProperties & appProperties() const
Definition XLDocument.hpp:430
std::shared_mutex & mutex() const
Get the document-level mutex for thread-safe operations.
Definition XLDocument.hpp:540
IZipArchive & archive() override
Access the low-level ZIP archive for advanced package manipulation. [[nodiscard]] is used to prevent ...
Definition XLDocument.hpp:416
void unregisterCellChangeListener(const void *token)
Remove a previously registered listener.
Definition XLDocument.cpp:957
XLDocument & operator=(XLDocument &&other) noexcept=default
const XLSharedStringTable & sharedStringTable() const
Read-only SST port (index ↔ string lookups only).
Definition XLDocument.hpp:466
std::function< void(std::string_view sheetName, uint32_t row, uint16_t col, bool formulaChanged)> XLCellChangeListener
Listener for cell content changes (value or formula).
Definition XLDocument.hpp:502
XLProperties & coreProperties()
Access core document metadata properties. [[nodiscard]] is used to prevent state-querying errors.
Definition XLDocument.hpp:436
XLAppProperties & appProperties()
Access application-specific properties (metadata). [[nodiscard]] is used to prevent state-querying er...
Definition XLDocument.hpp:429
XLXmlData * addXmlData(XLInternalAccess, const std::string &path, const std::string &id, XLContentType type)
Definition XLDocument.hpp:117
XLRelationships & workbookRelationships() override
Get workbook relationships.
Definition XLDocument.hpp:410
XLDocument(const XLDocument &other)=delete
void open(std::string_view fileName)
Open an existing .xlsx package. Uses std::string_view for zero-copy path processing.
Definition XLDocument.cpp:140
XLDocument & operator=(const XLDocument &other)=delete
size_t maxEntryUncompressedSize() const
Definition XLDocument.hpp:162
void setMaxEntryUncompressedSize(size_t maxBytes)
Cap uncompressed size for archive.getEntry() loads (0 = unlimited).
Definition XLDocument.hpp:161
void setFormulaNeedsRecalculation(bool status=true)
Marks the document to require formula recalculation on load.
Definition XLDocument.hpp:495
void notifyCellChanged(std::string_view sheetName, uint32_t row, uint16_t col, bool formulaChanged)
Notify listeners that a cell changed (value and/or formula).
Definition XLDocument.cpp:964
void suppressWarnings()
Definition XLDocument.cpp:84
XLDocument(XLDocument &&other) noexcept=default
void showWarnings()
Definition XLDocument.cpp:79
const IZipArchive & archive() const override
Definition XLDocument.hpp:417
The XLDrawing class is the base class for worksheet drawings (images, charts, etc....
Definition XLDrawing.hpp:398
Passkey Idiom for internal access control. Only the specified friend classes can instantiate this pas...
Definition XLInternalAccess.hpp:34
Abstract factory for worksheet-facing package parts.
Definition XLPackagePartFactory.hpp:53
Abstract package services port (archive + XML parts + relationships + content types).
Definition XLPackageServices.hpp:56
A class encapsulating modern Excel persons metadata (persons.xml).
Definition XLThreadedComments.hpp:129
Definition XLPivotTable.hpp:421
Definition XLPivotTable.hpp:443
Definition XLPivotTable.hpp:368
The XLProperties class represents the core properties of the document (docProps/core....
Definition XLProperties.hpp:36
Definition XLCommandQuery.hpp:116
Definition XLRelationships.hpp:150
Abstract read-only view of the workbook shared string table.
Definition XLSharedStringTable.hpp:51
This class encapsulate the Excel concept of Shared Strings. In Excel, instead of havig individual str...
Definition XLSharedStrings.hpp:78
An encapsulation of the styles file (xl/styles.xml) in an Excel document package.
Definition XLStyles.hpp:50
The XLVmlDrawing class is the base class for worksheet comments.
Definition XLDrawing.hpp:523
This class encapsulates the concept of a Workbook. It provides access to the individual sheets (works...
Definition XLWorkbook.hpp:97
The XLXmlData class encapsulates the properties and behaviour of the .xml files in an ....
Definition XLXmlData.hpp:79
The XLXmlSavingDeclaration class encapsulates the properties of an XML saving declaration,...
Definition XLXmlSavingDeclaration.hpp:29
Definition XLZipArchive.hpp:34
Definition IXLCellProvider.hpp:24
std::string getPathARelativeToPathB(std::string_view pathA, std::string_view pathB)
Definition XLUtilities.cpp:115
OPENXLSX_EXPORT uint16_t ExcelPasswordHash(std::string_view password)
Definition XLUtilities.cpp:49
XLContentType
Definition XLContentTypes.hpp:38
constexpr const bool XLForceOverwrite
Definition XLDocument.hpp:60
std::string eliminateDotAndDotDotFromPath(std::string_view path)
Definition XLUtilities.cpp:136
XLChartType
Definition XLChart.hpp:24
@ Bar
Definition XLChart.hpp:25
XLProperty
The XLDocumentProperties class is an enumeration of the possible properties (metadata) that can be se...
Definition XLDocument.hpp:67
@ AppVersion
Definition XLDocument.hpp:87
@ HyperlinkBase
Definition XLDocument.hpp:85
@ Company
Definition XLDocument.hpp:82
@ CreationDate
Definition XLDocument.hpp:75
@ LinksUpToDate
Definition XLDocument.hpp:83
@ Category
Definition XLDocument.hpp:77
@ ModificationDate
Definition XLDocument.hpp:76
@ DocSecurity
Definition XLDocument.hpp:79
@ LastModifiedBy
Definition XLDocument.hpp:73
@ HyperlinksChanged
Definition XLDocument.hpp:86
@ ScaleCrop
Definition XLDocument.hpp:80
@ Keywords
Definition XLDocument.hpp:71
@ LastPrinted
Definition XLDocument.hpp:74
@ Manager
Definition XLDocument.hpp:81
@ Description
Definition XLDocument.hpp:72
@ Title
Definition XLDocument.hpp:68
@ Subject
Definition XLDocument.hpp:69
@ Application
Definition XLDocument.hpp:78
@ Creator
Definition XLDocument.hpp:70
@ SharedDoc
Definition XLDocument.hpp:84
constexpr const bool XLDoNotOverwrite
Definition XLDocument.hpp:61
constexpr const unsigned int pugi_parse_settings
Definition XLDocument.hpp:58
XLTableCollection XLTables
Definition XLTables.hpp:379
OPENXLSX_EXPORT std::string ExcelPasswordHashAsString(std::string_view password)
Definition XLUtilities.cpp:66
OPENXLSX_EXPORT std::string BinaryAsHexString(gsl::span< const std::byte > data)
Definition XLUtilities.cpp:34
Definition XLCellIterator.hpp:131