Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / sc / source / filter / excel / xeextlst.cxx
bloba3c25320ff724d87733a8308544b2085c16630ac
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <xeextlst.hxx>
11 #include <xeroot.hxx>
12 #include <stlsheet.hxx>
13 #include <ftools.hxx>
14 #include <xestyle.hxx>
15 #include <stlpool.hxx>
16 #include <scitems.hxx>
17 #include <svl/itemset.hxx>
18 #include <svl/intitem.hxx>
20 #include <oox/export/utils.hxx>
21 #include <oox/token/namespaces.hxx>
22 #include <comphelper/processfactory.hxx>
24 using namespace ::oox;
26 XclExpExt::XclExpExt( const XclExpRoot& rRoot ):
27 XclExpRoot(rRoot)
31 XclExtLst::XclExtLst( const XclExpRoot& rRoot ):
32 XclExpRoot(rRoot)
36 XclExpExtNegativeColor::XclExpExtNegativeColor( const Color& rColor ):
37 maColor(rColor)
41 void XclExpExtNegativeColor::SaveXml( XclExpXmlStream& rStrm )
43 rStrm.GetCurrentStream()->singleElementNS( XML_x14, XML_negativeFillColor,
44 XML_rgb, XclXmlUtils::ToOString(maColor) );
47 XclExpExtAxisColor::XclExpExtAxisColor( const Color& rColor ):
48 maAxisColor(rColor)
52 void XclExpExtAxisColor::SaveXml( XclExpXmlStream& rStrm )
54 rStrm.GetCurrentStream()->singleElementNS( XML_x14, XML_axisColor,
55 XML_rgb, XclXmlUtils::ToOString(maAxisColor) );
58 XclExpExtIcon::XclExpExtIcon(const XclExpRoot& rRoot, const std::pair<ScIconSetType, sal_Int32>& rCustomEntry):
59 XclExpRoot(rRoot),
60 nIndex(rCustomEntry.second)
62 pIconSetName = ScIconSetFormat::getIconSetName(rCustomEntry.first);
65 void XclExpExtIcon::SaveXml(XclExpXmlStream& rStrm)
67 sax_fastparser::FSHelperPtr& rWorksheet = rStrm.GetCurrentStream();
69 if (nIndex == -1)
71 nIndex = 0;
72 pIconSetName = "NoIcons";
75 rWorksheet->singleElementNS(XML_x14, XML_cfIcon,
76 XML_iconSet, pIconSetName,
77 XML_iconId, OString::number(nIndex));
80 XclExpExtCfvo::XclExpExtCfvo( const XclExpRoot& rRoot, const ScColorScaleEntry& rEntry, const ScAddress& rSrcPos, bool bFirst ):
81 XclExpRoot(rRoot),
82 meType(rEntry.GetType()),
83 mbFirst(bFirst)
85 if( rEntry.GetType() == COLORSCALE_FORMULA )
87 const ScTokenArray* pArr = rEntry.GetFormula();
88 OUString aFormula;
89 if(pArr)
91 aFormula = XclXmlUtils::ToOUString( GetCompileFormulaContext(), rSrcPos, pArr);
93 maValue = OUStringToOString(aFormula, RTL_TEXTENCODING_UTF8 );
95 else
96 maValue = OString::number(rEntry.GetValue());
99 namespace {
101 const char* getColorScaleType( ScColorScaleEntryType eType, bool bFirst )
103 switch(eType)
105 case COLORSCALE_MIN:
106 return "min";
107 case COLORSCALE_MAX:
108 return "max";
109 case COLORSCALE_PERCENT:
110 return "percent";
111 case COLORSCALE_FORMULA:
112 return "formula";
113 case COLORSCALE_AUTO:
114 if(bFirst)
115 return "autoMin";
116 else
117 return "autoMax";
118 case COLORSCALE_PERCENTILE:
119 return "percentile";
120 default:
121 break;
123 return "num";
128 void XclExpExtCfvo::SaveXml( XclExpXmlStream& rStrm )
130 sax_fastparser::FSHelperPtr& rWorksheet = rStrm.GetCurrentStream();
131 rWorksheet->startElementNS(XML_x14, XML_cfvo, XML_type, getColorScaleType(meType, mbFirst));
133 if (meType == COLORSCALE_FORMULA ||
134 meType == COLORSCALE_PERCENT ||
135 meType == COLORSCALE_PERCENTILE ||
136 meType == COLORSCALE_VALUE)
138 rWorksheet->startElementNS(XML_xm, XML_f);
139 rWorksheet->writeEscaped(maValue.getStr());
140 rWorksheet->endElementNS(XML_xm, XML_f);
143 rWorksheet->endElementNS(XML_x14, XML_cfvo);
146 XclExpExtCF::XclExpExtCF( const XclExpRoot& rRoot, const ScCondFormatEntry& rFormat ):
147 XclExpRoot(rRoot),
148 mrFormat(rFormat)
152 void XclExpExtCF::SaveXml( XclExpXmlStream& rStrm )
154 OUString aStyleName = mrFormat.GetStyle();
155 SfxStyleSheetBase* pStyle = GetDoc().GetStyleSheetPool()->Find(aStyleName);
156 SfxItemSet& rSet = pStyle->GetItemSet();
158 std::unique_ptr<ScTokenArray> pTokenArray(mrFormat.CreateFlatCopiedTokenArray(0));
159 aFormula = XclXmlUtils::ToOUString( GetCompileFormulaContext(), mrFormat.GetValidSrcPos(), pTokenArray.get());
161 std::unique_ptr<XclExpColor> pColor(new XclExpColor);
162 if(!pColor->FillFromItemSet( rSet ))
163 pColor.reset();
165 std::unique_ptr<XclExpCellBorder> pBorder(new XclExpCellBorder);
166 if (!pBorder->FillFromItemSet( rSet, GetPalette(), GetBiff()) )
167 pBorder.reset();
169 std::unique_ptr<XclExpCellAlign> pAlign(new XclExpCellAlign);
170 if (!pAlign->FillFromItemSet( rSet, false, GetBiff()))
171 pAlign.reset();
173 std::unique_ptr<XclExpCellProt> pCellProt(new XclExpCellProt);
174 if (!pCellProt->FillFromItemSet( rSet ))
175 pCellProt.reset();
177 std::unique_ptr<XclExpDxfFont> pFont(new XclExpDxfFont(GetRoot(), rSet));
179 std::unique_ptr<XclExpNumFmt> pNumFormat;
180 const SfxPoolItem *pPoolItem = nullptr;
181 if( rSet.GetItemState( ATTR_VALUE_FORMAT, true, &pPoolItem ) == SfxItemState::SET )
183 sal_uInt32 nScNumFmt = static_cast< const SfxUInt32Item* >(pPoolItem)->GetValue();
184 XclExpNumFmtBuffer& rNumFmtBuffer = GetRoot().GetNumFmtBuffer();
185 sal_uInt32 nXclNumFmt = rNumFmtBuffer.Insert(nScNumFmt);
186 pNumFormat.reset(new XclExpNumFmt(nScNumFmt, nXclNumFmt, rNumFmtBuffer.GetFormatCode(nScNumFmt)));
189 XclExpDxf rDxf( GetRoot(),
190 std::move(pAlign),
191 std::move(pBorder),
192 std::move(pFont),
193 std::move(pNumFormat),
194 std::move(pCellProt),
195 std::move(pColor) );
197 sax_fastparser::FSHelperPtr& rWorksheet = rStrm.GetCurrentStream();
199 rWorksheet->startElementNS( XML_xm, XML_f );
200 rWorksheet->writeEscaped( aFormula );
201 rWorksheet->endElementNS( XML_xm, XML_f );
202 rDxf.SaveXmlExt( rStrm );
205 XclExpExtDataBar::XclExpExtDataBar( const XclExpRoot& rRoot, const ScDataBarFormat& rFormat, const ScAddress& rPos ):
206 XclExpRoot(rRoot)
208 const ScDataBarFormatData& rFormatData = *rFormat.GetDataBarData();
209 mpLowerLimit.reset(new XclExpExtCfvo(*this, *rFormatData.mpLowerLimit, rPos, true));
210 mpUpperLimit.reset(new XclExpExtCfvo(*this, *rFormatData.mpUpperLimit, rPos, false));
211 if (rFormatData.mpNegativeColor)
212 mpNegativeColor.reset(new XclExpExtNegativeColor(*rFormatData.mpNegativeColor));
213 else
214 mpNegativeColor.reset( new XclExpExtNegativeColor( rFormatData.maPositiveColor ) );
215 mpAxisColor.reset( new XclExpExtAxisColor( rFormatData.maAxisColor ) );
217 meAxisPosition = rFormatData.meAxisPosition;
218 mbGradient = rFormatData.mbGradient;
219 mnMinLength = rFormatData.mnMinLength;
220 mnMaxLength = rFormatData.mnMaxLength;
223 namespace {
225 const char* getAxisPosition(databar::ScAxisPosition eAxisPosition)
227 switch(eAxisPosition)
229 case databar::NONE:
230 return "none";
231 case databar::AUTOMATIC:
232 return "automatic";
233 case databar::MIDDLE:
234 return "middle";
236 return "";
239 const char* GetOperatorString(ScConditionMode eMode)
241 const char* pRet = nullptr;
242 switch(eMode)
244 case ScConditionMode::Equal:
245 pRet = "equal";
246 break;
247 case ScConditionMode::Less:
248 pRet = "lessThan";
249 break;
250 case ScConditionMode::Greater:
251 pRet = "greaterThan";
252 break;
253 case ScConditionMode::EqLess:
254 pRet = "lessThanOrEqual";
255 break;
256 case ScConditionMode::EqGreater:
257 pRet = "greaterThanOrEqual";
258 break;
259 case ScConditionMode::NotEqual:
260 pRet = "notEqual";
261 break;
262 case ScConditionMode::Between:
263 pRet = "between";
264 break;
265 case ScConditionMode::NotBetween:
266 pRet = "notBetween";
267 break;
268 case ScConditionMode::Duplicate:
269 pRet = nullptr;
270 break;
271 case ScConditionMode::NotDuplicate:
272 pRet = nullptr;
273 break;
274 case ScConditionMode::BeginsWith:
275 pRet = "beginsWith";
276 break;
277 case ScConditionMode::EndsWith:
278 pRet = "endsWith";
279 break;
280 case ScConditionMode::ContainsText:
281 pRet = "containsText";
282 break;
283 case ScConditionMode::NotContainsText:
284 pRet = "notContains";
285 break;
286 case ScConditionMode::Direct:
287 break;
288 case ScConditionMode::NONE:
289 default:
290 break;
292 return pRet;
297 void XclExpExtDataBar::SaveXml( XclExpXmlStream& rStrm )
299 sax_fastparser::FSHelperPtr& rWorksheet = rStrm.GetCurrentStream();
300 rWorksheet->startElementNS( XML_x14, XML_dataBar,
301 XML_minLength, OString::number(mnMinLength),
302 XML_maxLength, OString::number(mnMaxLength),
303 XML_axisPosition, getAxisPosition(meAxisPosition),
304 XML_gradient, ToPsz(mbGradient) );
306 mpLowerLimit->SaveXml( rStrm );
307 mpUpperLimit->SaveXml( rStrm );
308 mpNegativeColor->SaveXml( rStrm );
309 mpAxisColor->SaveXml( rStrm );
311 rWorksheet->endElementNS( XML_x14, XML_dataBar );
314 XclExpExtIconSet::XclExpExtIconSet(const XclExpRoot& rRoot, const ScIconSetFormat& rFormat, const ScAddress& rPos):
315 XclExpRoot(rRoot)
317 const ScIconSetFormatData& rData = *rFormat.GetIconSetData();
318 for (auto const& itr : rData.m_Entries)
320 maCfvos.AppendNewRecord(new XclExpExtCfvo(*this, *itr, rPos, false));
322 mbCustom = rData.mbCustom;
323 mbReverse = rData.mbReverse;
324 mbShowValue = rData.mbShowValue;
325 mpIconSetName = ScIconSetFormat::getIconSetName(rData.eIconSetType);
327 if (mbCustom)
329 for (const auto& rItem : rData.maCustomVector)
331 maCustom.AppendNewRecord(new XclExpExtIcon(*this, rItem));
336 void XclExpExtIconSet::SaveXml(XclExpXmlStream& rStrm)
338 sax_fastparser::FSHelperPtr& rWorksheet = rStrm.GetCurrentStream();
340 rWorksheet->startElementNS(XML_x14, XML_iconSet,
341 XML_iconSet, mpIconSetName,
342 XML_custom, mbCustom ? ToPsz10(mbCustom) : nullptr,
343 XML_reverse, ToPsz10(mbReverse),
344 XML_showValue, ToPsz10(mbShowValue));
346 maCfvos.SaveXml(rStrm);
348 if (mbCustom)
350 maCustom.SaveXml(rStrm);
353 rWorksheet->endElementNS(XML_x14, XML_iconSet);
356 XclExpExtCfRule::XclExpExtCfRule( const XclExpRoot& rRoot, const ScFormatEntry& rFormat, const ScAddress& rPos, const OString& rId, sal_Int32 nPriority ):
357 XclExpRoot(rRoot),
358 maId(rId),
359 pType(nullptr),
360 mnPriority(nPriority),
361 mOperator(nullptr)
363 switch (rFormat.GetType())
365 case ScFormatEntry::Type::Databar:
367 const ScDataBarFormat& rDataBar = static_cast<const ScDataBarFormat&>(rFormat);
368 mxEntry.reset( new XclExpExtDataBar( *this, rDataBar, rPos ) );
369 pType = "dataBar";
371 break;
372 case ScFormatEntry::Type::Iconset:
374 const ScIconSetFormat& rIconSet = static_cast<const ScIconSetFormat&>(rFormat);
375 mxEntry.reset(new XclExpExtIconSet(*this, rIconSet, rPos));
376 pType = "iconSet";
378 break;
379 case ScFormatEntry::Type::ExtCondition:
381 const ScCondFormatEntry& rCondFormat = static_cast<const ScCondFormatEntry&>(rFormat);
382 mxEntry.reset(new XclExpExtCF(*this, rCondFormat));
383 pType = "cellIs";
384 mOperator = GetOperatorString( rCondFormat.GetOperation() );
386 break;
387 default:
388 break;
392 void XclExpExtCfRule::SaveXml( XclExpXmlStream& rStrm )
394 if (!mxEntry)
395 return;
397 sax_fastparser::FSHelperPtr& rWorksheet = rStrm.GetCurrentStream();
398 rWorksheet->startElementNS( XML_x14, XML_cfRule,
399 XML_type, pType,
400 XML_priority, mnPriority == -1 ? nullptr : OString::number(mnPriority + 1).getStr(),
401 XML_operator, mOperator,
402 XML_id, maId );
404 mxEntry->SaveXml( rStrm );
406 rWorksheet->endElementNS( XML_x14, XML_cfRule );
410 XclExpExtConditionalFormatting::XclExpExtConditionalFormatting( const XclExpRoot& rRoot,
411 std::vector<XclExpExtCondFormatData>& rData, const ScRangeList& rRange):
412 XclExpRoot(rRoot),
413 maRange(rRange)
415 ScAddress aAddr = maRange.front().aStart;
416 for (const auto& rItem : rData)
418 const ScFormatEntry* pEntry = rItem.pEntry;
419 switch (pEntry->GetType())
421 case ScFormatEntry::Type::Iconset:
423 const ScIconSetFormat& rIconSet = static_cast<const ScIconSetFormat&>(*pEntry);
424 bool bNeedsExt = false;
425 switch (rIconSet.GetIconSetData()->eIconSetType)
427 case IconSet_3Triangles:
428 case IconSet_3Smilies:
429 case IconSet_3ColorSmilies:
430 case IconSet_5Boxes:
431 case IconSet_3Stars:
432 bNeedsExt = true;
433 break;
434 default:
435 break;
438 if (rIconSet.GetIconSetData()->mbCustom)
439 bNeedsExt = true;
441 if (bNeedsExt)
443 maCfRules.AppendNewRecord(new XclExpExtCfRule(*this, *pEntry, aAddr, rItem.aGUID, rItem.nPriority));
446 break;
447 case ScFormatEntry::Type::Databar:
448 maCfRules.AppendNewRecord(new XclExpExtCfRule( *this, *pEntry, aAddr, rItem.aGUID, rItem.nPriority));
449 break;
450 case ScFormatEntry::Type::ExtCondition:
451 maCfRules.AppendNewRecord(new XclExpExtCfRule( *this, *pEntry, aAddr, rItem.aGUID, rItem.nPriority));
452 break;
453 default:
454 break;
459 void XclExpExtConditionalFormatting::SaveXml( XclExpXmlStream& rStrm )
461 sax_fastparser::FSHelperPtr& rWorksheet = rStrm.GetCurrentStream();
462 rWorksheet->startElementNS( XML_x14, XML_conditionalFormatting,
463 FSNS( XML_xmlns, XML_xm ), rStrm.getNamespaceURL(OOX_NS(xm)).toUtf8() );
465 maCfRules.SaveXml( rStrm );
466 rWorksheet->startElementNS(XML_xm, XML_sqref);
467 rWorksheet->write(XclXmlUtils::ToOString(&rStrm.GetRoot().GetDoc(), maRange));
469 rWorksheet->endElementNS( XML_xm, XML_sqref );
471 rWorksheet->endElementNS( XML_x14, XML_conditionalFormatting );
474 XclExpExtCalcPr::XclExpExtCalcPr( const XclExpRoot& rRoot, formula::FormulaGrammar::AddressConvention eConv ):
475 XclExpExt( rRoot )
477 maURI = OString("{7626C862-2A13-11E5-B345-FEFF819CDC9F}");
479 switch (eConv)
481 case formula::FormulaGrammar::CONV_OOO:
482 maSyntax = OString("CalcA1");
483 break;
484 case formula::FormulaGrammar::CONV_XL_A1:
485 maSyntax = OString("ExcelA1");
486 break;
487 case formula::FormulaGrammar::CONV_XL_R1C1:
488 maSyntax = OString("ExcelR1C1");
489 break;
490 case formula::FormulaGrammar::CONV_A1_XL_A1:
491 maSyntax = OString("CalcA1ExcelA1");
492 break;
493 case formula::FormulaGrammar::CONV_UNSPECIFIED:
494 case formula::FormulaGrammar::CONV_ODF:
495 case formula::FormulaGrammar::CONV_XL_OOX:
496 case formula::FormulaGrammar::CONV_LOTUS_A1:
497 case formula::FormulaGrammar::CONV_LAST:
498 maSyntax = OString("Unspecified");
499 break;
503 void XclExpExtCalcPr::SaveXml( XclExpXmlStream& rStrm )
505 sax_fastparser::FSHelperPtr& rWorksheet = rStrm.GetCurrentStream();
506 rWorksheet->startElement( XML_ext,
507 FSNS(XML_xmlns, XML_loext), rStrm.getNamespaceURL(OOX_NS(loext)).toUtf8(),
508 XML_uri, maURI );
510 rWorksheet->singleElementNS(XML_loext, XML_extCalcPr, XML_stringRefSyntax, maSyntax);
512 rWorksheet->endElement( XML_ext );
515 XclExpExtCondFormat::XclExpExtCondFormat( const XclExpRoot& rRoot ):
516 XclExpExt( rRoot )
518 maURI = OString("{78C0D931-6437-407d-A8EE-F0AAD7539E65}");
521 void XclExpExtCondFormat::SaveXml( XclExpXmlStream& rStrm )
523 sax_fastparser::FSHelperPtr& rWorksheet = rStrm.GetCurrentStream();
524 rWorksheet->startElement( XML_ext,
525 FSNS(XML_xmlns, XML_x14), rStrm.getNamespaceURL(OOX_NS(xls14Lst)).toUtf8(),
526 XML_uri, maURI );
528 rWorksheet->startElementNS(XML_x14, XML_conditionalFormattings);
530 maCF.SaveXml( rStrm );
532 rWorksheet->endElementNS( XML_x14, XML_conditionalFormattings );
533 rWorksheet->endElement( XML_ext );
536 void XclExpExtCondFormat::AddRecord( const XclExpExtConditionalFormattingRef& aEntry )
538 maCF.AppendRecord( aEntry );
541 void XclExtLst::SaveXml( XclExpXmlStream& rStrm )
543 if(maExtEntries.IsEmpty())
544 return;
546 sax_fastparser::FSHelperPtr& rWorksheet = rStrm.GetCurrentStream();
547 rWorksheet->startElement(XML_extLst);
549 maExtEntries.SaveXml(rStrm);
551 rWorksheet->endElement( XML_extLst );
554 void XclExtLst::AddRecord( const XclExpExtRef& aEntry )
556 maExtEntries.AppendRecord( aEntry );
559 XclExpExtRef XclExtLst::GetItem( XclExpExtType eType )
561 size_t n = maExtEntries.GetSize();
562 for( size_t i = 0; i < n; ++i )
564 if (maExtEntries.GetRecord( i )->GetType() == eType)
565 return maExtEntries.GetRecord( i );
568 return XclExpExtRef();
572 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */