OpenXLSX 1.10.0
Loading...
Searching...
No Matches
XLWorksheet.hpp
Go to the documentation of this file.
1#include <optional>
2#include <memory>
3
4#ifndef OPENXLSX_XLWORKSHEET_HPP
5# define OPENXLSX_XLWORKSHEET_HPP
6
7# include "OpenXLSX-Exports.hpp"
8# include "XLConstants.hpp"
9# include "XLSheetBase.hpp"
10# include <string>
11# include <string_view>
12# include <vector>
13
14# include "XLCell.hpp"
15# include "XLCellRange.hpp"
16# include "XLCellReference.hpp"
17# include "XLChart.hpp"
18# include "XLColumn.hpp"
20# include "XLPageSetup.hpp"
21# include "XLRow.hpp"
22# include "XLSparkline.hpp"
23
24namespace OpenXLSX
25{
26 struct XLWorksheetImpl;
27 class XLComments;
28 class XLDataValidations;
29 class XLDrawing;
30 class XLVmlDrawing;
31 class XLMergeCells;
32 class XLPivotTable;
33 class XLPivotTableOptions;
34 class XLSlicer;
35 struct XLSlicerOptions;
36 class XLSlicerCollection;
37 class XLRelationships;
38 class XLStreamReader;
39 class XLStreamWriter;
40 class XLTableCollection;
41 class XLThreadedComments;
42
43 const std::vector<std::string_view> XLWorksheetNodeOrder = { // worksheet XML root node required child sequence
44 "sheetPr",
45 "dimension",
46 "sheetViews",
47 "sheetFormatPr",
48 "cols",
49 "sheetData",
50 "sheetCalcPr",
51 "sheetProtection",
52 "protectedRanges",
53 "scenarios",
54 "autoFilter",
55 "sortState",
56 "dataConsolidate",
57 "customSheetViews",
58 "mergeCells",
59 "phoneticPr",
60 "conditionalFormatting",
61 "dataValidations",
62 "hyperlinks",
63 "printOptions",
64 "pageMargins",
65 "pageSetup",
66 "headerFooter",
67 "rowBreaks",
68 "colBreaks",
69 "customProperties",
70 "cellWatches",
71 "ignoredErrors",
72 "smartTags",
73 "drawing",
74 "legacyDrawing",
75 "legacyDrawingHF",
76 "drawingHF",
77 "picture",
78 "oleObjects",
79 "controls",
80 "webPublishItems",
81 "tableParts",
82 "pivotTables",
83 "extLst"};
84
85 const std::vector<std::string_view> XLSheetViewNodeOrder = { // worksheet XML <sheetView> child sequence
86 "pane",
87 "selection",
88 "pivotSelection",
89 "extLst"};
90
95 struct OPENXLSX_EXPORT XLSheetProtectionOptions {
96 bool sheet = true;
97 bool objects = false;
98 bool scenarios = false;
99
100 bool formatCells = false;
101 bool formatColumns = false;
102 bool formatRows = false;
103 bool insertColumns = false;
104 bool insertRows = false;
105 bool insertHyperlinks = false;
106 bool deleteColumns = false;
107 bool deleteRows = false;
108 bool sort = false;
109 bool autoFilter = false;
110 bool pivotTables = false;
111
112 bool selectLockedCells = true;
113 bool selectUnlockedCells = true;
114 };
115
119 class OPENXLSX_EXPORT XLWorksheet final : public XLSheetBase<XLWorksheet>
120 {
121 friend class XLCell;
122 friend class XLRow;
123 friend class XLWorkbook;
124 friend class XLTableCollection;
125 friend class XLSlicerCollection;
126 friend class XLSheetBase<XLWorksheet>;
127 friend class XLRowDataProxy;
128
129 public:
130 XLWorksheet();
131 explicit XLWorksheet(XLXmlData* xmlData);
133 XLWorksheet(const XLWorksheet& other);
134 XLWorksheet(XLWorksheet&& other) noexcept;
135 XLWorksheet& operator=(const XLWorksheet& other);
136 XLWorksheet& operator=(XLWorksheet&& other);
137
138 XLCellAssignable cell(const std::string& ref) const;
139 XLCellAssignable cell(const XLCellReference& ref) const;
140 XLCellAssignable cell(uint32_t rowNumber, uint16_t columnNumber) const;
141 XLCellAssignable cell(XLRowIndex row, XLColIndex col) const { return cell(row.val, col.val); }
142
147 XLStreamWriter streamWriter();
148
153 XLStreamReader streamReader() const;
154
155 XLCellAssignable findCell(const std::string& ref) const;
156 XLCellAssignable findCell(const XLCellReference& ref) const;
157 XLCellAssignable findCell(uint32_t rowNumber, uint16_t columnNumber) const;
158
159 std::optional<XLCell> peekCell(const std::string& ref) const;
160 std::optional<XLCell> peekCell(const XLCellReference& ref) const;
161 std::optional<XLCell> peekCell(uint32_t rowNumber, uint16_t columnNumber) const;
162 std::optional<XLCell> peekCell(XLRowIndex row, XLColIndex col) const { return peekCell(row.val, col.val); }
163
164 XLCellRange range() const;
165 XLCellRange range(const XLCellReference& topLeft, const XLCellReference& bottomRight) const;
166 XLCellRange range(std::string const& topLeft, std::string const& bottomRight) const;
167 XLCellRange range(std::string const& rangeReference) const;
168
169 XLRowRange rows() const;
170 XLRowRange rows(uint32_t rowCount) const;
171 XLRowRange rows(uint32_t firstRow, uint32_t lastRow) const;
172
173 XLRow row(uint32_t rowNumber) const;
174
179 void appendRow(const std::vector<XLCellValue>& values);
180
186 template<typename T,
187 typename = std::enable_if_t<!std::is_same_v<T, std::vector<XLCellValue>> &&
188 std::is_base_of_v<std::bidirectional_iterator_tag,
189 typename std::iterator_traits<typename T::iterator>::iterator_category>,
190 T>>
191 void appendRow(const T& values)
192 { row(rowCount() + 1).values() = values; }
193
194 XLColumn column(uint16_t columnNumber) const;
195 XLColumn column(std::string const& columnRef) const;
196
200 void autoFitColumn(uint16_t columnNumber);
201
202 void groupRows(uint32_t rowFirst, uint32_t rowLast, uint8_t outlineLevel = 1, bool collapsed = false);
203 void groupColumns(uint16_t colFirst, uint16_t colLast, uint8_t outlineLevel = 1, bool collapsed = false);
204
205 void setAutoFilter(const XLCellRange& range);
206 void clearAutoFilter();
207 bool hasAutoFilter() const;
208 std::string autoFilter() const;
209 XLAutoFilter autofilterObject() const;
210
217 void addSortCondition(const std::string& ref, uint16_t colId, bool descending = false);
218
224 void applyAutoFilter();
225
226 XLCellReference lastCell() const noexcept;
227 uint16_t columnCount() const noexcept;
228 uint32_t rowCount() const noexcept;
229
230 bool deleteRow(uint32_t rowNumber);
231
241 bool insertRow(uint32_t rowNumber, uint32_t count = 1);
242
251 bool deleteRow(uint32_t rowNumber, uint32_t count);
252
259 bool insertColumn(uint16_t colNumber, uint16_t count = 1);
260
267 bool deleteColumn(uint16_t colNumber, uint16_t count = 1);
268 void updateSheetName(const std::string& oldName, const std::string& newName);
269 void updateDimension();
270
271 XLMergeCells& merges();
272 XLDataValidations& dataValidations();
273
274 XLCellRange mergeCells(XLCellRange const& rangeToMerge, bool emptyHiddenCells = false);
275 XLCellRange mergeCells(const std::string& rangeReference, bool emptyHiddenCells = false);
276 void unmergeCells(XLCellRange const& rangeToMerge);
277 void unmergeCells(const std::string& rangeReference);
278
279 XLStyleIndex getColumnFormat(uint16_t column) const;
280 XLStyleIndex getColumnFormat(const std::string& column) const;
281 bool setColumnFormat(uint16_t column, XLStyleIndex cellFormatIndex);
282 bool setColumnFormat(const std::string& column, XLStyleIndex cellFormatIndex);
283
284 XLStyleIndex getRowFormat(uint16_t row) const;
285 bool setRowFormat(uint32_t row, XLStyleIndex cellFormatIndex);
286
287 XLConditionalFormats conditionalFormats() const;
288 void addConditionalFormatting(const std::string& sqref, const XLCfRule& rule);
289 void addConditionalFormatting(const std::string& sqref, const XLCfRule& rule, const XLDxf& dxf);
290 void addConditionalFormatting(const XLCellRange& range, const XLCfRule& rule);
291 void addConditionalFormatting(const XLCellRange& range, const XLCfRule& rule, const XLDxf& dxf);
292
293 void removeConditionalFormatting(const std::string& sqref);
294 void removeConditionalFormatting(const XLCellRange& range);
295 void clearAllConditionalFormatting();
296
297 XLPageMargins pageMargins() const;
298 XLPrintOptions printOptions() const;
299 XLPageSetup pageSetup() const;
300 XLHeaderFooter headerFooter() const;
301
306 void setPrintArea(const std::string& sqref);
307
313 void setPrintTitleRows(uint32_t firstRow, uint32_t lastRow);
314
320 void setPrintTitleCols(uint16_t firstCol, uint16_t lastCol);
321
328 bool protect(const XLSheetProtectionOptions& options, std::string_view password = "");
329
330 bool protectSheet(bool set = true);
331 bool protectObjects(bool set = true);
332 bool protectScenarios(bool set = true);
333
334 bool allowInsertColumns(bool set = true);
335 bool allowInsertRows(bool set = true);
336 bool allowDeleteColumns(bool set = true);
337 bool allowDeleteRows(bool set = true);
338 bool allowFormatCells(bool set = true);
339 bool allowFormatColumns(bool set = true);
340 bool allowFormatRows(bool set = true);
341 bool allowInsertHyperlinks(bool set = true);
342 bool allowSort(bool set = true);
343 bool allowAutoFilter(bool set = true);
344 bool allowPivotTables(bool set = true);
345 bool allowSelectLockedCells(bool set = true);
346 bool allowSelectUnlockedCells(bool set = true);
347
348 bool denyInsertColumns() { return allowInsertColumns(false); }
349 bool denyInsertRows() { return allowInsertRows(false); }
350 bool denyDeleteColumns() { return allowDeleteColumns(false); }
351 bool denyDeleteRows() { return allowDeleteRows(false); }
352 bool denyFormatCells() { return allowFormatCells(false); }
353 bool denyFormatColumns() { return allowFormatColumns(false); }
354 bool denyFormatRows() { return allowFormatRows(false); }
355 bool denyInsertHyperlinks() { return allowInsertHyperlinks(false); }
356 bool denySort() { return allowSort(false); }
357 bool denyAutoFilter() { return allowAutoFilter(false); }
358 bool denyPivotTables() { return allowPivotTables(false); }
359 bool denySelectLockedCells() { return allowSelectLockedCells(false); }
360 bool denySelectUnlockedCells() { return allowSelectUnlockedCells(false); }
361
362 bool setPasswordHash(std::string hash);
363 bool setPassword(std::string password);
364 bool clearPassword();
365 bool clearSheetProtection();
366
367 bool sheetProtected() const;
368 bool objectsProtected() const;
369 bool scenariosProtected() const;
370
371 bool insertColumnsAllowed() const;
372 bool insertRowsAllowed() const;
373 bool deleteColumnsAllowed() const;
374 bool deleteRowsAllowed() const;
375 bool formatCellsAllowed() const;
376 bool formatColumnsAllowed() const;
377 bool formatRowsAllowed() const;
378 bool insertHyperlinksAllowed() const;
379 bool sortAllowed() const;
380 bool autoFilterAllowed() const;
381 bool pivotTablesAllowed() const;
382 bool selectLockedCellsAllowed() const;
383 bool selectUnlockedCellsAllowed() const;
384
385 std::string passwordHash() const;
386 bool passwordIsSet() const;
387 std::string sheetProtectionSummary() const;
388
395 void addSparkline(const std::string& location, const std::string& dataRange, XLSparklineType type = XLSparklineType::Line);
396
403 void addSparkline(const std::string& location, const std::string& dataRange, const XLSparklineOptions& options);
404
405 bool hasRelationships() const;
406 bool hasDrawing() const;
407 bool hasVmlDrawing() const;
408 bool hasComments() const;
409 bool hasThreadedComments() const;
410 bool hasTables() const;
411
412 XLDrawing& drawing();
413 void addImage(const std::string& name,
414 const std::string& data,
415 uint32_t row,
416 uint32_t col,
417 uint32_t width,
418 uint32_t height,
419 const XLImageOptions& options = XLImageOptions());
420
427 void addShape(std::string_view cellReference, const XLVectorShapeOptions& options);
428
437 void addTableSlicer(std::string_view cellReference,
438 const XLTable& table,
439 std::string_view columnName,
440 const XLSlicerOptions& options = XLSlicerOptions());
441
450 void addPivotSlicer(std::string_view cellReference,
451 const XLPivotTable& pivotTable,
452 std::string_view columnName,
453 const XLSlicerOptions& options = XLSlicerOptions());
454
475 XLSlicerCollection& slicers();
476
481 void deleteSlicer(const std::string& name);
482
483 void insertImage(const std::string& cellReference, const std::string& imagePath);
484
491 void insertImage(const std::string& cellReference, const std::string& imagePath, const XLImageOptions& options);
492 void addScaledImage(const std::string& name, const std::string& data, uint32_t row, uint32_t col, double scalingFactor = 1.0);
493 std::vector<XLDrawingItem> images();
494
495 XLChart addChart(XLChartType type, std::string_view name, uint32_t row, uint32_t col, uint32_t width, uint32_t height);
496 XLChart addChart(XLChartType type, const XLChartAnchor& anchor);
497
502 std::vector<XLPivotTable> pivotTables();
503
509 XLPivotTable addPivotTable(const XLPivotTableOptions& options);
510
516 bool deletePivotTable(std::string_view name);
517
518 XLVmlDrawing& vmlDrawing();
519 XLComments& comments();
520 XLThreadedComments& threadedComments();
527 void addNote(std::string_view cellRef, std::string_view text, std::string_view author = "");
528
533 void deleteComment(std::string_view cellRef);
534
539 void deleteNote(std::string_view cellRef);
540
547 XLThreadedComment addComment(std::string_view cellRef, std::string_view text, std::string_view author = "");
548
555 XLThreadedComment addReply(const std::string& parentId, const std::string& text, const std::string& author = "");
556
557 XLTableCollection& tables();
558
559 void addHyperlink(std::string_view cellRef, std::string_view url, std::string_view tooltip = "");
560 void addInternalHyperlink(std::string_view cellRef, std::string_view location, std::string_view tooltip = "");
561 [[nodiscard]] bool hasHyperlink(std::string_view cellRef) const;
562 [[nodiscard]] std::string getHyperlink(std::string_view cellRef) const;
563 void removeHyperlink(std::string_view cellRef);
564
565 [[nodiscard]] bool hasPanes() const;
566 void freezePanes(uint16_t column, uint32_t row);
567
573 void freezePanes(const std::string& topLeftCell);
574
575 void splitPanes(double xSplit, double ySplit, std::string_view topLeftCell = "", XLPane activePane = XLPane::BottomRight);
576 void clearPanes();
577
578 void setZoom(uint16_t scale);
579 [[nodiscard]] uint16_t zoom() const;
580
581 void insertRowBreak(uint32_t row);
582 void insertColBreak(uint16_t col);
583 void removeRowBreak(uint32_t row);
584 void removeColBreak(uint16_t col);
585
586 void setSheetViewMode(std::string_view mode);
587 [[nodiscard]] std::string sheetViewMode() const;
588
589 void setShowGridLines(bool show);
590 [[nodiscard]] bool showGridLines() const;
591
592 void setShowRowColHeaders(bool show);
593 [[nodiscard]] bool showRowColHeaders() const;
594
595 void fitToPages(uint32_t fitToWidth, uint32_t fitToHeight);
596
597 [[nodiscard]] static std::string makeInternalLocation(std::string_view sheetName, std::string_view cellRef);
598
599 private:
600 uint16_t sheetXmlNumber() const;
601 XLRelationships& relationships();
602
603 XLColor getColor_impl() const;
604 void setColor_impl(const XLColor& color);
605 bool isSelected_impl() const;
606 void setSelected_impl(bool selected);
607 bool isActive_impl() const;
608 bool setActive_impl();
609
610 XMLNode prepareSheetViewForPanes();
611
612 // ── Row/Column structural shift helpers ──────────────────────────────
613 // Each helper updates one subsystem for a row or column shift.
614 // rowDelta / colDelta > 0 means insert (push out); < 0 means delete (pull in).
615 // fromRow / fromCol is the 1-based first affected coordinate.
616
619 [[nodiscard]] static std::string
620 shiftCellRef(std::string_view ref, int32_t rowDelta, int32_t colDelta, uint32_t fromRow, uint16_t fromCol);
621
623 [[nodiscard]] static std::string
624 shiftFormulaRefs(std::string_view formula, int32_t rowDelta, int32_t colDelta, uint32_t fromRow, uint16_t fromCol);
625
627 void shiftSheetDataRows(int32_t delta, uint32_t fromRow);
628
630 void shiftSheetDataCols(int32_t delta, uint16_t fromCol);
631
633 void shiftColsNode(int32_t delta, uint16_t fromCol);
634
636 void shiftFormulas(int32_t rowDelta, int32_t colDelta, uint32_t fromRow, uint16_t fromCol);
637
639 void shiftDrawingAnchors(int32_t rowDelta, int32_t colDelta, uint32_t fromRow, uint16_t fromCol);
640
642 void shiftDataValidations(int32_t rowDelta, int32_t colDelta, uint32_t fromRow, uint16_t fromCol);
643
645 void shiftAutoFilter(int32_t rowDelta, int32_t colDelta, uint32_t fromRow, uint16_t fromCol);
646
647 private:
648 std::unique_ptr<XLWorksheetImpl> m_impl;
649 inline static const std::vector<std::string_view>& m_nodeOrder = XLWorksheetNodeOrder;
650
651 // O(1) Hint Cache for cell node DOM traversal
652 mutable uint32_t m_hintRowNumber{0};
653 mutable XMLNode m_hintRowNode{};
654 mutable uint16_t m_hintColNumber{0};
655 mutable XMLNode m_hintCellNode{};
656
657 // O(1) Dimension Cache
658 mutable uint16_t m_maxColumn{0};
659 mutable bool m_dimensionDirty{true};
660 };
661} // namespace OpenXLSX
662
663#endif
uint32_t firstRow
Definition XLDataValidation.cpp:230
uint16_t lastCol
Definition XLDataValidation.cpp:233
uint32_t lastRow
Definition XLDataValidation.cpp:231
uint16_t firstCol
Definition XLDataValidation.cpp:232
Definition XLXmlParser.hpp:84
Definition XLAutoFilter.hpp:90
Definition XLCell.hpp:224
Represents a rectangular area of cells within a worksheet.
Definition XLCellRange.hpp:30
Definition XLCellReference.hpp:34
An implementation class encapsulating the properties and behaviours of a spreadsheet cell.
Definition XLCell.hpp:41
Definition XLConditionalFormatting.hpp:267
Definition XLChart.hpp:251
Definition XLColor.hpp:22
Definition XLColumn.hpp:26
The XLComments class is the base class for worksheet comments.
Definition XLComments.hpp:88
Definition XLConditionalFormatting.hpp:397
Definition XLDataValidation.hpp:174
The XLDrawing class is the base class for worksheet drawings (images, charts, etc....
Definition XLDrawing.hpp:383
An encapsulation of a differential cell format item (DXF)
Definition XLStyles.hpp:2063
A class representing the page setup of a worksheet.
Definition XLPageSetup.hpp:85
Manages merged cell ranges in a worksheet.
Definition XLMergeCells.hpp:38
A class representing the page margins of a worksheet.
Definition XLPageSetup.hpp:20
Definition XLPageSetup.hpp:125
Definition XLPivotTable.hpp:24
Definition XLPivotTable.hpp:139
A class representing the print options of a worksheet.
Definition XLPageSetup.hpp:57
Definition XLRelationships.hpp:140
The XLRowDataProxy is used as a proxy object when getting or setting row data. The class facilitates ...
Definition XLRowData.hpp:231
Definition XLRow.hpp:395
The XLRow class represent a row in an Excel spreadsheet. Using XLRow objects, various row formatting ...
Definition XLRow.hpp:23
The XLSheetBase class is the base class for the XLWorksheet and XLChartsheet classes....
Definition XLSheetBase.hpp:54
Definition XLSlicerCollection.hpp:104
Definition XLStreamReader.hpp:16
Definition XLStreamWriter.hpp:45
The XLTableCollection class manages multiple tables within a worksheet.
Definition XLTables.hpp:42
The XLTable class represents a single Excel table (.xml file).
Definition XLTables.hpp:107
A proxy class encapsulating a single modern threaded comment.
Definition XLThreadedComments.hpp:17
A class encapsulating modern Excel threaded comments (ThreadedComments.xml)
Definition XLThreadedComments.hpp:56
The XLVmlDrawing class is the base class for worksheet comments.
Definition XLDrawing.hpp:508
This class encapsulates the concept of a Workbook. It provides access to the individual sheets (works...
Definition XLWorkbook.hpp:87
A class encapsulating an Excel worksheet. Access to XLWorksheet objects should be via the workbook ob...
Definition XLWorksheet.hpp:120
bool denyFormatCells()
Definition XLWorksheet.hpp:352
bool denySelectLockedCells()
Definition XLWorksheet.hpp:359
std::optional< XLCell > peekCell(XLRowIndex row, XLColIndex col) const
Definition XLWorksheet.hpp:162
bool denyFormatColumns()
Definition XLWorksheet.hpp:353
void appendRow(const T &values)
Append a new row with the given container of values.
Definition XLWorksheet.hpp:191
bool denyDeleteColumns()
Definition XLWorksheet.hpp:350
XLCellAssignable cell(XLRowIndex row, XLColIndex col) const
Definition XLWorksheet.hpp:141
bool denySort()
Definition XLWorksheet.hpp:356
bool denyAutoFilter()
Definition XLWorksheet.hpp:357
bool denyInsertRows()
Definition XLWorksheet.hpp:349
bool denyPivotTables()
Definition XLWorksheet.hpp:358
bool denySelectUnlockedCells()
Definition XLWorksheet.hpp:360
bool denyInsertHyperlinks()
Definition XLWorksheet.hpp:355
bool denyFormatRows()
Definition XLWorksheet.hpp:354
bool denyDeleteRows()
Definition XLWorksheet.hpp:351
The XLXmlData class encapsulates the properties and behaviour of the .xml files in an ....
Definition XLXmlData.hpp:68
Definition IZipArchive.hpp:18
XLSparklineType
The XLSparklineType enum represents the type of a sparkline.
Definition XLSparkline.hpp:13
const std::vector< std::string_view > XLWorksheetNodeOrder
Definition XLWorksheet.hpp:43
OpenXLSX_xml_node XMLNode
Definition XLXmlParser.hpp:63
size_t XLStyleIndex
Definition XLStyles.hpp:31
XLPane
The XLPane is an enumeration of the possible pane identifiers.
Definition XLSheetBase.hpp:35
const std::vector< std::string_view > XLSheetViewNodeOrder
Definition XLWorksheet.hpp:85
XLChartType
Definition XLChart.hpp:14
Definition XLCellIterator.hpp:121
Definition XLChart.hpp:166
Definition XLConstants.hpp:19
uint16_t val
Definition XLConstants.hpp:20
Options for inserting an image into a worksheet.
Definition XLImageOptions.hpp:23
Definition XLConstants.hpp:12
uint32_t val
Definition XLConstants.hpp:13
A structure defining all granular sheet protection options.
Definition XLWorksheet.hpp:95
Definition XLTables.hpp:25
The XLSparklineOptions struct encapsulates configuration for a sparkline group.
Definition XLSparkline.hpp:19
Definition XLDrawing.hpp:313