Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / sc / source / filter / inc / xetable.hxx
blob8a26e3dffb12bbdb897ea47be3f50761b76e103d
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 #ifndef INCLUDED_SC_SOURCE_FILTER_INC_XETABLE_HXX
21 #define INCLUDED_SC_SOURCE_FILTER_INC_XETABLE_HXX
23 #include "xltable.hxx"
25 #include <vector>
26 #include <tools/mempool.hxx>
27 #include "xladdress.hxx"
28 #include "xerecord.hxx"
29 #include "xestring.hxx"
30 #include "xeformula.hxx"
31 #include "xestyle.hxx"
32 #include "xeextlst.hxx"
34 #include <map>
35 #include <memory>
36 #include <unordered_map>
37 #include <unordered_set>
39 /* ============================================================================
40 Export of cell tables including row and column description.
41 - Managing all used and formatted cells in a sheet.
42 - Row and column properties, i.e. width/height, visibility.
43 - Find default row formatting and default column formatting.
44 - Merged cell ranges.
45 ============================================================================ */
47 // Helper records for cell records
49 /** Represents a STRING record that contains the result of a string formula. */
50 class XclExpStringRec : public XclExpRecord
52 public:
53 explicit XclExpStringRec( const XclExpRoot& rRoot, const OUString& rResult );
55 private:
56 virtual void WriteBody( XclExpStream& rStrm ) override;
58 private:
59 XclExpStringRef mxResult;
62 // Additional records for special formula ranges
64 /** Base record for additional range formula records (i.e. ARRAY, SHRFMLA). */
65 class XclExpRangeFmlaBase : public XclExpRecord
67 public:
68 /** Returns true, if the passed cell position is equal to own base position. */
69 bool IsBasePos( sal_uInt16 nXclCol, sal_uInt32 nXclRow ) const;
71 /** Derived classes create the token array for a corresponding FORMULA cell record. */
72 virtual XclTokenArrayRef CreateCellTokenArray( const XclExpRoot& rRoot ) const = 0;
73 /** Derived classes return true, if the own formula contains volatile functions. */
74 virtual bool IsVolatile() const = 0;
76 protected:
77 /** Constructs the record with a single cell. */
78 explicit XclExpRangeFmlaBase(
79 sal_uInt16 nRecId, sal_uInt32 nRecSize, const ScAddress& rScPos );
80 /** Constructs the record with a cell range. */
81 explicit XclExpRangeFmlaBase(
82 sal_uInt16 nRecId, sal_uInt32 nRecSize, const ScRange& rScRange );
84 /** Extends the cell range to include the passed cell address. */
85 void Extend( const ScAddress& rScPos );
87 /** Writes the range address covered by this record. */
88 void WriteRangeAddress( XclExpStream& rStrm ) const;
90 protected:
91 XclRange maXclRange; /// Range described by this record.
92 XclAddress maBaseXclPos; /// Address of base cell (first FORMULA record).
95 typedef std::shared_ptr< XclExpRangeFmlaBase > XclExpRangeFmlaRef;
97 // Array formulas =============================================================
99 class ScTokenArray;
101 /** Represents an ARRAY record that contains the token array of a matrix formula.
103 An ARRAY record is stored following the first FORMULA record that is part
104 of a matrix formula. All FORMULA records of a matrix formula contain a
105 reference to the ARRAY record, while the ARRAY record contains the formula
106 token array used by all formulas.
108 class XclExpArray : public XclExpRangeFmlaBase
110 public:
111 explicit XclExpArray( XclTokenArrayRef xTokArr, const ScRange& rScRange );
113 /** Creates and returns the token array for a corresponding FORMULA cell record. */
114 virtual XclTokenArrayRef CreateCellTokenArray( const XclExpRoot& rRoot ) const override;
115 /** Returns true, if the array formula contains volatile functions. */
116 virtual bool IsVolatile() const override;
118 private:
119 virtual void WriteBody( XclExpStream& rStrm ) override;
121 private:
122 XclTokenArrayRef mxTokArr; /// The token array of a matrix formula.
125 typedef std::shared_ptr< XclExpArray > XclExpArrayRef;
127 /** Caches all ARRAY records. */
128 class XclExpArrayBuffer : protected XclExpRoot
130 public:
131 explicit XclExpArrayBuffer( const XclExpRoot& rRoot );
133 /** Inserts a new ARRAY record into the buffer and returns it. */
134 XclExpArrayRef CreateArray( const ScTokenArray& rScTokArr, const ScRange& rScRange );
135 /** Tries to find an ARRAY record that corresponds to an ocMatRef token. */
136 XclExpArrayRef FindArray( const ScTokenArray& rScTokArr, const ScAddress& rBasePos ) const;
138 private:
139 typedef ::std::map< ScAddress, XclExpArrayRef > XclExpArrayMap;
140 XclExpArrayMap maRecMap; /// Map containing the ARRAY records.
143 // Shared formulas ============================================================
145 /** Represents a SHRFMLA record that contains the token array of a shared formula.
147 A SHRFMLA record is stored following the first FORMULA record that is part
148 of a shared formula. All FORMULA records of a shared formula contain a
149 reference to the SHRFMLA record, while the SHRFMLA record contains the
150 formula token array used by all formulas.
152 class XclExpShrfmla : public XclExpRangeFmlaBase
154 public:
155 /** Creates a SHRFMLA record that consists of the passed cell address only. */
156 explicit XclExpShrfmla( XclTokenArrayRef xTokArr, const ScAddress& rScPos );
158 /** Extends the cell range to include the passed cell address. */
159 void ExtendRange( const ScAddress& rScPos );
161 /** Creates and returns the token array for a corresponding FORMULA cell record. */
162 virtual XclTokenArrayRef CreateCellTokenArray( const XclExpRoot& rRoot ) const override;
163 /** Returns true, if the shared formula contains volatile functions. */
164 virtual bool IsVolatile() const override;
166 private:
167 virtual void WriteBody( XclExpStream& rStrm ) override;
169 private:
170 XclTokenArrayRef mxTokArr; /// The token array of a shared formula.
171 sal_uInt8 mnUsedCount; /// Number of FORMULA records referring to this record.
174 typedef std::shared_ptr< XclExpShrfmla > XclExpShrfmlaRef;
176 /** Caches all SHRFMLA records and provides functions to update their ranges. */
177 class XclExpShrfmlaBuffer : protected XclExpRoot
179 public:
180 explicit XclExpShrfmlaBuffer( const XclExpRoot& rRoot );
182 /** Tries to create a new or to update an existing SHRFMLA record.
183 @return An empty reference, if the passed token array does not contain
184 a shared formula. If the token array is a shared formula, this
185 function updates its cell range to include the passed cell position,
186 if there is a SHRFMLA record for the passed token array; otherwise
187 this function creates and returns a new SHRFMLA record. */
188 XclExpShrfmlaRef CreateOrExtendShrfmla( const ScFormulaCell& rScCell, const ScAddress& rScPos );
190 private:
192 * Check for presence of token that's not allowed in Excel's shared
193 * formula. Refer to the "SharedParsedFormula" section of [MS-XLS] spec
194 * for more info.
196 bool IsValidTokenArray( const ScTokenArray& rArray ) const;
198 typedef std::unordered_map<const ScTokenArray*, XclExpShrfmlaRef> TokensType;
199 typedef std::unordered_set<const ScTokenArray*> BadTokenArraysType;
201 TokensType maRecMap; /// Map containing the SHRFMLA records.
202 BadTokenArraysType maBadTokens; /// shared tokens we should *not* export as SHRFMLA
205 // Multiple operations ========================================================
207 struct XclMultipleOpRefs;
209 /** Represents a TABLEOP record for a multiple operations range. */
210 class XclExpTableop : public XclExpRangeFmlaBase
212 public:
213 explicit XclExpTableop( const ScAddress& rScPos,
214 const XclMultipleOpRefs& rRefs, sal_uInt8 nScMode );
216 /** Returns true, if the cell range has been extended to the passed position.
217 @descr All references passed in rRefs must fit the ranges passed in the constructor. */
218 bool TryExtend( const ScAddress& rScPos, const XclMultipleOpRefs& rRefs );
220 /** Finalizes the record. Tests on valid cell range and reference addresses. */
221 void Finalize();
223 /** Creates and returns the token array for a corresponding FORMULA cell record. */
224 virtual XclTokenArrayRef CreateCellTokenArray( const XclExpRoot& rRoot ) const override;
225 /** Returns true, if the multiple operations range is volatile. */
226 virtual bool IsVolatile() const override;
227 /** Writes the record if it is valid. */
228 virtual void Save( XclExpStream& rStrm ) override;
230 private:
231 /** Returns true, if the passed cell position can be appended to this record. */
232 bool IsAppendable( sal_uInt16 nXclCol, sal_uInt16 nXclRow ) const;
234 /** Writes the contents of the TABLEOP record. */
235 virtual void WriteBody( XclExpStream& rStrm ) override;
237 private:
238 sal_uInt16 mnLastAppXclCol;/// Column index of last appended cell.
239 sal_uInt16 mnColInpXclCol; /// Column index of column input cell.
240 sal_uInt32 mnColInpXclRow; /// Row index of column input cell.
241 sal_uInt16 mnRowInpXclCol; /// Column index of row input cell.
242 sal_uInt32 mnRowInpXclRow; /// Row index of row input cell.
243 sal_uInt8 mnScMode; /// Type of the multiple operation (Calc constant).
244 bool mbValid; /// true = Contains valid references.
247 typedef std::shared_ptr< XclExpTableop > XclExpTableopRef;
249 /** Contains all created TABLEOP records and supports creating or updating them. */
250 class XclExpTableopBuffer : protected XclExpRoot
252 public:
253 explicit XclExpTableopBuffer( const XclExpRoot& rRoot );
255 /** Tries to update an existing or to create a new TABLEOP record.
256 @return Reference to the TABLEOP record for this cell (existing or new),
257 or an empty reference, if rScTokArr does not contain a multiple
258 operations formula. */
259 XclExpTableopRef CreateOrExtendTableop(
260 const ScTokenArray& rScTokArr, const ScAddress& rScPos );
262 /** Finalizes all contained TABLEOP records. */
263 void Finalize();
265 private:
266 /** Tries to create a new TABLEOP record, if rRefs contains valid references. */
267 XclExpTableopRef TryCreate( const ScAddress& rScPos, const XclMultipleOpRefs& rRefs );
269 private:
270 typedef XclExpRecordList< XclExpTableop > XclExpTableopList;
271 XclExpTableopList maTableopList; /// List of all TABLEOP records.
274 // Cell records
276 /** The base class of all cell records. */
277 class XclExpCellBase : public XclExpRecord
279 public:
280 /** Returns the (first) address of the cell(s). */
281 inline const XclAddress& GetXclPos() const { return maXclPos; }
282 /** Returns the (first) Excel column index of the cell(s). */
283 inline sal_uInt16 GetXclCol() const { return maXclPos.mnCol; }
284 /** Returns the Excel row index of the cell. */
285 inline sal_uInt32 GetXclRow() const { return maXclPos.mnRow; }
287 /** Derived classes return the column index of the last contained cell. */
288 virtual sal_uInt16 GetLastXclCol() const = 0;
289 /** Derived classes return the XF identifier of the first contained cell. */
290 virtual sal_uInt32 GetFirstXFId() const = 0;
291 /** Derived classes return true, if this record does not contain at least one valid cell. */
292 virtual bool IsEmpty() const = 0;
293 /** Derived classes return whether the cell contains multi-line text. */
294 virtual bool IsMultiLineText() const;
296 /** Derived classes try to merge the contents of the passed cell to own data. */
297 virtual bool TryMerge( const XclExpCellBase& rCell );
298 /** Derived classes convert the XF identifier(s) into the Excel XF index(es).
299 @param rXFIndexes The converted XF index(es) are inserted here. */
300 virtual void ConvertXFIndexes( const XclExpRoot& rRoot ) = 0;
301 /** Derived classes for blank cells insert the Excel XF index(es) into the passed vector. */
302 virtual void GetBlankXFIndexes( ScfUInt16Vec& rXFIndexes ) const;
303 /** Derived classes for blank cells remove unused Excel XF index(es). */
304 virtual void RemoveUnusedBlankCells( const ScfUInt16Vec& rXFIndexes );
306 protected:
307 explicit XclExpCellBase(
308 sal_uInt16 nRecId, sal_Size nContSize, const XclAddress& rXclPos );
310 /** Sets this record to a new column position. */
311 inline void SetXclCol( sal_uInt16 nXclCol ) { maXclPos.mnCol = nXclCol; }
313 private:
314 XclAddress maXclPos; /// Address of the cell.
317 typedef std::shared_ptr< XclExpCellBase > XclExpCellRef;
319 // Single cell records ========================================================
321 /** Base class for all cell records not supporting multiple contents. */
322 class XclExpSingleCellBase : public XclExpCellBase
324 public:
325 /** Returns the last column, which is equal to the first column for single cells. */
326 virtual sal_uInt16 GetLastXclCol() const override;
327 /** Return the XF identifier of the cell. */
328 virtual sal_uInt32 GetFirstXFId() const override;
329 /** Returns true, if this record does not contain at least one valid cell. */
330 virtual bool IsEmpty() const override;
331 /** Converts the XF identifier into the Excel XF index. */
332 virtual void ConvertXFIndexes( const XclExpRoot& rRoot ) override;
333 /** Writes cell address, XF index, and calls WriteContents() for each cell. */
334 virtual void Save( XclExpStream& rStrm ) override;
336 protected:
337 explicit XclExpSingleCellBase( sal_uInt16 nRecId, sal_Size nContSize,
338 const XclAddress& rXclPos, sal_uInt32 nXFId );
340 explicit XclExpSingleCellBase( const XclExpRoot& rRoot,
341 sal_uInt16 nRecId, sal_Size nContSize, const XclAddress& rXclPos,
342 const ScPatternAttr* pPattern, sal_Int16 nScript, sal_uInt32 nForcedXFId );
344 inline void SetContSize( sal_Size nContSize ) { mnContSize = nContSize; }
345 inline sal_Size GetContSize() const { return mnContSize; }
347 inline void SetXFId( sal_uInt32 nXFId ) { maXFId.mnXFId = nXFId; }
348 inline sal_uInt32 GetXFId() const { return maXFId.mnXFId; }
350 private:
351 /** Writes cell address, XF index, and calls WriteContents() for each cell. */
352 virtual void WriteBody( XclExpStream& rStrm ) override;
353 /** Derived classes write the contents of the specified cell (without XF index). */
354 virtual void WriteContents( XclExpStream& rStrm ) = 0;
356 private:
357 XclExpXFId maXFId; /// The XF identifier of the cell formatting.
358 sal_Size mnContSize; /// The size of the cell contents.
361 /** Represents a NUMBER record that describes a cell with a double value. */
362 class XclExpNumberCell : public XclExpSingleCellBase
364 DECL_FIXEDMEMPOOL_NEWDEL( XclExpNumberCell )
366 public:
367 explicit XclExpNumberCell( const XclExpRoot& rRoot, const XclAddress& rXclPos,
368 const ScPatternAttr* pPattern, sal_uInt32 nForcedXFId,
369 double fValue );
371 virtual void SaveXml( XclExpXmlStream& rStrm ) override;
372 private:
373 virtual void WriteContents( XclExpStream& rStrm ) override;
375 private:
376 double mfValue; /// The cell value.
379 /** Represents a BOOLERR record that describes a cell with a Boolean value. */
380 class XclExpBooleanCell : public XclExpSingleCellBase
382 DECL_FIXEDMEMPOOL_NEWDEL( XclExpBooleanCell )
384 public:
385 explicit XclExpBooleanCell( const XclExpRoot& rRoot, const XclAddress& rXclPos,
386 const ScPatternAttr* pPattern, sal_uInt32 nForcedXFId,
387 bool bValue );
389 virtual void SaveXml( XclExpXmlStream& rStrm ) override;
390 private:
391 virtual void WriteContents( XclExpStream& rStrm ) override;
393 private:
394 bool mbValue; /// The cell value.
397 class XclExpHyperlinkHelper;
398 class EditTextObject;
400 /** Represents a text cell record.
402 May contain a BIFF2-BIFF7 LABEL record for a simple string, or a BIFF2-BIFF7
403 RSTRING record for a formatted string, or a BIFF8 LABELSST string for any
404 string (simply stores a reference to the Shared String Table).
406 class XclExpLabelCell : public XclExpSingleCellBase
408 DECL_FIXEDMEMPOOL_NEWDEL( XclExpLabelCell )
410 public:
411 /** Constructs the record from an unformatted Calc string cell. */
412 explicit XclExpLabelCell( const XclExpRoot& rRoot, const XclAddress& rXclPos,
413 const ScPatternAttr* pPattern, sal_uInt32 nForcedXFId,
414 const OUString& rStr );
416 /** Constructs the record from a formatted Calc edit cell. */
417 explicit XclExpLabelCell( const XclExpRoot& rRoot, const XclAddress& rXclPos,
418 const ScPatternAttr* pPattern, sal_uInt32 nForcedXFId,
419 const EditTextObject* pEditText, XclExpHyperlinkHelper& rHlinkHelper );
421 /** Returns true if the cell contains multi-line text. */
422 virtual bool IsMultiLineText() const override;
424 virtual void SaveXml( XclExpXmlStream& rStrm ) override;
425 private:
426 /** Initializes the record contents. Called from constructors. */
427 void Init( const XclExpRoot& rRoot,
428 const ScPatternAttr* pPattern, XclExpStringRef xText );
430 virtual void WriteContents( XclExpStream& rStrm ) override;
432 private:
433 XclExpStringRef mxText; /// The cell text.
434 sal_uInt32 mnSstIndex; /// Index into Shared String Table (only used for BIFF8).
435 bool mbLineBreak; /// True = cell has automatic linebreaks enabled.
438 class ScFormulaCell;
440 /** Represents a FORMULA record that describes a cell with a formula. */
441 class XclExpFormulaCell : public XclExpSingleCellBase
443 DECL_FIXEDMEMPOOL_NEWDEL( XclExpFormulaCell )
445 public:
446 explicit XclExpFormulaCell( const XclExpRoot& rRoot, const XclAddress& rXclPos,
447 const ScPatternAttr* pPattern, sal_uInt32 nForcedXFId,
448 const ScFormulaCell& rScFmlaCell,
449 XclExpArrayBuffer& rArrayBfr,
450 XclExpShrfmlaBuffer& rShrfmlaBfr,
451 XclExpTableopBuffer& rTableopBfr );
453 /** Writes the FORMULA record and additional records related to the formula. */
454 virtual void Save( XclExpStream& rStrm ) override;
455 virtual void SaveXml( XclExpXmlStream& rStrm ) override;
457 private:
458 virtual void WriteContents( XclExpStream& rStrm ) override;
460 private:
461 ScFormulaCell& mrScFmlaCell; /// The Calc formula cell.
462 XclTokenArrayRef mxTokArr; /// The token array of the formula.
463 XclExpRangeFmlaRef mxAddRec; /// Additional record for matrix/shared formulas.
464 XclExpRecordRef mxStringRec; /// STRING record for string result.
467 // Multiple cell records ======================================================
469 struct XclExpMultiXFId : public XclExpXFId
471 sal_uInt16 mnCount; /// Number of XF identifiers.
473 inline explicit XclExpMultiXFId( sal_uInt32 nXFId, sal_uInt16 nCount = 1 ) :
474 XclExpXFId( nXFId ), mnCount( nCount ) {}
477 /** Base class for all cell records supporting multiple contents. */
478 class XclExpMultiCellBase : public XclExpCellBase
480 public:
481 /** Returns the column index of the last cell this record describes. */
482 virtual sal_uInt16 GetLastXclCol() const override;
483 /** Return the XF identifier of the first contained cell. */
484 virtual sal_uInt32 GetFirstXFId() const override;
485 /** Returns true, if this record does not contain at least one valid cell. */
486 virtual bool IsEmpty() const override;
488 /** Convert all XF identifiers into the Excel XF indexes. */
489 virtual void ConvertXFIndexes( const XclExpRoot& rRoot ) override;
490 /** Writes the record, calls WriteContents() for each contained cell.
491 @descr May write several records, if unused XF indexes are contained. */
492 virtual void Save( XclExpStream& rStrm ) override;
493 virtual void SaveXml( XclExpXmlStream& rStrm ) override;
495 protected:
496 explicit XclExpMultiCellBase( sal_uInt16 nRecId, sal_uInt16 nMulRecId,
497 sal_Size nContSize, const XclAddress& rXclPos );
499 /** Returns the number of cells this record represents. */
500 sal_uInt16 GetCellCount() const;
502 /** Appends the passed XF identifier nCount times to the list of XF identifiers. */
503 void AppendXFId( const XclExpMultiXFId& rXFId );
504 /** Appends the passed cell format nCount times to the list of XF identifiers. */
505 void AppendXFId( const XclExpRoot& rRoot,
506 const ScPatternAttr* pPattern, sal_uInt16 nScript,
507 sal_uInt32 nForcedXFId, sal_uInt16 nCount = 1 );
509 /** Tries to merge the XF ID list of the passed cell with the own list. */
510 bool TryMergeXFIds( const XclExpMultiCellBase& rCell );
511 /** Inserts the Excel XF index(es) into the passed vector. */
512 void GetXFIndexes( ScfUInt16Vec& rXFIndexes ) const;
514 /** Removes unused Excel XF index(es).
515 @param rXFIndexes Specifies which XF indexes are used. */
516 void RemoveUnusedXFIndexes( const ScfUInt16Vec& rXFIndexes );
518 private:
519 /** Derived classes write the remaining contents of the specified cell (without XF index).
520 @param nRelCol Relative column index (starts with 0 for first cell of this record). */
521 virtual void WriteContents( XclExpStream& rStrm, sal_uInt16 nRelCol ) = 0;
522 virtual void WriteXmlContents( XclExpXmlStream& rStrm, const XclAddress& rAddress, sal_uInt32 nXFId, sal_uInt16 nRelCol ) = 0;
524 private:
525 typedef ::std::vector< XclExpMultiXFId > XclExpMultiXFIdDeq;
527 sal_uInt16 mnMulRecId; /// Record ID for multiple record variant.
528 sal_Size mnContSize; /// Data size of contents for one cell
529 XclExpMultiXFIdDeq maXFIds; /// The XF identifiers of the cell formatting.
532 /** Represents a BLANK or MULBLANK record that describes empty but formatted cells. */
533 class XclExpBlankCell : public XclExpMultiCellBase
535 DECL_FIXEDMEMPOOL_NEWDEL( XclExpBlankCell )
537 public:
538 explicit XclExpBlankCell( const XclAddress& rXclPos, const XclExpMultiXFId& rXFId );
540 explicit XclExpBlankCell( const XclExpRoot& rRoot,
541 const XclAddress& rXclPos, sal_uInt16 nLastXclCol,
542 const ScPatternAttr* pPattern, sal_uInt32 nForcedXFId );
544 /** Tries to merge the contents of the passed cell to own data. */
545 virtual bool TryMerge( const XclExpCellBase& rCell ) override;
546 /** Inserts the Excel XF index(es) into the passed vector. */
547 virtual void GetBlankXFIndexes( ScfUInt16Vec& rXFIndexes ) const override;
548 /** Tries to remove unused Excel XF index(es). */
549 virtual void RemoveUnusedBlankCells( const ScfUInt16Vec& rXFIndexes ) override;
551 private:
552 /** Writes the remaining contents of the specified cell (without XF index). */
553 virtual void WriteContents( XclExpStream& rStrm, sal_uInt16 nRelCol ) override;
554 virtual void WriteXmlContents( XclExpXmlStream& rStrm, const XclAddress& rAddress, sal_uInt32 nXFId, sal_uInt16 nRelCol ) override;
557 /** Represents an RK or MULRK record that describes cells with a compressed double values. */
558 class XclExpRkCell : public XclExpMultiCellBase
560 DECL_FIXEDMEMPOOL_NEWDEL( XclExpRkCell )
562 public:
563 explicit XclExpRkCell( const XclExpRoot& rRoot, const XclAddress& rXclPos,
564 const ScPatternAttr* pPattern, sal_uInt32 nForcedXFId,
565 sal_Int32 nRkValue );
567 /** Tries to merge the contents of the passed cell to own data. */
568 virtual bool TryMerge( const XclExpCellBase& rCell ) override;
570 private:
571 /** Writes the remaining contents of the specified cell (without XF index). */
572 virtual void WriteContents( XclExpStream& rStrm, sal_uInt16 nRelCol ) override;
573 virtual void WriteXmlContents( XclExpXmlStream& rStrm, const XclAddress& rAddress, sal_uInt32 nXFId, sal_uInt16 nRelCol ) override;
575 private:
576 ScfInt32Vec maRkValues; /// The cell values.
579 // Rows and Columns
581 class ScOutlineArray;
583 /** Base class for buffers containing row or column outline data. */
584 class XclExpOutlineBuffer
586 public:
587 /** Returns true, if a collapsed group ends at the last processed position. */
588 inline bool IsCollapsed() const { return mbCurrCollapse; }
589 /** Returns the highest level of an open group at the last processed position. */
590 inline sal_uInt8 GetLevel() const { return ::std::min( mnCurrLevel, EXC_OUTLINE_MAX ); }
592 protected:
593 /** Constructs the outline buffer.
594 @param bRows true = Process row outline array; false = Process column outline array. */
595 explicit XclExpOutlineBuffer( const XclExpRoot& rRoot, bool bRows );
597 /** Updates the current state by processing the settings at the passed Calc position. */
598 void UpdateColRow( SCCOLROW nScPos );
600 private:
601 /** Data about an outline level. */
602 struct XclExpLevelInfo
604 SCCOLROW mnScEndPos; /// The end position of a group in a level.
605 bool mbHidden; /// true = Group in this level is hidden.
606 inline explicit XclExpLevelInfo() : mnScEndPos( 0 ), mbHidden( false ) {}
608 typedef ::std::vector< XclExpLevelInfo > XclExpLevelInfoVec;
610 const ScOutlineArray* mpScOLArray; /// Pointer to Calc outline array.
611 XclExpLevelInfoVec maLevelInfos; /// Info for current row and all levels.
612 sal_uInt8 mnCurrLevel; /// Highest level of an open group for current position.
613 bool mbCurrCollapse; /// true = Collapsed group ends at current position.
616 /** The outline buffer for column outlines. */
617 class XclExpColOutlineBuffer : public XclExpOutlineBuffer
619 public:
620 inline explicit XclExpColOutlineBuffer( const XclExpRoot& rRoot ) :
621 XclExpOutlineBuffer( rRoot, false ) {}
623 /** Updates the current state by processing the settings of the passed Calc column. */
624 inline void Update( SCCOL nScCol )
625 { UpdateColRow( static_cast< SCCOLROW >( nScCol ) ); }
628 /** The outline buffer for row outlines. */
629 class XclExpRowOutlineBuffer : public XclExpOutlineBuffer
631 public:
632 inline explicit XclExpRowOutlineBuffer( const XclExpRoot& rRoot ) :
633 XclExpOutlineBuffer( rRoot, true ) {}
635 /** Updates the current state by processing the settings of the passed Calc row. */
636 inline void Update( SCROW nScRow )
637 { UpdateColRow( static_cast< SCCOLROW >( nScRow ) ); }
640 /** Represents a GUTS record containing the level count of row and column outlines. */
641 class XclExpGuts : public XclExpRecord
643 public:
644 explicit XclExpGuts( const XclExpRoot& rRoot );
646 private:
647 virtual void WriteBody( XclExpStream& rStrm ) override;
649 private:
650 sal_uInt16 mnColLevels; /// Number of visible column outline levels.
651 sal_uInt16 mnColWidth; /// Width of column outline area (pixels).
652 sal_uInt16 mnRowLevels; /// Number of visible row outline levels.
653 sal_uInt16 mnRowWidth; /// Width of row outline area (pixels).
656 /** Represents a DIMENSIONS record containing the used area of a sheet. */
657 class XclExpDimensions : public XclExpRecord
659 public:
660 explicit XclExpDimensions( const XclExpRoot& rRoot );
662 /** Sets the used area to the record. */
663 void SetDimensions(
664 sal_uInt16 nFirstUsedXclCol, sal_uInt32 nFirstUsedXclRow,
665 sal_uInt16 nFirstFreeXclCol, sal_uInt32 nFirstFreeXclRow );
667 virtual void SaveXml( XclExpXmlStream& rStrm ) override;
668 private:
669 /** Writes the contents of the DIMENSIONS record. */
670 virtual void WriteBody( XclExpStream& rStrm ) override;
672 private:
673 sal_uInt32 mnFirstUsedXclRow; /// First used row.
674 sal_uInt32 mnFirstFreeXclRow; /// First unused row after used area.
675 sal_uInt16 mnFirstUsedXclCol; /// First used column.
676 sal_uInt16 mnFirstFreeXclCol; /// First free column after used area.
679 /** Represents the DEFCOLWIDTH record containing the default column width of a sheet.
681 Excel stores the default column width in entire character widths of the '0'
682 character using the application default font (i.e. the default width is 10,
683 if the '0' character fits 10 times into a cell in a column with default
684 width.
686 Half of character width is reserved for non character display.
687 It is margin padding (two on each side) and padding for the gridlines.
689 The IsDefWidth() function returns true, if the passed width (measured in
690 1/256 of the width of the '0' character) could be converted exactly to the
691 default width. If the passed width is rounded up or down to get the default
692 width, the function returns false.
694 class XclExpDefcolwidth : public XclExpUInt16Record, protected XclExpRoot
696 public:
697 explicit XclExpDefcolwidth( const XclExpRoot& rRoot );
699 /** Returns true, if the own default width exactly matches the passed width. */
700 bool IsDefWidth( sal_uInt16 nXclColWidth ) const;
702 /** Sets the passed column width (in 1/256 character width) as default width. */
703 void SetDefWidth( sal_uInt16 nXclColWidth );
706 /** Contains the column settings for a range of columns.
708 After construction the record contains a temporary XF identifier returned
709 from the XF buffer. After creating the entire Excel document in memory, the
710 ConvertXFIndexes() function converts it into the real Excel XF index.
712 class XclExpColinfo : public XclExpRecord, protected XclExpRoot
714 public:
715 /** Constructs the record with the settings in the Calc document. */
716 explicit XclExpColinfo( const XclExpRoot& rRoot,
717 SCCOL nScCol, SCROW nLastScRow,
718 XclExpColOutlineBuffer& rOutlineBfr );
720 /** Converts the XF identifier into the Excel XF index. */
721 void ConvertXFIndexes();
723 /** Tries to merge this record with the passed record.
724 @descr Possible, if passed record directly follows this record and has equal contents.
725 @return true = This record is equal to passed record and has been updated. */
726 bool TryMerge( const XclExpColinfo& rColInfo );
728 /** Returns the Excel width of the column(s). */
729 inline sal_uInt16 GetColWidth() const { return mnWidth; }
730 /** Returns the final Excel XF index of the column(s). */
731 inline sal_uInt16 GetXFIndex() const { return maXFId.mnXFIndex; }
732 /** Returns the number of columns represented by this record. */
733 inline sal_uInt16 GetColCount() const { return mnLastXclCol - mnFirstXclCol + 1; }
735 /** Returns true, if the column has default format and width. */
736 bool IsDefault( const XclExpDefcolwidth& rDefColWidth ) const;
738 virtual void SaveXml( XclExpXmlStream& rStrm ) override;
740 private:
741 /** Writes the contents of this COLINFO record. */
742 virtual void WriteBody( XclExpStream& rStrm ) override;
744 private:
745 XclExpXFId maXFId; /// The XF identifier for column default format.
746 bool mbCustomWidth; /// True = Column width is different from default width
747 sal_uInt16 mnWidth; /// Excel width of the column.
748 sal_uInt16 mnScWidth; /// Calc width of the column.
749 sal_uInt16 mnFlags; /// Additional column flags.
750 sal_uInt8 mnOutlineLevel; /// Outline Level of column (for OOXML)
751 sal_uInt16 mnFirstXclCol; /// Index to first column.
752 sal_uInt16 mnLastXclCol; /// Index to last column.
755 /** Contains COLINFO records for all columns of a Calc sheet.
757 On construction one COLINFO record per column is created. After creating
758 the entire Excel document in memory, the ConvertXFIndexes() function converts
759 all temporary XF identifiers into real Excel XF indexes and merges all equal
760 COLINFO records together.
762 class XclExpColinfoBuffer : public XclExpRecordBase, protected XclExpRoot
764 public:
765 explicit XclExpColinfoBuffer( const XclExpRoot& rRoot );
767 /** Initializes the buffer: finds settings and formatting of all columns.
768 @param nLastScRow Last row used to find default formatting. */
769 void Initialize( SCROW nLastScRow );
770 /** Converts the XF identifiers into the Excel XF indexes and merges equal columns.
771 @param rXFIndexes Returns the final XF indexes of all columns. */
772 void Finalize( ScfUInt16Vec& rXFIndexes );
774 /** Writes all COLINFO records of this buffer. */
775 virtual void Save( XclExpStream& rStrm ) override;
776 virtual void SaveXml( XclExpXmlStream& rStrm ) override;
777 sal_uInt8 GetHighestOutlineLevel() { return maHighestOutlineLevel; }
779 private:
780 typedef XclExpRecordList< XclExpColinfo > XclExpColinfoList;
781 typedef XclExpColinfoList::RecordRefType XclExpColinfoRef;
783 XclExpColinfoList maColInfos; /// List of COLINFO records.
784 XclExpDefcolwidth maDefcolwidth; /// The DEFCOLWIDTH record.
785 XclExpColOutlineBuffer maOutlineBfr; /// Buffer for column outline groups.
786 sal_uInt8 maHighestOutlineLevel; /// Highest number of outline levels for columns in sheet.
789 class XclExpRow;
791 /** Contains all possible default row settings. */
792 struct XclExpDefaultRowData
794 sal_uInt16 mnFlags; /// Default flags for unspecified rows.
795 sal_uInt16 mnHeight; /// Default height for unspecified rows.
797 explicit XclExpDefaultRowData();
798 explicit XclExpDefaultRowData( const XclExpRow& rRow );
800 /** Returns true, if rows are hidden by default. */
801 inline bool IsHidden() const { return ::get_flag( mnFlags, EXC_DEFROW_HIDDEN ); }
802 /** Returns true, if the rows have a manually set height by default. */
803 inline bool IsUnsynced() const { return ::get_flag( mnFlags, EXC_DEFROW_UNSYNCED ); }
806 /** Represents a DEFROWHEIGHT record containing default format for unused rows. */
807 class XclExpDefrowheight : public XclExpRecord
809 public:
810 explicit XclExpDefrowheight();
812 /** Sets the passed default data as current record contents. */
813 void SetDefaultData( const XclExpDefaultRowData& rDefData );
814 XclExpDefaultRowData& GetDefaultData() { return maDefData; }
815 private:
816 /** Writes the contents of the record. */
817 virtual void WriteBody( XclExpStream& rStrm ) override;
819 private:
820 XclExpDefaultRowData maDefData; /// Record data.
823 /** Represents a ROW record and additionally contains all cells records of a row.
825 This class contains all cell records of a row in a spreadsheet. There are 2
826 cell records in Excel that support storing a range of cells in one record
827 (MULBLANK for multiple blank cells, and MULRK for multiple RK values). The
828 insertion functions try to merge a new inserted cell with existing
829 neighbors, if this is supported by the current type of cell record.
831 The Finalize() function converts the XF identifiers of all cell records to
832 the final Excel XF indexes. Then a default
834 class XclExpRow : public XclExpRecord, protected XclExpRoot
836 public:
837 /** Constructs the ROW record and converts the Calc row settings.
838 @param bAlwaysEmpty true = This row will not be filled with blank cells
839 in the Finalize() function. */
840 explicit XclExpRow( const XclExpRoot& rRoot, sal_uInt32 nXclRow,
841 XclExpRowOutlineBuffer& rOutlineBfr, bool bAlwaysEmpty );
843 /** Returns the excel row index of this ROW record. */
844 inline sal_uInt32 GetXclRow() const { return mnXclRow; }
845 /** Returns the height of the row in twips. */
846 inline sal_uInt16 GetHeight() const { return mnHeight; }
847 /** Returns true, if this row does not contain at least one valid cell. */
848 inline bool IsEmpty() const { return maCellList.IsEmpty(); }
849 /** Returns true, if this row is hidden. */
850 inline bool IsHidden() const { return ::get_flag( mnFlags, EXC_ROW_HIDDEN ); }
851 /** Returns true, if this row contains a manually set height. */
852 inline bool IsUnsynced() const { return ::get_flag( mnFlags, EXC_ROW_UNSYNCED ); }
853 /** Returns true, if this row is enabled (will be exported). */
854 inline bool IsEnabled() const { return mbEnabled; }
856 /** Appends the passed cell object to this row. */
857 void AppendCell( XclExpCellRef xCell, bool bIsMergedBase );
859 /** Converts all XF identifiers into the Excel XF indexes. */
860 void Finalize( const ScfUInt16Vec& rColXFIndexes,
861 bool bUpdateProgress );
863 /** Returns the column index of the first used cell in this row.
864 @descr This function can only be called after Finalize(). */
865 sal_uInt16 GetFirstUsedXclCol() const;
866 /** Returns the column index of the first unused cell following all used cells in this row.
867 @descr This function can only be called after Finalize(). */
868 sal_uInt16 GetFirstFreeXclCol() const;
870 /** Returns true, if this row may be omitted by using the DEFROWHEIGHT record.
871 @descr A row may be omitted, if it does not contain any cell or
872 explicit default cell formatting, and is not part of an outline.
873 This function can only be called after Finalize(). */
874 bool IsDefaultable() const;
875 /** Disables this row, if it is defaultable and has the passed default format.
876 @descr Disabled rows will not be saved.
877 This function can only be called after Finalize(). */
878 void DisableIfDefault( const XclExpDefaultRowData& rDefRowData );
880 /** Writes all cell records of this row. */
881 void WriteCellList( XclExpStream& rStrm );
883 /** Writes the ROW record if the row is not disabled (see DisableIfDefault() function). */
884 virtual void Save( XclExpStream& rStrm ) override;
885 virtual void SaveXml( XclExpXmlStream& rStrm ) override;
887 inline sal_uInt32 GetXclRowRpt() const { return mnXclRowRpt; }
888 inline void SetXclRowRpt( sal_uInt32 nRpt ){ mnXclRowRpt = nRpt; }
889 private:
890 /** Inserts a cell at the specified list position, tries to merge with neighbors. */
891 void InsertCell( XclExpCellRef xCell, size_t nPos, bool bIsMergedBase );
893 /** Writes the contents of the ROW record. */
894 virtual void WriteBody( XclExpStream& rStrm ) override;
896 private:
897 typedef XclExpRecordList< XclExpCellBase > XclExpCellList;
899 XclExpCellList maCellList; /// List of cell records for this row.
900 sal_uInt32 mnXclRow; /// Excel row index of this row.
901 sal_uInt16 mnHeight; /// Row height in twips.
902 sal_uInt16 mnFlags; /// Flags for the ROW record.
903 sal_uInt16 mnXFIndex; /// Default row formatting.
904 sal_uInt8 mnOutlineLevel; /// Outline Level of row (for OOXML)
905 sal_uInt32 mnXclRowRpt;
906 sal_uInt32 mnCurrentRow;
907 bool mbAlwaysEmpty; /// true = Do not add blank cells in Finalize().
908 bool mbEnabled; /// true = Write this ROW record.
911 /** Collects all rows which contain all cells of a sheet.
913 This row buffer automatically creates ROW records when cells are inserted
914 with the AppendCell() function. It is possible to force creation of more
915 ROW records with the CreateRows() function. In both cases, all preceding
916 missing ROW records are inserted too.
918 class XclExpRowBuffer : public XclExpRecordBase, protected XclExpRoot
920 public:
921 explicit XclExpRowBuffer( const XclExpRoot& rRoot );
923 /** Appends the passed cell object to the row that the cell specifies. */
924 void AppendCell( XclExpCellRef xCell, bool bIsMergedBase );
925 /** Forces insertion of all ROW records before the passed row. */
926 void CreateRows( SCROW nFirstFreeScRow );
928 /** Converts all XF identifiers into the Excel XF indexes and calculates default formats.
929 @param rDefRowData (out-param) The default row format is returned here.
930 @param rColXFIndexes The column default XF indexes. */
931 void Finalize( XclExpDefaultRowData& rDefRowData, const ScfUInt16Vec& rColXFIndexes );
933 /** Writes the DIMENSIONS record, all ROW records and all cell records. */
934 virtual void Save( XclExpStream& rStrm ) override;
935 virtual void SaveXml( XclExpXmlStream& rStrm ) override;
937 XclExpDimensions& GetDimensions() { return maDimensions; }
938 sal_uInt8 GetHighestOutlineLevel() { return maHighestOutlineLevel; }
940 private:
941 /** Returns access to the specified ROW record. Inserts preceding missing ROW records.
942 @param bRowAlwaysEmpty true = Created rows will not be filled with blank cells
943 in the XclExpRow::Finalize() function. */
944 XclExpRow& GetOrCreateRow( sal_uInt32 nXclRow, bool bRowAlwaysEmpty );
946 private:
947 typedef std::shared_ptr<XclExpRow> RowRef;
948 typedef ::std::map<sal_uInt32, RowRef> RowMap;
950 RowMap maRowMap;
951 XclExpRowOutlineBuffer maOutlineBfr; /// Buffer for row outline groups.
952 XclExpDimensions maDimensions; /// DIMENSIONS record for used area.
953 sal_uInt8 maHighestOutlineLevel; /// Highest number of outline levels for rows in sheet.
956 // Cell Table
958 class XclExpNote;
959 class XclExpMergedcells;
960 class XclExpHyperlink;
961 class XclExpDval;
963 /** This class contains the cell contents and more of an entire sheet.
965 The cell table includes the settings and default formatting of all columns,
966 the settings and default formatting of all used rows, and the contents of
967 all cells of one sheet in a spreadsheet document.
969 The constructor does all the work creating the cell table. It reads the
970 Calc sheet and converts all columns, rows, and cells to Excel record data.
971 Additionally, hyperlink records, note records, additional records for
972 formula cells, data validation records, and outline records are created.
974 The Finalize() function does even more work. It calculates default column
975 settings and removes column records that are equal to this default. The
976 same happens with rows: A default format is calculated for each row, and
977 all blank cells in this row that have the same format are removed. Then,
978 the most used row settings are calculated, and all empty rows that have the
979 same settings are removed too.
981 Records that are not stored inside the cell table area in an Excel file
982 (i.e. DEFROWHEIGHT record, NOTE records, MERGEDCELLS record, HLINK records,
983 DVAL and DV records for data validation) can be accessed with the function
984 CreateRecord(). It returns the reference to the respective record (or
985 record list) which can be inserted into a record list.
987 class XclExpCellTable : public XclExpRecordBase, protected XclExpRoot
989 public:
990 explicit XclExpCellTable( const XclExpRoot& rRoot );
992 /** Converts all XF identifiers into the Excel XF indexes and calculates default formats. */
993 void Finalize();
995 /** Returns the reference to an internal record specified by the passed record id.
996 @param nRecId The record identifier that specifies which record is
997 returned. Possible values are: EXC_ID_DEFROWHEIGHT, EXC_ID_NOTE,
998 EXC_ID_MERGEDCELLS, EXC_ID_HLINK, EXC_ID_DVAL. */
999 XclExpRecordRef CreateRecord( sal_uInt16 nRecId ) const;
1000 /** Saves the entire cell table. */
1001 virtual void Save( XclExpStream& rStrm ) override;
1002 virtual void SaveXml( XclExpXmlStream& rStrm ) override;
1004 private:
1005 typedef XclExpRecordList< XclExpNote > XclExpNoteList;
1006 typedef XclExpRecordList< XclExpHyperlink > XclExpHyperlinkList;
1008 typedef std::shared_ptr< XclExpDefrowheight > XclExpDefrowhRef;
1009 typedef std::shared_ptr< XclExpNoteList > XclExpNoteListRef;
1010 typedef std::shared_ptr< XclExpMergedcells > XclExpMergedcellsRef;
1011 typedef std::shared_ptr< XclExpHyperlinkList > XclExpHyperlinkRef;
1012 typedef std::shared_ptr< XclExpDval > XclExpDvalRef;
1013 typedef std::shared_ptr< XclExtLst > XclExtLstRef;
1015 XclExpColinfoBuffer maColInfoBfr; /// Buffer for column formatting.
1016 XclExpRowBuffer maRowBfr; /// Rows and cell records.
1017 XclExpArrayBuffer maArrayBfr; /// Buffer for ARRAY records.
1018 XclExpShrfmlaBuffer maShrfmlaBfr; /// Buffer for SHRFMLA records.
1019 XclExpTableopBuffer maTableopBfr; /// Buffer for TABLEOP records.
1020 XclExpDefrowhRef mxDefrowheight; /// DEFROWHEIGHT record for default row format.
1021 XclExpRecordRef mxGuts; /// GUTS record for outline areas.
1022 XclExpNoteListRef mxNoteList; /// List of NOTE records.
1023 XclExpMergedcellsRef mxMergedcells; /// MERGEDCELLS record for merged cell ranges.
1024 XclExpHyperlinkRef mxHyperlinkList; /// List of HLINK records.
1025 XclExpDvalRef mxDval; /// Data validation with DVAL and DV records.
1026 XclExtLstRef mxExtLst;
1029 #endif
1031 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */