OpenXLSX 1.9.1
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 struct XLSlicerOptions;
35 class XLRelationships;
36 class XLStreamReader;
37 class XLStreamWriter;
38 class XLTableCollection;
39 class XLThreadedComments;
40
41 const std::vector<std::string_view> XLWorksheetNodeOrder = { // worksheet XML root node required child sequence
42 "sheetPr",
43 "dimension",
44 "sheetViews",
45 "sheetFormatPr",
46 "cols",
47 "sheetData",
48 "sheetCalcPr",
49 "sheetProtection",
50 "protectedRanges",
51 "scenarios",
52 "autoFilter",
53 "sortState",
54 "dataConsolidate",
55 "customSheetViews",
56 "mergeCells",
57 "phoneticPr",
58 "conditionalFormatting",
59 "dataValidations",
60 "hyperlinks",
61 "printOptions",
62 "pageMargins",
63 "pageSetup",
64 "headerFooter",
65 "rowBreaks",
66 "colBreaks",
67 "customProperties",
68 "cellWatches",
69 "ignoredErrors",
70 "smartTags",
71 "drawing",
72 "legacyDrawing",
73 "legacyDrawingHF",
74 "drawingHF",
75 "picture",
76 "oleObjects",
77 "controls",
78 "webPublishItems",
79 "tableParts",
80 "pivotTables",
81 "extLst"};
82
83 const std::vector<std::string_view> XLSheetViewNodeOrder = { // worksheet XML <sheetView> child sequence
84 "pane",
85 "selection",
86 "pivotSelection",
87 "extLst"};
88
93 struct OPENXLSX_EXPORT XLSheetProtectionOptions {
94 bool sheet = true;
95 bool objects = false;
96 bool scenarios = false;
97
98 bool formatCells = false;
99 bool formatColumns = false;
100 bool formatRows = false;
101 bool insertColumns = false;
102 bool insertRows = false;
103 bool insertHyperlinks = false;
104 bool deleteColumns = false;
105 bool deleteRows = false;
106 bool sort = false;
107 bool autoFilter = false;
108 bool pivotTables = false;
109
110 bool selectLockedCells = true;
111 bool selectUnlockedCells = true;
112 };
113
117 class OPENXLSX_EXPORT XLWorksheet final : public XLSheetBase<XLWorksheet>
118 {
119 friend class XLCell;
120 friend class XLRow;
121 friend class XLWorkbook;
122 friend class XLTableCollection;
123 friend class XLSheetBase<XLWorksheet>;
124
125 public:
126 XLWorksheet();
127 explicit XLWorksheet(XLXmlData* xmlData);
129 XLWorksheet(const XLWorksheet& other);
130 XLWorksheet(XLWorksheet&& other) noexcept;
131 XLWorksheet& operator=(const XLWorksheet& other);
132 XLWorksheet& operator=(XLWorksheet&& other);
133
134 XLCellAssignable cell(const std::string& ref) const;
135 XLCellAssignable cell(const XLCellReference& ref) const;
136 XLCellAssignable cell(uint32_t rowNumber, uint16_t columnNumber) const;
137 XLCellAssignable cell(XLRowIndex row, XLColIndex col) const { return cell(row.val, col.val); }
138
143 XLStreamWriter streamWriter();
144
149 XLStreamReader streamReader() const;
150
151 XLCellAssignable findCell(const std::string& ref) const;
152 XLCellAssignable findCell(const XLCellReference& ref) const;
153 XLCellAssignable findCell(uint32_t rowNumber, uint16_t columnNumber) const;
154
155 std::optional<XLCell> peekCell(const std::string& ref) const;
156 std::optional<XLCell> peekCell(const XLCellReference& ref) const;
157 std::optional<XLCell> peekCell(uint32_t rowNumber, uint16_t columnNumber) const;
158 std::optional<XLCell> peekCell(XLRowIndex row, XLColIndex col) const { return peekCell(row.val, col.val); }
159
160 XLCellRange range() const;
161 XLCellRange range(const XLCellReference& topLeft, const XLCellReference& bottomRight) const;
162 XLCellRange range(std::string const& topLeft, std::string const& bottomRight) const;
163 XLCellRange range(std::string const& rangeReference) const;
164
165 XLRowRange rows() const;
166 XLRowRange rows(uint32_t rowCount) const;
167 XLRowRange rows(uint32_t firstRow, uint32_t lastRow) const;
168
169 XLRow row(uint32_t rowNumber) const;
170
175 void appendRow(const std::vector<XLCellValue>& values);
176
182 template<typename T,
183 typename = std::enable_if_t<!std::is_same_v<T, std::vector<XLCellValue>> &&
184 std::is_base_of_v<std::bidirectional_iterator_tag,
185 typename std::iterator_traits<typename T::iterator>::iterator_category>,
186 T>>
187 void appendRow(const T& values)
188 { row(rowCount() + 1).values() = values; }
189
190 XLColumn column(uint16_t columnNumber) const;
191 XLColumn column(std::string const& columnRef) const;
192
196 void autoFitColumn(uint16_t columnNumber);
197
198 void groupRows(uint32_t rowFirst, uint32_t rowLast, uint8_t outlineLevel = 1, bool collapsed = false);
199 void groupColumns(uint16_t colFirst, uint16_t colLast, uint8_t outlineLevel = 1, bool collapsed = false);
200
201 void setAutoFilter(const XLCellRange& range);
202 void clearAutoFilter();
203 bool hasAutoFilter() const;
204 std::string autoFilter() const;
205 XLAutoFilter autofilterObject() const;
206
213 void addSortCondition(const std::string& ref, uint16_t colId, bool descending = false);
214
220 void applyAutoFilter();
221
222 XLCellReference lastCell() const noexcept;
223 uint16_t columnCount() const noexcept;
224 uint32_t rowCount() const noexcept;
225
226 bool deleteRow(uint32_t rowNumber);
227
237 bool insertRow(uint32_t rowNumber, uint32_t count = 1);
238
247 bool deleteRow(uint32_t rowNumber, uint32_t count);
248
255 bool insertColumn(uint16_t colNumber, uint16_t count = 1);
256
263 bool deleteColumn(uint16_t colNumber, uint16_t count = 1);
264 void updateSheetName(const std::string& oldName, const std::string& newName);
265 void updateDimension();
266
267 XLMergeCells& merges();
268 XLDataValidations& dataValidations();
269
270 XLCellRange mergeCells(XLCellRange const& rangeToMerge, bool emptyHiddenCells = false);
271 XLCellRange mergeCells(const std::string& rangeReference, bool emptyHiddenCells = false);
272 void unmergeCells(XLCellRange const& rangeToMerge);
273 void unmergeCells(const std::string& rangeReference);
274
275 XLStyleIndex getColumnFormat(uint16_t column) const;
276 XLStyleIndex getColumnFormat(const std::string& column) const;
277 bool setColumnFormat(uint16_t column, XLStyleIndex cellFormatIndex);
278 bool setColumnFormat(const std::string& column, XLStyleIndex cellFormatIndex);
279
280 XLStyleIndex getRowFormat(uint16_t row) const;
281 bool setRowFormat(uint32_t row, XLStyleIndex cellFormatIndex);
282
283 XLConditionalFormats conditionalFormats() const;
284 void addConditionalFormatting(const std::string& sqref, const XLCfRule& rule);
285 void addConditionalFormatting(const std::string& sqref, const XLCfRule& rule, const XLDxf& dxf);
286 void addConditionalFormatting(const XLCellRange& range, const XLCfRule& rule);
287 void addConditionalFormatting(const XLCellRange& range, const XLCfRule& rule, const XLDxf& dxf);
288
289 void removeConditionalFormatting(const std::string& sqref);
290 void removeConditionalFormatting(const XLCellRange& range);
291 void clearAllConditionalFormatting();
292
293 XLPageMargins pageMargins() const;
294 XLPrintOptions printOptions() const;
295 XLPageSetup pageSetup() const;
296 XLHeaderFooter headerFooter() const;
297
302 void setPrintArea(const std::string& sqref);
303
309 void setPrintTitleRows(uint32_t firstRow, uint32_t lastRow);
310
316 void setPrintTitleCols(uint16_t firstCol, uint16_t lastCol);
317
324 bool protect(const XLSheetProtectionOptions& options, std::string_view password = "");
325
326 bool protectSheet(bool set = true);
327 bool protectObjects(bool set = true);
328 bool protectScenarios(bool set = true);
329
330 bool allowInsertColumns(bool set = true);
331 bool allowInsertRows(bool set = true);
332 bool allowDeleteColumns(bool set = true);
333 bool allowDeleteRows(bool set = true);
334 bool allowFormatCells(bool set = true);
335 bool allowFormatColumns(bool set = true);
336 bool allowFormatRows(bool set = true);
337 bool allowInsertHyperlinks(bool set = true);
338 bool allowSort(bool set = true);
339 bool allowAutoFilter(bool set = true);
340 bool allowPivotTables(bool set = true);
341 bool allowSelectLockedCells(bool set = true);
342 bool allowSelectUnlockedCells(bool set = true);
343
344 bool denyInsertColumns() { return allowInsertColumns(false); }
345 bool denyInsertRows() { return allowInsertRows(false); }
346 bool denyDeleteColumns() { return allowDeleteColumns(false); }
347 bool denyDeleteRows() { return allowDeleteRows(false); }
348 bool denyFormatCells() { return allowFormatCells(false); }
349 bool denyFormatColumns() { return allowFormatColumns(false); }
350 bool denyFormatRows() { return allowFormatRows(false); }
351 bool denyInsertHyperlinks() { return allowInsertHyperlinks(false); }
352 bool denySort() { return allowSort(false); }
353 bool denyAutoFilter() { return allowAutoFilter(false); }
354 bool denyPivotTables() { return allowPivotTables(false); }
355 bool denySelectLockedCells() { return allowSelectLockedCells(false); }
356 bool denySelectUnlockedCells() { return allowSelectUnlockedCells(false); }
357
358 bool setPasswordHash(std::string hash);
359 bool setPassword(std::string password);
360 bool clearPassword();
361 bool clearSheetProtection();
362
363 bool sheetProtected() const;
364 bool objectsProtected() const;
365 bool scenariosProtected() const;
366
367 bool insertColumnsAllowed() const;
368 bool insertRowsAllowed() const;
369 bool deleteColumnsAllowed() const;
370 bool deleteRowsAllowed() const;
371 bool formatCellsAllowed() const;
372 bool formatColumnsAllowed() const;
373 bool formatRowsAllowed() const;
374 bool insertHyperlinksAllowed() const;
375 bool sortAllowed() const;
376 bool autoFilterAllowed() const;
377 bool pivotTablesAllowed() const;
378 bool selectLockedCellsAllowed() const;
379 bool selectUnlockedCellsAllowed() const;
380
381 std::string passwordHash() const;
382 bool passwordIsSet() const;
383 std::string sheetProtectionSummary() const;
384
391 void addSparkline(const std::string& location, const std::string& dataRange, XLSparklineType type = XLSparklineType::Line);
392
399 void addSparkline(const std::string& location, const std::string& dataRange, const XLSparklineOptions& options);
400
401 bool hasRelationships() const;
402 bool hasDrawing() const;
403 bool hasVmlDrawing() const;
404 bool hasComments() const;
405 bool hasThreadedComments() const;
406 bool hasTables() const;
407
408 XLDrawing& drawing();
409 void addImage(const std::string& name,
410 const std::string& data,
411 uint32_t row,
412 uint32_t col,
413 uint32_t width,
414 uint32_t height,
415 const XLImageOptions& options = XLImageOptions());
416
423 void addShape(std::string_view cellReference, const XLVectorShapeOptions& options);
424
432 void addTableSlicer(std::string_view cellReference,
433 const XLTable& table,
434 std::string_view columnName,
435 const XLSlicerOptions& options = XLSlicerOptions());
436
444 void addPivotSlicer(std::string_view cellReference,
445 const XLPivotTable& pivotTable,
446 std::string_view columnName,
447 const XLSlicerOptions& options = XLSlicerOptions());
448
449 void insertImage(const std::string& cellReference, const std::string& imagePath);
450
457 void insertImage(const std::string& cellReference, const std::string& imagePath, const XLImageOptions& options);
458 void addScaledImage(const std::string& name, const std::string& data, uint32_t row, uint32_t col, double scalingFactor = 1.0);
459 std::vector<XLDrawingItem> images();
460
461 XLChart addChart(XLChartType type, std::string_view name, uint32_t row, uint32_t col, uint32_t width, uint32_t height);
462 XLChart addChart(XLChartType type, const XLChartAnchor& anchor);
463
468 std::vector<XLPivotTable> pivotTables();
469
475 XLPivotTable addPivotTable(const XLPivotTableOptions& options);
476
482 bool deletePivotTable(std::string_view name);
483
484 XLVmlDrawing& vmlDrawing();
485 XLComments& comments();
486 XLThreadedComments& threadedComments();
493 void addNote(std::string_view cellRef, std::string_view text, std::string_view author = "");
494
499 void deleteComment(std::string_view cellRef);
500
505 void deleteNote(std::string_view cellRef);
506
513 XLThreadedComment addComment(std::string_view cellRef, std::string_view text, std::string_view author = "");
514
521 XLThreadedComment addReply(const std::string& parentId, const std::string& text, const std::string& author = "");
522
523 XLTableCollection& tables();
524
525 void addHyperlink(std::string_view cellRef, std::string_view url, std::string_view tooltip = "");
526 void addInternalHyperlink(std::string_view cellRef, std::string_view location, std::string_view tooltip = "");
527 [[nodiscard]] bool hasHyperlink(std::string_view cellRef) const;
528 [[nodiscard]] std::string getHyperlink(std::string_view cellRef) const;
529 void removeHyperlink(std::string_view cellRef);
530
531 [[nodiscard]] bool hasPanes() const;
532 void freezePanes(uint16_t column, uint32_t row);
533
539 void freezePanes(const std::string& topLeftCell);
540
541 void splitPanes(double xSplit, double ySplit, std::string_view topLeftCell = "", XLPane activePane = XLPane::BottomRight);
542 void clearPanes();
543
544 void setZoom(uint16_t scale);
545 [[nodiscard]] uint16_t zoom() const;
546
547 void insertRowBreak(uint32_t row);
548 void insertColBreak(uint16_t col);
549 void removeRowBreak(uint32_t row);
550 void removeColBreak(uint16_t col);
551
552 void setSheetViewMode(std::string_view mode);
553 [[nodiscard]] std::string sheetViewMode() const;
554
555 void setShowGridLines(bool show);
556 [[nodiscard]] bool showGridLines() const;
557
558 void setShowRowColHeaders(bool show);
559 [[nodiscard]] bool showRowColHeaders() const;
560
561 void fitToPages(uint32_t fitToWidth, uint32_t fitToHeight);
562
563 [[nodiscard]] static std::string makeInternalLocation(std::string_view sheetName, std::string_view cellRef);
564
565 private:
566 uint16_t sheetXmlNumber() const;
567 XLRelationships& relationships();
568
569 XLColor getColor_impl() const;
570 void setColor_impl(const XLColor& color);
571 bool isSelected_impl() const;
572 void setSelected_impl(bool selected);
573 bool isActive_impl() const;
574 bool setActive_impl();
575
576 XMLNode prepareSheetViewForPanes();
577
578 // ── Row/Column structural shift helpers ──────────────────────────────
579 // Each helper updates one subsystem for a row or column shift.
580 // rowDelta / colDelta > 0 means insert (push out); < 0 means delete (pull in).
581 // fromRow / fromCol is the 1-based first affected coordinate.
582
585 [[nodiscard]] static std::string
586 shiftCellRef(std::string_view ref, int32_t rowDelta, int32_t colDelta, uint32_t fromRow, uint16_t fromCol);
587
589 [[nodiscard]] static std::string
590 shiftFormulaRefs(std::string_view formula, int32_t rowDelta, int32_t colDelta, uint32_t fromRow, uint16_t fromCol);
591
593 void shiftSheetDataRows(int32_t delta, uint32_t fromRow);
594
596 void shiftSheetDataCols(int32_t delta, uint16_t fromCol);
597
599 void shiftColsNode(int32_t delta, uint16_t fromCol);
600
602 void shiftFormulas(int32_t rowDelta, int32_t colDelta, uint32_t fromRow, uint16_t fromCol);
603
605 void shiftDrawingAnchors(int32_t rowDelta, int32_t colDelta, uint32_t fromRow, uint16_t fromCol);
606
608 void shiftDataValidations(int32_t rowDelta, int32_t colDelta, uint32_t fromRow, uint16_t fromCol);
609
611 void shiftAutoFilter(int32_t rowDelta, int32_t colDelta, uint32_t fromRow, uint16_t fromCol);
612
613 private:
614 std::unique_ptr<XLWorksheetImpl> m_impl;
615 inline static const std::vector<std::string_view>& m_nodeOrder = XLWorksheetNodeOrder;
616
617 // O(1) Hint Cache for cell node DOM traversal
618 mutable uint32_t m_hintRowNumber{0};
619 mutable XMLNode m_hintRowNode{};
620 mutable uint16_t m_hintColNumber{0};
621 mutable XMLNode m_hintCellNode{};
622 };
623} // namespace OpenXLSX
624
625#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
XLXmlData * xmlData
Definition XLDocument.cpp:1422
return XLRelationships(xmlData, relsFilename)
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:223
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:2059
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:23
Definition XLPivotTable.hpp:126
A class representing the print options of a worksheet.
Definition XLPageSetup.hpp:57
Definition XLRelationships.hpp:140
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 XLStreamReader.hpp:16
Definition XLStreamWriter.hpp:45
The XLTableCollection class manages multiple tables within a worksheet.
Definition XLTables.hpp:40
The XLTable class represents a single Excel table (.xml file).
Definition XLTables.hpp:105
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:118
bool denyFormatCells()
Definition XLWorksheet.hpp:348
bool denySelectLockedCells()
Definition XLWorksheet.hpp:355
std::optional< XLCell > peekCell(XLRowIndex row, XLColIndex col) const
Definition XLWorksheet.hpp:158
bool denyFormatColumns()
Definition XLWorksheet.hpp:349
void appendRow(const T &values)
Append a new row with the given container of values.
Definition XLWorksheet.hpp:187
bool denyDeleteColumns()
Definition XLWorksheet.hpp:346
XLCellAssignable cell(XLRowIndex row, XLColIndex col) const
Definition XLWorksheet.hpp:137
bool denySort()
Definition XLWorksheet.hpp:352
bool denyAutoFilter()
Definition XLWorksheet.hpp:353
bool denyInsertRows()
Definition XLWorksheet.hpp:345
bool denyPivotTables()
Definition XLWorksheet.hpp:354
bool denySelectUnlockedCells()
Definition XLWorksheet.hpp:356
bool denyInsertHyperlinks()
Definition XLWorksheet.hpp:351
bool denyFormatRows()
Definition XLWorksheet.hpp:350
bool denyDeleteRows()
Definition XLWorksheet.hpp:347
The XLXmlData class encapsulates the properties and behaviour of the .xml files in an ....
Definition XLXmlData.hpp:29
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:41
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:83
XLChartType
Definition XLChart.hpp:14
Definition XLCellIterator.hpp:121
Definition XLChart.hpp:140
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:93
Definition XLTables.hpp:25
The XLSparklineOptions struct encapsulates configuration for a sparkline group.
Definition XLSparkline.hpp:19
Definition XLDrawing.hpp:313