Version 7.5.1.1, tag libreoffice-7.5.1.1
[LibreOffice.git] / sc / source / filter / inc / xetable.hxx
blobed16140f6d68d7c32664a6a6be9d8d83236f939a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #pragma once
22 #include "xltable.hxx"
24 #include <vector>
25 #include "xladdress.hxx"
26 #include "xecontent.hxx"
27 #include "xerecord.hxx"
28 #include "xestyle.hxx"
29 #include "xlformula.hxx"
31 #include <map>
32 #include <memory>
33 #include <unordered_map>
34 #include <o3tl/sorted_vector.hxx>
36 class XclExtLst;
38 /* ============================================================================
39 Export of cell tables including row and column description.
40 - Managing all used and formatted cells in a sheet.
41 - Row and column properties, i.e. width/height, visibility.
42 - Find default row formatting and default column formatting.
43 - Merged cell ranges.
44 ============================================================================ */
46 // Helper records for cell records
48 /** Represents a STRING record that contains the result of a string formula. */
49 class XclExpStringRec : public XclExpRecord
51 public:
52 explicit XclExpStringRec( const XclExpRoot& rRoot, const OUString& rResult );
54 private:
55 virtual void WriteBody( XclExpStream& rStrm ) override;
57 private:
58 XclExpStringRef mxResult;
61 // Additional records for special formula ranges
63 /** Base record for additional range formula records (i.e. ARRAY, SHRFMLA). */
64 class XclExpRangeFmlaBase : public XclExpRecord
66 public:
67 /** Returns true, if the passed cell position is equal to own base position. */
68 bool IsBasePos( sal_uInt16 nXclCol, sal_uInt32 nXclRow ) const;
70 /** Derived classes create the token array for a corresponding FORMULA cell record. */
71 virtual XclTokenArrayRef CreateCellTokenArray( const XclExpRoot& rRoot ) const = 0;
72 /** Derived classes return true, if the own formula contains volatile functions. */
73 virtual bool IsVolatile() const = 0;
75 protected:
76 /** Constructs the record with a single cell. */
77 explicit XclExpRangeFmlaBase(
78 sal_uInt16 nRecId, sal_uInt32 nRecSize, const ScAddress& rScPos );
79 /** Constructs the record with a cell range. */
80 explicit XclExpRangeFmlaBase(
81 sal_uInt16 nRecId, sal_uInt32 nRecSize, const ScRange& rScRange );
83 /** Extends the cell range to include the passed cell address. */
84 void Extend( const ScAddress& rScPos );
86 /** Writes the range address covered by this record. */
87 void WriteRangeAddress( XclExpStream& rStrm ) const;
89 protected:
90 XclRange maXclRange; /// Range described by this record.
91 XclAddress maBaseXclPos; /// Address of base cell (first FORMULA record).
94 typedef rtl::Reference< XclExpRangeFmlaBase > XclExpRangeFmlaRef;
96 // Array formulas =============================================================
98 class ScTokenArray;
100 /** Represents an ARRAY record that contains the token array of a matrix formula.
102 An ARRAY record is stored following the first FORMULA record that is part
103 of a matrix formula. All FORMULA records of a matrix formula contain a
104 reference to the ARRAY record, while the ARRAY record contains the formula
105 token array used by all formulas.
107 class XclExpArray : public XclExpRangeFmlaBase
109 public:
110 explicit XclExpArray( const XclTokenArrayRef& xTokArr, const ScRange& rScRange );
112 /** Creates and returns the token array for a corresponding FORMULA cell record. */
113 virtual XclTokenArrayRef CreateCellTokenArray( const XclExpRoot& rRoot ) const override;
114 /** Returns true, if the array formula contains volatile functions. */
115 virtual bool IsVolatile() const override;
117 private:
118 virtual void WriteBody( XclExpStream& rStrm ) override;
120 private:
121 XclTokenArrayRef mxTokArr; /// The token array of a matrix formula.
124 typedef rtl::Reference< XclExpArray > XclExpArrayRef;
126 /** Caches all ARRAY records. */
127 class XclExpArrayBuffer : protected XclExpRoot
129 public:
130 explicit XclExpArrayBuffer( const XclExpRoot& rRoot );
132 /** Inserts a new ARRAY record into the buffer and returns it. */
133 XclExpArrayRef CreateArray( const ScTokenArray& rScTokArr, const ScRange& rScRange );
134 /** Tries to find an ARRAY record that corresponds to an ocMatRef token. */
135 XclExpArrayRef FindArray( const ScTokenArray& rScTokArr, const ScAddress& rBasePos ) const;
137 private:
138 typedef ::std::map< ScAddress, XclExpArrayRef > XclExpArrayMap;
139 XclExpArrayMap maRecMap; /// Map containing the ARRAY records.
142 // Shared formulas ============================================================
144 /** Represents a SHRFMLA record that contains the token array of a shared formula.
146 A SHRFMLA record is stored following the first FORMULA record that is part
147 of a shared formula. All FORMULA records of a shared formula contain a
148 reference to the SHRFMLA record, while the SHRFMLA record contains the
149 formula token array used by all formulas.
151 class XclExpShrfmla : public XclExpRangeFmlaBase
153 public:
154 /** Creates a SHRFMLA record that consists of the passed cell address only. */
155 explicit XclExpShrfmla( const XclTokenArrayRef& xTokArr, const ScAddress& rScPos );
157 /** Extends the cell range to include the passed cell address. */
158 void ExtendRange( const ScAddress& rScPos );
160 /** Creates and returns the token array for a corresponding FORMULA cell record. */
161 virtual XclTokenArrayRef CreateCellTokenArray( const XclExpRoot& rRoot ) const override;
162 /** Returns true, if the shared formula contains volatile functions. */
163 virtual bool IsVolatile() const override;
165 private:
166 virtual void WriteBody( XclExpStream& rStrm ) override;
168 private:
169 XclTokenArrayRef mxTokArr; /// The token array of a shared formula.
170 sal_uInt8 mnUsedCount; /// Number of FORMULA records referring to this record.
173 typedef rtl::Reference< XclExpShrfmla > XclExpShrfmlaRef;
175 /** Caches all SHRFMLA records and provides functions to update their ranges. */
176 class XclExpShrfmlaBuffer : protected XclExpRoot
178 public:
179 explicit XclExpShrfmlaBuffer( const XclExpRoot& rRoot );
181 /** Tries to create a new or to update an existing SHRFMLA record.
182 @return An empty reference, if the passed token array does not contain
183 a shared formula. If the token array is a shared formula, this
184 function updates its cell range to include the passed cell position,
185 if there is a SHRFMLA record for the passed token array; otherwise
186 this function creates and returns a new SHRFMLA record. */
187 XclExpShrfmlaRef CreateOrExtendShrfmla( const ScFormulaCell& rScCell, const ScAddress& rScPos );
189 private:
191 * Check for presence of token that's not allowed in Excel's shared
192 * formula. Refer to the "SharedParsedFormula" section of [MS-XLS] spec
193 * for more info.
195 bool IsValidTokenArray( const ScTokenArray& rArray ) const;
197 typedef std::unordered_map<const ScTokenArray*, XclExpShrfmlaRef> TokensType;
198 typedef o3tl::sorted_vector<const ScTokenArray*> BadTokenArraysType;
200 TokensType maRecMap; /// Map containing the SHRFMLA records.
201 BadTokenArraysType maBadTokens; /// shared tokens we should *not* export as SHRFMLA
204 // Multiple operations ========================================================
206 /** Represents a TABLEOP record for a multiple operations range. */
207 class XclExpTableop : public XclExpRangeFmlaBase
209 public:
210 explicit XclExpTableop( const ScAddress& rScPos,
211 const XclMultipleOpRefs& rRefs, sal_uInt8 nScMode );
213 /** Returns true, if the cell range has been extended to the passed position.
214 @descr All references passed in rRefs must fit the ranges passed in the constructor. */
215 bool TryExtend( const ScAddress& rScPos, const XclMultipleOpRefs& rRefs );
217 /** Finalizes the record. Tests on valid cell range and reference addresses. */
218 void Finalize();
220 /** Creates and returns the token array for a corresponding FORMULA cell record. */
221 virtual XclTokenArrayRef CreateCellTokenArray( const XclExpRoot& rRoot ) const override;
222 /** Returns true, if the multiple operations range is volatile. */
223 virtual bool IsVolatile() const override;
224 /** Writes the record if it is valid. */
225 virtual void Save( XclExpStream& rStrm ) override;
227 private:
228 /** Returns true, if the passed cell position can be appended to this record. */
229 bool IsAppendable( sal_uInt16 nXclCol, sal_uInt16 nXclRow ) const;
231 /** Writes the contents of the TABLEOP record. */
232 virtual void WriteBody( XclExpStream& rStrm ) override;
234 private:
235 sal_uInt16 mnLastAppXclCol;/// Column index of last appended cell.
236 sal_uInt16 mnColInpXclCol; /// Column index of column input cell.
237 sal_uInt32 mnColInpXclRow; /// Row index of column input cell.
238 sal_uInt16 mnRowInpXclCol; /// Column index of row input cell.
239 sal_uInt32 mnRowInpXclRow; /// Row index of row input cell.
240 sal_uInt8 mnScMode; /// Type of the multiple operation (Calc constant).
241 bool mbValid; /// true = Contains valid references.
244 typedef rtl::Reference< XclExpTableop > XclExpTableopRef;
246 /** Contains all created TABLEOP records and supports creating or updating them. */
247 class XclExpTableopBuffer : protected XclExpRoot
249 public:
250 explicit XclExpTableopBuffer( const XclExpRoot& rRoot );
252 /** Tries to update an existing or to create a new TABLEOP record.
253 @return Reference to the TABLEOP record for this cell (existing or new),
254 or an empty reference, if rScTokArr does not contain a multiple
255 operations formula. */
256 XclExpTableopRef CreateOrExtendTableop(
257 const ScTokenArray& rScTokArr, const ScAddress& rScPos );
259 /** Finalizes all contained TABLEOP records. */
260 void Finalize();
262 private:
263 /** Tries to create a new TABLEOP record, if rRefs contains valid references. */
264 XclExpTableopRef TryCreate( const ScAddress& rScPos, const XclMultipleOpRefs& rRefs );
266 private:
267 XclExpRecordList< XclExpTableop > maTableopList; /// List of all TABLEOP records.
270 // Cell records
272 /** The base class of all cell records. */
273 class XclExpCellBase : public XclExpRecord
275 public:
276 /** Returns the (first) address of the cell(s). */
277 const XclAddress& GetXclPos() const { return maXclPos; }
278 /** Returns the (first) Excel column index of the cell(s). */
279 sal_uInt16 GetXclCol() const { return maXclPos.mnCol; }
280 /** Returns the Excel row index of the cell. */
281 sal_uInt32 GetXclRow() const { return maXclPos.mnRow; }
283 /** Derived classes return the column index of the last contained cell. */
284 virtual sal_uInt16 GetLastXclCol() const = 0;
285 /** Derived classes return the XF identifier of the first contained cell. */
286 virtual sal_uInt32 GetFirstXFId() const = 0;
287 /** Derived classes return true, if this record does not contain at least one valid cell. */
288 virtual bool IsEmpty() const = 0;
289 /** Derived classes return whether the cell contains multi-line text. */
290 virtual bool IsMultiLineText() const;
292 /** Derived classes try to merge the contents of the passed cell to own data. */
293 virtual bool TryMerge( const XclExpCellBase& rCell );
294 /** Derived classes convert the XF identifier(s) into the Excel XF index(es).
295 @param rXFIndexes The converted XF index(es) are inserted here. */
296 virtual void ConvertXFIndexes( const XclExpRoot& rRoot ) = 0;
297 /** Derived classes for blank cells insert the Excel XF index(es) into the passed vector. */
298 virtual void GetBlankXFIndexes( ScfUInt16Vec& rXFIndexes ) const;
299 /** Derived classes for blank cells remove unused Excel XF index(es). */
300 virtual void RemoveUnusedBlankCells( const ScfUInt16Vec& rXFIndexes, size_t nStartAllNotFound );
302 protected:
303 explicit XclExpCellBase(
304 sal_uInt16 nRecId, std::size_t nContSize, const XclAddress& rXclPos );
306 /** Sets this record to a new column position. */
307 void SetXclCol( sal_uInt16 nXclCol ) { maXclPos.mnCol = nXclCol; }
309 private:
310 XclAddress maXclPos; /// Address of the cell.
313 typedef rtl::Reference< XclExpCellBase > XclExpCellRef;
315 // Single cell records ========================================================
317 /** Base class for all cell records not supporting multiple contents. */
318 class XclExpSingleCellBase : public XclExpCellBase
320 public:
321 /** Returns the last column, which is equal to the first column for single cells. */
322 virtual sal_uInt16 GetLastXclCol() const override;
323 /** Return the XF identifier of the cell. */
324 virtual sal_uInt32 GetFirstXFId() const override;
325 /** Returns true, if this record does not contain at least one valid cell. */
326 virtual bool IsEmpty() const override;
327 /** Converts the XF identifier into the Excel XF index. */
328 virtual void ConvertXFIndexes( const XclExpRoot& rRoot ) override;
329 /** Writes cell address, XF index, and calls WriteContents() for each cell. */
330 virtual void Save( XclExpStream& rStrm ) override;
332 protected:
333 explicit XclExpSingleCellBase( sal_uInt16 nRecId, std::size_t nContSize,
334 const XclAddress& rXclPos, sal_uInt32 nXFId );
336 explicit XclExpSingleCellBase( const XclExpRoot& rRoot,
337 sal_uInt16 nRecId, std::size_t nContSize, const XclAddress& rXclPos,
338 const ScPatternAttr* pPattern, sal_Int16 nScript, sal_uInt32 nForcedXFId );
340 void SetContSize( std::size_t nContSize ) { mnContSize = nContSize; }
341 std::size_t GetContSize() const { return mnContSize; }
343 void SetXFId( sal_uInt32 nXFId ) { maXFId.mnXFId = nXFId; }
344 sal_uInt32 GetXFId() const { return maXFId.mnXFId; }
346 private:
347 /** Writes cell address, XF index, and calls WriteContents() for each cell. */
348 virtual void WriteBody( XclExpStream& rStrm ) override;
349 /** Derived classes write the contents of the specified cell (without XF index). */
350 virtual void WriteContents( XclExpStream& rStrm ) = 0;
352 private:
353 XclExpXFId maXFId; /// The XF identifier of the cell formatting.
354 std::size_t mnContSize; /// The size of the cell contents.
357 /** Represents a NUMBER record that describes a cell with a double value. */
358 class XclExpNumberCell : public XclExpSingleCellBase
360 public:
361 explicit XclExpNumberCell( const XclExpRoot& rRoot, const XclAddress& rXclPos,
362 const ScPatternAttr* pPattern, sal_uInt32 nForcedXFId,
363 double fValue );
365 virtual void SaveXml( XclExpXmlStream& rStrm ) override;
366 private:
367 virtual void WriteContents( XclExpStream& rStrm ) override;
369 private:
370 double mfValue; /// The cell value.
373 /** Represents a BOOLERR record that describes a cell with a Boolean value. */
374 class XclExpBooleanCell : public XclExpSingleCellBase
376 public:
377 explicit XclExpBooleanCell( const XclExpRoot& rRoot, const XclAddress& rXclPos,
378 const ScPatternAttr* pPattern, sal_uInt32 nForcedXFId,
379 bool bValue );
381 virtual void SaveXml( XclExpXmlStream& rStrm ) override;
382 private:
383 virtual void WriteContents( XclExpStream& rStrm ) override;
385 private:
386 bool mbValue; /// The cell value.
389 class XclExpHyperlinkHelper;
390 class EditTextObject;
392 /** Represents a text cell record.
394 May contain a BIFF2-BIFF7 LABEL record for a simple string, or a BIFF2-BIFF7
395 RSTRING record for a formatted string, or a BIFF8 LABELSST string for any
396 string (simply stores a reference to the Shared String Table).
398 class XclExpLabelCell : public XclExpSingleCellBase
400 public:
401 /** Constructs the record from an unformatted Calc string cell. */
402 explicit XclExpLabelCell( const XclExpRoot& rRoot, const XclAddress& rXclPos,
403 const ScPatternAttr* pPattern, sal_uInt32 nForcedXFId,
404 const OUString& rStr );
406 /** Constructs the record from a formatted Calc edit cell. */
407 explicit XclExpLabelCell( const XclExpRoot& rRoot, const XclAddress& rXclPos,
408 const ScPatternAttr* pPattern, sal_uInt32 nForcedXFId,
409 const EditTextObject* pEditText, XclExpHyperlinkHelper& rHlinkHelper );
411 /** Returns true if the cell contains multi-line text. */
412 virtual bool IsMultiLineText() const override;
414 virtual void SaveXml( XclExpXmlStream& rStrm ) override;
415 private:
416 /** Initializes the record contents. Called from constructors. */
417 void Init( const XclExpRoot& rRoot,
418 const ScPatternAttr* pPattern, XclExpStringRef const & xText );
420 virtual void WriteContents( XclExpStream& rStrm ) override;
422 private:
423 XclExpStringRef mxText; /// The cell text.
424 sal_uInt32 mnSstIndex; /// Index into Shared String Table (only used for BIFF8).
425 bool mbLineBreak; /// True = cell has automatic linebreaks enabled.
428 class ScFormulaCell;
430 /** Represents a FORMULA record that describes a cell with a formula. */
431 class XclExpFormulaCell : public XclExpSingleCellBase
433 public:
434 explicit XclExpFormulaCell( const XclExpRoot& rRoot, const XclAddress& rXclPos,
435 const ScPatternAttr* pPattern, sal_uInt32 nForcedXFId,
436 const ScFormulaCell& rScFmlaCell,
437 XclExpArrayBuffer& rArrayBfr,
438 XclExpShrfmlaBuffer& rShrfmlaBfr,
439 XclExpTableopBuffer& rTableopBfr );
441 /** Writes the FORMULA record and additional records related to the formula. */
442 virtual void Save( XclExpStream& rStrm ) override;
443 virtual void SaveXml( XclExpXmlStream& rStrm ) override;
445 private:
446 virtual void WriteContents( XclExpStream& rStrm ) override;
448 private:
449 ScFormulaCell& mrScFmlaCell; /// The Calc formula cell.
450 XclTokenArrayRef mxTokArr; /// The token array of the formula.
451 XclExpRangeFmlaRef mxAddRec; /// Additional record for matrix/shared formulas.
452 XclExpRecordRef mxStringRec; /// STRING record for string result.
455 // Multiple cell records ======================================================
457 struct XclExpMultiXFId : public XclExpXFId
459 sal_uInt16 mnCount; /// Number of XF identifiers.
461 explicit XclExpMultiXFId( sal_uInt32 nXFId, sal_uInt16 nCount = 1 ) :
462 XclExpXFId( nXFId ), mnCount( nCount ) {}
465 /** Base class for all cell records supporting multiple contents. */
466 class XclExpMultiCellBase : public XclExpCellBase
468 public:
469 /** Returns the column index of the last cell this record describes. */
470 virtual sal_uInt16 GetLastXclCol() const override;
471 /** Return the XF identifier of the first contained cell. */
472 virtual sal_uInt32 GetFirstXFId() const override;
473 /** Returns true, if this record does not contain at least one valid cell. */
474 virtual bool IsEmpty() const override;
476 /** Convert all XF identifiers into the Excel XF indexes. */
477 virtual void ConvertXFIndexes( const XclExpRoot& rRoot ) override;
478 /** Writes the record, calls WriteContents() for each contained cell.
479 @descr May write several records, if unused XF indexes are contained. */
480 virtual void Save( XclExpStream& rStrm ) override;
481 virtual void SaveXml( XclExpXmlStream& rStrm ) override;
483 protected:
484 explicit XclExpMultiCellBase( sal_uInt16 nRecId, sal_uInt16 nMulRecId,
485 std::size_t nContSize, const XclAddress& rXclPos );
487 /** Returns the number of cells this record represents. */
488 sal_uInt16 GetCellCount() const;
490 /** Appends the passed XF identifier nCount times to the list of XF identifiers. */
491 void AppendXFId( const XclExpMultiXFId& rXFId );
492 /** Appends the passed cell format nCount times to the list of XF identifiers. */
493 void AppendXFId( const XclExpRoot& rRoot,
494 const ScPatternAttr* pPattern, sal_uInt16 nScript,
495 sal_uInt32 nForcedXFId, sal_uInt16 nCount = 1 );
497 /** Tries to merge the XF ID list of the passed cell with the own list. */
498 bool TryMergeXFIds( const XclExpMultiCellBase& rCell );
499 /** Inserts the Excel XF index(es) into the passed vector. */
500 void GetXFIndexes( ScfUInt16Vec& rXFIndexes ) const;
502 /** Removes unused Excel XF index(es).
503 @param rXFIndexes Specifies which XF indexes are used.
504 @param nStartAllNotFound Index in rXFIndexes which starts EXC_XF_NOTFOUND until the end.
506 void RemoveUnusedXFIndexes( const ScfUInt16Vec& rXFIndexes, size_t nStartAllNotFound );
508 /** Return starting column at which all indexes until the end are EXC_XF_DEFAULTCELL .*/
509 sal_uInt16 GetStartColAllDefaultCell() const;
511 private:
512 /** Derived classes write the remaining contents of the specified cell (without XF index).
513 @param nRelCol Relative column index (starts with 0 for first cell of this record). */
514 virtual void WriteContents( XclExpStream& rStrm, sal_uInt16 nRelCol ) = 0;
515 virtual void WriteXmlContents( XclExpXmlStream& rStrm, const XclAddress& rAddress, sal_uInt32 nXFId, sal_uInt16 nRelCol ) = 0;
517 private:
518 typedef ::std::vector< XclExpMultiXFId > XclExpMultiXFIdDeq;
520 sal_uInt16 mnMulRecId; /// Record ID for multiple record variant.
521 std::size_t mnContSize; /// Data size of contents for one cell
522 XclExpMultiXFIdDeq maXFIds; /// The XF identifiers of the cell formatting.
525 /** Represents a BLANK or MULBLANK record that describes empty but formatted cells. */
526 class XclExpBlankCell : public XclExpMultiCellBase
528 public:
529 explicit XclExpBlankCell( const XclAddress& rXclPos, const XclExpMultiXFId& rXFId );
531 explicit XclExpBlankCell( const XclExpRoot& rRoot,
532 const XclAddress& rXclPos, sal_uInt16 nLastXclCol,
533 const ScPatternAttr* pPattern, sal_uInt32 nForcedXFId );
535 /** Tries to merge the contents of the passed cell to own data. */
536 virtual bool TryMerge( const XclExpCellBase& rCell ) override;
537 /** Inserts the Excel XF index(es) into the passed vector. */
538 virtual void GetBlankXFIndexes( ScfUInt16Vec& rXFIndexes ) const override;
539 /** Tries to remove unused Excel XF index(es). */
540 virtual void RemoveUnusedBlankCells( const ScfUInt16Vec& rXFIndexes, size_t nStartAllNotFound ) override;
542 using XclExpMultiCellBase::GetStartColAllDefaultCell;
544 private:
545 /** Writes the remaining contents of the specified cell (without XF index). */
546 virtual void WriteContents( XclExpStream& rStrm, sal_uInt16 nRelCol ) override;
547 virtual void WriteXmlContents( XclExpXmlStream& rStrm, const XclAddress& rAddress, sal_uInt32 nXFId, sal_uInt16 nRelCol ) override;
550 /** Represents an RK or MULRK record that describes cells with a compressed double values. */
551 class XclExpRkCell : public XclExpMultiCellBase
553 public:
554 explicit XclExpRkCell( const XclExpRoot& rRoot, const XclAddress& rXclPos,
555 const ScPatternAttr* pPattern, sal_uInt32 nForcedXFId,
556 sal_Int32 nRkValue );
558 /** Tries to merge the contents of the passed cell to own data. */
559 virtual bool TryMerge( const XclExpCellBase& rCell ) override;
561 private:
562 /** Writes the remaining contents of the specified cell (without XF index). */
563 virtual void WriteContents( XclExpStream& rStrm, sal_uInt16 nRelCol ) override;
564 virtual void WriteXmlContents( XclExpXmlStream& rStrm, const XclAddress& rAddress, sal_uInt32 nXFId, sal_uInt16 nRelCol ) override;
566 private:
567 ScfInt32Vec maRkValues; /// The cell values.
570 // Rows and Columns
572 class ScOutlineArray;
574 /** Base class for buffers containing row or column outline data. */
575 class XclExpOutlineBuffer
577 public:
578 /** Returns true, if a collapsed group ends at the last processed position. */
579 bool IsCollapsed() const { return mbCurrCollapse; }
580 /** Returns the highest level of an open group at the last processed position. */
581 sal_uInt8 GetLevel() const { return ::std::min( mnCurrLevel, EXC_OUTLINE_MAX ); }
583 protected:
584 /** Constructs the outline buffer.
585 @param bRows true = Process row outline array; false = Process column outline array. */
586 explicit XclExpOutlineBuffer( const XclExpRoot& rRoot, bool bRows );
588 /** Updates the current state by processing the settings at the passed Calc position. */
589 void UpdateColRow( SCCOLROW nScPos );
591 private:
592 /** Data about an outline level. */
593 struct XclExpLevelInfo
595 SCCOLROW mnScEndPos; /// The end position of a group in a level.
596 bool mbHidden; /// true = Group in this level is hidden.
597 explicit XclExpLevelInfo() : mnScEndPos( 0 ), mbHidden( false ) {}
600 const ScOutlineArray* mpScOLArray; /// Pointer to Calc outline array.
601 std::vector< XclExpLevelInfo >
602 maLevelInfos; /// Info for current row and all levels.
603 sal_uInt8 mnCurrLevel; /// Highest level of an open group for current position.
604 bool mbCurrCollapse; /// true = Collapsed group ends at current position.
607 /** The outline buffer for column outlines. */
608 class XclExpColOutlineBuffer : public XclExpOutlineBuffer
610 public:
611 explicit XclExpColOutlineBuffer( const XclExpRoot& rRoot ) :
612 XclExpOutlineBuffer( rRoot, false ) {}
614 /** Updates the current state by processing the settings of the passed Calc column. */
615 void Update( SCCOL nScCol )
616 { UpdateColRow( static_cast< SCCOLROW >( nScCol ) ); }
619 /** The outline buffer for row outlines. */
620 class XclExpRowOutlineBuffer : public XclExpOutlineBuffer
622 public:
623 explicit XclExpRowOutlineBuffer( const XclExpRoot& rRoot ) :
624 XclExpOutlineBuffer( rRoot, true ) {}
626 /** Updates the current state by processing the settings of the passed Calc row. */
627 void Update( SCROW nScRow )
628 { UpdateColRow( static_cast< SCCOLROW >( nScRow ) ); }
631 /** Represents a GUTS record containing the level count of row and column outlines. */
632 class XclExpGuts : public XclExpRecord
634 public:
635 explicit XclExpGuts( const XclExpRoot& rRoot );
637 private:
638 virtual void WriteBody( XclExpStream& rStrm ) override;
640 private:
641 sal_uInt16 mnColLevels; /// Number of visible column outline levels.
642 sal_uInt16 mnColWidth; /// Width of column outline area (pixels).
643 sal_uInt16 mnRowLevels; /// Number of visible row outline levels.
644 sal_uInt16 mnRowWidth; /// Width of row outline area (pixels).
647 /** Represents a DIMENSIONS record containing the used area of a sheet. */
648 class XclExpDimensions : public XclExpRecord
650 public:
651 explicit XclExpDimensions( const XclExpRoot& rRoot );
653 /** Sets the used area to the record. */
654 void SetDimensions(
655 sal_uInt16 nFirstUsedXclCol, sal_uInt32 nFirstUsedXclRow,
656 sal_uInt16 nFirstFreeXclCol, sal_uInt32 nFirstFreeXclRow );
658 virtual void SaveXml( XclExpXmlStream& rStrm ) override;
659 private:
660 /** Writes the contents of the DIMENSIONS record. */
661 virtual void WriteBody( XclExpStream& rStrm ) override;
663 private:
664 const XclExpRoot& mrRoot;
665 sal_uInt32 mnFirstUsedXclRow; /// First used row.
666 sal_uInt32 mnFirstFreeXclRow; /// First unused row after used area.
667 sal_uInt16 mnFirstUsedXclCol; /// First used column.
668 sal_uInt16 mnFirstFreeXclCol; /// First free column after used area.
671 /** Represents the DEFCOLWIDTH record containing the default column width of a sheet.
673 Excel stores the default column width in entire character widths of the '0'
674 character using the application default font (i.e. the default width is 10,
675 if the '0' character fits 10 times into a cell in a column with default
676 width.
678 Half of character width is reserved for non character display.
679 It is margin padding (two on each side) and padding for the gridlines.
681 The IsDefWidth() function returns true, if the passed width (measured in
682 1/256 of the width of the '0' character) could be converted exactly to the
683 default width. If the passed width is rounded up or down to get the default
684 width, the function returns false.
686 class XclExpDefcolwidth : public XclExpDoubleRecord, protected XclExpRoot
688 public:
689 explicit XclExpDefcolwidth( const XclExpRoot& rRoot );
691 /** Returns true, if the own default width exactly matches the passed width. */
692 bool IsDefWidth( sal_uInt16 nXclColWidth ) const;
694 /** Sets the passed column width (in 1/256 character width) as default width. */
695 void SetDefWidth( sal_uInt16 nXclColWidth, bool bXLS );
697 virtual void Save(XclExpStream& rStrm) override;
700 /** Contains the column settings for a range of columns.
702 After construction the record contains a temporary XF identifier returned
703 from the XF buffer. After creating the entire Excel document in memory, the
704 ConvertXFIndexes() function converts it into the real Excel XF index.
706 class XclExpColinfo : public XclExpRecord, protected XclExpRoot
708 public:
709 /** Constructs the record with the settings in the Calc document. */
710 explicit XclExpColinfo( const XclExpRoot& rRoot,
711 SCCOL nScCol, SCROW nLastScRow,
712 XclExpColOutlineBuffer& rOutlineBfr );
714 /** Converts the XF identifier into the Excel XF index. */
715 void ConvertXFIndexes();
717 /** Tries to merge this record with the passed record.
718 @descr Possible, if passed record directly follows this record and has equal contents.
719 @return true = This record is equal to passed record and has been updated. */
720 bool TryMerge( const XclExpColinfo& rColInfo );
722 /** Returns the Excel width of the column(s). */
723 sal_uInt16 GetColWidth() const { return mnWidth; }
724 /** Returns the final Excel XF index of the column(s). */
725 sal_uInt16 GetXFIndex() const { return maXFId.mnXFIndex; }
726 /** Returns the number of columns represented by this record. */
727 sal_uInt16 GetColCount() const { return mnLastXclCol - mnFirstXclCol + 1; }
729 /** Returns true, if the column has default format and width. Also sets mbCustomWidth */
730 bool IsDefault( const XclExpDefcolwidth& rDefColWidth );
732 virtual void SaveXml( XclExpXmlStream& rStrm ) override;
734 private:
735 /** Writes the contents of this COLINFO record. */
736 virtual void WriteBody( XclExpStream& rStrm ) override;
738 private:
739 XclExpXFId maXFId; /// The XF identifier for column default format.
740 bool mbCustomWidth; /// True = Column width is different from default width
741 sal_uInt16 mnWidth; /// Excel width of the column.
742 sal_uInt16 mnScWidth; /// Calc width of the column.
743 sal_uInt16 mnFlags; /// Additional column flags.
744 sal_uInt8 mnOutlineLevel; /// Outline Level of column (for OOXML)
745 sal_uInt16 mnFirstXclCol; /// Index to first column.
746 sal_uInt16 mnLastXclCol; /// Index to last column.
749 /** Contains COLINFO records for all columns of a Calc sheet.
751 On construction one COLINFO record per column is created. After creating
752 the entire Excel document in memory, the ConvertXFIndexes() function converts
753 all temporary XF identifiers into real Excel XF indexes and merges all equal
754 COLINFO records together.
756 class XclExpColinfoBuffer : public XclExpRecordBase, protected XclExpRoot
758 public:
759 explicit XclExpColinfoBuffer( const XclExpRoot& rRoot );
761 /** Initializes the buffer: finds settings and formatting of all columns.
762 @param nLastScRow Last row used to find default formatting. */
763 void Initialize( SCROW nLastScRow );
764 /** Converts the XF identifiers into the Excel XF indexes and merges equal columns.
765 @param rXFIndexes Returns the final XF indexes of all columns. */
766 void Finalize( ScfUInt16Vec& rXFIndexes, bool bXLS );
768 /** Writes all COLINFO records of this buffer. */
769 virtual void Save( XclExpStream& rStrm ) override;
770 virtual void SaveXml( XclExpXmlStream& rStrm ) override;
771 sal_uInt8 GetHighestOutlineLevel() const { return mnHighestOutlineLevel; }
772 double GetDefColWidth() const { return maDefcolwidth.GetValue(); }
774 private:
775 typedef XclExpRecordList< XclExpColinfo > XclExpColinfoList;
776 typedef XclExpColinfoList::RecordRefType XclExpColinfoRef;
778 XclExpColinfoList maColInfos; /// List of COLINFO records.
779 XclExpDefcolwidth maDefcolwidth; /// The DEFCOLWIDTH record.
780 XclExpColOutlineBuffer maOutlineBfr; /// Buffer for column outline groups.
781 sal_uInt8 mnHighestOutlineLevel; /// Highest number of outline levels for columns in sheet.
784 class XclExpRow;
786 /** Contains all possible default row settings. */
787 struct XclExpDefaultRowData
789 sal_uInt16 mnFlags; /// Default flags for unspecified rows.
790 sal_uInt16 mnHeight; /// Default height for unspecified rows.
792 explicit XclExpDefaultRowData();
793 explicit XclExpDefaultRowData( const XclExpRow& rRow );
795 /** Returns true, if rows are hidden by default. */
796 bool IsHidden() const { return ::get_flag( mnFlags, EXC_DEFROW_HIDDEN ); }
797 /** Returns true, if the rows have a manually set height by default. */
798 bool IsUnsynced() const { return ::get_flag( mnFlags, EXC_DEFROW_UNSYNCED ); }
801 /** Represents a DEFROWHEIGHT record containing default format for unused rows. */
802 class XclExpDefrowheight : public XclExpRecord
804 public:
805 explicit XclExpDefrowheight();
807 /** Sets the passed default data as current record contents. */
808 void SetDefaultData( const XclExpDefaultRowData& rDefData );
809 XclExpDefaultRowData& GetDefaultData() { return maDefData; }
810 private:
811 /** Writes the contents of the record. */
812 virtual void WriteBody( XclExpStream& rStrm ) override;
814 private:
815 XclExpDefaultRowData maDefData; /// Record data.
818 /** Represents a ROW record and additionally contains all cells records of a row.
820 This class contains all cell records of a row in a spreadsheet. There are 2
821 cell records in Excel that support storing a range of cells in one record
822 (MULBLANK for multiple blank cells, and MULRK for multiple RK values). The
823 insertion functions try to merge a new inserted cell with existing
824 neighbors, if this is supported by the current type of cell record.
826 The Finalize() function converts the XF identifiers of all cell records to
827 the final Excel XF indexes. Then a default
829 class XclExpRow : public XclExpRecord, protected XclExpRoot
831 public:
832 /** Constructs the ROW record and converts the Calc row settings.
833 @param bAlwaysEmpty true = This row will not be filled with blank cells
834 in the Finalize() function. */
835 explicit XclExpRow( const XclExpRoot& rRoot, sal_uInt32 nXclRow,
836 XclExpRowOutlineBuffer& rOutlineBfr, bool bAlwaysEmpty, bool bHidden, sal_uInt16 nHeight );
838 /** Returns the excel row index of this ROW record. */
839 sal_uInt32 GetXclRow() const { return mnXclRow; }
840 /** Returns the height of the row in twips. */
841 sal_uInt16 GetHeight() const { return mnHeight; }
842 /** Returns true, if this row does not contain at least one valid cell. */
843 bool IsEmpty() const { return maCellList.IsEmpty(); }
844 /** Returns true, if this row is hidden. */
845 bool IsHidden() const { return ::get_flag( mnFlags, EXC_ROW_HIDDEN ); }
846 /** Returns true, if this row contains a manually set height. */
847 bool IsUnsynced() const { return ::get_flag( mnFlags, EXC_ROW_UNSYNCED ); }
848 /** Returns true, if this row is enabled (will be exported). */
849 bool IsEnabled() const { return mbEnabled; }
851 /** Appends the passed cell object to this row. */
852 void AppendCell( XclExpCellRef const & xCell, bool bIsMergedBase );
854 /** Converts all XF identifiers into the Excel XF indexes. */
855 void Finalize( const ScfUInt16Vec& rColXFIndexes,
856 ScfUInt16Vec& aXFIndexes,
857 size_t nStartColAllDefault,
858 bool bUpdateProgress );
860 /** Returns the column index of the first used cell in this row.
861 @descr This function can only be called after Finalize(). */
862 sal_uInt16 GetFirstUsedXclCol() const;
863 /** Returns the column index of the first unused cell following all used cells in this row.
864 @descr This function can only be called after Finalize(). */
865 sal_uInt16 GetFirstFreeXclCol() const;
867 /** Returns true, if this row may be omitted by using the DEFROWHEIGHT record.
868 @descr A row may be omitted, if it does not contain any cell or
869 explicit default cell formatting, and is not part of an outline.
870 This function can only be called after Finalize(). */
871 bool IsDefaultable() const;
872 /** Disables this row, if it is defaultable and has the passed default format.
873 @descr Disabled rows will not be saved.
874 This function can only be called after Finalize(). */
875 void DisableIfDefault( const XclExpDefaultRowData& rDefRowData );
877 /** Writes all cell records of this row. */
878 void WriteCellList( XclExpStream& rStrm );
880 /** Writes the ROW record if the row is not disabled (see DisableIfDefault() function). */
881 virtual void Save( XclExpStream& rStrm ) override;
882 virtual void SaveXml( XclExpXmlStream& rStrm ) override;
884 sal_uInt32 GetXclRowRpt() const { return mnXclRowRpt; }
885 void SetXclRowRpt( sal_uInt32 nRpt ){ mnXclRowRpt = nRpt; }
886 private:
887 /** Inserts a cell at the specified list position, tries to merge with neighbors. */
888 void InsertCell( XclExpCellRef xCell, size_t nPos, bool bIsMergedBase );
890 /** Writes the contents of the ROW record. */
891 virtual void WriteBody( XclExpStream& rStrm ) override;
893 private:
894 XclExpRecordList< XclExpCellBase >
895 maCellList; /// List of cell records for this row.
896 sal_uInt32 mnXclRow; /// Excel row index of this row.
897 sal_uInt16 mnHeight; /// Row height in twips.
898 sal_uInt16 mnFlags; /// Flags for the ROW record.
899 sal_uInt16 mnXFIndex; /// Default row formatting.
900 sal_uInt8 mnOutlineLevel; /// Outline Level of row (for OOXML)
901 sal_uInt32 mnXclRowRpt;
902 sal_uInt32 mnCurrentRow;
903 bool mbAlwaysEmpty; /// true = Do not add blank cells in Finalize().
904 bool mbEnabled; /// true = Write this ROW record.
907 /** Collects all rows which contain all cells of a sheet.
909 This row buffer automatically creates ROW records when cells are inserted
910 with the AppendCell() function. It is possible to force creation of more
911 ROW records with the CreateRows() function. In both cases, all preceding
912 missing ROW records are inserted too.
914 class XclExpRowBuffer : public XclExpRecordBase, protected XclExpRoot
916 public:
917 explicit XclExpRowBuffer( const XclExpRoot& rRoot );
919 /** Appends the passed cell object to the row that the cell specifies. */
920 void AppendCell( XclExpCellRef const & xCell, bool bIsMergedBase );
921 /** Forces insertion of all ROW records before the passed row. */
922 void CreateRows( SCROW nFirstFreeScRow );
924 /** Converts all XF identifiers into the Excel XF indexes and calculates default formats.
925 @param rDefRowData (out-param) The default row format is returned here.
926 @param rColXFIndexes The column default XF indexes.
927 @param nStartColAllDefault Index in rColXFIndexes which starts EXC_XF_DEFAULTCELL until the end.
929 void Finalize( XclExpDefaultRowData& rDefRowData, const ScfUInt16Vec& rColXFIndexes,
930 size_t nStartColAllDefault );
932 /** Writes the DIMENSIONS record, all ROW records and all cell records. */
933 virtual void Save( XclExpStream& rStrm ) override;
934 virtual void SaveXml( XclExpXmlStream& rStrm ) override;
936 XclExpDimensions& GetDimensions() { return maDimensions; }
937 sal_uInt8 GetHighestOutlineLevel() const { return mnHighestOutlineLevel; }
939 private:
940 /** Returns access to the specified ROW record. Inserts preceding missing ROW records.
941 @param bRowAlwaysEmpty true = Created rows will not be filled with blank cells
942 in the XclExpRow::Finalize() function. */
943 XclExpRow& GetOrCreateRow( sal_uInt32 nXclRow, bool bRowAlwaysEmpty );
945 private:
946 typedef std::shared_ptr<XclExpRow> RowRef;
947 typedef ::std::map<sal_uInt32, RowRef> RowMap;
949 RowMap maRowMap;
950 XclExpRowOutlineBuffer maOutlineBfr; /// Buffer for row outline groups.
951 XclExpDimensions maDimensions; /// DIMENSIONS record for used area.
952 sal_uInt8 mnHighestOutlineLevel; /// Highest number of outline levels for rows in sheet.
955 // Cell Table
957 class XclExpNote;
958 class XclExpMergedcells;
959 class XclExpHyperlink;
960 class XclExpDval;
962 /** This class contains the cell contents and more of an entire sheet.
964 The cell table includes the settings and default formatting of all columns,
965 the settings and default formatting of all used rows, and the contents of
966 all cells of one sheet in a spreadsheet document.
968 The constructor does all the work creating the cell table. It reads the
969 Calc sheet and converts all columns, rows, and cells to Excel record data.
970 Additionally, hyperlink records, note records, additional records for
971 formula cells, data validation records, and outline records are created.
973 The Finalize() function does even more work. It calculates default column
974 settings and removes column records that are equal to this default. The
975 same happens with rows: A default format is calculated for each row, and
976 all blank cells in this row that have the same format are removed. Then,
977 the most used row settings are calculated, and all empty rows that have the
978 same settings are removed too.
980 Records that are not stored inside the cell table area in an Excel file
981 (i.e. DEFROWHEIGHT record, NOTE records, MERGEDCELLS record, HLINK records,
982 DVAL and DV records for data validation) can be accessed with the function
983 CreateRecord(). It returns the reference to the respective record (or
984 record list) which can be inserted into a record list.
986 class XclExpCellTable : public XclExpRecordBase, protected XclExpRoot
988 public:
989 explicit XclExpCellTable( const XclExpRoot& rRoot );
991 /** Converts all XF identifiers into the Excel XF indexes and calculates default formats. */
992 void Finalize(bool bXLS);
994 /** Returns the reference to an internal record specified by the passed record id.
995 @param nRecId The record identifier that specifies which record is
996 returned. Possible values are: EXC_ID_DEFROWHEIGHT, EXC_ID_NOTE,
997 EXC_ID_MERGEDCELLS, EXC_ID_HLINK, EXC_ID_DVAL. */
998 XclExpRecordRef CreateRecord( sal_uInt16 nRecId ) const;
999 /** Saves the entire cell table. */
1000 virtual void Save( XclExpStream& rStrm ) override;
1001 virtual void SaveXml( XclExpXmlStream& rStrm ) override;
1003 private:
1004 typedef XclExpRecordList< XclExpNote > XclExpNoteList;
1005 typedef XclExpRecordList< XclExpHyperlink > XclExpHyperlinkList;
1007 typedef rtl::Reference< XclExpDefrowheight > XclExpDefrowhRef;
1008 typedef rtl::Reference< XclExpNoteList > XclExpNoteListRef;
1009 typedef rtl::Reference< XclExpMergedcells > XclExpMergedcellsRef;
1010 typedef rtl::Reference< XclExpHyperlinkList > XclExpHyperlinkRef;
1011 typedef rtl::Reference< XclExpDval > XclExpDvalRef;
1012 typedef rtl::Reference< XclExtLst > XclExtLstRef;
1014 XclExpColinfoBuffer maColInfoBfr; /// Buffer for column formatting.
1015 XclExpRowBuffer maRowBfr; /// Rows and cell records.
1016 XclExpArrayBuffer maArrayBfr; /// Buffer for ARRAY records.
1017 XclExpShrfmlaBuffer maShrfmlaBfr; /// Buffer for SHRFMLA records.
1018 XclExpTableopBuffer maTableopBfr; /// Buffer for TABLEOP records.
1019 XclExpDefrowhRef mxDefrowheight; /// DEFROWHEIGHT record for default row format.
1020 XclExpRecordRef mxGuts; /// GUTS record for outline areas.
1021 XclExpNoteListRef mxNoteList; /// List of NOTE records.
1022 XclExpMergedcellsRef mxMergedcells; /// MERGEDCELLS record for merged cell ranges.
1023 XclExpHyperlinkRef mxHyperlinkList; /// List of HLINK records.
1024 XclExpDvalRef mxDval; /// Data validation with DVAL and DV records.
1025 XclExtLstRef mxExtLst;
1028 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */