1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: xipivot.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef SC_XIPIVOT_HXX
32 #define SC_XIPIVOT_HXX
35 #include "xlpivot.hxx"
39 class ScDPSaveDimension
;
41 // ============================================================================
43 // ============================================================================
45 /** Represents a data item in a pivot cache. */
46 class XclImpPCItem
: public XclPCItem
49 explicit XclImpPCItem( XclImpStream
& rStrm
);
51 /** Inserts the item data into the passed document. */
52 void WriteToSource( const XclImpRoot
& rRoot
, const ScAddress
& rScPos
) const;
55 /** Reads an SXDOUBLE record describing a floating-point item. */
56 void ReadSxdouble( XclImpStream
& rStrm
);
57 /** Reads an SXBOOLEAN record describing a boolean item. */
58 void ReadSxboolean( XclImpStream
& rStrm
);
59 /** Reads an SXERROR record describing an error code item. */
60 void ReadSxerror( XclImpStream
& rStrm
);
61 /** Reads an SXINTEGER record describing an integer item. */
62 void ReadSxinteger( XclImpStream
& rStrm
);
63 /** Reads an SXSTRING record describing a text item. */
64 void ReadSxstring( XclImpStream
& rStrm
);
65 /** Reads an SXDATETIME record describing a date/time item. */
66 void ReadSxdatetime( XclImpStream
& rStrm
);
67 /** Reads an SXEMPTY record describing an empty item. */
68 void ReadSxempty( XclImpStream
& rStrm
);
71 typedef ScfRef
< XclImpPCItem
> XclImpPCItemRef
;
73 // ============================================================================
75 struct ScDPNumGroupInfo
;
76 class XclImpPivotCache
;
78 /** Represents a field in a pivot cache (a column of data items in the source area). */
79 class XclImpPCField
: public XclPCField
, protected XclImpRoot
82 /** Creates a pivot cache field by reading an SXFIELD record. */
83 explicit XclImpPCField( const XclImpRoot
& rRoot
,
84 XclImpPivotCache
& rPCache
, sal_uInt16 nFieldIdx
);
85 virtual ~XclImpPCField();
87 // general field/item access ----------------------------------------------
89 /** Returns the name of the field, uses the passed visible name if supported. */
90 const String
& GetFieldName( const ScfStringVec
& rVisNames
) const;
92 /** Returns the base field if this is a grouping field. */
93 const XclImpPCField
* GetGroupBaseField() const;
95 /** Returns the number of items of this field. */
96 sal_uInt16
GetItemCount() const;
97 /** Returns the item at the specified position or 0 on error. */
98 const XclImpPCItem
* GetItem( sal_uInt16 nItemIdx
) const;
99 /** Returns the item representing a limit value in numeric/date/time grouping fields.
100 @param nItemIdx One of EXC_SXFIELD_INDEX_MIN, EXC_SXFIELD_INDEX_MAX, or EXC_SXFIELD_INDEX_STEP. */
101 const XclImpPCItem
* GetLimitItem( sal_uInt16 nItemIdx
) const;
103 /** Inserts the field name into the document. */
104 void WriteFieldNameToSource( SCCOL nScCol
, SCTAB nScTab
) const;
105 /** Inserts the specified item data into the document. */
106 void WriteOrigItemToSource( SCROW nScRow
, SCTAB nScTab
, sal_uInt16 nItemIdx
) const;
107 /** Inserts the data of the last inserted item into the document. */
108 void WriteLastOrigItemToSource( SCROW nScRow
, SCTAB nScTab
) const;
110 // records ----------------------------------------------------------------
112 /** Reads the SXFIELD record describing the field. */
113 void ReadSxfield( XclImpStream
& rStrm
);
114 /** Reads an item data record describing a new item. */
115 void ReadItem( XclImpStream
& rStrm
);
116 /** Reads the SXNUMGROUP record describing numeric grouping fields. */
117 void ReadSxnumgroup( XclImpStream
& rStrm
);
118 /** Reads the SXGROUPINFO record describing the item order in grouping fields. */
119 void ReadSxgroupinfo( XclImpStream
& rStrm
);
121 // grouping ---------------------------------------------------------------
123 /** Inserts grouping information of this field into the passed ScDPSaveData. */
124 void ConvertGroupField( ScDPSaveData
& rSaveData
, const ScfStringVec
& rVisNames
) const;
126 // ------------------------------------------------------------------------
128 /** Inserts standard grouping information of this field into the passed ScDPSaveData. */
129 void ConvertStdGroupField( ScDPSaveData
& rSaveData
, const ScfStringVec
& rVisNames
) const;
130 /** Inserts numeric grouping information of this field into the passed ScDPSaveData. */
131 void ConvertNumGroupField( ScDPSaveData
& rSaveData
, const ScfStringVec
& rVisNames
) const;
132 /** Inserts date grouping information of this field into the passed ScDPSaveData. */
133 void ConvertDateGroupField( ScDPSaveData
& rSaveData
, const ScfStringVec
& rVisNames
) const;
135 /** Returns a Calc struct with numeric grouping data. */
136 ScDPNumGroupInfo
GetScNumGroupInfo() const;
137 /** Returns a Calc struct with date grouping data. */
138 ScDPNumGroupInfo
GetScDateGroupInfo() const;
140 /** Returns a limit value for numeric grouping fields. */
141 const double* GetNumGroupLimit( sal_uInt16 nLimitIdx
) const;
142 /** Returns a limit value for date grouping fields (minimum/maximum only). */
143 const DateTime
* GetDateGroupLimit( sal_uInt16 nLimitIdx
) const;
144 /** Returns the step value for date grouping fields. */
145 const sal_Int16
* GetDateGroupStep() const;
148 typedef ::std::vector
< XclImpPCItemRef
> XclImpPCItemVec
;
150 XclImpPivotCache
& mrPCache
; /// Parent pivot cache containing this field.
151 XclImpPCItemVec maItems
; /// List of all displayed data items.
152 XclImpPCItemVec maOrigItems
; /// List of all source data items.
153 XclImpPCItemVec maNumGroupItems
; /// List of items containing numeric grouping limits.
154 mutable SCCOL mnSourceScCol
; /// Column index of source data for this field.
155 bool mbNumGroupInfoRead
; /// true = Numeric grouping info read (SXNUMGROUP record).
158 typedef ScfRef
< XclImpPCField
> XclImpPCFieldRef
;
160 // ============================================================================
162 class XclImpPivotCache
: protected XclImpRoot
165 explicit XclImpPivotCache( const XclImpRoot
& rRoot
);
166 virtual ~XclImpPivotCache();
168 // data access ------------------------------------------------------------
170 /** Returns the data source range read from the DCONREF record. */
171 inline const ScRange
& GetSourceRange() const { return maSrcRange
; }
173 /** Returns the number of pivot cache fields. */
174 sal_uInt16
GetFieldCount() const;
175 /** Returns read-only access to a pivot cache field. */
176 const XclImpPCField
* GetField( sal_uInt16 nFieldIdx
) const;
178 // records ----------------------------------------------------------------
180 /** Reads an SXIDSTM record containing a pivot cache stream identifier and the pivot cache. */
181 void ReadSxidstm( XclImpStream
& rStrm
);
182 /** Reads an SXVS record containing the source type of the pivot cache. */
183 void ReadSxvs( XclImpStream
& rStrm
);
184 /** Reads a DCONREF record containing the source range of the pivot cache. */
185 void ReadDconref( XclImpStream
& rStrm
);
186 /** Reads the entire pivot cache stream. Uses decrypter from passed stream. */
187 void ReadPivotCacheStream( XclImpStream
& rStrm
);
189 bool IsRefreshOnLoad() const;
192 typedef ::std::vector
< XclImpPCFieldRef
> XclImpPCFieldVec
;
194 XclPCInfo maPCInfo
; /// Pivot cache settings (SXDB record).
195 XclImpPCFieldVec maFields
; /// List of pivot cache fields.
196 ScRange maSrcRange
; /// Source range in the spreadsheet.
197 String maUrl
; /// URL of the source data.
198 String maTabName
; /// Sheet name of the source data.
199 sal_uInt16 mnStrmId
; /// Pivot cache stream identifier.
200 sal_uInt16 mnSrcType
; /// Source data type.
201 bool mbSelfRef
; /// true = Source data from own document.
204 typedef ScfRef
< XclImpPivotCache
> XclImpPivotCacheRef
;
206 // ============================================================================
208 // ============================================================================
210 class XclImpPivotTable
;
212 // ============================================================================
217 explicit XclImpPTItem( const XclImpPCField
* pCacheField
);
219 /** Returns the internal name of the item or 0, if no name could be found. */
220 const String
* GetItemName() const;
221 /** Returns the displayed name of the item or 0, if no name could be found. */
222 const String
* GetVisItemName() const;
224 /** Reads an SXVI record containing data of this item. */
225 void ReadSxvi( XclImpStream
& rStrm
);
227 /** Inserts this item into the passed ScDPSaveDimension. */
228 void ConvertItem( ScDPSaveDimension
& rSaveDim
) const;
231 XclPTItemInfo maItemInfo
; /// General data for this item.
232 const XclImpPCField
* mpCacheField
; /// Corresponding pivot cache field.
235 typedef ScfRef
< XclImpPTItem
> XclImpPTItemRef
;
237 // ============================================================================
242 explicit XclImpPTField( const XclImpPivotTable
& rPTable
, sal_uInt16 nCacheIdx
);
244 // general field/item access ----------------------------------------------
246 /** Returns the corresponding pivot cache field of this field. */
247 const XclImpPCField
* GetCacheField() const;
248 /** Returns the name of this field that is used to create the Calc dimensions. */
249 const String
& GetFieldName() const;
250 /** Returns the internally set visible name of this field. */
251 const String
& GetVisFieldName() const;
253 /** Returns the specified item. */
254 const XclImpPTItem
* GetItem( sal_uInt16 nItemIdx
) const;
255 /** Returns the internal name of the specified item. */
256 const String
* GetItemName( sal_uInt16 nItemIdx
) const;
257 /** Returns the displayed name of the specified item. */
258 const String
* GetVisItemName( sal_uInt16 nItemIdx
) const;
260 /** Returns the flags of the axes this field is part of. */
261 inline sal_uInt16
GetAxes() const { return maFieldInfo
.mnAxes
; }
262 /** Sets the flags of the axes this field is part of. */
263 inline void SetAxes( sal_uInt16 nAxes
) { maFieldInfo
.mnAxes
= nAxes
; }
265 // records ----------------------------------------------------------------
267 /** Reads an SXVD record describing the field. */
268 void ReadSxvd( XclImpStream
& rStrm
);
269 /** Reads an SXVDEX record describing extended options of the field. */
270 void ReadSxvdex( XclImpStream
& rStrm
);
271 /** Reads an SXVI record describing a new item of this field. */
272 void ReadSxvi( XclImpStream
& rStrm
);
274 // row/column fields ------------------------------------------------------
276 void ConvertRowColField( ScDPSaveData
& rSaveData
) const;
278 // page fields ------------------------------------------------------------
280 void SetPageFieldInfo( const XclPTPageFieldInfo
& rPageInfo
);
281 void ConvertPageField( ScDPSaveData
& rSaveData
) const;
283 // hidden fields ----------------------------------------------------------
285 void ConvertHiddenField( ScDPSaveData
& rSaveData
) const;
287 // data fields ------------------------------------------------------------
289 bool HasDataFieldInfo() const;
290 void AddDataFieldInfo( const XclPTDataFieldInfo
& rDataInfo
);
291 void ConvertDataField( ScDPSaveData
& rSaveData
) const;
293 // ------------------------------------------------------------------------
295 ScDPSaveDimension
* ConvertRCPField( ScDPSaveData
& rSaveData
) const;
296 void ConvertFieldInfo( ScDPSaveDimension
& rSaveDim
) const;
298 void ConvertDataField( ScDPSaveDimension
& rSaveDim
, const XclPTDataFieldInfo
& rDataInfo
) const;
299 void ConvertDataFieldInfo( ScDPSaveDimension
& rSaveDim
, const XclPTDataFieldInfo
& rDataInfo
) const;
300 void ConvertItems( ScDPSaveDimension
& rSaveDim
) const;
303 typedef ::std::list
< XclPTDataFieldInfo
> XclPTDataFieldInfoList
;
304 typedef ::std::vector
< XclImpPTItemRef
> XclImpPTItemVec
;
306 const XclImpPivotTable
& mrPTable
; /// Parent pivot table containing this field.
307 XclPTFieldInfo maFieldInfo
; /// General field info (SXVD record).
308 XclPTFieldExtInfo maFieldExtInfo
; /// Extended field info (SXVDEX record).
309 XclPTPageFieldInfo maPageInfo
; /// Page field info (entry from SXPI record).
310 XclPTDataFieldInfoList maDataInfoList
; /// List of extended data field info (SXDI records).
311 XclImpPTItemVec maItems
; /// List of all items of this field.
314 typedef ScfRef
< XclImpPTField
> XclImpPTFieldRef
;
316 // ============================================================================
318 class XclImpPivotTable
: protected XclImpRoot
321 explicit XclImpPivotTable( const XclImpRoot
& rRoot
);
322 virtual ~XclImpPivotTable();
324 // cache/field access, misc. ----------------------------------------------
326 inline XclImpPivotCacheRef
GetPivotCache() const { return mxPCache
; }
327 inline const ScfStringVec
& GetVisFieldNames() const { return maVisFieldNames
; }
329 sal_uInt16
GetFieldCount() const;
330 const XclImpPTField
* GetField( sal_uInt16 nFieldIdx
) const;
331 XclImpPTField
* GetFieldAcc( sal_uInt16 nFieldIdx
);
332 const String
& GetFieldName( sal_uInt16 nFieldIdx
) const;
334 const XclImpPTField
* GetDataField( sal_uInt16 nDataFieldIdx
) const;
335 const String
& GetDataFieldName( sal_uInt16 nDataFieldIdx
) const;
337 // records ----------------------------------------------------------------
339 /** Reads an SXVIEW record starting a new pivot table. */
340 void ReadSxview( XclImpStream
& rStrm
);
341 /** Reads an SXVD record describing a new field. */
342 void ReadSxvd( XclImpStream
& rStrm
);
343 /** Reads an SXVI record describing a new item of the current field. */
344 void ReadSxvi( XclImpStream
& rStrm
);
345 /** Reads an SXVDEX record describing extended options of the current field. */
346 void ReadSxvdex( XclImpStream
& rStrm
);
347 /** Reads an SXIVD record containing the row field or column field order. */
348 void ReadSxivd( XclImpStream
& rStrm
);
349 /** Reads an SXPI record containing page field data. */
350 void ReadSxpi( XclImpStream
& rStrm
);
351 /** Reads an SXDI record containing data field data. */
352 void ReadSxdi( XclImpStream
& rStrm
);
353 /** Reads an SXEX record containing additional settings for the pivot table. */
354 void ReadSxex( XclImpStream
& rStrm
);
355 /** Reads an SXVIEWEX9 record that specifies the pivot tables
357 void ReadSxViewEx9( XclImpStream
& rStrm
);
359 // ------------------------------------------------------------------------
361 /** Inserts the pivot table into the Calc document. */
366 void ApplyMergeFlags(const ScRange
& rOutRange
, const ScDPSaveData
& rSaveData
);
368 // ------------------------------------------------------------------------
370 typedef ::std::vector
< XclImpPTFieldRef
> XclImpPTFieldVec
;
372 XclImpPivotCacheRef mxPCache
; /// Pivot cache containing field/item names.
374 XclPTInfo maPTInfo
; /// General info about the pivot table (SXVIEW record).
375 XclPTExtInfo maPTExtInfo
; /// Extended info about the pivot table (SXEX record).
376 XclPTViewEx9Info maPTViewEx9Info
; /// (SXVIEWEX9 record)
377 XclImpPTFieldVec maFields
; /// Vector containing all fields.
378 XclImpPTFieldRef mxCurrField
; /// Current field for importing additional info.
379 ScfStringVec maVisFieldNames
; /// Vector containing all visible field names.
380 ScfUInt16Vec maRowFields
; /// Row field indexes.
381 ScfUInt16Vec maColFields
; /// Column field indexes.
382 ScfUInt16Vec maPageFields
; /// Page field indexes.
383 ScfUInt16Vec maOrigDataFields
; /// Original data field indexes.
384 ScfUInt16Vec maFiltDataFields
; /// Filtered data field indexes.
385 XclImpPTField maDataOrientField
; /// Special data field orientation field.
386 ScRange maOutScRange
; /// Output range in the Calc document.
390 typedef ScfRef
< XclImpPivotTable
> XclImpPivotTableRef
;
392 // ============================================================================
393 // ============================================================================
395 /** The main class for pivot table import.
397 This class contains functions to read all records related to pivot tables
400 class XclImpPivotTableManager
: protected XclImpRoot
403 explicit XclImpPivotTableManager( const XclImpRoot
& rRoot
);
404 virtual ~XclImpPivotTableManager();
406 // pivot cache records ----------------------------------------------------
408 /** Returns the pivot cache with the specified 0-based index. */
409 XclImpPivotCacheRef
GetPivotCache( sal_uInt16 nCacheIdx
);
411 /** Reads an SXIDSTM record containing a pivot cache stream identifier and the pivot cache. */
412 void ReadSxidstm( XclImpStream
& rStrm
);
413 /** Reads an SXVS record containing the source type of a pivot cache. */
414 void ReadSxvs( XclImpStream
& rStrm
);
415 /** Reads a DCONREF record containing the source range of a pivot cache. */
416 void ReadDconref( XclImpStream
& rStrm
);
418 // pivot table records ----------------------------------------------------
420 /** Reads an SXVIEW record describing a new pivot table. */
421 void ReadSxview( XclImpStream
& rStrm
);
422 /** Reads an SXVD record describing a new field. */
423 void ReadSxvd( XclImpStream
& rStrm
);
424 /** Reads an SXVDEX record describing extended options of a field. */
425 void ReadSxvdex( XclImpStream
& rStrm
);
426 /** Reads an SXIVD record containing the row field or column field order. */
427 void ReadSxivd( XclImpStream
& rStrm
);
428 /** Reads an SXPI record containing page field data. */
429 void ReadSxpi( XclImpStream
& rStrm
);
430 /** Reads an SXDI record containing data field data. */
431 void ReadSxdi( XclImpStream
& rStrm
);
432 /** Reads an SXVI record describing a new item of the current field. */
433 void ReadSxvi( XclImpStream
& rStrm
);
434 /** Reads an SXEX record containing additional settings for a pivot table. */
435 void ReadSxex( XclImpStream
& rStrm
);
436 /** Reads an SXVIEWEX9 record that specifies the pivot tables
438 void ReadSxViewEx9( XclImpStream
& rStrm
);
440 // ------------------------------------------------------------------------
442 /** Reads all used pivot caches and creates additional sheets for external data sources. */
443 void ReadPivotCaches( XclImpStream
& rStrm
);
444 /** Inserts all pivot tables into the Calc document. */
445 void ConvertPivotTables();
447 void MaybeRefreshPivotTables();
450 typedef ::std::vector
< XclImpPivotCacheRef
> XclImpPivotCacheVec
;
451 typedef ::std::vector
< XclImpPivotTableRef
> XclImpPivotTableVec
;
453 XclImpPivotCacheVec maPCaches
; /// List of all pivot caches.
454 XclImpPivotTableVec maPTables
; /// List of all pivot tables.
457 // ============================================================================