Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "pivottablebuffer.hxx"
21 :
22 : #include <set>
23 : #include <com/sun/star/container/XIndexAccess.hpp>
24 : #include <com/sun/star/container/XNameAccess.hpp>
25 : #include <com/sun/star/sheet/CellFlags.hpp>
26 : #include <com/sun/star/sheet/DataPilotFieldAutoShowInfo.hpp>
27 : #include <com/sun/star/sheet/DataPilotFieldLayoutInfo.hpp>
28 : #include <com/sun/star/sheet/DataPilotFieldLayoutMode.hpp>
29 : #include <com/sun/star/sheet/DataPilotFieldOrientation.hpp>
30 : #include <com/sun/star/sheet/DataPilotFieldReference.hpp>
31 : #include <com/sun/star/sheet/DataPilotFieldReferenceItemType.hpp>
32 : #include <com/sun/star/sheet/DataPilotFieldReferenceType.hpp>
33 : #include <com/sun/star/sheet/DataPilotFieldShowItemsMode.hpp>
34 : #include <com/sun/star/sheet/DataPilotFieldSortInfo.hpp>
35 : #include <com/sun/star/sheet/DataPilotFieldSortMode.hpp>
36 : #include <com/sun/star/sheet/GeneralFunction.hpp>
37 : #include <com/sun/star/sheet/XDataPilotDataLayoutFieldSupplier.hpp>
38 : #include <com/sun/star/sheet/XDataPilotField.hpp>
39 : #include <com/sun/star/sheet/XDataPilotTablesSupplier.hpp>
40 : #include <com/sun/star/sheet/XSheetOperation.hpp>
41 : #include <oox/helper/attributelist.hxx>
42 : #include <oox/helper/containerhelper.hxx>
43 : #include <oox/helper/propertyset.hxx>
44 : #include <oox/token/properties.hxx>
45 : #include "addressconverter.hxx"
46 : #include "biffinputstream.hxx"
47 :
48 : #include "dapiuno.hxx"
49 : #include "dpobject.hxx"
50 : #include "dpsave.hxx"
51 :
52 : namespace oox {
53 : namespace xls {
54 :
55 : using namespace ::com::sun::star::container;
56 : using namespace ::com::sun::star::sheet;
57 : using namespace ::com::sun::star::table;
58 : using namespace ::com::sun::star::uno;
59 : using namespace com::sun::star;
60 :
61 : namespace {
62 :
63 : const sal_Int32 OOX_PT_DATALAYOUTFIELD = -2; /// Placeholder index of data layout field.
64 :
65 : const sal_Int32 OOX_PT_PREVIOUS_ITEM = 0x001000FC; /// Calculation of data item result is based on previous item.
66 : const sal_Int32 OOX_PT_NEXT_ITEM = 0x001000FD; /// Calculation of data item result is based on next item.
67 :
68 : const sal_uInt32 BIFF12_PTFIELD_DATAFIELD = 0x00000008;
69 : const sal_uInt32 BIFF12_PTFIELD_DEFAULT = 0x00000100;
70 : const sal_uInt32 BIFF12_PTFIELD_SUM = 0x00000200;
71 : const sal_uInt32 BIFF12_PTFIELD_COUNTA = 0x00000400;
72 : const sal_uInt32 BIFF12_PTFIELD_AVERAGE = 0x00000800;
73 : const sal_uInt32 BIFF12_PTFIELD_MAX = 0x00001000;
74 : const sal_uInt32 BIFF12_PTFIELD_MIN = 0x00002000;
75 : const sal_uInt32 BIFF12_PTFIELD_PRODUCT = 0x00004000;
76 : const sal_uInt32 BIFF12_PTFIELD_COUNT = 0x00008000;
77 : const sal_uInt32 BIFF12_PTFIELD_STDDEV = 0x00010000;
78 : const sal_uInt32 BIFF12_PTFIELD_STDDEVP = 0x00020000;
79 : const sal_uInt32 BIFF12_PTFIELD_VAR = 0x00040000;
80 : const sal_uInt32 BIFF12_PTFIELD_VARP = 0x00080000;
81 :
82 : const sal_uInt32 BIFF12_PTFIELD_SHOWALL = 0x00000020;
83 : const sal_uInt32 BIFF12_PTFIELD_OUTLINE = 0x00000040;
84 : const sal_uInt32 BIFF12_PTFIELD_INSERTBLANKROW = 0x00000080;
85 : const sal_uInt32 BIFF12_PTFIELD_SUBTOTALTOP = 0x00000100;
86 : const sal_uInt32 BIFF12_PTFIELD_INSERTPAGEBREAK = 0x00000800;
87 : const sal_uInt32 BIFF12_PTFIELD_AUTOSORT = 0x00001000;
88 : const sal_uInt32 BIFF12_PTFIELD_SORTASCENDING = 0x00002000;
89 : const sal_uInt32 BIFF12_PTFIELD_AUTOSHOW = 0x00004000;
90 : const sal_uInt32 BIFF12_PTFIELD_AUTOSHOWTOP = 0x00008000;
91 : const sal_uInt32 BIFF12_PTFIELD_MULTIPAGEITEMS = 0x00080000;
92 :
93 : const sal_uInt16 BIFF12_PTFITEM_HIDDEN = 0x0001;
94 : const sal_uInt16 BIFF12_PTFITEM_HIDEDETAILS = 0x0002;
95 :
96 : const sal_uInt8 BIFF12_PTPAGEFIELD_HASNAME = 0x01;
97 : const sal_Int32 BIFF12_PTPAGEFIELD_MULTIITEMS = 0x001000FE;
98 :
99 : const sal_uInt16 BIFF12_PTFILTER_HASNAME = 0x0001;
100 : const sal_uInt16 BIFF12_PTFILTER_HASDESCRIPTION = 0x0002;
101 : const sal_uInt16 BIFF12_PTFILTER_HASSTRVALUE1 = 0x0004;
102 : const sal_uInt16 BIFF12_PTFILTER_HASSTRVALUE2 = 0x0008;
103 :
104 : const sal_uInt8 BIFF12_TOP10FILTER_TOP = 0x01;
105 : const sal_uInt8 BIFF12_TOP10FILTER_PERCENT = 0x02;
106 :
107 : const sal_uInt32 BIFF12_PTDEF_SHOWITEMS = 0x00000100;
108 : const sal_uInt32 BIFF12_PTDEF_DISABLEFIELDLIST = 0x00000400;
109 : const sal_uInt32 BIFF12_PTDEF_HIDECALCMEMBERS = 0x00001000;
110 : const sal_uInt32 BIFF12_PTDEF_WITHHIDDENTOTALS = 0x00002000;
111 : const sal_uInt32 BIFF12_PTDEF_HIDEDRILL = 0x00100000;
112 : const sal_uInt32 BIFF12_PTDEF_PRINTDRILL = 0x00200000;
113 : const sal_uInt32 BIFF12_PTDEF_HIDEHEADERS = 0x80000000;
114 :
115 : const sal_uInt32 BIFF12_PTDEF_SHOWEMPTYROW = 0x00000004;
116 : const sal_uInt32 BIFF12_PTDEF_SHOWEMPTYCOL = 0x00000008;
117 : const sal_uInt32 BIFF12_PTDEF_ENABLEDRILL = 0x00000020;
118 : const sal_uInt32 BIFF12_PTDEF_PRESERVEFORMATTING = 0x00000080;
119 : const sal_uInt32 BIFF12_PTDEF_USEAUTOFORMAT = 0x00000100;
120 : const sal_uInt32 BIFF12_PTDEF_SHOWERROR = 0x00000200;
121 : const sal_uInt32 BIFF12_PTDEF_SHOWMISSING = 0x00000400;
122 : const sal_uInt32 BIFF12_PTDEF_PAGEOVERTHENDOWN = 0x00000800;
123 : const sal_uInt32 BIFF12_PTDEF_SUBTOTALHIDDENITEMS = 0x00001000;
124 : const sal_uInt32 BIFF12_PTDEF_ROWGRANDTOTALS = 0x00002000;
125 : const sal_uInt32 BIFF12_PTDEF_COLGRANDTOTALS = 0x00004000;
126 : const sal_uInt32 BIFF12_PTDEF_FIELDPRINTTITLES = 0x00008000;
127 : const sal_uInt32 BIFF12_PTDEF_ITEMPRINTTITLES = 0x00020000;
128 : const sal_uInt32 BIFF12_PTDEF_MERGEITEM = 0x00040000;
129 : const sal_uInt32 BIFF12_PTDEF_HASDATACAPTION = 0x00080000;
130 : const sal_uInt32 BIFF12_PTDEF_HASGRANDTOTALCAPTION = 0x00100000;
131 : const sal_uInt32 BIFF12_PTDEF_HASPAGESTYLE = 0x00200000;
132 : const sal_uInt32 BIFF12_PTDEF_HASPIVOTTABLESTYLE = 0x00400000;
133 : const sal_uInt32 BIFF12_PTDEF_HASVACATEDSTYLE = 0x00800000;
134 : const sal_uInt32 BIFF12_PTDEF_APPLYNUMFMT = 0x01000000;
135 : const sal_uInt32 BIFF12_PTDEF_APPLYFONT = 0x02000000;
136 : const sal_uInt32 BIFF12_PTDEF_APPLYALIGNMENT = 0x04000000;
137 : const sal_uInt32 BIFF12_PTDEF_APPLYBORDER = 0x08000000;
138 : const sal_uInt32 BIFF12_PTDEF_APPLYFILL = 0x10000000;
139 : const sal_uInt32 BIFF12_PTDEF_APPLYPROTECTION = 0x20000000;
140 : const sal_uInt32 BIFF12_PTDEF_HASTAG = 0x40000000;
141 :
142 : const sal_uInt32 BIFF12_PTDEF_NOERRORCAPTION = 0x00000040;
143 : const sal_uInt32 BIFF12_PTDEF_NOMISSINGCAPTION = 0x00000080;
144 : const sal_uInt32 BIFF12_PTDEF_HASROWHEADERCAPTION = 0x00000400;
145 : const sal_uInt32 BIFF12_PTDEF_HASCOLHEADERCAPTION = 0x00000800;
146 : const sal_uInt32 BIFF12_PTDEF_FIELDLISTSORTASC = 0x00001000;
147 : const sal_uInt32 BIFF12_PTDEF_NOCUSTOMLISTSORT = 0x00004000;
148 :
149 : const sal_uInt8 BIFF12_PTDEF_ROWAXIS = 1;
150 : const sal_uInt8 BIFF12_PTDEF_COLAXIS = 2;
151 :
152 : } // namespace
153 :
154 40 : PTFieldItemModel::PTFieldItemModel() :
155 : mnCacheItem( -1 ),
156 : mnType( XML_data ),
157 : mbShowDetails( true ),
158 40 : mbHidden( false )
159 : {
160 40 : }
161 :
162 0 : void PTFieldItemModel::setBiffType( sal_uInt16 nType )
163 : {
164 : static const sal_Int32 spnTypes[] = { XML_data, XML_default,
165 : XML_sum, XML_countA, XML_avg, XML_max, XML_min, XML_product, XML_count,
166 : XML_stdDev, XML_stdDevP, XML_var, XML_varP, XML_grand, XML_blank };
167 0 : mnType = STATIC_ARRAY_SELECT( spnTypes, nType, XML_data );
168 0 : }
169 :
170 56 : PTFieldModel::PTFieldModel() :
171 : mnAxis( XML_TOKEN_INVALID ),
172 : mnNumFmtId( 0 ),
173 : mnAutoShowItems( 10 ),
174 : mnAutoShowRankBy( -1 ),
175 : mnSortType( XML_manual ),
176 : mnSortRefField( -1 ),
177 : mnSortRefItem( -1 ),
178 : mbDataField( false ),
179 : mbDefaultSubtotal( true ),
180 : mbSumSubtotal( false ),
181 : mbCountASubtotal( false ),
182 : mbAverageSubtotal( false ),
183 : mbMaxSubtotal( false ),
184 : mbMinSubtotal( false ),
185 : mbProductSubtotal( false ),
186 : mbCountSubtotal( false ),
187 : mbStdDevSubtotal( false ),
188 : mbStdDevPSubtotal( false ),
189 : mbVarSubtotal( false ),
190 : mbVarPSubtotal( false ),
191 : mbShowAll( true ),
192 : mbOutline( true ),
193 : mbSubtotalTop( true ),
194 : mbInsertBlankRow( false ),
195 : mbInsertPageBreak( false ),
196 : mbAutoShow( false ),
197 : mbTopAutoShow( true ),
198 56 : mbMultiPageItems( false )
199 : {
200 56 : }
201 :
202 0 : void PTFieldModel::setBiffAxis( sal_uInt8 nAxis )
203 : {
204 : /* Weird. The axis field is organized as bit field, but only one of the
205 : row/col/page flags are allowed at the same time and refer to the values
206 : 'axisRow', 'axisCol', and 'axisPage' of the XML attribute
207 : 'pivotField@axis'. Additionally, the fourth bit determines if the field
208 : is a data field, which may appear combined with the row/col/page flags.
209 : Therefore, this bit is unrelated to the 'axisValues' value of the
210 : 'pivotField@axis' attribute, but refers to the 'pivotField@dataField'
211 : boolean attribute. */
212 : static const sal_Int32 spnAxisIds[] = { XML_TOKEN_INVALID, XML_axisRow, XML_axisCol, XML_TOKEN_INVALID, XML_axisPage };
213 0 : mnAxis = STATIC_ARRAY_SELECT( spnAxisIds, nAxis, XML_TOKEN_INVALID );
214 0 : }
215 :
216 8 : PTPageFieldModel::PTPageFieldModel() :
217 : mnField( -1 ),
218 8 : mnItem( BIFF12_PTPAGEFIELD_MULTIITEMS )
219 : {
220 8 : }
221 :
222 12 : PTDataFieldModel::PTDataFieldModel() :
223 : mnField( -1 ),
224 : mnSubtotal( XML_sum ),
225 : mnShowDataAs( XML_normal ),
226 : mnBaseField( -1 ),
227 : mnBaseItem( -1 ),
228 12 : mnNumFmtId( 0 )
229 : {
230 12 : }
231 :
232 0 : void PTDataFieldModel::setBiffSubtotal( sal_Int32 nSubtotal )
233 : {
234 : static const sal_Int32 spnSubtotals[] = { XML_sum, XML_count, XML_average, XML_max, XML_min, XML_product, XML_countNums, XML_stdDev, XML_stdDevp, XML_var, XML_varp };
235 0 : mnSubtotal = STATIC_ARRAY_SELECT( spnSubtotals, nSubtotal, XML_TOKEN_INVALID );
236 0 : }
237 :
238 0 : void PTDataFieldModel::setBiffShowDataAs( sal_Int32 nShowDataAs )
239 : {
240 : static const sal_Int32 spnShowDataAs[] = { XML_normal, XML_difference, XML_percent, XML_percentDiff, XML_runTotal, XML_percentOfRow, XML_percentOfCol, XML_percentOfTotal, XML_index };
241 0 : mnShowDataAs = STATIC_ARRAY_SELECT( spnShowDataAs, nShowDataAs, XML_TOKEN_INVALID );
242 0 : }
243 :
244 56 : PivotTableField::PivotTableField( PivotTable& rPivotTable, sal_Int32 nFieldIndex ) :
245 : WorkbookHelper( rPivotTable ),
246 : mrPivotTable( rPivotTable ),
247 56 : mnFieldIndex( nFieldIndex )
248 : {
249 56 : }
250 :
251 48 : void PivotTableField::importPivotField( const AttributeList& rAttribs )
252 : {
253 : /* The documentation mentions a value 'axisValues' for the attribute
254 : 'pivotField@axis'. But this value is not used to mark a data field, as
255 : data fields may be inserted in one of the row/column/page dimensions at
256 : the same time. Therefore, the boolean attribute 'pivotField@dataField'
257 : is really used to mark data fields. */
258 48 : maModel.mnAxis = rAttribs.getToken( XML_axis, XML_TOKEN_INVALID );
259 48 : maModel.mnNumFmtId = rAttribs.getInteger( XML_numFmtId, 0 );
260 48 : maModel.mnAutoShowItems = rAttribs.getInteger( XML_itemPageCount, 10 );
261 48 : maModel.mnAutoShowRankBy = rAttribs.getInteger( XML_rankBy, -1 );
262 48 : maModel.mnSortType = rAttribs.getToken( XML_sortType, XML_manual );
263 48 : maModel.mbDataField = rAttribs.getBool( XML_dataField, false );
264 48 : maModel.mbDefaultSubtotal = rAttribs.getBool( XML_defaultSubtotal, true );
265 48 : maModel.mbSumSubtotal = rAttribs.getBool( XML_sumSubtotal, false );
266 48 : maModel.mbCountASubtotal = rAttribs.getBool( XML_countASubtotal, false );
267 48 : maModel.mbAverageSubtotal = rAttribs.getBool( XML_avgSubtotal, false );
268 48 : maModel.mbMaxSubtotal = rAttribs.getBool( XML_maxSubtotal, false );
269 48 : maModel.mbMinSubtotal = rAttribs.getBool( XML_minSubtotal, false );
270 48 : maModel.mbProductSubtotal = rAttribs.getBool( XML_productSubtotal, false );
271 48 : maModel.mbCountSubtotal = rAttribs.getBool( XML_countSubtotal, false );
272 48 : maModel.mbStdDevSubtotal = rAttribs.getBool( XML_stdDevSubtotal, false );
273 48 : maModel.mbStdDevPSubtotal = rAttribs.getBool( XML_stdDevPSubtotal, false );
274 48 : maModel.mbVarSubtotal = rAttribs.getBool( XML_varSubtotal, false );
275 48 : maModel.mbVarPSubtotal = rAttribs.getBool( XML_varPSubtotal, false );
276 48 : maModel.mbShowAll = rAttribs.getBool( XML_showAll, true );
277 48 : maModel.mbOutline = rAttribs.getBool( XML_outline, true );
278 48 : maModel.mbSubtotalTop = rAttribs.getBool( XML_subtotalTop, true );
279 48 : maModel.mbInsertBlankRow = rAttribs.getBool( XML_insertBlankRow, false );
280 48 : maModel.mbInsertPageBreak = rAttribs.getBool( XML_insertPageBreak, false );
281 48 : maModel.mbAutoShow = rAttribs.getBool( XML_autoShow, false );
282 48 : maModel.mbTopAutoShow = rAttribs.getBool( XML_topAutoShow, true );
283 48 : maModel.mbMultiPageItems = rAttribs.getBool( XML_multipleItemSelectionAllowed, false );
284 48 : }
285 :
286 40 : void PivotTableField::importItem( const AttributeList& rAttribs )
287 : {
288 40 : PTFieldItemModel aModel;
289 40 : aModel.mnCacheItem = rAttribs.getInteger( XML_x, -1 );
290 40 : aModel.mnType = rAttribs.getToken( XML_t, XML_data );
291 40 : aModel.mbShowDetails = rAttribs.getBool( XML_sd, true );
292 40 : aModel.mbHidden = rAttribs.getBool( XML_h, false );
293 40 : aModel.msCaption = rAttribs.getXString( XML_n, OUString() );
294 40 : maItems.push_back( aModel );
295 40 : }
296 :
297 0 : void PivotTableField::importReference( const AttributeList& rAttribs )
298 : {
299 : // field index is stored as unsigned integer
300 0 : maModel.mnSortRefField = static_cast< sal_Int32 >( rAttribs.getUnsigned( XML_field, SAL_MAX_UINT32 ) );
301 0 : }
302 :
303 0 : void PivotTableField::importReferenceItem( const AttributeList& rAttribs )
304 : {
305 0 : maModel.mnSortRefItem = rAttribs.getInteger( XML_v, -1 );
306 0 : }
307 :
308 0 : void PivotTableField::importPTField( SequenceInputStream& rStrm )
309 : {
310 : sal_uInt32 nFlags1, nFlags2;
311 0 : rStrm >> nFlags1 >> maModel.mnNumFmtId >> nFlags2 >> maModel.mnAutoShowItems >> maModel.mnAutoShowRankBy;
312 :
313 0 : maModel.setBiffAxis( extractValue< sal_uInt8 >( nFlags1, 0, 3 ) );
314 0 : maModel.mbDataField = getFlag( nFlags1, BIFF12_PTFIELD_DATAFIELD );
315 0 : maModel.mbDefaultSubtotal = getFlag( nFlags1, BIFF12_PTFIELD_DEFAULT );
316 0 : maModel.mbSumSubtotal = getFlag( nFlags1, BIFF12_PTFIELD_SUM );
317 0 : maModel.mbCountASubtotal = getFlag( nFlags1, BIFF12_PTFIELD_COUNTA );
318 0 : maModel.mbAverageSubtotal = getFlag( nFlags1, BIFF12_PTFIELD_AVERAGE );
319 0 : maModel.mbMaxSubtotal = getFlag( nFlags1, BIFF12_PTFIELD_MAX );
320 0 : maModel.mbMinSubtotal = getFlag( nFlags1, BIFF12_PTFIELD_MIN );
321 0 : maModel.mbProductSubtotal = getFlag( nFlags1, BIFF12_PTFIELD_PRODUCT );
322 0 : maModel.mbCountSubtotal = getFlag( nFlags1, BIFF12_PTFIELD_COUNT );
323 0 : maModel.mbStdDevSubtotal = getFlag( nFlags1, BIFF12_PTFIELD_STDDEV );
324 0 : maModel.mbStdDevPSubtotal = getFlag( nFlags1, BIFF12_PTFIELD_STDDEVP );
325 0 : maModel.mbVarSubtotal = getFlag( nFlags1, BIFF12_PTFIELD_VAR );
326 0 : maModel.mbVarPSubtotal = getFlag( nFlags1, BIFF12_PTFIELD_VARP );
327 :
328 0 : maModel.mbShowAll = getFlag( nFlags2, BIFF12_PTFIELD_SHOWALL );
329 0 : maModel.mbOutline = getFlag( nFlags2, BIFF12_PTFIELD_OUTLINE );
330 0 : maModel.mbSubtotalTop = getFlag( nFlags2, BIFF12_PTFIELD_SUBTOTALTOP );
331 0 : maModel.mbInsertBlankRow = getFlag( nFlags2, BIFF12_PTFIELD_INSERTBLANKROW );
332 0 : maModel.mbInsertPageBreak = getFlag( nFlags2, BIFF12_PTFIELD_INSERTPAGEBREAK );
333 0 : maModel.mbAutoShow = getFlag( nFlags2, BIFF12_PTFIELD_AUTOSHOW );
334 0 : maModel.mbTopAutoShow = getFlag( nFlags2, BIFF12_PTFIELD_AUTOSHOWTOP );
335 0 : maModel.mbMultiPageItems = getFlag( nFlags2, BIFF12_PTFIELD_MULTIPAGEITEMS );
336 :
337 0 : bool bAutoSort = getFlag( nFlags2, BIFF12_PTFIELD_AUTOSORT );
338 0 : bool bAscending = getFlag( nFlags2, BIFF12_PTFIELD_SORTASCENDING );
339 0 : maModel.mnSortType = bAutoSort ? (bAscending ? XML_ascending : XML_descending) : XML_manual;
340 0 : }
341 :
342 0 : void PivotTableField::importPTFItem( SequenceInputStream& rStrm )
343 : {
344 0 : PTFieldItemModel aModel;
345 : sal_uInt8 nType;
346 : sal_uInt16 nFlags;
347 0 : rStrm >> nType >> nFlags >> aModel.mnCacheItem;
348 :
349 0 : aModel.setBiffType( nType );
350 0 : aModel.mbShowDetails = !getFlag( nFlags, BIFF12_PTFITEM_HIDEDETAILS );
351 0 : aModel.mbHidden = getFlag( nFlags, BIFF12_PTFITEM_HIDDEN );
352 :
353 0 : maItems.push_back( aModel );
354 0 : }
355 :
356 0 : void PivotTableField::importPTReference( SequenceInputStream& rStrm )
357 : {
358 0 : rStrm >> maModel.mnSortRefField;
359 0 : }
360 :
361 0 : void PivotTableField::importPTReferenceItem( SequenceInputStream& rStrm )
362 : {
363 0 : rStrm >> maModel.mnSortRefItem;
364 0 : }
365 :
366 48 : void PivotTableField::finalizeImport( const Reference< XDataPilotDescriptor >& rxDPDesc )
367 : {
368 : /* Process all fields based on source data, other fields (e.g. group
369 : fields) are processed from here. PivotCacahe::getDatabaseIndex()
370 : returns -1 for all fields not based on source data. */
371 48 : Reference< XDataPilotField > xDPField;
372 48 : sal_Int32 nDatabaseIdx = mrPivotTable.getCacheDatabaseIndex( mnFieldIndex );
373 48 : if( (nDatabaseIdx >= 0) && rxDPDesc.is() ) try
374 : {
375 : // try to get the source field and its name from passed DataPilot descriptor
376 48 : Reference< XIndexAccess > xDPFieldsIA( rxDPDesc->getDataPilotFields(), UNO_SET_THROW );
377 48 : xDPField.set( xDPFieldsIA->getByIndex( nDatabaseIdx ), UNO_QUERY_THROW );
378 96 : Reference< XNamed > xDPFieldName( xDPField, UNO_QUERY_THROW );
379 48 : maDPFieldName = xDPFieldName->getName();
380 : OSL_ENSURE( !maDPFieldName.isEmpty(), "PivotTableField::finalizeImport - no field name in source data found" );
381 :
382 : // try to convert grouping settings
383 48 : if( const PivotCacheField* pCacheField = mrPivotTable.getCacheField( mnFieldIndex ) )
384 : {
385 : // numeric grouping is done inplace, no nested group fields will appear
386 48 : if( pCacheField->hasNumericGrouping() )
387 : {
388 0 : pCacheField->convertNumericGrouping( xDPField );
389 : }
390 48 : else if( pCacheField->hasDateGrouping() )
391 : {
392 : // first date group settings are inplace
393 0 : pCacheField->createDateGroupField( xDPField );
394 : // create all nested group fields (if any)
395 0 : mrPivotTable.finalizeDateGroupingImport( xDPField, mnFieldIndex );
396 : }
397 48 : else if( pCacheField->hasParentGrouping() )
398 : {
399 :
400 : // create a list of all item names, needed to map between original and group items
401 0 : ::std::vector< OUString > aItems;
402 0 : pCacheField->getCacheItemNames( aItems );
403 0 : PivotCacheGroupItemVector aItemNames;
404 0 : for( ::std::vector< OUString >::iterator aIt = aItems.begin(), aEnd = aItems.end(); aIt != aEnd; ++aIt )
405 0 : aItemNames.push_back( PivotCacheGroupItem( *aIt ) );
406 : // create all nested group fields (if any)
407 0 : mrPivotTable.finalizeParentGroupingImport( xDPField, *pCacheField, aItemNames );
408 : }
409 48 : }
410 : }
411 0 : catch( Exception& )
412 : {
413 48 : }
414 48 : }
415 :
416 0 : void PivotTableField::finalizeDateGroupingImport( const Reference< XDataPilotField >& rxBaseDPField, sal_Int32 nBaseFieldIdx )
417 : {
418 0 : if( maDPFieldName.isEmpty() ) // prevent endless loops if file format is broken
419 : {
420 0 : if( const PivotCacheField* pCacheField = mrPivotTable.getCacheField( mnFieldIndex ) )
421 : {
422 0 : if( !pCacheField->isDatabaseField() && pCacheField->hasDateGrouping() && (pCacheField->getGroupBaseField() == nBaseFieldIdx) )
423 : {
424 0 : maDPFieldName = pCacheField->createDateGroupField( rxBaseDPField );
425 : OSL_ENSURE( !maDPFieldName.isEmpty(), "PivotTableField::finalizeDateGroupingImport - cannot create date group field" );
426 : }
427 : }
428 : }
429 0 : }
430 :
431 0 : void PivotTableField::finalizeParentGroupingImport( const Reference< XDataPilotField >& rxBaseDPField, const PivotCacheField& rBaseCacheField, PivotCacheGroupItemVector& orItemNames )
432 : {
433 0 : if( maDPFieldName.isEmpty() ) // prevent endless loops if file format is broken
434 : {
435 0 : if( const PivotCacheField* pCacheField = mrPivotTable.getCacheField( mnFieldIndex ) )
436 : {
437 : // data field can have user defined groupname captions, apply them
438 : // if they do
439 0 : IdCaptionPairList captionList;
440 0 : for( ItemModelVector::iterator aIt = maItems.begin(), aEnd = maItems.end(); aIt != aEnd; ++aIt )
441 : {
442 0 : if ( aIt->mnType == XML_data && aIt->msCaption.getLength() )
443 0 : captionList.push_back( IdCaptionPair( aIt->mnCacheItem, aIt->msCaption ) );
444 : }
445 : // #FIXME find another way out of this const nightmare prison
446 0 : if ( !captionList.empty() )
447 0 : const_cast<PivotCacheField*>( pCacheField )->applyItemCaptions( captionList );
448 0 : maDPFieldName = pCacheField->createParentGroupField( rxBaseDPField, rBaseCacheField, orItemNames );
449 : // on success, try to create nested group fields
450 0 : Reference< XDataPilotField > xDPField = mrPivotTable.getDataPilotField( maDPFieldName );
451 0 : if( xDPField.is() )
452 0 : mrPivotTable.finalizeParentGroupingImport( xDPField, *pCacheField, orItemNames );
453 : }
454 : }
455 0 : }
456 :
457 8 : void PivotTableField::convertRowField()
458 : {
459 8 : convertRowColPageField( XML_axisRow );
460 8 : }
461 :
462 8 : void PivotTableField::convertColField()
463 : {
464 8 : convertRowColPageField( XML_axisCol );
465 8 : }
466 :
467 28 : void PivotTableField::convertHiddenField()
468 : {
469 28 : convertRowColPageField( XML_TOKEN_INVALID );
470 28 : }
471 :
472 8 : void PivotTableField::convertPageField( const PTPageFieldModel& rPageField )
473 : {
474 : OSL_ENSURE( rPageField.mnField == mnFieldIndex, "PivotTableField::convertPageField - wrong field index" );
475 : // convert all settings common for row/column/page fields
476 8 : Reference< XDataPilotField > xDPField = convertRowColPageField( XML_axisPage );
477 :
478 8 : if( xDPField.is() )
479 : {
480 8 : PropertySet aPropSet( xDPField );
481 : using namespace ::com::sun::star::sheet;
482 :
483 : // find cache item used as 'selected page'
484 8 : sal_Int32 nCacheItem = -1;
485 8 : if( maModel.mbMultiPageItems )
486 : {
487 : // multiple items may be selected
488 : OSL_ENSURE( rPageField.mnItem == BIFF12_PTPAGEFIELD_MULTIITEMS, "PivotTableField::convertPageField - unexpected cache item index" );
489 : // try to find a single visible item
490 0 : bool bHasMultiItems = false;
491 0 : for( ItemModelVector::iterator aIt = maItems.begin(), aEnd = maItems.end(); (aIt != aEnd) && !bHasMultiItems; ++aIt )
492 : {
493 0 : if( (aIt->mnType == XML_data) && !aIt->mbHidden )
494 : {
495 0 : bHasMultiItems = nCacheItem >= 0;
496 0 : nCacheItem = bHasMultiItems ? -1 : aIt->mnCacheItem;
497 : }
498 : }
499 : }
500 : else
501 : {
502 : // single item may be selected
503 8 : if( (0 <= rPageField.mnItem) && (rPageField.mnItem < static_cast< sal_Int32 >( maItems.size() )) )
504 0 : nCacheItem = maItems[ rPageField.mnItem ].mnCacheItem;
505 : }
506 :
507 8 : if( nCacheItem >= 0 )
508 : {
509 0 : if( const PivotCacheField* pCacheField = mrPivotTable.getCacheField( mnFieldIndex ) )
510 : {
511 0 : if( const PivotCacheItem* pSharedItem = pCacheField->getCacheItem( nCacheItem ) )
512 : {
513 0 : OUString aSelectedPage = pSharedItem->getName();
514 0 : aPropSet.setProperty( PROP_SelectedPage, aSelectedPage );
515 : }
516 : }
517 8 : }
518 8 : }
519 8 : }
520 :
521 12 : void PivotTableField::convertDataField( const PTDataFieldModel& rDataField )
522 : {
523 : OSL_ENSURE( rDataField.mnField == mnFieldIndex, "PivotTableField::convertDataField - wrong field index" );
524 : OSL_ENSURE( maModel.mbDataField, "PivotTableField::convertDataField - not a data field" );
525 12 : Reference< XDataPilotField > xDPField = mrPivotTable.getDataPilotField( maDPFieldName );
526 12 : if( xDPField.is() )
527 : {
528 12 : PropertySet aPropSet( xDPField );
529 : using namespace ::com::sun::star::sheet;
530 :
531 : // field orientation
532 12 : aPropSet.setProperty( PROP_Orientation, DataPilotFieldOrientation_DATA );
533 :
534 : /* Field aggregation function. Documentation is a little bit confused
535 : about which names to use for the count functions. The name 'count'
536 : means 'count all', and 'countNum' means 'count numbers'. On the
537 : other hand, for subtotals, 'countA' means 'count all', and 'count'
538 : means 'count numbers' (see above). */
539 12 : GeneralFunction eAggFunc = GeneralFunction_SUM;
540 12 : switch( rDataField.mnSubtotal )
541 : {
542 8 : case XML_sum: eAggFunc = GeneralFunction_SUM; break;
543 4 : case XML_count: eAggFunc = GeneralFunction_COUNT; break;
544 0 : case XML_average: eAggFunc = GeneralFunction_AVERAGE; break;
545 0 : case XML_max: eAggFunc = GeneralFunction_MAX; break;
546 0 : case XML_min: eAggFunc = GeneralFunction_MIN; break;
547 0 : case XML_product: eAggFunc = GeneralFunction_PRODUCT; break;
548 0 : case XML_countNums: eAggFunc = GeneralFunction_COUNTNUMS; break;
549 0 : case XML_stdDev: eAggFunc = GeneralFunction_STDEV; break;
550 0 : case XML_stdDevp: eAggFunc = GeneralFunction_STDEVP; break;
551 0 : case XML_var: eAggFunc = GeneralFunction_VAR; break;
552 0 : case XML_varp: eAggFunc = GeneralFunction_VARP; break;
553 : default: OSL_FAIL( "PivotTableField::convertDataField - unknown aggregation function" );
554 : }
555 12 : aPropSet.setProperty( PROP_Function, eAggFunc );
556 :
557 : // field reference ('show data as')
558 12 : DataPilotFieldReference aReference;
559 12 : aReference.ReferenceType = DataPilotFieldReferenceType::NONE;
560 12 : switch( rDataField.mnShowDataAs )
561 : {
562 0 : case XML_difference: aReference.ReferenceType = DataPilotFieldReferenceType::ITEM_DIFFERENCE; break;
563 0 : case XML_percent: aReference.ReferenceType = DataPilotFieldReferenceType::ITEM_PERCENTAGE; break;
564 0 : case XML_percentDiff: aReference.ReferenceType = DataPilotFieldReferenceType::ITEM_PERCENTAGE_DIFFERENCE; break;
565 0 : case XML_runTotal: aReference.ReferenceType = DataPilotFieldReferenceType::RUNNING_TOTAL; break;
566 0 : case XML_percentOfRow: aReference.ReferenceType = DataPilotFieldReferenceType::ROW_PERCENTAGE; break;
567 0 : case XML_percentOfCol: aReference.ReferenceType = DataPilotFieldReferenceType::COLUMN_PERCENTAGE; break;
568 0 : case XML_percentOfTotal: aReference.ReferenceType = DataPilotFieldReferenceType::TOTAL_PERCENTAGE; break;
569 0 : case XML_index: aReference.ReferenceType = DataPilotFieldReferenceType::INDEX; break;
570 : }
571 12 : if( aReference.ReferenceType != DataPilotFieldReferenceType::NONE )
572 : {
573 0 : if( const PivotCacheField* pCacheField = mrPivotTable.getCacheField( rDataField.mnBaseField ) )
574 : {
575 0 : aReference.ReferenceField = pCacheField->getName();
576 0 : switch( rDataField.mnBaseItem )
577 : {
578 : case OOX_PT_PREVIOUS_ITEM:
579 0 : aReference.ReferenceItemType = DataPilotFieldReferenceItemType::PREVIOUS;
580 0 : break;
581 : case OOX_PT_NEXT_ITEM:
582 0 : aReference.ReferenceItemType = DataPilotFieldReferenceItemType::NEXT;
583 0 : break;
584 : default:
585 0 : aReference.ReferenceItemType = DataPilotFieldReferenceItemType::NAMED;
586 0 : if( const PivotCacheItem* pCacheItem = pCacheField->getCacheItem( rDataField.mnBaseItem ) )
587 0 : aReference.ReferenceItemName = pCacheItem->getName();
588 : }
589 0 : aPropSet.setProperty( PROP_Reference, aReference );
590 : }
591 12 : }
592 12 : }
593 12 : }
594 :
595 : // private --------------------------------------------------------------------
596 :
597 52 : Reference< XDataPilotField > PivotTableField::convertRowColPageField( sal_Int32 nAxis )
598 : {
599 52 : bool bDataLayout = mnFieldIndex == OOX_PT_DATALAYOUTFIELD;
600 52 : Reference< XDataPilotField > xDPField = bDataLayout ? mrPivotTable.getDataLayoutField() : mrPivotTable.getDataPilotField( maDPFieldName );
601 : OSL_ENSURE( bDataLayout || (nAxis == maModel.mnAxis), "PivotTableField::convertRowColPageField - field axis mismatch" );
602 :
603 52 : if( xDPField.is() )
604 : {
605 : // TODO: Use this to set properties directly, bypassing the slow uno layer.
606 52 : ScDPObject* pDPObj = mrPivotTable.getDPObject();
607 :
608 52 : PropertySet aPropSet( xDPField );
609 : using namespace ::com::sun::star::sheet;
610 :
611 : // field orientation
612 52 : DataPilotFieldOrientation eFieldOrient = DataPilotFieldOrientation_HIDDEN;
613 52 : switch( nAxis )
614 : {
615 8 : case XML_axisRow: eFieldOrient = DataPilotFieldOrientation_ROW; break;
616 8 : case XML_axisCol: eFieldOrient = DataPilotFieldOrientation_COLUMN; break;
617 8 : case XML_axisPage: eFieldOrient = DataPilotFieldOrientation_PAGE; break;
618 : }
619 52 : if( eFieldOrient != DataPilotFieldOrientation_HIDDEN )
620 24 : aPropSet.setProperty( PROP_Orientation, eFieldOrient );
621 :
622 : // all other settings not for the data layout field
623 52 : if( !bDataLayout )
624 : {
625 : /* Field subtotal functions. Ignore the 'defaultSubtotal' flag, if
626 : explicit functions are set. This is different behaviour between
627 : XML (where 'defaultSubtotal' is set regardless of other
628 : functions) and binary formats (where 'defaultSubtotal' is not
629 : set if other functions are set). */
630 48 : ::std::vector< GeneralFunction > aSubtotals;
631 : /* Order of subtotals is fixed in Excel. Documentation is a little
632 : bit confused about which names to use for the count functions.
633 : For subtotals, 'countA' means 'count all', and 'count' means
634 : 'count numbers'. On the other hand, for the data field
635 : aggregation function, 'count' means 'count all', and 'countNum'
636 : means 'count numbers' (see below). */
637 48 : if( maModel.mbSumSubtotal ) aSubtotals.push_back( GeneralFunction_SUM );
638 48 : if( maModel.mbCountASubtotal ) aSubtotals.push_back( GeneralFunction_COUNT );
639 48 : if( maModel.mbAverageSubtotal ) aSubtotals.push_back( GeneralFunction_AVERAGE );
640 48 : if( maModel.mbMaxSubtotal ) aSubtotals.push_back( GeneralFunction_MAX );
641 48 : if( maModel.mbMinSubtotal ) aSubtotals.push_back( GeneralFunction_MIN );
642 48 : if( maModel.mbProductSubtotal ) aSubtotals.push_back( GeneralFunction_PRODUCT );
643 48 : if( maModel.mbCountSubtotal ) aSubtotals.push_back( GeneralFunction_COUNTNUMS );
644 48 : if( maModel.mbStdDevSubtotal ) aSubtotals.push_back( GeneralFunction_STDEV );
645 48 : if( maModel.mbStdDevPSubtotal ) aSubtotals.push_back( GeneralFunction_STDEVP );
646 48 : if( maModel.mbVarSubtotal ) aSubtotals.push_back( GeneralFunction_VAR );
647 48 : if( maModel.mbVarPSubtotal ) aSubtotals.push_back( GeneralFunction_VARP );
648 : // if no function is set manually, check the 'defaultSubtotal' flag
649 48 : if( aSubtotals.empty() && maModel.mbDefaultSubtotal )
650 48 : aSubtotals.push_back( GeneralFunction_AUTO );
651 48 : aPropSet.setProperty( PROP_Subtotals, ContainerHelper::vectorToSequence( aSubtotals ) );
652 :
653 : // layout settings
654 48 : DataPilotFieldLayoutInfo aLayoutInfo;
655 : aLayoutInfo.LayoutMode = maModel.mbOutline ?
656 : (maModel.mbSubtotalTop ? DataPilotFieldLayoutMode::OUTLINE_SUBTOTALS_TOP : DataPilotFieldLayoutMode::OUTLINE_SUBTOTALS_BOTTOM) :
657 48 : DataPilotFieldLayoutMode::TABULAR_LAYOUT;
658 48 : aLayoutInfo.AddEmptyLines = maModel.mbInsertBlankRow;
659 48 : aPropSet.setProperty( PROP_LayoutInfo, aLayoutInfo );
660 48 : aPropSet.setProperty( PROP_ShowEmpty, maModel.mbShowAll );
661 :
662 : // auto show (OOXML/BIFF12 only)
663 48 : if( maModel.mbAutoShow )
664 : {
665 0 : DataPilotFieldAutoShowInfo aAutoShowInfo;
666 0 : aAutoShowInfo.IsEnabled = sal_True;
667 0 : aAutoShowInfo.ShowItemsMode = maModel.mbTopAutoShow ? DataPilotFieldShowItemsMode::FROM_TOP : DataPilotFieldShowItemsMode::FROM_BOTTOM;
668 0 : aAutoShowInfo.ItemCount = maModel.mnAutoShowItems;
669 0 : if( const PivotCacheField* pCacheField = mrPivotTable.getCacheFieldOfDataField( maModel.mnAutoShowRankBy ) )
670 0 : aAutoShowInfo.DataField = pCacheField->getName();
671 0 : aPropSet.setProperty( PROP_AutoShowInfo, aAutoShowInfo );
672 : }
673 :
674 : // auto sort
675 96 : DataPilotFieldSortInfo aSortInfo;
676 48 : aSortInfo.IsAscending = maModel.mnSortType == XML_ascending;
677 48 : if( (maModel.mnSortType != XML_ascending) && (maModel.mnSortType != XML_descending) )
678 : {
679 48 : aSortInfo.Mode = DataPilotFieldSortMode::MANUAL;
680 : }
681 : else
682 : {
683 0 : const PivotCacheField* pCacheField = (maModel.mnSortRefField == OOX_PT_DATALAYOUTFIELD) ?
684 0 : mrPivotTable.getCacheFieldOfDataField( maModel.mnSortRefItem ) : 0;
685 0 : if( pCacheField )
686 : {
687 0 : aSortInfo.Mode = DataPilotFieldSortMode::DATA;
688 0 : aSortInfo.Field = pCacheField->getName();
689 : }
690 : else
691 : {
692 0 : aSortInfo.Mode = DataPilotFieldSortMode::NAME;
693 : }
694 : }
695 48 : aPropSet.setProperty( PROP_SortInfo, aSortInfo );
696 :
697 : // item settings
698 48 : if (const PivotCacheField* pCacheField = mrPivotTable.getCacheField(mnFieldIndex))
699 : {
700 48 : ScDPSaveData* pSaveData = pDPObj->GetSaveData();
701 48 : ScDPSaveDimension* pDim = pSaveData->GetDimensionByName(pCacheField->getName());
702 :
703 : try
704 : {
705 88 : for( ItemModelVector::iterator aIt = maItems.begin(), aEnd = maItems.end(); aIt != aEnd; ++aIt )
706 : {
707 40 : if (aIt->mnType != XML_data)
708 10 : continue;
709 :
710 30 : const PivotCacheItem* pSharedItem = pCacheField->getCacheItem(aIt->mnCacheItem);
711 30 : if (!pSharedItem)
712 0 : continue;
713 :
714 : try
715 : {
716 30 : ScDPSaveMember* pMem = pDim->GetMemberByName(pSharedItem->getName());
717 30 : pMem->SetShowDetails(aIt->mbShowDetails);
718 30 : pMem->SetIsVisible(!aIt->mbHidden);
719 : }
720 0 : catch( Exception& )
721 : {
722 : // catch every failed container access to be able to process following items
723 : }
724 : }
725 : }
726 0 : catch (const Exception&) {}
727 48 : }
728 52 : }
729 : }
730 52 : return xDPField;
731 : }
732 :
733 0 : PTFilterModel::PTFilterModel() :
734 : mfValue( 0.0 ),
735 : mnField( -1 ),
736 : mnMemPropField( -1 ),
737 : mnType( XML_TOKEN_INVALID ),
738 : mnEvalOrder( 0 ),
739 : mnId( -1 ),
740 : mnMeasureField( -1 ),
741 : mnMeasureHier( -1 ),
742 0 : mbTopFilter( true )
743 : {
744 0 : }
745 :
746 0 : PivotTableFilter::PivotTableFilter( const PivotTable& rPivotTable ) :
747 : WorkbookHelper( rPivotTable ),
748 0 : mrPivotTable( rPivotTable )
749 : {
750 0 : }
751 :
752 0 : void PivotTableFilter::importFilter( const AttributeList& rAttribs )
753 : {
754 0 : maModel.maName = rAttribs.getXString( XML_name, OUString() );
755 0 : maModel.maDescription = rAttribs.getXString( XML_description, OUString() );
756 0 : maModel.maStrValue1 = rAttribs.getXString( XML_stringValue1, OUString() );
757 0 : maModel.maStrValue2 = rAttribs.getXString( XML_stringValue2, OUString() );
758 0 : maModel.mnField = rAttribs.getInteger( XML_fld, -1 );
759 0 : maModel.mnMemPropField = rAttribs.getInteger( XML_mpFld, -1 );
760 0 : maModel.mnType = rAttribs.getToken( XML_type, XML_TOKEN_INVALID );
761 0 : maModel.mnEvalOrder = rAttribs.getInteger( XML_evalOrder, 0 );
762 0 : maModel.mnId = rAttribs.getInteger( XML_id, -1 );
763 0 : maModel.mnMeasureField = rAttribs.getInteger( XML_iMeasureFld, -1 );
764 0 : maModel.mnMeasureHier = rAttribs.getInteger( XML_iMeasureHier, -1 );
765 0 : }
766 :
767 0 : void PivotTableFilter::importTop10( const AttributeList& rAttribs )
768 : {
769 : OSL_ENSURE( rAttribs.getBool( XML_percent, false ) == (maModel.mnType == XML_percent),
770 : "PivotTableFilter::importTop10 - unexpected value of percent attribute" );
771 0 : maModel.mfValue = rAttribs.getDouble( XML_val, 0.0 );
772 0 : maModel.mbTopFilter = rAttribs.getBool( XML_top, true );
773 0 : }
774 :
775 0 : void PivotTableFilter::importPTFilter( SequenceInputStream& rStrm )
776 : {
777 : sal_Int32 nType;
778 : sal_uInt16 nFlags;
779 0 : rStrm >> maModel.mnField >> maModel.mnMemPropField >> nType;
780 0 : rStrm.skip( 4 ); // unused
781 0 : rStrm >> maModel.mnId >> maModel.mnMeasureField >> maModel.mnMeasureHier >> nFlags;
782 0 : if( getFlag( nFlags, BIFF12_PTFILTER_HASNAME ) )
783 0 : rStrm >> maModel.maName;
784 0 : if( getFlag( nFlags, BIFF12_PTFILTER_HASDESCRIPTION ) )
785 0 : rStrm >> maModel.maDescription;
786 0 : if( getFlag( nFlags, BIFF12_PTFILTER_HASSTRVALUE1 ) )
787 0 : rStrm >> maModel.maStrValue1;
788 0 : if( getFlag( nFlags, BIFF12_PTFILTER_HASSTRVALUE2 ) )
789 0 : rStrm >> maModel.maStrValue2;
790 :
791 : static const sal_Int32 spnTypes[] =
792 : {
793 : XML_unknown,
794 : // data field top10 filter (1-3)
795 : XML_count, XML_percent, XML_sum,
796 : // caption filter (4-17)
797 : XML_captionEqual, XML_captionNotEqual,
798 : XML_captionBeginsWith, XML_captionNotBeginsWith, XML_captionEndsWith, XML_captionNotEndsWith,
799 : XML_captionContains, XML_captionNotContains, XML_captionGreaterThan, XML_captionGreaterThanOrEqual,
800 : XML_captionLessThan, XML_captionLessThanOrEqual, XML_captionBetween, XML_captionNotBetween,
801 : // value filter (18-25)
802 : XML_valueEqual, XML_valueNotEqual, XML_valueGreaterThan, XML_valueGreaterThanOrEqual,
803 : XML_valueLessThan, XML_valueLessThanOrEqual, XML_valueBetween, XML_valueNotBetween,
804 : // date filter (26-65)
805 : XML_dateEqual, XML_dateOlderThan, XML_dateNewerThan, XML_dateBetween,
806 : XML_tomorrow, XML_today, XML_yesterday, XML_nextWeek, XML_thisWeek, XML_lastWeek,
807 : XML_nextMonth, XML_thisMonth, XML_lastMonth, XML_nextQuarter, XML_thisQuarter, XML_lastQuarter,
808 : XML_nextYear, XML_thisYear, XML_lastYear, XML_yearToDate, XML_Q1, XML_Q2, XML_Q3, XML_Q4,
809 : XML_M1, XML_M2, XML_M3, XML_M4, XML_M5, XML_M6, XML_M7, XML_M8, XML_M9, XML_M10, XML_M11, XML_M12,
810 : XML_dateNotEqual, XML_dateOlderThanOrEqual, XML_dateNewerThanOrEqual, XML_dateNotBetween
811 : };
812 0 : maModel.mnType = STATIC_ARRAY_SELECT( spnTypes, nType, XML_TOKEN_INVALID );
813 0 : }
814 :
815 0 : void PivotTableFilter::importTop10Filter( SequenceInputStream& rStrm )
816 : {
817 : sal_uInt8 nFlags;
818 0 : rStrm >> nFlags >> maModel.mfValue;
819 :
820 : SAL_WARN_IF(
821 : getFlag(nFlags, BIFF12_TOP10FILTER_PERCENT) != (maModel.mnType == XML_percent),
822 : "sc.filter",
823 : "PivotTableFilter::importTop10 - unexpected value of percent attribute");
824 0 : maModel.mbTopFilter = getFlag( nFlags, BIFF12_TOP10FILTER_TOP );
825 0 : }
826 :
827 0 : void PivotTableFilter::finalizeImport()
828 : {
829 : // only simple top10 filter supported
830 0 : if( maModel.mnType == XML_count )
831 : {
832 0 : PropertySet aPropSet( mrPivotTable.getDataPilotField( maModel.mnField ) );
833 0 : if( aPropSet.is() )
834 : {
835 : using namespace ::com::sun::star::sheet;
836 0 : DataPilotFieldAutoShowInfo aAutoShowInfo;
837 0 : aAutoShowInfo.IsEnabled = sal_True;
838 0 : aAutoShowInfo.ShowItemsMode = maModel.mbTopFilter ? DataPilotFieldShowItemsMode::FROM_TOP : DataPilotFieldShowItemsMode::FROM_BOTTOM;
839 0 : aAutoShowInfo.ItemCount = getLimitedValue< sal_Int32, double >( maModel.mfValue, 0, SAL_MAX_INT32 );
840 0 : if( const PivotCacheField* pCacheField = mrPivotTable.getCacheFieldOfDataField( maModel.mnMeasureField ) )
841 0 : aAutoShowInfo.DataField = pCacheField->getName();
842 0 : aPropSet.setProperty( PROP_AutoShowInfo, aAutoShowInfo );
843 0 : }
844 : }
845 0 : }
846 :
847 8 : PTDefinitionModel::PTDefinitionModel() :
848 : mnCacheId( -1 ),
849 : mnDataPosition( 0 ),
850 : mnPageWrap( 0 ),
851 : mnIndent( 1 ),
852 : mnChartFormat( 0 ),
853 : mnRowFields( 0 ),
854 : mnColFields( 0 ),
855 : mbDataOnRows( false ),
856 : mbShowError( false ),
857 : mbShowMissing( true ),
858 : mbShowItems( true ),
859 : mbDisableFieldList( false ),
860 : mbShowCalcMembers( true ),
861 : mbVisualTotals( true ),
862 : mbShowDrill( true ),
863 : mbPrintDrill( false ),
864 : mbEnableDrill( true ),
865 : mbPreserveFormatting( true ),
866 : mbUseAutoFormat( false ),
867 : mbPageOverThenDown( false ),
868 : mbSubtotalHiddenItems( false ),
869 : mbRowGrandTotals( true ),
870 : mbColGrandTotals( true ),
871 : mbFieldPrintTitles( false ),
872 : mbItemPrintTitles( false ),
873 : mbMergeItem( false ),
874 : mbShowEmptyRow( false ),
875 : mbShowEmptyCol( false ),
876 : mbShowHeaders( true ),
877 : mbFieldListSortAsc( false ),
878 8 : mbCustomListSort( true )
879 : {
880 8 : }
881 :
882 8 : PTLocationModel::PTLocationModel() :
883 : mnFirstHeaderRow( 0 ),
884 : mnFirstDataRow( 0 ),
885 : mnFirstDataCol( 0 ),
886 : mnRowPageCount( 0 ),
887 8 : mnColPageCount( 0 )
888 : {
889 8 : }
890 :
891 8 : PivotTable::PivotTable( const WorkbookHelper& rHelper ) :
892 : WorkbookHelper( rHelper ),
893 : mpDPObject(NULL),
894 : maDataField( *this, OOX_PT_DATALAYOUTFIELD ),
895 8 : mpPivotCache( 0 )
896 : {
897 8 : }
898 :
899 8 : void PivotTable::importPivotTableDefinition( const AttributeList& rAttribs )
900 : {
901 8 : maDefModel.maName = rAttribs.getXString( XML_name, OUString() );
902 8 : maDefModel.maDataCaption = rAttribs.getXString( XML_dataCaption , OUString() );
903 8 : maDefModel.maGrandTotalCaption = rAttribs.getXString( XML_grandTotalCaption, OUString() );
904 8 : maDefModel.maRowHeaderCaption = rAttribs.getXString( XML_rowHeaderCaption, OUString() );
905 8 : maDefModel.maColHeaderCaption = rAttribs.getXString( XML_colHeaderCaption, OUString() );
906 8 : maDefModel.maErrorCaption = rAttribs.getXString( XML_errorCaption, OUString() );
907 8 : maDefModel.maMissingCaption = rAttribs.getXString( XML_missingCaption, OUString() );
908 8 : maDefModel.maPageStyle = rAttribs.getXString( XML_pageStyle, OUString() );
909 8 : maDefModel.maPivotTableStyle = rAttribs.getXString( XML_pivotTableStyle, OUString() );
910 8 : maDefModel.maVacatedStyle = rAttribs.getXString( XML_vacatedStyle, OUString() );
911 8 : maDefModel.maTag = rAttribs.getXString( XML_tag, OUString() );
912 8 : maDefModel.mnCacheId = rAttribs.getInteger( XML_cacheId, -1 );
913 8 : maDefModel.mnDataPosition = rAttribs.getInteger( XML_dataPosition, 0 );
914 8 : maDefModel.mnPageWrap = rAttribs.getInteger( XML_pageWrap, 0 );
915 8 : maDefModel.mnIndent = rAttribs.getInteger( XML_indent, 1 );
916 8 : maDefModel.mnChartFormat = rAttribs.getInteger( XML_chartFormat, 0 );
917 8 : maDefModel.mnAutoFormatId = rAttribs.getInteger( XML_autoFormatId, 0 );
918 8 : maDefModel.mbDataOnRows = rAttribs.getBool( XML_dataOnRows, false );
919 8 : maDefModel.mbShowError = rAttribs.getBool( XML_showError, false );
920 8 : maDefModel.mbShowMissing = rAttribs.getBool( XML_showMissing, true );
921 8 : maDefModel.mbShowItems = rAttribs.getBool( XML_showItems, true );
922 8 : maDefModel.mbDisableFieldList = rAttribs.getBool( XML_disableFieldList, false );
923 8 : maDefModel.mbShowCalcMembers = rAttribs.getBool( XML_showCalcMbrs, true );
924 8 : maDefModel.mbVisualTotals = rAttribs.getBool( XML_visualTotals, true );
925 8 : maDefModel.mbShowDrill = rAttribs.getBool( XML_showDrill, true );
926 8 : maDefModel.mbPrintDrill = rAttribs.getBool( XML_printDrill, false );
927 8 : maDefModel.mbEnableDrill = rAttribs.getBool( XML_enableDrill, true );
928 8 : maDefModel.mbPreserveFormatting = rAttribs.getBool( XML_preserveFormatting, true );
929 8 : maDefModel.mbUseAutoFormat = rAttribs.getBool( XML_useAutoFormatting, false );
930 8 : maDefModel.mbPageOverThenDown = rAttribs.getBool( XML_pageOverThenDown, false );
931 8 : maDefModel.mbSubtotalHiddenItems = rAttribs.getBool( XML_subtotalHiddenItems, false );
932 8 : maDefModel.mbRowGrandTotals = rAttribs.getBool( XML_rowGrandTotals, true );
933 8 : maDefModel.mbColGrandTotals = rAttribs.getBool( XML_colGrandTotals, true );
934 8 : maDefModel.mbFieldPrintTitles = rAttribs.getBool( XML_fieldPrintTitles, false );
935 8 : maDefModel.mbItemPrintTitles = rAttribs.getBool( XML_itemPrintTitles, false );
936 8 : maDefModel.mbMergeItem = rAttribs.getBool( XML_mergeItem, false );
937 8 : maDefModel.mbShowEmptyRow = rAttribs.getBool( XML_showEmptyRow, false );
938 8 : maDefModel.mbShowEmptyCol = rAttribs.getBool( XML_showEmptyCol, false );
939 8 : maDefModel.mbShowHeaders = rAttribs.getBool( XML_showHeaders, true );
940 8 : maDefModel.mbFieldListSortAsc = rAttribs.getBool( XML_fieldListSortAscending, false );
941 8 : maDefModel.mbCustomListSort = rAttribs.getBool( XML_customListSort, true );
942 8 : maDefModel.mbApplyNumFmt = rAttribs.getBool( XML_applyNumberFormats, false );
943 8 : maDefModel.mbApplyFont = rAttribs.getBool( XML_applyFontFormats, false );
944 8 : maDefModel.mbApplyAlignment = rAttribs.getBool( XML_applyAlignmentFormats, false );
945 8 : maDefModel.mbApplyBorder = rAttribs.getBool( XML_applyBorderFormats, false );
946 8 : maDefModel.mbApplyFill = rAttribs.getBool( XML_applyPatternFormats, false );
947 : // OOXML and BIFF12 documentation differ: OOXML mentions width/height, BIFF12 mentions protection
948 8 : maDefModel.mbApplyProtection = rAttribs.getBool( XML_applyWidthHeightFormats, false );
949 8 : }
950 :
951 8 : void PivotTable::importLocation( const AttributeList& rAttribs, sal_Int16 nSheet )
952 : {
953 8 : getAddressConverter().convertToCellRangeUnchecked( maLocationModel.maRange, rAttribs.getString( XML_ref, OUString() ), nSheet );
954 8 : maLocationModel.mnFirstHeaderRow = rAttribs.getInteger( XML_firstHeaderRow, 0 );
955 8 : maLocationModel.mnFirstDataRow = rAttribs.getInteger( XML_firstDataRow, 0 );
956 8 : maLocationModel.mnFirstDataCol = rAttribs.getInteger( XML_firstDataCol, 0 );
957 8 : maLocationModel.mnRowPageCount = rAttribs.getInteger( XML_rowPageCount, 0 );
958 8 : maLocationModel.mnColPageCount = rAttribs.getInteger( XML_colPageCount, 0 );
959 8 : }
960 :
961 8 : void PivotTable::importRowField( const AttributeList& rAttribs )
962 : {
963 8 : importField( maRowFields, rAttribs );
964 8 : }
965 :
966 8 : void PivotTable::importColField( const AttributeList& rAttribs )
967 : {
968 8 : importField( maColFields, rAttribs );
969 8 : }
970 :
971 8 : void PivotTable::importPageField( const AttributeList& rAttribs )
972 : {
973 8 : PTPageFieldModel aModel;
974 8 : aModel.maName = rAttribs.getXString( XML_name, OUString() );
975 8 : aModel.mnField = rAttribs.getInteger( XML_fld, -1 );
976 : // specification is wrong, XML_item is not the cache item, but the field item
977 8 : aModel.mnItem = rAttribs.getInteger( XML_item, BIFF12_PTPAGEFIELD_MULTIITEMS );
978 8 : maPageFields.push_back( aModel );
979 8 : }
980 :
981 12 : void PivotTable::importDataField( const AttributeList& rAttribs )
982 : {
983 12 : PTDataFieldModel aModel;
984 12 : aModel.maName = rAttribs.getXString( XML_name, OUString() );
985 12 : aModel.mnField = rAttribs.getInteger( XML_fld, -1 );
986 12 : aModel.mnSubtotal = rAttribs.getToken( XML_subtotal, XML_sum );
987 12 : aModel.mnShowDataAs = rAttribs.getToken( XML_showDataAs, XML_normal );
988 12 : aModel.mnBaseField = rAttribs.getInteger( XML_baseField, -1 );
989 12 : aModel.mnBaseItem = rAttribs.getInteger( XML_baseItem, -1 );
990 12 : aModel.mnNumFmtId = rAttribs.getInteger( XML_numFmtId, 0 );
991 12 : maDataFields.push_back( aModel );
992 12 : }
993 :
994 0 : void PivotTable::importPTDefinition( SequenceInputStream& rStrm )
995 : {
996 : sal_uInt32 nFlags1, nFlags2, nFlags3;
997 : sal_uInt8 nDataAxis;
998 0 : rStrm >> nFlags1 >> nFlags2 >> nFlags3 >> nDataAxis;
999 0 : maDefModel.mnPageWrap = rStrm.readuInt8();
1000 0 : rStrm.skip( 2 ); // refresh versions
1001 0 : rStrm >> maDefModel.mnDataPosition;
1002 0 : maDefModel.mnAutoFormatId = rStrm.readuInt16();
1003 0 : rStrm.skip( 2 ); // unused
1004 0 : rStrm >> maDefModel.mnChartFormat >> maDefModel.mnCacheId >> maDefModel.maName;
1005 0 : if( getFlag( nFlags2, BIFF12_PTDEF_HASDATACAPTION ) )
1006 0 : rStrm >> maDefModel.maDataCaption;
1007 0 : if( getFlag( nFlags2, BIFF12_PTDEF_HASGRANDTOTALCAPTION ) )
1008 0 : rStrm >> maDefModel.maGrandTotalCaption;
1009 0 : if( !getFlag( nFlags3, BIFF12_PTDEF_NOERRORCAPTION ) ) // missing flag indicates existing string
1010 0 : rStrm >> maDefModel.maErrorCaption;
1011 0 : if( !getFlag( nFlags3, BIFF12_PTDEF_NOMISSINGCAPTION ) ) // missing flag indicates existing string
1012 0 : rStrm >> maDefModel.maMissingCaption;
1013 0 : if( getFlag( nFlags2, BIFF12_PTDEF_HASPAGESTYLE ) )
1014 0 : rStrm >> maDefModel.maPageStyle;
1015 0 : if( getFlag( nFlags2, BIFF12_PTDEF_HASPIVOTTABLESTYLE ) )
1016 0 : rStrm >> maDefModel.maPivotTableStyle;
1017 0 : if( getFlag( nFlags2, BIFF12_PTDEF_HASVACATEDSTYLE ) )
1018 0 : rStrm >> maDefModel.maVacatedStyle;
1019 0 : if( getFlag( nFlags2, BIFF12_PTDEF_HASTAG ) )
1020 0 : rStrm >> maDefModel.maTag;
1021 0 : if( getFlag( nFlags3, BIFF12_PTDEF_HASCOLHEADERCAPTION ) ) // TODO: right order (col/row)? spec is unclear
1022 0 : rStrm >> maDefModel.maColHeaderCaption;
1023 0 : if( getFlag( nFlags3, BIFF12_PTDEF_HASROWHEADERCAPTION ) )
1024 0 : rStrm >> maDefModel.maRowHeaderCaption;
1025 :
1026 : SAL_WARN_IF(
1027 : (nDataAxis != BIFF12_PTDEF_ROWAXIS) && (nDataAxis != BIFF12_PTDEF_COLAXIS),
1028 : "sc.filter",
1029 : "PivotTable::importPTDefinition - unexpected axis position for data field");
1030 :
1031 0 : maDefModel.mnIndent = extractValue< sal_uInt8 >( nFlags1, 24, 7 );
1032 0 : maDefModel.mbDataOnRows = nDataAxis == BIFF12_PTDEF_ROWAXIS;
1033 0 : maDefModel.mbShowError = getFlag( nFlags2, BIFF12_PTDEF_SHOWERROR );
1034 0 : maDefModel.mbShowMissing = getFlag( nFlags2, BIFF12_PTDEF_SHOWMISSING );
1035 0 : maDefModel.mbShowItems = getFlag( nFlags1, BIFF12_PTDEF_SHOWITEMS );
1036 0 : maDefModel.mbDisableFieldList = getFlag( nFlags1, BIFF12_PTDEF_DISABLEFIELDLIST );
1037 0 : maDefModel.mbShowCalcMembers = !getFlag( nFlags1, BIFF12_PTDEF_HIDECALCMEMBERS );
1038 0 : maDefModel.mbVisualTotals = !getFlag( nFlags1, BIFF12_PTDEF_WITHHIDDENTOTALS );
1039 0 : maDefModel.mbShowDrill = !getFlag( nFlags1, BIFF12_PTDEF_HIDEDRILL );
1040 0 : maDefModel.mbPrintDrill = getFlag( nFlags1, BIFF12_PTDEF_PRINTDRILL );
1041 0 : maDefModel.mbEnableDrill = getFlag( nFlags2, BIFF12_PTDEF_ENABLEDRILL );
1042 0 : maDefModel.mbPreserveFormatting = getFlag( nFlags2, BIFF12_PTDEF_PRESERVEFORMATTING );
1043 0 : maDefModel.mbUseAutoFormat = getFlag( nFlags2, BIFF12_PTDEF_USEAUTOFORMAT );
1044 0 : maDefModel.mbPageOverThenDown = getFlag( nFlags2, BIFF12_PTDEF_PAGEOVERTHENDOWN );
1045 0 : maDefModel.mbSubtotalHiddenItems = getFlag( nFlags2, BIFF12_PTDEF_SUBTOTALHIDDENITEMS );
1046 0 : maDefModel.mbRowGrandTotals = getFlag( nFlags2, BIFF12_PTDEF_ROWGRANDTOTALS );
1047 0 : maDefModel.mbColGrandTotals = getFlag( nFlags2, BIFF12_PTDEF_COLGRANDTOTALS );
1048 0 : maDefModel.mbFieldPrintTitles = getFlag( nFlags2, BIFF12_PTDEF_FIELDPRINTTITLES );
1049 0 : maDefModel.mbItemPrintTitles = getFlag( nFlags2, BIFF12_PTDEF_ITEMPRINTTITLES );
1050 0 : maDefModel.mbMergeItem = getFlag( nFlags2, BIFF12_PTDEF_MERGEITEM );
1051 0 : maDefModel.mbApplyNumFmt = getFlag( nFlags2, BIFF12_PTDEF_APPLYNUMFMT );
1052 0 : maDefModel.mbApplyFont = getFlag( nFlags2, BIFF12_PTDEF_APPLYFONT );
1053 0 : maDefModel.mbApplyAlignment = getFlag( nFlags2, BIFF12_PTDEF_APPLYALIGNMENT );
1054 0 : maDefModel.mbApplyBorder = getFlag( nFlags2, BIFF12_PTDEF_APPLYBORDER );
1055 0 : maDefModel.mbApplyFill = getFlag( nFlags2, BIFF12_PTDEF_APPLYFILL );
1056 0 : maDefModel.mbApplyProtection = getFlag( nFlags2, BIFF12_PTDEF_APPLYPROTECTION );
1057 0 : maDefModel.mbShowEmptyRow = getFlag( nFlags2, BIFF12_PTDEF_SHOWEMPTYROW );
1058 0 : maDefModel.mbShowEmptyCol = getFlag( nFlags2, BIFF12_PTDEF_SHOWEMPTYCOL );
1059 0 : maDefModel.mbShowHeaders = !getFlag( nFlags1, BIFF12_PTDEF_HIDEHEADERS );
1060 0 : maDefModel.mbFieldListSortAsc = getFlag( nFlags3, BIFF12_PTDEF_FIELDLISTSORTASC );
1061 0 : maDefModel.mbCustomListSort = !getFlag( nFlags3, BIFF12_PTDEF_NOCUSTOMLISTSORT );
1062 0 : }
1063 :
1064 0 : void PivotTable::importPTLocation( SequenceInputStream& rStrm, sal_Int16 nSheet )
1065 : {
1066 0 : BinRange aBinRange;
1067 0 : rStrm >> aBinRange >> maLocationModel.mnFirstHeaderRow
1068 0 : >> maLocationModel.mnFirstDataRow >> maLocationModel.mnFirstDataCol
1069 0 : >> maLocationModel.mnRowPageCount >> maLocationModel.mnColPageCount;
1070 0 : getAddressConverter().convertToCellRangeUnchecked( maLocationModel.maRange, aBinRange, nSheet );
1071 0 : }
1072 :
1073 0 : void PivotTable::importPTRowFields( SequenceInputStream& rStrm )
1074 : {
1075 0 : importFields( maRowFields, rStrm );
1076 0 : }
1077 :
1078 0 : void PivotTable::importPTColFields( SequenceInputStream& rStrm )
1079 : {
1080 0 : importFields( maColFields, rStrm );
1081 0 : }
1082 :
1083 0 : void PivotTable::importPTPageField( SequenceInputStream& rStrm )
1084 : {
1085 0 : PTPageFieldModel aModel;
1086 : sal_uInt8 nFlags;
1087 0 : rStrm >> aModel.mnField >> aModel.mnItem;
1088 0 : rStrm.skip( 4 ); // hierarchy
1089 0 : rStrm >> nFlags;
1090 0 : if( getFlag( nFlags, BIFF12_PTPAGEFIELD_HASNAME ) )
1091 0 : rStrm >> aModel.maName;
1092 0 : maPageFields.push_back( aModel );
1093 0 : }
1094 :
1095 0 : void PivotTable::importPTDataField( SequenceInputStream& rStrm )
1096 : {
1097 0 : PTDataFieldModel aModel;
1098 : sal_Int32 nSubtotal, nShowDataAs;
1099 : sal_uInt8 nHasName;
1100 0 : rStrm >> aModel.mnField >> nSubtotal >> nShowDataAs >> aModel.mnBaseField >> aModel.mnBaseItem >> aModel.mnNumFmtId >> nHasName;
1101 0 : if( nHasName == 1 )
1102 0 : rStrm >> aModel.maName;
1103 0 : aModel.setBiffSubtotal( nSubtotal );
1104 0 : aModel.setBiffShowDataAs( nShowDataAs );
1105 0 : maDataFields.push_back( aModel );
1106 0 : }
1107 :
1108 48 : PivotTableField& PivotTable::createTableField()
1109 : {
1110 48 : sal_Int32 nFieldIndex = static_cast< sal_Int32 >( maFields.size() );
1111 48 : PivotTableFieldVector::value_type xTableField( new PivotTableField( *this, nFieldIndex ) );
1112 48 : maFields.push_back( xTableField );
1113 48 : return *xTableField;
1114 : }
1115 :
1116 0 : PivotTableFilter& PivotTable::createTableFilter()
1117 : {
1118 0 : PivotTableFilterVector::value_type xTableFilter( new PivotTableFilter( *this ) );
1119 0 : maFilters.push_back( xTableFilter );
1120 0 : return *xTableFilter;
1121 : }
1122 :
1123 8 : void PivotTable::finalizeImport()
1124 : {
1125 8 : if( getAddressConverter().validateCellRange( maLocationModel.maRange, true, true ) )
1126 : {
1127 8 : mpPivotCache = getPivotCaches().importPivotCacheFragment( maDefModel.mnCacheId );
1128 8 : if( mpPivotCache && mpPivotCache->isValidDataSource() && !maDefModel.maName.isEmpty() )
1129 : {
1130 : // clear destination area of the original pivot table
1131 : try
1132 : {
1133 8 : Reference< XSheetOperation > xSheetOp( getCellRangeFromDoc( maLocationModel.maRange ), UNO_QUERY_THROW );
1134 : using namespace ::com::sun::star::sheet::CellFlags;
1135 8 : xSheetOp->clearContents( VALUE | DATETIME | STRING | FORMULA | HARDATTR | STYLES | EDITATTR | FORMATTED );
1136 : }
1137 0 : catch( Exception& )
1138 : {
1139 : }
1140 :
1141 : try
1142 : {
1143 : // create a new data pilot descriptor based on the source data
1144 8 : Reference< XDataPilotTablesSupplier > xDPTablesSupp( getSheetFromDoc( maLocationModel.maRange.Sheet ), UNO_QUERY_THROW );
1145 16 : Reference< XDataPilotTables > xDPTables( xDPTablesSupp->getDataPilotTables(), UNO_SET_THROW );
1146 8 : mxDPDescriptor.set( xDPTables->createDataPilotDescriptor(), UNO_SET_THROW );
1147 8 : mxDPDescriptor->setSourceRange( mpPivotCache->getSourceRange() );
1148 8 : mxDPDescriptor->setTag( maDefModel.maTag );
1149 :
1150 : // TODO: This is a hack. Eventually we need to convert the whole thing to the internal API.
1151 8 : ScDataPilotDescriptorBase* pImpl = ScDataPilotDescriptorBase::getImplementation(mxDPDescriptor);
1152 8 : if (!pImpl)
1153 0 : return;
1154 :
1155 8 : mpDPObject = pImpl->GetDPObject();
1156 8 : if (!mpDPObject)
1157 0 : return;
1158 :
1159 : // global data pilot properties
1160 16 : PropertySet aDescProp( mxDPDescriptor );
1161 8 : aDescProp.setProperty( PROP_ColumnGrand, maDefModel.mbColGrandTotals );
1162 8 : aDescProp.setProperty( PROP_RowGrand, maDefModel.mbRowGrandTotals );
1163 8 : aDescProp.setProperty( PROP_ShowFilterButton, false );
1164 8 : aDescProp.setProperty( PROP_DrillDownOnDoubleClick, maDefModel.mbEnableDrill );
1165 :
1166 : // finalize all fields, this finds field names and creates grouping fields
1167 8 : maFields.forEachMem( &PivotTableField::finalizeImport, ::boost::cref( mxDPDescriptor ) );
1168 :
1169 : // all row fields
1170 16 : for( IndexVector::iterator aIt = maRowFields.begin(), aEnd = maRowFields.end(); aIt != aEnd; ++aIt )
1171 8 : if( PivotTableField* pField = getTableField( *aIt ) )
1172 8 : pField->convertRowField();
1173 :
1174 : // all column fields
1175 16 : for( IndexVector::iterator aIt = maColFields.begin(), aEnd = maColFields.end(); aIt != aEnd; ++aIt )
1176 8 : if( PivotTableField* pField = getTableField( *aIt ) )
1177 8 : pField->convertColField();
1178 :
1179 : // all page fields
1180 16 : for( PageFieldVector::iterator aIt = maPageFields.begin(), aEnd = maPageFields.end(); aIt != aEnd; ++aIt )
1181 8 : if( PivotTableField* pField = getTableField( aIt->mnField ) )
1182 8 : pField->convertPageField( *aIt );
1183 :
1184 : // all hidden fields
1185 16 : ::std::set< sal_Int32 > aVisFields;
1186 8 : aVisFields.insert( maRowFields.begin(), maRowFields.end() );
1187 8 : aVisFields.insert( maColFields.begin(), maColFields.end() );
1188 16 : for( PageFieldVector::iterator aIt = maPageFields.begin(), aEnd = maPageFields.end(); aIt != aEnd; ++aIt )
1189 8 : aVisFields.insert( aIt->mnField );
1190 56 : for( PivotTableFieldVector::iterator aBeg = maFields.begin(), aIt = aBeg, aEnd = maFields.end(); aIt != aEnd; ++aIt )
1191 48 : if( aVisFields.count( static_cast< sal_Int32 >( aIt - aBeg ) ) == 0 )
1192 28 : (*aIt)->convertHiddenField();
1193 :
1194 : // all data fields
1195 20 : for( DataFieldVector::iterator aIt = maDataFields.begin(), aEnd = maDataFields.end(); aIt != aEnd; ++aIt )
1196 : {
1197 12 : if( const PivotCacheField* pCacheField = getCacheField( aIt->mnField ) )
1198 : {
1199 12 : if ( pCacheField-> getGroupBaseField() != -1 )
1200 0 : aIt->mnField = pCacheField-> getGroupBaseField();
1201 : }
1202 12 : if( PivotTableField* pField = getTableField( aIt->mnField ) )
1203 12 : pField->convertDataField( *aIt );
1204 : }
1205 :
1206 : // filters
1207 8 : maFilters.forEachMem( &PivotTableFilter::finalizeImport );
1208 :
1209 : // calculate base position of table
1210 8 : CellAddress aPos( maLocationModel.maRange.Sheet, maLocationModel.maRange.StartColumn, maLocationModel.maRange.StartRow );
1211 : /* If page fields exist, include them into the destination
1212 : area (they are excluded in Excel). Add an extra blank row. */
1213 8 : if( !maPageFields.empty() )
1214 4 : aPos.Row = ::std::max< sal_Int32 >( static_cast< sal_Int32 >( aPos.Row - maPageFields.size() - 1 ), 0 );
1215 :
1216 : // insert the DataPilot table into the sheet
1217 16 : xDPTables->insertNewByName( maDefModel.maName, aPos, mxDPDescriptor );
1218 : }
1219 0 : catch( Exception& )
1220 : {
1221 : OSL_FAIL( "PivotTable::finalizeImport - exception while creating the DataPilot table" );
1222 : }
1223 : }
1224 : }
1225 : }
1226 :
1227 0 : void PivotTable::finalizeDateGroupingImport( const Reference< XDataPilotField >& rxBaseDPField, sal_Int32 nBaseFieldIdx )
1228 : {
1229 : // process all fields, there is no chaining information in the cache fields
1230 0 : maFields.forEachMem( &PivotTableField::finalizeDateGroupingImport, ::boost::cref( rxBaseDPField ), nBaseFieldIdx );
1231 0 : }
1232 :
1233 0 : void PivotTable::finalizeParentGroupingImport( const Reference< XDataPilotField >& rxBaseDPField,
1234 : const PivotCacheField& rBaseCacheField, PivotCacheGroupItemVector& orItemNames )
1235 : {
1236 : // try to create parent group fields that group the items of the passed base field
1237 0 : if( PivotTableField* pParentTableField = maFields.get( rBaseCacheField.getParentGroupField() ).get() )
1238 0 : pParentTableField->finalizeParentGroupingImport( rxBaseDPField, rBaseCacheField, orItemNames );
1239 0 : }
1240 :
1241 60 : Reference< XDataPilotField > PivotTable::getDataPilotField( const OUString& rFieldName ) const
1242 : {
1243 60 : Reference< XDataPilotField > xDPField;
1244 60 : if( !rFieldName.isEmpty() && mxDPDescriptor.is() ) try
1245 : {
1246 60 : Reference< XNameAccess > xDPFieldsNA( mxDPDescriptor->getDataPilotFields(), UNO_QUERY_THROW );
1247 60 : xDPField.set( xDPFieldsNA->getByName( rFieldName ), UNO_QUERY );
1248 : }
1249 0 : catch( Exception& )
1250 : {
1251 : }
1252 60 : return xDPField;
1253 : }
1254 :
1255 0 : Reference< XDataPilotField > PivotTable::getDataPilotField( sal_Int32 nFieldIdx ) const
1256 : {
1257 0 : Reference< XDataPilotField > xDPField;
1258 0 : if( const PivotTableField* pTableField = maFields.get( nFieldIdx ).get() )
1259 0 : xDPField = getDataPilotField( pTableField->getDPFieldName() );
1260 0 : return xDPField;
1261 : }
1262 :
1263 4 : Reference< XDataPilotField > PivotTable::getDataLayoutField() const
1264 : {
1265 4 : Reference< XDataPilotField > xDPField;
1266 : try
1267 : {
1268 4 : Reference< XDataPilotDataLayoutFieldSupplier > xDPDataFieldSupp( mxDPDescriptor, UNO_QUERY_THROW );
1269 4 : xDPField = xDPDataFieldSupp->getDataLayoutField();
1270 : }
1271 0 : catch( Exception& )
1272 : {
1273 : }
1274 4 : return xDPField;
1275 : }
1276 :
1277 108 : const PivotCacheField* PivotTable::getCacheField( sal_Int32 nFieldIdx ) const
1278 : {
1279 108 : return mpPivotCache ? mpPivotCache->getCacheField( nFieldIdx ) : 0;
1280 : }
1281 :
1282 0 : const PivotCacheField* PivotTable::getCacheFieldOfDataField( sal_Int32 nDataItemIdx ) const
1283 : {
1284 0 : const PTDataFieldModel* pDataField = ContainerHelper::getVectorElement( maDataFields, nDataItemIdx );
1285 0 : return pDataField ? getCacheField( pDataField->mnField ) : 0;
1286 : }
1287 :
1288 48 : sal_Int32 PivotTable::getCacheDatabaseIndex( sal_Int32 nFieldIdx ) const
1289 : {
1290 48 : return mpPivotCache ? mpPivotCache->getCacheDatabaseIndex( nFieldIdx ) : -1;
1291 : }
1292 :
1293 : // private --------------------------------------------------------------------
1294 :
1295 36 : PivotTableField* PivotTable::getTableField( sal_Int32 nFieldIdx )
1296 : {
1297 36 : return (nFieldIdx == OOX_PT_DATALAYOUTFIELD) ? &maDataField : maFields.get( nFieldIdx ).get();
1298 : }
1299 :
1300 16 : void PivotTable::importField( IndexVector& orFields, const AttributeList& rAttribs )
1301 : {
1302 16 : orFields.push_back( rAttribs.getInteger( XML_x, -1 ) );
1303 16 : }
1304 :
1305 0 : void PivotTable::importFields( IndexVector& orFields, SequenceInputStream& rStrm )
1306 : {
1307 : OSL_ENSURE( orFields.empty(), "PivotTable::importFields - multiple record instances" );
1308 0 : orFields.clear();
1309 0 : sal_Int32 nCount = rStrm.readInt32();
1310 : OSL_ENSURE( 4 * nCount == rStrm.getRemaining(), "PivotTable::importFields - invalid field count" );
1311 0 : nCount = static_cast< sal_Int32 >( rStrm.getRemaining() / 4 );
1312 0 : for( sal_Int32 nIdx = 0; nIdx < nCount; ++nIdx )
1313 0 : orFields.push_back( rStrm.readInt32() );
1314 0 : }
1315 :
1316 128 : PivotTableBuffer::PivotTableBuffer( const WorkbookHelper& rHelper ) :
1317 128 : WorkbookHelper( rHelper )
1318 : {
1319 128 : }
1320 :
1321 8 : PivotTable& PivotTableBuffer::createPivotTable()
1322 : {
1323 8 : PivotTableVector::value_type xTable( new PivotTable( *this ) );
1324 8 : maTables.push_back( xTable );
1325 8 : return *xTable;
1326 : }
1327 :
1328 128 : void PivotTableBuffer::finalizeImport()
1329 : {
1330 128 : maTables.forEachMem( &PivotTable::finalizeImport );
1331 128 : }
1332 :
1333 : } // namespace xls
1334 48 : } // namespace oox
1335 :
1336 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|