1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #include <xepivotxml.hxx>
11 #include <dpcache.hxx>
12 #include <dpdimsave.hxx>
13 #include <dpitemdata.hxx>
14 #include <dpobject.hxx>
17 #include <document.hxx>
18 #include <generalfunction.hxx>
19 #include <unonames.hxx>
20 #include <xestyle.hxx>
23 #include <o3tl/temporary.hxx>
24 #include <o3tl/safeint.hxx>
25 #include <oox/export/utils.hxx>
26 #include <oox/token/namespaces.hxx>
27 #include <sax/tools/converter.hxx>
28 #include <sax/fastattribs.hxx>
29 #include <svl/numformat.hxx>
31 #include <com/sun/star/beans/XPropertySet.hpp>
32 #include <com/sun/star/sheet/DataPilotFieldGroupBy.hpp>
33 #include <com/sun/star/sheet/DataPilotFieldOrientation.hpp>
34 #include <com/sun/star/sheet/DataPilotFieldLayoutMode.hpp>
35 #include <com/sun/star/sheet/DataPilotOutputRangeType.hpp>
36 #include <com/sun/star/sheet/XDimensionsSupplier.hpp>
41 using namespace com::sun::star
;
45 void savePivotCacheRecordsXml( XclExpXmlStream
& rStrm
, const ScDPCache
& rCache
)
47 SCROW nCount
= rCache
.GetDataSize();
48 size_t nFieldCount
= rCache
.GetFieldCount();
50 sax_fastparser::FSHelperPtr
& pRecStrm
= rStrm
.GetCurrentStream();
51 pRecStrm
->startElement(XML_pivotCacheRecords
,
52 XML_xmlns
, rStrm
.getNamespaceURL(OOX_NS(xls
)).toUtf8(),
53 FSNS(XML_xmlns
, XML_r
), rStrm
.getNamespaceURL(OOX_NS(officeRel
)).toUtf8(),
54 XML_count
, OString::number(static_cast<tools::Long
>(nCount
)));
56 for (SCROW i
= 0; i
< nCount
; ++i
)
58 pRecStrm
->startElement(XML_r
);
59 for (size_t nField
= 0; nField
< nFieldCount
; ++nField
)
61 const ScDPCache::IndexArrayType
* pArray
= rCache
.GetFieldIndexArray(nField
);
63 assert(o3tl::make_unsigned(i
) < pArray
->size());
65 // We are using XML_x reference (like: <x v="0"/>), instead of values here (eg: <s v="No Discount"/>).
66 // That's why in SavePivotCacheXml method, we need to list all items.
67 pRecStrm
->singleElement(XML_x
, XML_v
, OString::number((*pArray
)[i
]));
69 pRecStrm
->endElement(XML_r
);
72 pRecStrm
->endElement(XML_pivotCacheRecords
);
75 const char* toOOXMLAxisType( sheet::DataPilotFieldOrientation eOrient
)
79 case sheet::DataPilotFieldOrientation_COLUMN
:
81 case sheet::DataPilotFieldOrientation_ROW
:
83 case sheet::DataPilotFieldOrientation_PAGE
:
85 case sheet::DataPilotFieldOrientation_DATA
:
87 case sheet::DataPilotFieldOrientation_HIDDEN
:
95 const char* toOOXMLSubtotalType(ScGeneralFunction eFunc
)
99 case ScGeneralFunction::SUM
:
101 case ScGeneralFunction::COUNT
:
103 case ScGeneralFunction::AVERAGE
:
105 case ScGeneralFunction::MAX
:
107 case ScGeneralFunction::MIN
:
109 case ScGeneralFunction::PRODUCT
:
111 case ScGeneralFunction::COUNTNUMS
:
113 case ScGeneralFunction::STDEV
:
115 case ScGeneralFunction::STDEVP
:
117 case ScGeneralFunction::VAR
:
119 case ScGeneralFunction::VARP
:
129 XclExpXmlPivotCaches::XclExpXmlPivotCaches( const XclExpRoot
& rRoot
) :
132 void XclExpXmlPivotCaches::SaveXml( XclExpXmlStream
& rStrm
)
134 sax_fastparser::FSHelperPtr
& pWorkbookStrm
= rStrm
.GetCurrentStream();
135 pWorkbookStrm
->startElement(XML_pivotCaches
);
137 for (size_t i
= 0, n
= maCaches
.size(); i
< n
; ++i
)
139 const Entry
& rEntry
= maCaches
[i
];
141 sal_Int32 nCacheId
= i
+ 1;
143 sax_fastparser::FSHelperPtr pPCStrm
= rStrm
.CreateOutputStream(
144 XclXmlUtils::GetStreamName("xl/pivotCache/", "pivotCacheDefinition", nCacheId
),
145 XclXmlUtils::GetStreamName(nullptr, "pivotCache/pivotCacheDefinition", nCacheId
),
146 rStrm
.GetCurrentStream()->getOutputStream(),
147 CREATE_XL_CONTENT_TYPE("pivotCacheDefinition"),
148 CREATE_OFFICEDOC_RELATION_TYPE("pivotCacheDefinition"),
151 pWorkbookStrm
->singleElement(XML_pivotCache
,
152 XML_cacheId
, OString::number(nCacheId
),
153 FSNS(XML_r
, XML_id
), aRelId
.toUtf8());
155 rStrm
.PushStream(pPCStrm
);
156 SavePivotCacheXml(rStrm
, rEntry
, nCacheId
);
160 pWorkbookStrm
->endElement(XML_pivotCaches
);
163 void XclExpXmlPivotCaches::SetCaches( std::vector
<Entry
>&& rCaches
)
165 maCaches
= std::move(rCaches
);
168 bool XclExpXmlPivotCaches::HasCaches() const
170 return !maCaches
.empty();
173 const XclExpXmlPivotCaches::Entry
* XclExpXmlPivotCaches::GetCache( sal_Int32 nCacheId
) const
176 // cache ID is 1-based.
179 size_t nPos
= nCacheId
- 1;
180 if (nPos
>= maCaches
.size())
183 return &maCaches
[nPos
];
188 * Create combined date and time string according the requirements of Excel.
189 * A single point in time can be represented by concatenating a complete date expression,
190 * the letter T as a delimiter, and a valid time expression. For example, "2007-04-05T14:30".
192 * fSerialDateTime - a number representing the number of days since 1900-Jan-0 (integer portion of the number),
193 * plus a fractional portion of a 24 hour day (fractional portion of the number).
195 OUString
GetExcelFormattedDate( double fSerialDateTime
, const SvNumberFormatter
& rFormatter
)
197 // tdf#125055: properly round the value to seconds when truncating nanoseconds below
198 constexpr double fHalfSecond
= 1 / 86400.0 * 0.5;
199 css::util::DateTime aUDateTime
200 = (DateTime(rFormatter
.GetNullDate()) + fSerialDateTime
+ fHalfSecond
).GetUNODateTime();
201 // We need to reset nanoseconds, to avoid string like: "1982-02-18T16:04:47.999999849"
202 aUDateTime
.NanoSeconds
= 0;
204 ::sax::Converter::convertDateTime(sBuf
, aUDateTime
, nullptr, true);
205 return sBuf
.makeStringAndClear();
208 // Excel seems to expect different order of group item values; we need to rearrange elements
209 // to output "<date1" first, then elements, then ">date2" last.
210 // Since ScDPItemData::DateFirst is -1, ScDPItemData::DateLast is 10000, and other date group
211 // items would fit between those in order (like 0 = Jan, 1 = Feb, etc.), we can simply sort
212 // the items by value.
213 std::vector
<OUString
> SortGroupItems(const ScDPCache
& rCache
, tools::Long nDim
)
218 const ScDPItemData
* pData
;
220 std::vector
<ItemData
> aDataToSort
;
222 rCache
.GetGroupDimMemberIds(nDim
, aGIIds
);
223 for (sal_Int32 id
: aGIIds
)
225 const ScDPItemData
* pGIData
= rCache
.GetItemDataById(nDim
, id
);
226 if (pGIData
->GetType() == ScDPItemData::GroupValue
)
228 auto aGroupVal
= pGIData
->GetGroupValue();
229 aDataToSort
.push_back({ aGroupVal
.mnValue
, pGIData
});
232 std::sort(aDataToSort
.begin(), aDataToSort
.end(),
233 [](const ItemData
& a
, const ItemData
& b
) { return a
.nVal
< b
.nVal
; });
235 std::vector
<OUString
> aSortedResult
;
236 for (const auto& el
: aDataToSort
)
238 aSortedResult
.push_back(rCache
.GetFormattedString(nDim
, *el
.pData
, false));
240 return aSortedResult
;
244 void XclExpXmlPivotCaches::SavePivotCacheXml( XclExpXmlStream
& rStrm
, const Entry
& rEntry
, sal_Int32 nCounter
)
246 assert(rEntry
.mpCache
);
247 const ScDPCache
& rCache
= *rEntry
.mpCache
;
249 sax_fastparser::FSHelperPtr
& pDefStrm
= rStrm
.GetCurrentStream();
252 sax_fastparser::FSHelperPtr pRecStrm
= rStrm
.CreateOutputStream(
253 XclXmlUtils::GetStreamName("xl/pivotCache/", "pivotCacheRecords", nCounter
),
254 XclXmlUtils::GetStreamName(nullptr, "pivotCacheRecords", nCounter
),
255 pDefStrm
->getOutputStream(),
256 CREATE_XL_CONTENT_TYPE("pivotCacheRecords"),
257 CREATE_OFFICEDOC_RELATION_TYPE("pivotCacheRecords"),
260 rStrm
.PushStream(pRecStrm
);
261 savePivotCacheRecordsXml(rStrm
, rCache
);
264 pDefStrm
->startElement(XML_pivotCacheDefinition
,
265 XML_xmlns
, rStrm
.getNamespaceURL(OOX_NS(xls
)).toUtf8(),
266 FSNS(XML_xmlns
, XML_r
), rStrm
.getNamespaceURL(OOX_NS(officeRel
)).toUtf8(),
267 FSNS(XML_r
, XML_id
), aRelId
.toUtf8(),
268 XML_recordCount
, OString::number(rEntry
.mpCache
->GetDataSize()),
269 XML_createdVersion
, "3"); // MS Excel 2007, tdf#112936: setting version number makes MSO to handle the pivot table differently
271 pDefStrm
->startElement(XML_cacheSource
, XML_type
, "worksheet");
274 GetDoc().GetName(rEntry
.maSrcRange
.aStart
.Tab(), aSheetName
);
275 pDefStrm
->singleElement(XML_worksheetSource
,
276 XML_ref
, XclXmlUtils::ToOString(rStrm
.GetRoot().GetDoc(), rEntry
.maSrcRange
),
277 XML_sheet
, aSheetName
.toUtf8());
279 pDefStrm
->endElement(XML_cacheSource
);
281 size_t nCount
= rCache
.GetFieldCount();
282 const size_t nGroupFieldCount
= rCache
.GetGroupFieldCount();
283 pDefStrm
->startElement(XML_cacheFields
,
284 XML_count
, OString::number(static_cast<tools::Long
>(nCount
+ nGroupFieldCount
)));
286 auto WriteFieldGroup
= [this, &rCache
, pDefStrm
](size_t i
, size_t base
) {
287 const sal_Int32 nDatePart
= rCache
.GetGroupType(i
);
293 case sheet::DataPilotFieldGroupBy::SECONDS
:
294 sGroupBy
= "seconds";
296 case sheet::DataPilotFieldGroupBy::MINUTES
:
297 sGroupBy
= "minutes";
299 case sheet::DataPilotFieldGroupBy::HOURS
:
302 case sheet::DataPilotFieldGroupBy::DAYS
:
305 case sheet::DataPilotFieldGroupBy::MONTHS
:
308 case sheet::DataPilotFieldGroupBy::QUARTERS
:
309 sGroupBy
= "quarters";
311 case sheet::DataPilotFieldGroupBy::YEARS
:
316 // fieldGroup element
317 pDefStrm
->startElement(XML_fieldGroup
, XML_base
, OString::number(base
));
319 SvNumberFormatter
& rFormatter
= GetFormatter();
322 const ScDPNumGroupInfo
* pGI
= rCache
.GetNumGroupInfo(i
);
323 auto pGroupAttList
= sax_fastparser::FastSerializerHelper::createAttrList();
324 pGroupAttList
->add(XML_groupBy
, sGroupBy
);
325 // Possible TODO: find out when to write autoStart attribute for years grouping
326 pGroupAttList
->add(XML_startDate
, GetExcelFormattedDate(pGI
->mfStart
, rFormatter
).toUtf8());
327 pGroupAttList
->add(XML_endDate
, GetExcelFormattedDate(pGI
->mfEnd
, rFormatter
).toUtf8());
329 pGroupAttList
->add(XML_groupInterval
, OString::number(pGI
->mfStep
));
330 pDefStrm
->singleElement(XML_rangePr
, pGroupAttList
);
332 // groupItems element
333 auto aElemVec
= SortGroupItems(rCache
, i
);
334 pDefStrm
->startElement(XML_groupItems
, XML_count
, OString::number(aElemVec
.size()));
335 for (const auto& sElem
: aElemVec
)
337 pDefStrm
->singleElement(XML_s
, XML_v
, sElem
.toUtf8());
339 pDefStrm
->endElement(XML_groupItems
);
340 pDefStrm
->endElement(XML_fieldGroup
);
343 for (size_t i
= 0; i
< nCount
; ++i
)
345 OUString aName
= rCache
.GetDimensionName(i
);
347 pDefStrm
->startElement(XML_cacheField
,
348 XML_name
, aName
.toUtf8(),
349 XML_numFmtId
, OString::number(0));
351 const ScDPCache::ScDPItemDataVec
& rFieldItems
= rCache
.GetDimMemberValues(i
);
353 std::set
<ScDPItemData::Type
> aDPTypes
;
354 double fMin
= std::numeric_limits
<double>::infinity(), fMax
= -std::numeric_limits
<double>::infinity();
355 bool isValueInteger
= true;
356 bool isContainsDate
= rCache
.IsDateDimension(i
);
357 bool isLongText
= false;
358 for (const auto& rFieldItem
: rFieldItems
)
360 ScDPItemData::Type eType
= rFieldItem
.GetType();
361 // tdf#123939 : error and string are same for cache; if both are present, keep only one
362 if (eType
== ScDPItemData::Error
)
363 eType
= ScDPItemData::String
;
364 aDPTypes
.insert(eType
);
365 if (eType
== ScDPItemData::Value
)
367 double fVal
= rFieldItem
.GetValue();
368 fMin
= std::min(fMin
, fVal
);
369 fMax
= std::max(fMax
, fVal
);
371 // Check if all values are integers
372 if (isValueInteger
&& (modf(fVal
, &o3tl::temporary(double())) != 0.0))
374 isValueInteger
= false;
377 else if (eType
== ScDPItemData::String
&& !isLongText
)
379 isLongText
= rFieldItem
.GetString().getLength() > 255;
383 auto pAttList
= sax_fastparser::FastSerializerHelper::createAttrList();
384 // TODO In same cases, disable listing of items, as it is done in MS Excel.
385 // Exporting savePivotCacheRecordsXml method needs to be updated accordingly
386 //bool bListItems = true;
388 std::set
<ScDPItemData::Type
> aDPTypesWithoutBlank
= aDPTypes
;
389 aDPTypesWithoutBlank
.erase(ScDPItemData::Empty
);
391 const bool isContainsString
= aDPTypesWithoutBlank
.count(ScDPItemData::String
) > 0;
392 const bool isContainsBlank
= aDPTypes
.count(ScDPItemData::Empty
) > 0;
393 const bool isContainsNumber
394 = !isContainsDate
&& aDPTypesWithoutBlank
.count(ScDPItemData::Value
) > 0;
395 bool isContainsNonDate
= !(isContainsDate
&& aDPTypesWithoutBlank
.size() <= 1);
397 // XML_containsSemiMixedTypes possible values:
398 // 1 - (Default) at least one text value, or can also contain a mix of other data types and blank values,
399 // or blank values only
400 // 0 - the field does not have a mix of text and other values
401 if (!(isContainsString
|| (aDPTypes
.size() > 1) || (isContainsBlank
&& aDPTypesWithoutBlank
.empty())))
402 pAttList
->add(XML_containsSemiMixedTypes
, ToPsz10(false));
404 if (!isContainsNonDate
)
405 pAttList
->add(XML_containsNonDate
, ToPsz10(false));
408 pAttList
->add(XML_containsDate
, ToPsz10(true));
410 // default for containsString field is true, so we are writing only when is false
411 if (!isContainsString
)
412 pAttList
->add(XML_containsString
, ToPsz10(false));
415 pAttList
->add(XML_containsBlank
, ToPsz10(true));
417 // XML_containsMixedType possible values:
418 // 1 - field contains more than one data type
419 // 0 - (Default) only one data type. The field can still contain blank values (that's why we are using aDPTypesWithoutBlank)
420 if (aDPTypesWithoutBlank
.size() > 1)
421 pAttList
->add(XML_containsMixedTypes
, ToPsz10(true));
423 // If field contain mixed types (Date and Numbers), MS Excel is saving only "minDate" and "maxDate" and not "minValue" and "maxValue"
424 // Example how Excel is saving mixed Date and Numbers:
425 // <sharedItems containsSemiMixedTypes="0" containsDate="1" containsString="0" containsMixedTypes="1" minDate="1900-01-03T22:26:04" maxDate="1900-01-07T14:02:04" />
426 // Example how Excel is saving Dates only:
427 // <sharedItems containsSemiMixedTypes="0" containsNonDate="0" containsDate="1" containsString="0" minDate="1903-08-24T07:40:48" maxDate="2024-05-23T07:12:00"/>
428 if (isContainsNumber
)
429 pAttList
->add(XML_containsNumber
, ToPsz10(true));
431 if (isValueInteger
&& isContainsNumber
)
432 pAttList
->add(XML_containsInteger
, ToPsz10(true));
435 // Number type fields could be mixed with blank types, and it shouldn't be treated as listed items.
437 // <cacheField name="employeeID" numFmtId="0">
438 // <sharedItems containsString="0" containsBlank="1" containsNumber="1" containsInteger="1" minValue="35" maxValue="89"/>
440 if (isContainsNumber
)
442 pAttList
->add(XML_minValue
, OString::number(fMin
));
443 pAttList
->add(XML_maxValue
, OString::number(fMax
));
448 pAttList
->add(XML_minDate
, GetExcelFormattedDate(fMin
, GetFormatter()).toUtf8());
449 pAttList
->add(XML_maxDate
, GetExcelFormattedDate(fMax
, GetFormatter()).toUtf8());
452 //if (bListItems) // see TODO above
454 pAttList
->add(XML_count
, OString::number(static_cast<tools::Long
>(rFieldItems
.size())));
459 pAttList
->add(XML_longText
, ToPsz10(true));
462 pDefStrm
->startElement(XML_sharedItems
, pAttList
);
464 //if (bListItems) // see TODO above
466 for (const ScDPItemData
& rItem
: rFieldItems
)
468 switch (rItem
.GetType())
470 case ScDPItemData::String
:
471 pDefStrm
->singleElement(XML_s
, XML_v
, rItem
.GetString().toUtf8());
473 case ScDPItemData::Value
:
476 pDefStrm
->singleElement(XML_d
,
477 XML_v
, GetExcelFormattedDate(rItem
.GetValue(), GetFormatter()).toUtf8());
480 pDefStrm
->singleElement(XML_n
,
481 XML_v
, OString::number(rItem
.GetValue()));
483 case ScDPItemData::Empty
:
484 pDefStrm
->singleElement(XML_m
);
486 case ScDPItemData::Error
:
487 pDefStrm
->singleElement(XML_e
,
488 XML_v
, rItem
.GetString().toUtf8());
490 case ScDPItemData::GroupValue
: // Should not happen here!
491 case ScDPItemData::RangeStart
:
492 // TODO : What do we do with these types?
493 pDefStrm
->singleElement(XML_m
);
501 pDefStrm
->endElement(XML_sharedItems
);
503 WriteFieldGroup(i
, i
);
505 pDefStrm
->endElement(XML_cacheField
);
508 ScDPObject
* pDPObject
509 = rCache
.GetAllReferences().empty() ? nullptr : *rCache
.GetAllReferences().begin();
511 for (size_t i
= nCount
; pDPObject
&& i
< nCount
+ nGroupFieldCount
; ++i
)
513 const OUString aName
= pDPObject
->GetDimName(i
, o3tl::temporary(bool()));
514 // tdf#126748: DPObject might not reference all group fields, when there are several
515 // DPObjects referencing this cache. Trying to get a dimension data for a field not used
516 // in a given DPObject will give nullptr, and dereferencing it then will crash. To avoid
517 // the crash, until there's a correct method to find the names of group fields in cache,
518 // just skip the fields, creating bad cache data, which is of course a temporary hack.
519 // TODO: reimplement the whole block to get the names from another source, not from first
524 ScDPSaveData
* pSaveData
= pDPObject
->GetSaveData();
527 const ScDPSaveGroupDimension
* pDim
= pSaveData
->GetDimensionData()->GetNamedGroupDim(aName
);
530 const SCCOL nBase
= rCache
.GetDimensionIndex(pDim
->GetSourceDimName());
533 pDefStrm
->startElement(XML_cacheField
, XML_name
, aName
.toUtf8(), XML_numFmtId
,
534 OString::number(0), XML_databaseField
, ToPsz10(false));
535 WriteFieldGroup(i
, nBase
);
536 pDefStrm
->endElement(XML_cacheField
);
539 pDefStrm
->endElement(XML_cacheFields
);
541 pDefStrm
->endElement(XML_pivotCacheDefinition
);
544 XclExpXmlPivotTableManager::XclExpXmlPivotTableManager( const XclExpRoot
& rRoot
) :
545 XclExpRoot(rRoot
), maCaches(rRoot
) {}
547 void XclExpXmlPivotTableManager::Initialize()
549 ScDocument
& rDoc
= GetDoc();
550 if (!rDoc
.HasPivotTable())
551 // No pivot table to export.
554 ScDPCollection
* pDPColl
= rDoc
.GetDPCollection();
558 // Update caches from DPObject
559 for (size_t i
= 0; i
< pDPColl
->GetCount(); ++i
)
561 ScDPObject
& rDPObj
= (*pDPColl
)[i
];
562 rDPObj
.SyncAllDimensionMembers();
563 (void)rDPObj
.GetOutputRangeByType(sheet::DataPilotOutputRangeType::TABLE
);
566 // Go through the caches first.
568 std::vector
<XclExpXmlPivotCaches::Entry
> aCaches
;
569 const ScDPCollection::SheetCaches
& rSheetCaches
= pDPColl
->GetSheetCaches();
570 const std::vector
<ScRange
>& rRanges
= rSheetCaches
.getAllRanges();
571 for (const auto & rRange
: rRanges
)
573 const ScDPCache
* pCache
= rSheetCaches
.getExistingCache(rRange
);
577 // Get all pivot objects that reference this cache, and set up an
578 // object to cache ID mapping.
579 const ScDPCache::ScDPObjectSet
& rRefs
= pCache
->GetAllReferences();
580 for (const auto& rRef
: rRefs
)
581 maCacheIdMap
.emplace(rRef
, aCaches
.size()+1);
583 XclExpXmlPivotCaches::Entry aEntry
;
584 aEntry
.mpCache
= pCache
;
585 aEntry
.maSrcRange
= rRange
;
586 aCaches
.push_back(aEntry
); // Cache ID equals position + 1.
589 // TODO : Handle name and database caches as well.
591 for (size_t i
= 0, n
= pDPColl
->GetCount(); i
< n
; ++i
)
593 const ScDPObject
& rDPObj
= (*pDPColl
)[i
];
595 // Get the cache ID for this pivot table.
596 CacheIdMapType::iterator itCache
= maCacheIdMap
.find(&rDPObj
);
597 if (itCache
== maCacheIdMap
.end())
598 // No cache ID found. Something is wrong here...
601 sal_Int32 nCacheId
= itCache
->second
;
602 SCTAB nTab
= rDPObj
.GetOutRange().aStart
.Tab();
604 TablesType::iterator it
= m_Tables
.find(nTab
);
605 if (it
== m_Tables
.end())
607 // Insert a new instance for this sheet index.
608 std::pair
<TablesType::iterator
, bool> r
=
609 m_Tables
.insert(std::make_pair(nTab
, std::make_unique
<XclExpXmlPivotTables
>(GetRoot(), maCaches
)));
613 XclExpXmlPivotTables
*const p
= it
->second
.get();
614 p
->AppendTable(&rDPObj
, nCacheId
, i
+1);
617 maCaches
.SetCaches(std::move(aCaches
));
620 XclExpXmlPivotCaches
& XclExpXmlPivotTableManager::GetCaches()
625 XclExpXmlPivotTables
* XclExpXmlPivotTableManager::GetTablesBySheet( SCTAB nTab
)
627 TablesType::iterator
const it
= m_Tables
.find(nTab
);
628 return it
== m_Tables
.end() ? nullptr : it
->second
.get();
631 XclExpXmlPivotTables::Entry::Entry( const ScDPObject
* pTable
, sal_Int32 nCacheId
, sal_Int32 nPivotId
) :
632 mpTable(pTable
), mnCacheId(nCacheId
), mnPivotId(nPivotId
) {}
634 XclExpXmlPivotTables::XclExpXmlPivotTables( const XclExpRoot
& rRoot
, const XclExpXmlPivotCaches
& rCaches
) :
635 XclExpRoot(rRoot
), mrCaches(rCaches
) {}
637 void XclExpXmlPivotTables::SaveXml( XclExpXmlStream
& rStrm
)
639 sax_fastparser::FSHelperPtr
& pWSStrm
= rStrm
.GetCurrentStream(); // worksheet stream
641 for (const auto& rTable
: maTables
)
643 const ScDPObject
& rObj
= *rTable
.mpTable
;
644 sal_Int32 nCacheId
= rTable
.mnCacheId
;
645 sal_Int32 nPivotId
= rTable
.mnPivotId
;
647 sax_fastparser::FSHelperPtr pPivotStrm
= rStrm
.CreateOutputStream(
648 XclXmlUtils::GetStreamName("xl/pivotTables/", "pivotTable", nPivotId
),
649 XclXmlUtils::GetStreamName(nullptr, "../pivotTables/pivotTable", nPivotId
),
650 pWSStrm
->getOutputStream(),
651 CREATE_XL_CONTENT_TYPE("pivotTable"),
652 CREATE_OFFICEDOC_RELATION_TYPE("pivotTable"));
654 rStrm
.PushStream(pPivotStrm
);
655 SavePivotTableXml(rStrm
, rObj
, nCacheId
);
664 tools::Long mnPos
; // field index in pivot cache.
665 const ScDPSaveDimension
* mpDim
;
667 DataField( tools::Long nPos
, const ScDPSaveDimension
* pDim
) : mnPos(nPos
), mpDim(pDim
) {}
670 /** Returns an OOXML subtotal function name string. See ECMA-376-1:2016 18.18.43 */
671 OString
GetSubtotalFuncName(ScGeneralFunction eFunc
)
675 case ScGeneralFunction::SUM
: return "sum";
676 case ScGeneralFunction::COUNT
: return "count";
677 case ScGeneralFunction::AVERAGE
: return "avg";
678 case ScGeneralFunction::MAX
: return "max";
679 case ScGeneralFunction::MIN
: return "min";
680 case ScGeneralFunction::PRODUCT
: return "product";
681 case ScGeneralFunction::COUNTNUMS
: return "countA";
682 case ScGeneralFunction::STDEV
: return "stdDev";
683 case ScGeneralFunction::STDEVP
: return "stdDevP";
684 case ScGeneralFunction::VAR
: return "var";
685 case ScGeneralFunction::VARP
: return "varP";
691 sal_Int32
GetSubtotalAttrToken(ScGeneralFunction eFunc
)
695 case ScGeneralFunction::SUM
: return XML_sumSubtotal
;
696 case ScGeneralFunction::COUNT
: return XML_countSubtotal
;
697 case ScGeneralFunction::AVERAGE
: return XML_avgSubtotal
;
698 case ScGeneralFunction::MAX
: return XML_maxSubtotal
;
699 case ScGeneralFunction::MIN
: return XML_minSubtotal
;
700 case ScGeneralFunction::PRODUCT
: return XML_productSubtotal
;
701 case ScGeneralFunction::COUNTNUMS
: return XML_countASubtotal
;
702 case ScGeneralFunction::STDEV
: return XML_stdDevSubtotal
;
703 case ScGeneralFunction::STDEVP
: return XML_stdDevPSubtotal
;
704 case ScGeneralFunction::VAR
: return XML_varSubtotal
;
705 case ScGeneralFunction::VARP
: return XML_varPSubtotal
;
708 return XML_defaultSubtotal
;
711 // An item is expected to contain sequences of css::xml::FastAttribute and css::xml::Attribute
712 void WriteGrabBagItemToStream(XclExpXmlStream
& rStrm
, sal_Int32 tokenId
, const css::uno::Any
& rItem
)
714 css::uno::Sequence
<css::uno::Any
> aSeqs
;
715 if(!(rItem
>>= aSeqs
))
718 auto& pStrm
= rStrm
.GetCurrentStream();
719 pStrm
->write("<")->writeId(tokenId
);
721 css::uno::Sequence
<css::xml::FastAttribute
> aFastSeq
;
722 css::uno::Sequence
<css::xml::Attribute
> aUnkSeq
;
723 for (const auto& a
: std::as_const(aSeqs
))
727 for (const auto& rAttr
: std::as_const(aFastSeq
))
728 rStrm
.WriteAttributes(rAttr
.Token
, rAttr
.Value
);
730 else if (a
>>= aUnkSeq
)
732 for (const auto& rAttr
: std::as_const(aUnkSeq
))
736 ->writeEscaped(rAttr
.Value
)
745 void XclExpXmlPivotTables::SavePivotTableXml( XclExpXmlStream
& rStrm
, const ScDPObject
& rDPObj
, sal_Int32 nCacheId
)
747 typedef std::unordered_map
<OUString
, long> NameToIdMapType
;
749 const XclExpXmlPivotCaches::Entry
* pCacheEntry
= mrCaches
.GetCache(nCacheId
);
751 // Something is horribly wrong. Check your logic.
754 const ScDPCache
& rCache
= *pCacheEntry
->mpCache
;
756 const ScDPSaveData
& rSaveData
= *rDPObj
.GetSaveData();
758 size_t nFieldCount
= rCache
.GetFieldCount() + rCache
.GetGroupFieldCount();
759 std::vector
<const ScDPSaveDimension
*> aCachedDims
;
760 NameToIdMapType aNameToIdMap
;
762 aCachedDims
.reserve(nFieldCount
);
763 for (size_t i
= 0; i
< nFieldCount
; ++i
)
765 OUString aName
= const_cast<ScDPObject
&>(rDPObj
).GetDimName(i
, o3tl::temporary(bool()));
766 aNameToIdMap
.emplace(aName
, aCachedDims
.size());
767 const ScDPSaveDimension
* pDim
= rSaveData
.GetExistingDimensionByName(aName
);
768 aCachedDims
.push_back(pDim
);
771 std::vector
<tools::Long
> aRowFields
;
772 std::vector
<tools::Long
> aColFields
;
773 std::vector
<tools::Long
> aPageFields
;
774 std::vector
<DataField
> aDataFields
;
776 tools::Long nDataDimCount
= rSaveData
.GetDataDimensionCount();
777 // Use dimensions in the save data to get their correct ordering.
778 // Dimension order here is significant as they specify the order of
779 // appearance in each axis.
780 const ScDPSaveData::DimsType
& rDims
= rSaveData
.GetDimensions();
781 bool bTabularMode
= false;
782 for (const auto & i
: rDims
)
784 const ScDPSaveDimension
& rDim
= *i
;
786 tools::Long nPos
= -1; // position in cache
787 if (rDim
.IsDataLayout())
788 nPos
= -2; // Excel uses an index of -2 to indicate a data layout field.
791 OUString aSrcName
= ScDPUtil::getSourceDimensionName(rDim
.GetName());
792 NameToIdMapType::iterator it
= aNameToIdMap
.find(aSrcName
);
793 if (it
!= aNameToIdMap
.end())
799 if (!aCachedDims
[nPos
])
803 sheet::DataPilotFieldOrientation eOrient
= rDim
.GetOrientation();
807 case sheet::DataPilotFieldOrientation_COLUMN
:
808 if (nPos
== -2 && nDataDimCount
<= 1)
810 aColFields
.push_back(nPos
);
812 case sheet::DataPilotFieldOrientation_ROW
:
813 aRowFields
.push_back(nPos
);
815 case sheet::DataPilotFieldOrientation_PAGE
:
816 aPageFields
.push_back(nPos
);
818 case sheet::DataPilotFieldOrientation_DATA
:
819 aDataFields
.emplace_back(nPos
, &rDim
);
821 case sheet::DataPilotFieldOrientation_HIDDEN
:
825 if(rDim
.GetLayoutInfo())
826 bTabularMode
|= (rDim
.GetLayoutInfo()->LayoutMode
== sheet::DataPilotFieldLayoutMode::TABULAR_LAYOUT
);
829 sax_fastparser::FSHelperPtr
& pPivotStrm
= rStrm
.GetCurrentStream();
830 pPivotStrm
->startElement(XML_pivotTableDefinition
,
831 XML_xmlns
, rStrm
.getNamespaceURL(OOX_NS(xls
)).toUtf8(),
832 XML_name
, rDPObj
.GetName().toUtf8(),
833 XML_cacheId
, OString::number(nCacheId
),
834 XML_applyNumberFormats
, ToPsz10(false),
835 XML_applyBorderFormats
, ToPsz10(false),
836 XML_applyFontFormats
, ToPsz10(false),
837 XML_applyPatternFormats
, ToPsz10(false),
838 XML_applyAlignmentFormats
, ToPsz10(false),
839 XML_applyWidthHeightFormats
, ToPsz10(false),
840 XML_dataCaption
, "Values",
841 XML_useAutoFormatting
, ToPsz10(false),
842 XML_itemPrintTitles
, ToPsz10(true),
843 XML_indent
, ToPsz10(false),
844 XML_outline
, ToPsz10(!bTabularMode
),
845 XML_outlineData
, ToPsz10(!bTabularMode
),
846 XML_compact
, ToPsz10(false),
847 XML_compactData
, ToPsz10(false));
849 // NB: Excel's range does not include page field area (if any).
850 ScRange aOutRange
= rDPObj
.GetOutputRangeByType(sheet::DataPilotOutputRangeType::TABLE
);
852 sal_Int32 nFirstHeaderRow
= rDPObj
.GetHeaderLayout() ? 2 : 1;
853 sal_Int32 nFirstDataRow
= 2;
854 sal_Int32 nFirstDataCol
= 1;
855 ScRange aResRange
= rDPObj
.GetOutputRangeByType(sheet::DataPilotOutputRangeType::RESULT
);
857 if (!aOutRange
.IsValid())
858 aOutRange
= rDPObj
.GetOutRange();
860 if (aOutRange
.IsValid() && aResRange
.IsValid())
862 nFirstDataRow
= aResRange
.aStart
.Row() - aOutRange
.aStart
.Row();
863 nFirstDataCol
= aResRange
.aStart
.Col() - aOutRange
.aStart
.Col();
866 pPivotStrm
->write("<")->writeId(XML_location
);
867 rStrm
.WriteAttributes(XML_ref
,
868 XclXmlUtils::ToOString(rStrm
.GetRoot().GetDoc(), aOutRange
),
869 XML_firstHeaderRow
, OUString::number(nFirstHeaderRow
),
870 XML_firstDataRow
, OUString::number(nFirstDataRow
),
871 XML_firstDataCol
, OUString::number(nFirstDataCol
));
873 if (!aPageFields
.empty())
875 rStrm
.WriteAttributes(XML_rowPageCount
, OUString::number(static_cast<tools::Long
>(aPageFields
.size())));
876 rStrm
.WriteAttributes(XML_colPageCount
, OUString::number(1));
879 pPivotStrm
->write("/>");
881 // <pivotFields> - It must contain all fields in the pivot cache even if
882 // only some of them are used in the pivot table. The order must be as
883 // they appear in the cache.
885 pPivotStrm
->startElement(XML_pivotFields
,
886 XML_count
, OString::number(static_cast<tools::Long
>(aCachedDims
.size())));
888 for (size_t i
= 0; i
< nFieldCount
; ++i
)
890 const ScDPSaveDimension
* pDim
= aCachedDims
[i
];
893 pPivotStrm
->singleElement(XML_pivotField
,
894 XML_compact
, ToPsz10(false),
895 XML_showAll
, ToPsz10(false));
899 bool bDimInTabularMode
= false;
900 if(pDim
->GetLayoutInfo())
901 bDimInTabularMode
= (pDim
->GetLayoutInfo()->LayoutMode
== sheet::DataPilotFieldLayoutMode::TABULAR_LAYOUT
);
903 sheet::DataPilotFieldOrientation eOrient
= pDim
->GetOrientation();
905 if (eOrient
== sheet::DataPilotFieldOrientation_HIDDEN
)
907 if(bDimInTabularMode
)
909 pPivotStrm
->singleElement(XML_pivotField
,
910 XML_compact
, ToPsz10(false),
911 XML_showAll
, ToPsz10(false),
912 XML_outline
, ToPsz10(false));
916 pPivotStrm
->singleElement(XML_pivotField
,
917 XML_compact
, ToPsz10(false),
918 XML_showAll
, ToPsz10(false));
923 if (eOrient
== sheet::DataPilotFieldOrientation_DATA
)
925 if(bDimInTabularMode
)
927 pPivotStrm
->singleElement(XML_pivotField
,
928 XML_dataField
, ToPsz10(true),
929 XML_compact
, ToPsz10(false),
930 XML_showAll
, ToPsz10(false),
931 XML_outline
, ToPsz10(false));
935 pPivotStrm
->singleElement(XML_pivotField
,
936 XML_dataField
, ToPsz10(true),
937 XML_compact
, ToPsz10(false),
938 XML_showAll
, ToPsz10(false));
944 std::vector
<ScDPLabelData::Member
> aMembers
;
946 // We need to get the members in actual order, getting which requires non-const reference here
947 auto& dpo
= const_cast<ScDPObject
&>(rDPObj
);
948 dpo
.GetMembers(i
, dpo
.GetUsedHierarchy(i
), aMembers
);
951 std::vector
<OUString
> aCacheFieldItems
;
952 if (i
< rCache
.GetFieldCount() && !rCache
.GetGroupType(i
))
954 for (const auto& it
: rCache
.GetDimMemberValues(i
))
956 OUString sFormattedName
;
957 if (it
.HasStringData() || it
.IsEmpty())
958 sFormattedName
= it
.GetString();
960 sFormattedName
= const_cast<ScDPObject
&>(rDPObj
).GetFormattedString(
961 pDim
->GetName(), it
.GetValue());
962 aCacheFieldItems
.push_back(sFormattedName
);
967 aCacheFieldItems
= SortGroupItems(rCache
, i
);
969 // The pair contains the member index in cache and if it is hidden
970 std::vector
< std::pair
<size_t, bool> > aMemberSequence
;
971 std::set
<size_t> aUsedCachePositions
;
972 for (const auto & rMember
: aMembers
)
974 auto it
= std::find(aCacheFieldItems
.begin(), aCacheFieldItems
.end(), rMember
.maName
);
975 if (it
!= aCacheFieldItems
.end())
977 size_t nCachePos
= static_cast<size_t>(std::distance(aCacheFieldItems
.begin(), it
));
978 auto aInserted
= aUsedCachePositions
.insert(nCachePos
);
979 if (aInserted
.second
)
980 aMemberSequence
.emplace_back(std::make_pair(nCachePos
, !rMember
.mbVisible
));
983 // Now add all remaining cache items as hidden
984 for (size_t nItem
= 0; nItem
< aCacheFieldItems
.size(); ++nItem
)
986 if (aUsedCachePositions
.find(nItem
) == aUsedCachePositions
.end())
987 aMemberSequence
.emplace_back(nItem
, true);
990 // tdf#125086: check if this field *also* appears in Data region
991 bool bAppearsInData
= false;
993 OUString aSrcName
= ScDPUtil::getSourceDimensionName(pDim
->GetName());
994 const auto it
= std::find_if(
995 aDataFields
.begin(), aDataFields
.end(), [&aSrcName
](const DataField
& rDataField
) {
997 = ScDPUtil::getSourceDimensionName(rDataField
.mpDim
->GetName());
998 return aThisName
== aSrcName
;
1000 if (it
!= aDataFields
.end())
1001 bAppearsInData
= true;
1004 auto pAttList
= sax_fastparser::FastSerializerHelper::createAttrList();
1005 pAttList
->add(XML_axis
, toOOXMLAxisType(eOrient
));
1007 pAttList
->add(XML_dataField
, ToPsz10(true));
1008 pAttList
->add(XML_compact
, ToPsz10(false));
1009 pAttList
->add(XML_showAll
, ToPsz10(false));
1011 tools::Long nSubTotalCount
= pDim
->GetSubTotalsCount();
1012 std::vector
<OString
> aSubtotalSequence
;
1013 bool bHasDefaultSubtotal
= false;
1014 for (tools::Long nSubTotal
= 0; nSubTotal
< nSubTotalCount
; ++nSubTotal
)
1016 ScGeneralFunction eFunc
= pDim
->GetSubTotalFunc(nSubTotal
);
1017 aSubtotalSequence
.push_back(GetSubtotalFuncName(eFunc
));
1018 sal_Int32 nAttToken
= GetSubtotalAttrToken(eFunc
);
1019 if (nAttToken
== XML_defaultSubtotal
)
1020 bHasDefaultSubtotal
= true;
1021 else if (!pAttList
->hasAttribute(nAttToken
))
1022 pAttList
->add(nAttToken
, ToPsz10(true));
1024 // XML_defaultSubtotal is true by default; only write it if it's false
1025 if (!bHasDefaultSubtotal
)
1026 pAttList
->add(XML_defaultSubtotal
, ToPsz10(false));
1028 if(bDimInTabularMode
)
1029 pAttList
->add( XML_outline
, ToPsz10(false));
1030 pPivotStrm
->startElement(XML_pivotField
, pAttList
);
1032 pPivotStrm
->startElement(XML_items
,
1033 XML_count
, OString::number(static_cast<tools::Long
>(aMemberSequence
.size() + aSubtotalSequence
.size())));
1035 for (const auto & nMember
: aMemberSequence
)
1037 auto pItemAttList
= sax_fastparser::FastSerializerHelper::createAttrList();
1039 pItemAttList
->add(XML_h
, ToPsz10(true));
1040 pItemAttList
->add(XML_x
, OString::number(static_cast<tools::Long
>(nMember
.first
)));
1041 pPivotStrm
->singleElement(XML_item
, pItemAttList
);
1044 for (const OString
& sSubtotal
: aSubtotalSequence
)
1046 pPivotStrm
->singleElement(XML_item
, XML_t
, sSubtotal
);
1049 pPivotStrm
->endElement(XML_items
);
1050 pPivotStrm
->endElement(XML_pivotField
);
1053 pPivotStrm
->endElement(XML_pivotFields
);
1057 if (!aRowFields
.empty())
1059 pPivotStrm
->startElement(XML_rowFields
,
1060 XML_count
, OString::number(static_cast<tools::Long
>(aRowFields
.size())));
1062 for (const auto& rRowField
: aRowFields
)
1064 pPivotStrm
->singleElement(XML_field
, XML_x
, OString::number(rRowField
));
1067 pPivotStrm
->endElement(XML_rowFields
);
1074 if (!aColFields
.empty())
1076 pPivotStrm
->startElement(XML_colFields
,
1077 XML_count
, OString::number(static_cast<tools::Long
>(aColFields
.size())));
1079 for (const auto& rColField
: aColFields
)
1081 pPivotStrm
->singleElement(XML_field
, XML_x
, OString::number(rColField
));
1084 pPivotStrm
->endElement(XML_colFields
);
1091 if (!aPageFields
.empty())
1093 pPivotStrm
->startElement(XML_pageFields
,
1094 XML_count
, OString::number(static_cast<tools::Long
>(aPageFields
.size())));
1096 for (const auto& rPageField
: aPageFields
)
1098 pPivotStrm
->singleElement(XML_pageField
,
1099 XML_fld
, OString::number(rPageField
),
1100 XML_hier
, OString::number(-1)); // TODO : handle this correctly.
1103 pPivotStrm
->endElement(XML_pageFields
);
1108 if (!aDataFields
.empty())
1110 css::uno::Reference
<css::container::XNameAccess
> xDimsByName
;
1111 if (auto xDimSupplier
= const_cast<ScDPObject
&>(rDPObj
).GetSource())
1112 xDimsByName
= xDimSupplier
->getDimensions();
1114 pPivotStrm
->startElement(XML_dataFields
,
1115 XML_count
, OString::number(static_cast<tools::Long
>(aDataFields
.size())));
1117 for (const auto& rDataField
: aDataFields
)
1119 tools::Long nDimIdx
= rDataField
.mnPos
;
1120 assert(aCachedDims
[nDimIdx
]); // the loop above should have screened for NULL's.
1121 const ScDPSaveDimension
& rDim
= *rDataField
.mpDim
;
1122 std::optional
<OUString
> pName
= rDim
.GetLayoutName();
1123 // tdf#124651: despite being optional in CT_DataField according to ECMA-376 Part 1,
1124 // Excel (at least 2016) seems to insist on the presence of "name" attribute in
1125 // dataField element.
1126 // tdf#124881: try to create a meaningful name; don't use empty string.
1128 pName
= ScDPUtil::getDisplayedMeasureName(
1129 rDim
.GetName(), ScDPUtil::toSubTotalFunc(rDim
.GetFunction()));
1130 auto pItemAttList
= sax_fastparser::FastSerializerHelper::createAttrList();
1131 pItemAttList
->add(XML_name
, pName
->toUtf8());
1132 pItemAttList
->add(XML_fld
, OString::number(nDimIdx
));
1133 const char* pSubtotal
= toOOXMLSubtotalType(rDim
.GetFunction());
1135 pItemAttList
->add(XML_subtotal
, pSubtotal
);
1140 css::uno::Reference
<css::beans::XPropertySet
> xDimProps(
1141 xDimsByName
->getByName(rDim
.GetName()), uno::UNO_QUERY_THROW
);
1142 css::uno::Any aVal
= xDimProps
->getPropertyValue(SC_UNONAME_NUMFMT
);
1143 sal_uInt32 nScNumFmt
= aVal
.get
<sal_uInt32
>();
1144 sal_uInt16 nXclNumFmt
= GetRoot().GetNumFmtBuffer().Insert(nScNumFmt
);
1145 pItemAttList
->add(XML_numFmtId
, OString::number(nXclNumFmt
));
1147 catch (uno::Exception
&)
1149 SAL_WARN("sc.filter",
1150 "Couldn't get number format for data field " << rDim
.GetName());
1151 // Just skip exporting number format
1154 pPivotStrm
->singleElement(XML_dataField
, pItemAttList
);
1157 pPivotStrm
->endElement(XML_dataFields
);
1160 // Now add style info (use grab bag, or just a set which is default on Excel 2007 through 2016)
1161 if (const auto [bHas
, aVal
] = rDPObj
.GetInteropGrabBagValue("pivotTableStyleInfo"); bHas
)
1162 WriteGrabBagItemToStream(rStrm
, XML_pivotTableStyleInfo
, aVal
);
1164 pPivotStrm
->singleElement(XML_pivotTableStyleInfo
, XML_name
, "PivotStyleLight16",
1165 XML_showRowHeaders
, "1", XML_showColHeaders
, "1",
1166 XML_showRowStripes
, "0", XML_showColStripes
, "0",
1167 XML_showLastColumn
, "1");
1169 OUString aBuf
= "../pivotCache/pivotCacheDefinition" +
1170 OUString::number(nCacheId
) +
1174 pPivotStrm
->getOutputStream(),
1175 CREATE_OFFICEDOC_RELATION_TYPE("pivotCacheDefinition"),
1178 pPivotStrm
->endElement(XML_pivotTableDefinition
);
1181 void XclExpXmlPivotTables::AppendTable( const ScDPObject
* pTable
, sal_Int32 nCacheId
, sal_Int32 nPivotId
)
1183 maTables
.emplace_back(pTable
, nCacheId
, nPivotId
);
1186 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */