LibreOffice Module sc (master)  1
condformatbuffer.cxx
Go to the documentation of this file.
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 <memory>
21 #include <unordered_set>
22 #include <unordered_map>
23 #include <condformatbuffer.hxx>
24 #include <formulaparser.hxx>
25 
26 #include <com/sun/star/sheet/ConditionOperator2.hpp>
27 #include <sal/log.hxx>
28 #include <osl/diagnose.h>
29 #include <svl/sharedstringpool.hxx>
30 #include <oox/core/filterbase.hxx>
33 #include <oox/token/tokens.hxx>
34 #include <addressconverter.hxx>
35 #include <biffhelper.hxx>
36 #include <stylesbuffer.hxx>
37 #include <themebuffer.hxx>
38 
39 #include <colorscale.hxx>
40 #include <conditio.hxx>
41 #include <document.hxx>
42 #include <tokenarray.hxx>
43 #include <tokenuno.hxx>
44 #include <extlstcontext.hxx>
45 
46 namespace oox::xls {
47 
48 using namespace ::com::sun::star::sheet;
49 using namespace ::com::sun::star::uno;
50 
51 namespace {
52 
53 const sal_Int32 BIFF12_CFRULE_TYPE_CELLIS = 1;
54 const sal_Int32 BIFF12_CFRULE_TYPE_EXPRESSION = 2;
55 const sal_Int32 BIFF12_CFRULE_TYPE_COLORSCALE = 3;
56 const sal_Int32 BIFF12_CFRULE_TYPE_DATABAR = 4;
57 const sal_Int32 BIFF12_CFRULE_TYPE_TOPTEN = 5;
58 const sal_Int32 BIFF12_CFRULE_TYPE_ICONSET = 6;
59 
60 const sal_Int32 BIFF12_CFRULE_SUB_CELLIS = 0;
61 const sal_Int32 BIFF12_CFRULE_SUB_EXPRESSION = 1;
62 const sal_Int32 BIFF12_CFRULE_SUB_COLORSCALE = 2;
63 const sal_Int32 BIFF12_CFRULE_SUB_DATABAR = 3;
64 const sal_Int32 BIFF12_CFRULE_SUB_ICONSET = 4;
65 const sal_Int32 BIFF12_CFRULE_SUB_TOPTEN = 5;
66 const sal_Int32 BIFF12_CFRULE_SUB_UNIQUE = 7;
67 const sal_Int32 BIFF12_CFRULE_SUB_TEXT = 8;
68 const sal_Int32 BIFF12_CFRULE_SUB_BLANK = 9;
69 const sal_Int32 BIFF12_CFRULE_SUB_NOTBLANK = 10;
70 const sal_Int32 BIFF12_CFRULE_SUB_ERROR = 11;
71 const sal_Int32 BIFF12_CFRULE_SUB_NOTERROR = 12;
72 const sal_Int32 BIFF12_CFRULE_SUB_TODAY = 15;
73 const sal_Int32 BIFF12_CFRULE_SUB_TOMORROW = 16;
74 const sal_Int32 BIFF12_CFRULE_SUB_YESTERDAY = 17;
75 const sal_Int32 BIFF12_CFRULE_SUB_LAST7DAYS = 18;
76 const sal_Int32 BIFF12_CFRULE_SUB_LASTMONTH = 19;
77 const sal_Int32 BIFF12_CFRULE_SUB_NEXTMONTH = 20;
78 const sal_Int32 BIFF12_CFRULE_SUB_THISWEEK = 21;
79 const sal_Int32 BIFF12_CFRULE_SUB_NEXTWEEK = 22;
80 const sal_Int32 BIFF12_CFRULE_SUB_LASTWEEK = 23;
81 const sal_Int32 BIFF12_CFRULE_SUB_THISMONTH = 24;
82 const sal_Int32 BIFF12_CFRULE_SUB_ABOVEAVERAGE = 25;
83 const sal_Int32 BIFF12_CFRULE_SUB_BELOWAVERAGE = 26;
84 const sal_Int32 BIFF12_CFRULE_SUB_DUPLICATE = 27;
85 const sal_Int32 BIFF12_CFRULE_SUB_EQABOVEAVERAGE = 29;
86 const sal_Int32 BIFF12_CFRULE_SUB_EQBELOWAVERAGE = 30;
87 
88 const sal_Int32 BIFF12_CFRULE_TIMEOP_TODAY = 0;
89 const sal_Int32 BIFF12_CFRULE_TIMEOP_YESTERDAY = 1;
90 const sal_Int32 BIFF12_CFRULE_TIMEOP_LAST7DAYS = 2;
91 const sal_Int32 BIFF12_CFRULE_TIMEOP_THISWEEK = 3;
92 const sal_Int32 BIFF12_CFRULE_TIMEOP_LASTWEEK = 4;
93 const sal_Int32 BIFF12_CFRULE_TIMEOP_LASTMONTH = 5;
94 const sal_Int32 BIFF12_CFRULE_TIMEOP_TOMORROW = 6;
95 const sal_Int32 BIFF12_CFRULE_TIMEOP_NEXTWEEK = 7;
96 const sal_Int32 BIFF12_CFRULE_TIMEOP_NEXTMONTH = 8;
97 const sal_Int32 BIFF12_CFRULE_TIMEOP_THISMONTH = 9;
98 
99 const sal_uInt16 BIFF12_CFRULE_STOPIFTRUE = 0x0002;
100 const sal_uInt16 BIFF12_CFRULE_ABOVEAVERAGE = 0x0004;
101 const sal_uInt16 BIFF12_CFRULE_BOTTOM = 0x0008;
102 const sal_uInt16 BIFF12_CFRULE_PERCENT = 0x0010;
103 
104 bool isValue(const OUString& rStr, double& rVal)
105 {
106  sal_Int32 nEnd = -1;
107  rVal = rtl::math::stringToDouble(rStr.trim(), '.', ',', nullptr, &nEnd);
108 
109  return nEnd >= rStr.getLength();
110 }
111 
112 void SetCfvoData( ColorScaleRuleModelEntry* pEntry, const AttributeList& rAttribs )
113 {
114  OUString aType = rAttribs.getString( XML_type, OUString() );
115  OUString aVal = rAttribs.getString(XML_val, OUString());
116 
117  double nVal = 0.0;
118  bool bVal = isValue(aVal, nVal);
119  if( !bVal || aType == "formula" )
120  {
121  pEntry->maFormula = aVal;
122  }
123  else
124  {
125  pEntry->mnVal = nVal;
126  }
127 
128  if (aType == "num")
129  {
130  pEntry->mbNum = true;
131  }
132  else if( aType == "min" )
133  {
134  pEntry->mbMin = true;
135  }
136  else if( aType == "max" )
137  {
138  pEntry->mbMax = true;
139  }
140  else if( aType == "percent" )
141  {
142  pEntry->mbPercent = true;
143  }
144  else if( aType == "percentile" )
145  {
146  pEntry->mbPercentile = true;
147  }
148 }
149 
150 }
151 
153  WorksheetHelper( rFormat ),
154  mnCfvo(0),
155  mnCol(0)
156 {
157 }
158 
160 {
161  if(mnCfvo >= maColorScaleRuleEntries.size())
162  maColorScaleRuleEntries.emplace_back();
163 
164  SetCfvoData( &maColorScaleRuleEntries[mnCfvo], rAttribs );
165 
166  ++mnCfvo;
167 }
168 
169 namespace {
170 
171 ::Color importOOXColor(const AttributeList& rAttribs, const ThemeBuffer& rThemeBuffer, const GraphicHelper& rGraphicHelper)
172 {
173  ::Color nColor;
174  if( rAttribs.hasAttribute( XML_rgb ) )
175  nColor = ::Color(rAttribs.getUnsignedHex( XML_rgb, UNSIGNED_RGB_TRANSPARENT ));
176  else if( rAttribs.hasAttribute( XML_theme ) )
177  {
178  sal_uInt32 nThemeIndex = rAttribs.getUnsigned( XML_theme, 0 );
179 
180  // Excel has a bug in the mapping of index 0, 1, 2 and 3.
181  if (nThemeIndex == 0)
182  nThemeIndex = 1;
183  else if (nThemeIndex == 1)
184  nThemeIndex = 0;
185  else if (nThemeIndex == 2)
186  nThemeIndex = 3;
187  else if (nThemeIndex == 3)
188  nThemeIndex = 2;
189 
190  nColor = rThemeBuffer.getColorByIndex( nThemeIndex );
191  }
192 
193  ::Color aColor;
194  double nTint = rAttribs.getDouble(XML_tint, 0.0);
195  if (nTint != 0.0)
196  {
197  oox::drawingml::Color aDMColor;
198  aDMColor.setSrgbClr(nColor);
199  aDMColor.addExcelTintTransformation(nTint);
200  aColor = aDMColor.getColor(rGraphicHelper);
201  }
202  else
203  aColor = nColor.GetRGBColor();
204 
205  return aColor;
206 }
207 
208 }
209 
211 {
212  ThemeBuffer& rThemeBuffer = getTheme();
213  GraphicHelper& rGraphicHelper = getBaseFilter().getGraphicHelper();
214  ::Color aColor = importOOXColor(rAttribs, rThemeBuffer, rGraphicHelper);
215 
216  if(mnCol >= maColorScaleRuleEntries.size())
217  maColorScaleRuleEntries.emplace_back();
218 
219  maColorScaleRuleEntries[mnCol].maColor = aColor;
220  ++mnCol;
221 }
222 
223 namespace {
224 
225 ScColorScaleEntry* ConvertToModel( const ColorScaleRuleModelEntry& rEntry, ScDocument* pDoc, const ScAddress& rAddr )
226 {
227  ScColorScaleEntry* pEntry = new ScColorScaleEntry(rEntry.mnVal, rEntry.maColor);
228 
229  if(rEntry.mbMin)
230  pEntry->SetType(COLORSCALE_MIN);
231  if(rEntry.mbMax)
232  pEntry->SetType(COLORSCALE_MAX);
233  if(rEntry.mbPercent)
234  pEntry->SetType(COLORSCALE_PERCENT);
235  if(rEntry.mbPercentile)
237  if (rEntry.mbNum)
238  pEntry->SetType(COLORSCALE_VALUE);
239 
240  if(!rEntry.maFormula.isEmpty())
241  {
242  pEntry->SetType(COLORSCALE_FORMULA);
244  }
245 
246  return pEntry;
247 }
248 
249 }
250 
252 {
254  {
255  ScColorScaleEntry* pEntry = ConvertToModel( rEntry, pDoc, rAddr );
256 
257  pFormat->AddEntry( pEntry );
258  }
259 }
260 
262  WorksheetHelper( rFormat ),
263  mxFormat(new ScDataBarFormatData)
264 {
265  mxFormat->meAxisPosition = databar::NONE;
266 }
267 
269 {
270  ThemeBuffer& rThemeBuffer = getTheme();
271  GraphicHelper& rGraphicHelper = getBaseFilter().getGraphicHelper();
272  ::Color aColor = importOOXColor(rAttribs, rThemeBuffer, rGraphicHelper);
273 
274  mxFormat->maPositiveColor = aColor;
275 }
276 
277 void DataBarRule::importCfvo( const AttributeList& rAttribs )
278 {
279  ColorScaleRuleModelEntry* pEntry;
280  if(!mpLowerLimit)
281  {
283  pEntry = mpLowerLimit.get();
284  }
285  else
286  {
288  pEntry = mpUpperLimit.get();
289  }
290 
291  SetCfvoData( pEntry, rAttribs );
292 }
293 
295 {
296  mxFormat->mbOnlyBar = !rAttribs.getBool( XML_showValue, true );
297  mxFormat->mnMinLength = rAttribs.getUnsigned( XML_minLength, 10);
298  mxFormat->mnMaxLength = rAttribs.getUnsigned( XML_maxLength, 90);
299 }
300 
301 void DataBarRule::SetData( ScDataBarFormat* pFormat, ScDocument* pDoc, const ScAddress& rAddr )
302 {
303  ScColorScaleEntry* pUpperEntry = ConvertToModel(*mpUpperLimit, pDoc, rAddr);
304  ScColorScaleEntry* pLowerEntry = ConvertToModel(*mpLowerLimit, pDoc, rAddr);
305 
306  mxFormat->mpUpperLimit.reset( pUpperEntry );
307  mxFormat->mpLowerLimit.reset( pLowerEntry );
308  pFormat->SetDataBarData(mxFormat.release());
309 }
310 
312  WorksheetHelper( rParent ),
313  mxFormatData( new ScIconSetFormatData ),
314  mbCustom(false)
315 {
316 }
317 
318 void IconSetRule::importCfvo( const AttributeList& rAttribs )
319 {
320  ColorScaleRuleModelEntry aNewEntry;
321  SetCfvoData(&aNewEntry, rAttribs);
322 
323  maEntries.push_back(aNewEntry);
324 }
325 
327 {
328  maIconSetType = rAttribs.getString( XML_iconSet, "3TrafficLights1" );
329  mxFormatData->mbShowValue = rAttribs.getBool( XML_showValue, true );
330  mxFormatData->mbReverse = rAttribs.getBool( XML_reverse, false );
331  mbCustom = rAttribs.getBool(XML_custom, false);
332 }
333 
334 void IconSetRule::importFormula(const OUString& rFormula)
335 {
336  ColorScaleRuleModelEntry& rEntry = maEntries.back();
337  double nVal = 0.0;
338  if ((rEntry.mbNum || rEntry.mbPercent || rEntry.mbPercentile) && isValue(rFormula, nVal))
339  {
340  rEntry.mnVal = nVal;
341  }
342  else if (!rFormula.isEmpty())
343  rEntry.maFormula = rFormula;
344 }
345 
346 namespace {
347 
348 ScIconSetType getType(const OUString& rName)
349 {
350  ScIconSetType eIconSetType = IconSet_3TrafficLights1;
351  const ScIconSetMap* pIconSetMap = ScIconSetFormat::g_IconSetMap;
352  for(size_t i = 0; pIconSetMap[i].pName; ++i)
353  {
354  if(OUString::createFromAscii(pIconSetMap[i].pName) == rName)
355  {
356  eIconSetType = pIconSetMap[i].eType;
357  break;
358  }
359  }
360 
361  return eIconSetType;
362 }
363 
364 }
365 
367 {
368  OUString aIconSet = rAttribs.getString(XML_iconSet, OUString());
369  sal_Int32 nIndex = rAttribs.getInteger(XML_iconId, -1);
370  if (aIconSet == "NoIcons")
371  {
372  nIndex = -1;
373  }
374 
375  ScIconSetType eIconSetType = getType(aIconSet);
376  mxFormatData->maCustomVector.emplace_back(eIconSetType, nIndex);
377 }
378 
379 void IconSetRule::SetData( ScIconSetFormat* pFormat, ScDocument* pDoc, const ScAddress& rPos )
380 {
381  for(const ColorScaleRuleModelEntry & rEntry : maEntries)
382  {
383  ScColorScaleEntry* pModelEntry = ConvertToModel( rEntry, pDoc, rPos );
384  mxFormatData->m_Entries.emplace_back(pModelEntry);
385  }
386 
387  mxFormatData->eIconSetType = getType(maIconSetType);
388  mxFormatData->mbCustom = mbCustom;
389  pFormat->SetIconSetData(mxFormatData.release());
390 }
391 
393  mnPriority( -1 ),
395  mnOperator( XML_TOKEN_INVALID ),
396  mnTimePeriod( XML_TOKEN_INVALID ),
397  mnRank( 0 ),
398  mnStdDev( 0 ),
399  mnDxfId( -1 ),
400  mbStopIfTrue( false ),
401  mbBottom( false ),
402  mbPercent( false ),
403  mbAboveAverage( true ),
404  mbEqualAverage( false )
405 {
406 }
407 
408 void CondFormatRuleModel::setBiffOperator( sal_Int32 nOperator )
409 {
410  static const sal_Int32 spnOperators[] = {
411  XML_TOKEN_INVALID, XML_between, XML_notBetween, XML_equal, XML_notEqual,
412  XML_greaterThan, XML_lessThan, XML_greaterThanOrEqual, XML_lessThanOrEqual };
413  mnOperator = STATIC_ARRAY_SELECT( spnOperators, nOperator, XML_TOKEN_INVALID );
414 }
415 
416 void CondFormatRuleModel::setBiff12TextType( sal_Int32 nOperator )
417 {
418  // note: type XML_notContainsText vs. operator XML_notContains
419  static const sal_Int32 spnTypes[] = { XML_containsText, XML_notContainsText, XML_beginsWith, XML_endsWith };
420  mnType = STATIC_ARRAY_SELECT( spnTypes, nOperator, XML_TOKEN_INVALID );
421  static const sal_Int32 spnOperators[] = { XML_containsText, XML_notContains, XML_beginsWith, XML_endsWith };
422  mnOperator = STATIC_ARRAY_SELECT( spnOperators, nOperator, XML_TOKEN_INVALID );
423 }
424 
426  WorksheetHelper( rCondFormat ),
427  mrCondFormat( rCondFormat ),
428  mpFormat(pFormat),
429  mpFormatEntry(nullptr)
430 {
431 }
432 
434 {
435  maModel.maText = rAttribs.getString( XML_text, OUString() );
436  maModel.mnPriority = rAttribs.getInteger( XML_priority, -1 );
437  maModel.mnType = rAttribs.getToken( XML_type, XML_TOKEN_INVALID );
438  maModel.mnOperator = rAttribs.getToken( XML_operator, XML_TOKEN_INVALID );
439  maModel.mnTimePeriod = rAttribs.getToken( XML_timePeriod, XML_TOKEN_INVALID );
440  maModel.mnRank = rAttribs.getInteger( XML_rank, 0 );
441  maModel.mnStdDev = rAttribs.getInteger( XML_stdDev, 0 );
442  maModel.mnDxfId = rAttribs.getInteger( XML_dxfId, -1 );
443  maModel.mbStopIfTrue = rAttribs.getBool( XML_stopIfTrue, false );
444  maModel.mbBottom = rAttribs.getBool( XML_bottom, false );
445  maModel.mbPercent = rAttribs.getBool( XML_percent, false );
446  maModel.mbAboveAverage = rAttribs.getBool( XML_aboveAverage, true );
447  maModel.mbEqualAverage = rAttribs.getBool( XML_equalAverage, false );
448 
449  if(maModel.mnType == XML_colorScale)
450  {
451  //import the remaining values
452 
453  }
454 }
455 
456 void CondFormatRule::appendFormula( const OUString& rFormula )
457 {
459  ApiTokenSequence aTokens = getFormulaParser().importFormula( aBaseAddr, rFormula );
460  maModel.maFormulas.push_back( aTokens );
461 }
462 
464 {
465  sal_Int32 nType, nSubType, nOperator, nFmla1Size, nFmla2Size, nFmla3Size;
466  sal_uInt16 nFlags;
467  nType = rStrm.readInt32();
468  nSubType = rStrm.readInt32();
469  maModel.mnDxfId = rStrm.readInt32();
470  maModel.mnPriority = rStrm.readInt32();
471  nOperator = rStrm.readInt32();
472  rStrm.skip( 8 );
473  nFlags = rStrm.readuInt16();
474  nFmla1Size = rStrm.readInt32();
475  nFmla2Size = rStrm.readInt32();
476  nFmla3Size = rStrm.readInt32();
477  rStrm >> maModel.maText;
478 
479  /* Import the formulas. For no obvious reason, the sizes of the formulas
480  are already stored before. Nevertheless the following formulas contain
481  their own sizes. */
482 
483  // first formula
484  // I am not bored enough to bother simplifying these expressions
485  SAL_WARN_IF( !( (nFmla1Size >= 0) || ((nFmla2Size == 0) && (nFmla3Size == 0)) ), "sc.filter", "CondFormatRule::importCfRule - missing first formula" );
486  SAL_WARN_IF( !( (nFmla1Size > 0) == (rStrm.getRemaining() >= 8) ), "sc.filter", "CondFormatRule::importCfRule - formula size mismatch" );
487  if( rStrm.getRemaining() >= 8 )
488  {
491  maModel.maFormulas.push_back( aTokens );
492 
493  // second formula
494  OSL_ENSURE( (nFmla2Size >= 0) || (nFmla3Size == 0), "CondFormatRule::importCfRule - missing second formula" );
495  OSL_ENSURE( (nFmla2Size > 0) == (rStrm.getRemaining() >= 8), "CondFormatRule::importCfRule - formula size mismatch" );
496  if( rStrm.getRemaining() >= 8 )
497  {
498  aTokens = getFormulaParser().importFormula( aBaseAddr, FormulaType::CondFormat, rStrm );
499  maModel.maFormulas.push_back( aTokens );
500 
501  // third formula
502  OSL_ENSURE( (nFmla3Size > 0) == (rStrm.getRemaining() >= 8), "CondFormatRule::importCfRule - formula size mismatch" );
503  if( rStrm.getRemaining() >= 8 )
504  {
505  aTokens = getFormulaParser().importFormula( aBaseAddr, FormulaType::CondFormat, rStrm );
506  maModel.maFormulas.push_back( aTokens );
507  }
508  }
509  }
510 
511  // flags
512  maModel.mbStopIfTrue = getFlag( nFlags, BIFF12_CFRULE_STOPIFTRUE );
513  maModel.mbBottom = getFlag( nFlags, BIFF12_CFRULE_BOTTOM );
514  maModel.mbPercent = getFlag( nFlags, BIFF12_CFRULE_PERCENT );
515  maModel.mbAboveAverage = getFlag( nFlags, BIFF12_CFRULE_ABOVEAVERAGE );
516  // no flag for equalAverage, must be determined from subtype below...
517 
518  // Convert the type/operator settings. This is a real mess...
519  switch( nType )
520  {
521  case BIFF12_CFRULE_TYPE_CELLIS:
522  SAL_WARN_IF(
523  nSubType != BIFF12_CFRULE_SUB_CELLIS, "sc.filter",
524  "CondFormatRule::importCfRule - rule type/subtype mismatch");
525  maModel.mnType = XML_cellIs;
526  maModel.setBiffOperator( nOperator );
527  OSL_ENSURE( maModel.mnOperator != XML_TOKEN_INVALID, "CondFormatRule::importCfRule - unknown operator" );
528  break;
529  case BIFF12_CFRULE_TYPE_EXPRESSION:
530  // here we have to look at the subtype to find the real type...
531  switch( nSubType )
532  {
533  case BIFF12_CFRULE_SUB_EXPRESSION:
534  OSL_ENSURE( nOperator == 0, "CondFormatRule::importCfRule - unexpected operator value" );
535  maModel.mnType = XML_expression;
536  break;
537  case BIFF12_CFRULE_SUB_UNIQUE:
538  OSL_ENSURE( nOperator == 0, "CondFormatRule::importCfRule - unexpected operator value" );
539  maModel.mnType = XML_uniqueValues;
540  break;
541  case BIFF12_CFRULE_SUB_TEXT:
542  maModel.setBiff12TextType( nOperator );
543  OSL_ENSURE( maModel.mnType != XML_TOKEN_INVALID, "CondFormatRule::importCfRule - unexpected operator value" );
544  break;
545  case BIFF12_CFRULE_SUB_BLANK:
546  OSL_ENSURE( nOperator == 0, "CondFormatRule::importCfRule - unexpected operator value" );
547  maModel.mnType = XML_containsBlanks;
548  break;
549  case BIFF12_CFRULE_SUB_NOTBLANK:
550  OSL_ENSURE( nOperator == 0, "CondFormatRule::importCfRule - unexpected operator value" );
551  maModel.mnType = XML_notContainsBlanks;
552  break;
553  case BIFF12_CFRULE_SUB_ERROR:
554  OSL_ENSURE( nOperator == 0, "CondFormatRule::importCfRule - unexpected operator value" );
555  maModel.mnType = XML_containsErrors;
556  break;
557  case BIFF12_CFRULE_SUB_NOTERROR:
558  OSL_ENSURE( nOperator == 0, "CondFormatRule::importCfRule - unexpected operator value" );
559  maModel.mnType = XML_notContainsErrors;
560  break;
561  case BIFF12_CFRULE_SUB_TODAY:
562  SAL_WARN_IF(
563  nOperator != BIFF12_CFRULE_TIMEOP_TODAY, "sc.filter",
564  "CondFormatRule::importCfRule - unexpected time operator value");
565  maModel.mnType = XML_timePeriod;
566  maModel.mnTimePeriod = XML_today;
567  break;
568  case BIFF12_CFRULE_SUB_TOMORROW:
569  SAL_WARN_IF(
570  nOperator != BIFF12_CFRULE_TIMEOP_TOMORROW, "sc.filter",
571  "CondFormatRule::importCfRule - unexpected time operator value");
572  maModel.mnType = XML_timePeriod;
573  maModel.mnTimePeriod = XML_tomorrow;
574  break;
575  case BIFF12_CFRULE_SUB_YESTERDAY:
576  SAL_WARN_IF(
577  nOperator != BIFF12_CFRULE_TIMEOP_YESTERDAY,
578  "sc.filter",
579  "CondFormatRule::importCfRule - unexpected time operator value");
580  maModel.mnType = XML_timePeriod;
581  maModel.mnTimePeriod = XML_yesterday;
582  break;
583  case BIFF12_CFRULE_SUB_LAST7DAYS:
584  SAL_WARN_IF(
585  nOperator != BIFF12_CFRULE_TIMEOP_LAST7DAYS,
586  "sc.filter",
587  "CondFormatRule::importCfRule - unexpected time operator value");
588  maModel.mnType = XML_timePeriod;
589  maModel.mnTimePeriod = XML_last7Days;
590  break;
591  case BIFF12_CFRULE_SUB_LASTMONTH:
592  SAL_WARN_IF(
593  nOperator != BIFF12_CFRULE_TIMEOP_LASTMONTH,
594  "sc.filter",
595  "CondFormatRule::importCfRule - unexpected time operator value");
596  maModel.mnType = XML_timePeriod;
597  maModel.mnTimePeriod = XML_lastMonth;
598  break;
599  case BIFF12_CFRULE_SUB_NEXTMONTH:
600  SAL_WARN_IF(
601  nOperator != BIFF12_CFRULE_TIMEOP_NEXTMONTH,
602  "sc.filter",
603  "CondFormatRule::importCfRule - unexpected time operator value");
604  maModel.mnType = XML_timePeriod;
605  maModel.mnTimePeriod = XML_nextMonth;
606  break;
607  case BIFF12_CFRULE_SUB_THISWEEK:
608  SAL_WARN_IF(
609  nOperator != BIFF12_CFRULE_TIMEOP_THISWEEK, "sc.filter",
610  "CondFormatRule::importCfRule - unexpected time operator value");
611  maModel.mnType = XML_timePeriod;
612  maModel.mnTimePeriod = XML_thisWeek;
613  break;
614  case BIFF12_CFRULE_SUB_NEXTWEEK:
615  SAL_WARN_IF(
616  nOperator != BIFF12_CFRULE_TIMEOP_NEXTWEEK, "sc.filter",
617  "CondFormatRule::importCfRule - unexpected time operator value");
618  maModel.mnType = XML_timePeriod;
619  maModel.mnTimePeriod = XML_nextWeek;
620  break;
621  case BIFF12_CFRULE_SUB_LASTWEEK:
622  SAL_WARN_IF(
623  nOperator != BIFF12_CFRULE_TIMEOP_LASTWEEK, "sc.filter",
624  "CondFormatRule::importCfRule - unexpected time operator value");
625  maModel.mnType = XML_timePeriod;
626  maModel.mnTimePeriod = XML_lastWeek;
627  break;
628  case BIFF12_CFRULE_SUB_THISMONTH:
629  SAL_WARN_IF(
630  nOperator != BIFF12_CFRULE_TIMEOP_THISMONTH,
631  "sc.filter",
632  "CondFormatRule::importCfRule - unexpected time operator value");
633  maModel.mnType = XML_timePeriod;
634  maModel.mnTimePeriod = XML_thisMonth;
635  break;
636  case BIFF12_CFRULE_SUB_ABOVEAVERAGE:
637  OSL_ENSURE( maModel.mbAboveAverage, "CondFormatRule::importCfRule - wrong above-average flag" );
638  maModel.mnType = XML_aboveAverage;
639  maModel.mnStdDev = nOperator; // operator field used for standard deviation
640  maModel.mbAboveAverage = true;
641  maModel.mbEqualAverage = false; // does not exist as real flag...
642  break;
643  case BIFF12_CFRULE_SUB_BELOWAVERAGE:
644  OSL_ENSURE( !maModel.mbAboveAverage, "CondFormatRule::importCfRule - wrong above-average flag" );
645  maModel.mnType = XML_aboveAverage;
646  maModel.mnStdDev = nOperator; // operator field used for standard deviation
647  maModel.mbAboveAverage = false;
648  maModel.mbEqualAverage = false; // does not exist as real flag...
649  break;
650  case BIFF12_CFRULE_SUB_DUPLICATE:
651  OSL_ENSURE( nOperator == 0, "CondFormatRule::importCfRule - unexpected operator value" );
652  maModel.mnType = XML_duplicateValues;
653  break;
654  case BIFF12_CFRULE_SUB_EQABOVEAVERAGE:
655  OSL_ENSURE( maModel.mbAboveAverage, "CondFormatRule::importCfRule - wrong above-average flag" );
656  maModel.mnType = XML_aboveAverage;
657  maModel.mnStdDev = nOperator; // operator field used for standard deviation
658  maModel.mbAboveAverage = true;
659  maModel.mbEqualAverage = true; // does not exist as real flag...
660  break;
661  case BIFF12_CFRULE_SUB_EQBELOWAVERAGE:
662  OSL_ENSURE( !maModel.mbAboveAverage, "CondFormatRule::importCfRule - wrong above-average flag" );
663  maModel.mnType = XML_aboveAverage;
664  maModel.mnStdDev = nOperator; // operator field used for standard deviation
665  maModel.mbAboveAverage = false;
666  maModel.mbEqualAverage = true; // does not exist as real flag...
667  break;
668  }
669  break;
670  case BIFF12_CFRULE_TYPE_COLORSCALE:
671  SAL_WARN_IF(
672  nSubType != BIFF12_CFRULE_SUB_COLORSCALE, "sc.filter",
673  "CondFormatRule::importCfRule - rule type/subtype mismatch");
674  OSL_ENSURE( nOperator == 0, "CondFormatRule::importCfRule - unexpected operator value" );
675  maModel.mnType = XML_colorScale;
676  break;
677  case BIFF12_CFRULE_TYPE_DATABAR:
678  SAL_WARN_IF(
679  nSubType != BIFF12_CFRULE_SUB_DATABAR, "sc.filter",
680  "CondFormatRule::importCfRule - rule type/subtype mismatch");
681  OSL_ENSURE( nOperator == 0, "CondFormatRule::importCfRule - unexpected operator value" );
682  maModel.mnType = XML_dataBar;
683  break;
684  case BIFF12_CFRULE_TYPE_TOPTEN:
685  SAL_WARN_IF(
686  nSubType != BIFF12_CFRULE_SUB_TOPTEN, "sc.filter",
687  "CondFormatRule::importCfRule - rule type/subtype mismatch");
688  maModel.mnType = XML_top10;
689  maModel.mnRank = nOperator; // operator field used for rank value
690  break;
691  case BIFF12_CFRULE_TYPE_ICONSET:
692  SAL_WARN_IF(
693  nSubType != BIFF12_CFRULE_SUB_ICONSET, "sc.filter",
694  "CondFormatRule::importCfRule - rule type/subtype mismatch");
695  OSL_ENSURE( nOperator == 0, "CondFormatRule::importCfRule - unexpected operator value" );
696  maModel.mnType = XML_iconSet;
697  break;
698  default:
699  OSL_FAIL( "CondFormatRule::importCfRule - unknown rule type" );
700  }
701 }
702 
703 void CondFormatRule::setFormatEntry(sal_Int32 nPriority, ScFormatEntry* pEntry)
704 {
705  maModel.mnPriority = nPriority;
706  mpFormatEntry = pEntry;
707 }
708 
710 {
711  if (mpFormatEntry)
712  {
714  return;
715  }
716 
718 
719  /* Replacement formula for unsupported rule types (text comparison rules,
720  time period rules, cell type rules). The replacement formulas below may
721  contain several placeholders:
722  - '#B' will be replaced by the current relative base address (may occur
723  several times).
724  - '#R' will be replaced by the entire range list of the conditional
725  formatting (absolute addresses).
726  - '#T' will be replaced by the quoted comparison text.
727  - '#L' will be replaced by the length of the comparison text (from
728  the 'text' attribute) used in text comparison rules.
729  - '#K' will be replaced by the rank (from the 'rank' attribute) used in
730  top-10 rules.
731  - '#M' will be replaced by the top/bottom flag (from the 'bottom'
732  attribute) used in the RANK function in top-10 rules.
733  - '#C' will be replaced by one of the comparison operators <, >, <=, or
734  >=, according to the 'aboveAverage' and 'equalAverage' flags.
735  */
736  OUString aReplaceFormula;
737 
738  switch( maModel.mnType )
739  {
740  case XML_cellIs:
742  break;
743  case XML_duplicateValues:
744  eOperator = ScConditionMode::Duplicate;
745  break;
746  case XML_uniqueValues:
747  eOperator = ScConditionMode::NotDuplicate;
748  break;
749  case XML_expression:
750  eOperator = ScConditionMode::Direct;
751  break;
752  case XML_containsText:
753  OSL_ENSURE( maModel.mnOperator == XML_containsText, "CondFormatRule::finalizeImport - unexpected operator" );
754  eOperator = ScConditionMode::ContainsText;
755  break;
756  case XML_notContainsText:
757  // note: type XML_notContainsText vs. operator XML_notContains
758  OSL_ENSURE( maModel.mnOperator == XML_notContains, "CondFormatRule::finalizeImport - unexpected operator" );
760  break;
761  case XML_beginsWith:
762  OSL_ENSURE( maModel.mnOperator == XML_beginsWith, "CondFormatRule::finalizeImport - unexpected operator" );
763  eOperator = ScConditionMode::BeginsWith;
764  break;
765  case XML_endsWith:
766  OSL_ENSURE( maModel.mnOperator == XML_endsWith, "CondFormatRule::finalizeImport - unexpected operator" );
767  eOperator = ScConditionMode::EndsWith;
768  break;
769  case XML_timePeriod:
770  break;
771  case XML_containsBlanks:
772  aReplaceFormula = "LEN(TRIM(#B))=0";
773  break;
774  case XML_notContainsBlanks:
775  aReplaceFormula = "LEN(TRIM(#B))>0";
776  break;
777  case XML_containsErrors:
778  eOperator = ScConditionMode::Error;
779  break;
780  case XML_notContainsErrors:
781  eOperator = ScConditionMode::NoError;
782  break;
783  case XML_top10:
784  if(maModel.mbPercent)
785  {
786  if(maModel.mbBottom)
787  eOperator = ScConditionMode::BottomPercent;
788  else
789  eOperator = ScConditionMode::TopPercent;
790  }
791  else
792  {
793  if(maModel.mbBottom)
794  eOperator = ScConditionMode::Bottom10;
795  else
796  eOperator = ScConditionMode::Top10;
797  }
798  break;
799  case XML_aboveAverage:
801  {
804  else
805  eOperator = ScConditionMode::AboveAverage;
806  }
807  else
808  {
811  else
812  eOperator = ScConditionMode::BelowAverage;
813  }
814  break;
815  case XML_colorScale:
816  break;
817  }
818 
819  if( !aReplaceFormula.isEmpty() )
820  {
821  OUString aAddress;
822  sal_Int32 nStrPos = aReplaceFormula.getLength();
823  while( (nStrPos = aReplaceFormula.lastIndexOf( '#', nStrPos )) >= 0 )
824  {
825  switch( aReplaceFormula[ nStrPos + 1 ] )
826  {
827  case 'B': // current base address
828  if( aAddress.isEmpty() )
830  aReplaceFormula = aReplaceFormula.replaceAt( nStrPos, 2, aAddress );
831  break;
832  default:
833  OSL_FAIL( "CondFormatRule::finalizeImport - unknown placeholder" );
834  }
835  }
836 
837  // set the replacement formula
838  maModel.maFormulas.clear();
839  appendFormula( aReplaceFormula );
840  eOperator = ScConditionMode::Direct;
841  }
842 
844 
845  if( eOperator == ScConditionMode::Error || eOperator == ScConditionMode::NoError )
846  {
847  ScDocument& rDoc = getScDocument();
848  OUString aStyleName = getStyles().createDxfStyle( maModel.mnDxfId );
849  ScCondFormatEntry* pNewEntry = new ScCondFormatEntry( eOperator, nullptr, nullptr, &rDoc, aPos, aStyleName );
850  mpFormat->AddEntry(pNewEntry);
851  }
852  else if( eOperator == ScConditionMode::BeginsWith || eOperator == ScConditionMode::EndsWith ||
854  {
855  ScDocument& rDoc = getScDocument();
856  ScTokenArray aTokenArray(&rDoc);
858  aTokenArray.AddString(rSPool.intern(maModel.maText));
859  OUString aStyleName = getStyles().createDxfStyle( maModel.mnDxfId );
860  ScCondFormatEntry* pNewEntry = new ScCondFormatEntry( eOperator, &aTokenArray, nullptr, &rDoc, aPos, aStyleName );
861  mpFormat->AddEntry(pNewEntry);
862  }
863  else if( (eOperator != ScConditionMode::NONE) && !maModel.maFormulas.empty() )
864  {
865  ScDocument& rDoc = getScDocument();
866  std::unique_ptr<ScTokenArray> pTokenArray2;
867  if( maModel.maFormulas.size() >= 2)
868  {
869  pTokenArray2.reset(new ScTokenArray(&rDoc));
871  rDoc.CheckLinkFormulaNeedingCheck(*pTokenArray2);
872  }
873 
874  ScTokenArray aTokenArray(&rDoc);
875  OUString aStyleName = getStyles().createDxfStyle( maModel.mnDxfId );
876  ScTokenConversion::ConvertToTokenArray( rDoc, aTokenArray, maModel.maFormulas[ 0 ] );
877  rDoc.CheckLinkFormulaNeedingCheck( aTokenArray);
878  ScCondFormatEntry* pNewEntry = new ScCondFormatEntry(eOperator,
879  &aTokenArray, pTokenArray2.get(), &rDoc, aPos, aStyleName);
880  mpFormat->AddEntry(pNewEntry);
881  }
882  else if ( eOperator == ScConditionMode::Top10 || eOperator == ScConditionMode::Bottom10 ||
883  eOperator == ScConditionMode::TopPercent || eOperator == ScConditionMode::BottomPercent )
884  {
885  ScDocument& rDoc = getScDocument();
886  ScTokenArray aTokenArray(&rDoc);
887  aTokenArray.AddDouble( maModel.mnRank );
888  OUString aStyleName = getStyles().createDxfStyle( maModel.mnDxfId );
889  ScCondFormatEntry* pNewEntry = new ScCondFormatEntry( eOperator, &aTokenArray, nullptr, &rDoc, aPos, aStyleName );
890  mpFormat->AddEntry(pNewEntry);
891  }
892  else if( eOperator == ScConditionMode::AboveAverage || eOperator == ScConditionMode::BelowAverage ||
894  {
895  ScDocument& rDoc = getScDocument();
896  // actually that is still unsupported
897  ScTokenArray aTokenArrayDev(&rDoc);
898  aTokenArrayDev.AddDouble( maModel.mnStdDev );
899  OUString aStyleName = getStyles().createDxfStyle( maModel.mnDxfId );
900  ScCondFormatEntry* pNewEntry = new ScCondFormatEntry( eOperator, &aTokenArrayDev, nullptr, &rDoc, aPos, aStyleName );
901  mpFormat->AddEntry(pNewEntry);
902  }
903  else if( eOperator == ScConditionMode::Duplicate || eOperator == ScConditionMode::NotDuplicate )
904  {
905  ScDocument& rDoc = getScDocument();
906  OUString aStyleName = getStyles().createDxfStyle( maModel.mnDxfId );
907  ScCondFormatEntry* pNewEntry = new ScCondFormatEntry( eOperator, nullptr, nullptr, &rDoc, aPos, aStyleName );
908  mpFormat->AddEntry(pNewEntry);
909  }
910  else if( maModel.mnType == XML_timePeriod )
911  {
913  switch( maModel.mnTimePeriod )
914  {
915  case XML_yesterday:
916  eDateType = condformat::YESTERDAY;
917  break;
918  case XML_today:
919  eDateType = condformat::TODAY;
920  break;
921  case XML_tomorrow:
922  eDateType = condformat::TOMORROW;
923  break;
924  case XML_last7Days:
925  eDateType = condformat::LAST7DAYS;
926  break;
927  case XML_lastWeek:
928  eDateType = condformat::LASTWEEK;
929  break;
930  case XML_thisWeek:
931  eDateType = condformat::THISWEEK;
932  break;
933  case XML_nextWeek:
934  eDateType = condformat::NEXTWEEK;
935  break;
936  case XML_lastMonth:
937  eDateType = condformat::LASTMONTH;
938  break;
939  case XML_thisMonth:
940  eDateType = condformat::THISMONTH;
941  break;
942  case XML_nextMonth:
943  eDateType = condformat::NEXTMONTH;
944  break;
945  default:
946  SAL_WARN("sc.filter", "CondFormatRule::finalizeImport - unknown time period type" );
947  }
948 
949  ScDocument& rDoc = getScDocument();
950  ScCondDateFormatEntry* pFormatEntry = new ScCondDateFormatEntry(&rDoc);
951  pFormatEntry->SetDateType(eDateType);
952  OUString aStyleName = getStyles().createDxfStyle( maModel.mnDxfId );
953  pFormatEntry->SetStyleName( aStyleName );
954 
955  mpFormat->AddEntry(pFormatEntry);
956  }
957  else if( mpColor )
958  {
959  ScDocument& rDoc = getScDocument();
960  ScColorScaleFormat* pFormatEntry = new ScColorScaleFormat(&rDoc);
961 
962  mpFormat->AddEntry(pFormatEntry);
963 
964  mpColor->AddEntries( pFormatEntry, &rDoc, aPos );
965  }
966  else if (mpDataBar)
967  {
968  ScDocument& rDoc = getScDocument();
969  ScDataBarFormat* pFormatEntry = new ScDataBarFormat(&rDoc);
970 
971  mpFormat->AddEntry(pFormatEntry);
972  mpDataBar->SetData( pFormatEntry, &rDoc, aPos );
973 
974  }
975  else if(mpIconSet)
976  {
977  ScDocument& rDoc = getScDocument();
978  ScIconSetFormat* pFormatEntry = new ScIconSetFormat(&rDoc);
979 
980  mpFormat->AddEntry(pFormatEntry);
981  mpIconSet->SetData( pFormatEntry, &rDoc, aPos );
982  }
983 }
984 
986 {
987  if(!mpColor)
988  mpColor.reset( new ColorScaleRule(mrCondFormat) );
989 
990  return mpColor.get();
991 }
992 
994 {
995  if(!mpDataBar)
996  mpDataBar.reset( new DataBarRule(mrCondFormat) );
997 
998  return mpDataBar.get();
999 }
1000 
1002 {
1003  if(!mpIconSet)
1004  mpIconSet.reset( new IconSetRule(mrCondFormat) );
1005 
1006  return mpIconSet.get();
1007 }
1008 
1010  mbPivot( false )
1011 {
1012 }
1013 
1015  WorksheetHelper( rHelper ),
1016  mpFormat(nullptr),
1017  mbReadyForFinalize(false)
1018 {
1019 }
1020 
1022 {
1023  getAddressConverter().convertToCellRangeList( maModel.maRanges, rAttribs.getString( XML_sqref, OUString() ), getSheetIndex(), true );
1024  maModel.mbPivot = rAttribs.getBool( XML_pivot, false );
1026 }
1027 
1029 {
1030  CondFormatRuleRef xRule = createRule();
1031  xRule->importCfRule( rAttribs );
1032  insertRule( xRule );
1033  return xRule;
1034 }
1035 
1037 {
1038  BinRangeList aRanges;
1039  rStrm.skip( 8 );
1040  rStrm >> aRanges;
1043 }
1044 
1046 {
1047  CondFormatRuleRef xRule = createRule();
1048  xRule->importCfRule( rStrm );
1049  insertRule( xRule );
1050 }
1051 
1053 {
1054  // probably some error in the xml if we are not ready
1055  if ( !mbReadyForFinalize )
1056  return;
1057  ScDocument& rDoc = getScDocument();
1061  sal_Int32 nIndex = getScDocument().AddCondFormat(std::unique_ptr<ScConditionalFormat>(mpFormat), nTab);
1062 
1063  rDoc.AddCondFormatData( maModel.maRanges, nTab, nIndex );
1064 }
1065 
1067 {
1068  return std::make_shared<CondFormatRule>( *this, mpFormat );
1069 }
1070 
1072 {
1073  if( xRule && (xRule->getPriority() > 0) )
1074  {
1075  OSL_ENSURE( maRules.find( xRule->getPriority() ) == maRules.end(), "CondFormat::insertRule - multiple rules with equal priority" );
1076  maRules[ xRule->getPriority() ] = xRule;
1077  }
1078 }
1079 
1081  WorksheetHelper( rHelper )
1082 {
1083 }
1084 
1086 {
1087  CondFormatRef xCondFmt = createCondFormat();
1088  xCondFmt->importConditionalFormatting( rAttribs );
1089  return xCondFmt;
1090 }
1091 
1092 namespace {
1093 
1094 ScConditionalFormat* findFormatByRange(const ScRangeList& rRange, const ScDocument* pDoc, SCTAB nTab)
1095 {
1096  ScConditionalFormatList* pList = pDoc->GetCondFormList(nTab);
1097  for (auto const& it : *pList)
1098  {
1099  if (it->GetRange() == rRange)
1100  {
1101  return it.get();
1102  }
1103  }
1104 
1105  return nullptr;
1106 }
1107 
1108 class ScRangeListHasher
1109 {
1110 public:
1111  size_t operator() (ScRangeList const& rRanges) const
1112  {
1113  size_t nHash = 0;
1114  for (size_t nIdx = 0; nIdx < rRanges.size(); ++nIdx)
1115  nHash += rRanges[nIdx].hashArea();
1116  return nHash;
1117  }
1118 };
1119 
1120 }
1121 
1123 {
1124  std::unordered_set<size_t> aDoneExtCFs;
1125  typedef std::unordered_map<ScRangeList, CondFormat*, ScRangeListHasher> RangeMap;
1126  typedef std::vector<std::unique_ptr<ScFormatEntry>> FormatEntries;
1127  RangeMap aRangeMap;
1128  for (auto& rxCondFormat : maCondFormats)
1129  {
1130  if (aRangeMap.find(rxCondFormat->getRanges()) != aRangeMap.end())
1131  continue;
1132  aRangeMap[rxCondFormat->getRanges()] = rxCondFormat.get();
1133  }
1134 
1135  size_t nExtCFIndex = 0;
1136  for (const auto& rxExtCondFormat : maExtCondFormats)
1137  {
1138  ScDocument* pDoc = &getScDocument();
1139  const ScRangeList& rRange = rxExtCondFormat->getRange();
1140  RangeMap::iterator it = aRangeMap.find(rRange);
1141  if (it != aRangeMap.end())
1142  {
1143  CondFormat& rCondFormat = *it->second;
1144  const FormatEntries& rEntries = rxExtCondFormat->getEntries();
1145  const std::vector<sal_Int32>& rPriorities = rxExtCondFormat->getPriorities();
1146  size_t nEntryIdx = 0;
1147  for (const auto& rxEntry : rEntries)
1148  {
1149  CondFormatRuleRef xRule = rCondFormat.createRule();
1150  ScFormatEntry* pNewEntry = rxEntry->Clone(pDoc);
1151  sal_Int32 nPriority = rPriorities[nEntryIdx];
1152  if (nPriority == -1)
1153  nPriority = mnNonPrioritizedRuleNextPriority++;
1154  xRule->setFormatEntry(nPriority, pNewEntry);
1155  rCondFormat.insertRule(xRule);
1156  ++nEntryIdx;
1157  }
1158 
1159  aDoneExtCFs.insert(nExtCFIndex);
1160  }
1161 
1162  ++nExtCFIndex;
1163  }
1164 
1165  for( const auto& rxCondFormat : maCondFormats )
1166  {
1167  if ( rxCondFormat)
1168  rxCondFormat->finalizeImport();
1169  }
1170  for ( const auto& rxCfRule : maCfRules )
1171  {
1172  if ( rxCfRule )
1173  rxCfRule->finalizeImport();
1174  }
1175 
1176  nExtCFIndex = 0;
1177  for (const auto& rxExtCondFormat : maExtCondFormats)
1178  {
1179  if (aDoneExtCFs.count(nExtCFIndex))
1180  {
1181  ++nExtCFIndex;
1182  continue;
1183  }
1184 
1185  ScDocument* pDoc = &getScDocument();
1186  const ScRangeList& rRange = rxExtCondFormat->getRange();
1187  SCTAB nTab = rRange.front().aStart.Tab();
1188  ScConditionalFormat* pFormat = findFormatByRange(rRange, pDoc, nTab);
1189  if (!pFormat)
1190  {
1191  // create new conditional format and insert it
1192  auto pNewFormat = std::make_unique<ScConditionalFormat>(0, pDoc);
1193  pFormat = pNewFormat.get();
1194  pNewFormat->SetRange(rRange);
1195  sal_uLong nKey = pDoc->AddCondFormat(std::move(pNewFormat), nTab);
1196  pDoc->AddCondFormatData(rRange, nTab, nKey);
1197  }
1198 
1199  const std::vector< std::unique_ptr<ScFormatEntry> >& rEntries = rxExtCondFormat->getEntries();
1200  for (const auto& rxEntry : rEntries)
1201  {
1202  pFormat->AddEntry(rxEntry->Clone(pDoc));
1203  }
1204 
1205  ++nExtCFIndex;
1206  }
1207 
1208  rStyleIdx = 0; // Resets <extlst> <cfRule> style index.
1209 }
1210 
1212 {
1213  CondFormatRef xCondFmt = createCondFormat();
1214  xCondFmt->importCondFormatting( rStrm );
1215  return xCondFmt;
1216 }
1217 
1219 {
1220  ExtCfDataBarRuleRef extRule = std::make_shared<ExtCfDataBarRule>( pTarget, *this );
1221  maCfRules.push_back( extRule );
1222  return extRule;
1223 }
1224 
1225 std::vector< std::unique_ptr<ExtCfCondFormat> >& CondFormatBuffer::importExtCondFormat()
1226 {
1227  return maExtCondFormats;
1228 }
1229 
1231 {
1232  switch( nToken )
1233  {
1234  case XML_between: return ConditionOperator2::BETWEEN;
1235  case XML_equal: return ConditionOperator2::EQUAL;
1236  case XML_greaterThan: return ConditionOperator2::GREATER;
1237  case XML_greaterThanOrEqual: return ConditionOperator2::GREATER_EQUAL;
1238  case XML_lessThan: return ConditionOperator2::LESS;
1239  case XML_lessThanOrEqual: return ConditionOperator2::LESS_EQUAL;
1240  case XML_notBetween: return ConditionOperator2::NOT_BETWEEN;
1241  case XML_notEqual: return ConditionOperator2::NOT_EQUAL;
1242  case XML_duplicateValues: return ConditionOperator2::DUPLICATE;
1243  }
1244  return ConditionOperator2::NONE;
1245 }
1246 
1248 {
1249  switch( nToken )
1250  {
1251  case XML_between: return ScConditionMode::Between;
1252  case XML_equal: return ScConditionMode::Equal;
1253  case XML_greaterThan: return ScConditionMode::Greater;
1254  case XML_greaterThanOrEqual: return ScConditionMode::EqGreater;
1255  case XML_lessThan: return ScConditionMode::Less;
1256  case XML_lessThanOrEqual: return ScConditionMode::EqLess;
1257  case XML_notBetween: return ScConditionMode::NotBetween;
1258  case XML_notEqual: return ScConditionMode::NotEqual;
1259  case XML_duplicateValues: return ScConditionMode::Duplicate;
1260  case XML_uniqueValues: return ScConditionMode::NotDuplicate;
1261  }
1262  return ScConditionMode::NONE;
1263 }
1264 
1265 // private --------------------------------------------------------------------
1266 
1268 {
1269  CondFormatRef xCondFmt = std::make_shared<CondFormat>( *this );
1270  maCondFormats.push_back( xCondFmt );
1271  return xCondFmt;
1272 }
1273 
1275  WorksheetHelper(rParent),
1276  mnRuleType( ExtCfDataBarRule::UNKNOWN ),
1277  mpTarget(pTarget)
1278 {
1279 }
1280 
1282 {
1283  switch ( mnRuleType )
1284  {
1285  case DATABAR:
1286  {
1287  ScDataBarFormatData* pDataBar = mpTarget;
1288  if( maModel.maAxisPosition == "none" )
1289  pDataBar->meAxisPosition = databar::NONE;
1290  else if( maModel.maAxisPosition == "middle" )
1291  pDataBar->meAxisPosition = databar::MIDDLE;
1292  else
1293  pDataBar->meAxisPosition = databar::AUTOMATIC;
1294  pDataBar->mbGradient = maModel.mbGradient;
1295  break;
1296  }
1297  case AXISCOLOR:
1298  {
1299  ScDataBarFormatData* pDataBar = mpTarget;
1300  pDataBar->maAxisColor = maModel.mnAxisColor;
1301  break;
1302  }
1303  case NEGATIVEFILLCOLOR:
1304  {
1305  ScDataBarFormatData* pDataBar = mpTarget;
1306  pDataBar->mpNegativeColor.reset( new ::Color(maModel.mnNegativeColor) );
1307  pDataBar->mbNeg = true;
1308  break;
1309  }
1310  case CFVO:
1311  {
1312  ScDataBarFormatData* pDataBar = mpTarget;
1313  ScColorScaleEntry* pEntry = nullptr;
1314  if(maModel.mbIsLower)
1315  pEntry = pDataBar->mpLowerLimit.get();
1316  else
1317  pEntry = pDataBar->mpUpperLimit.get();
1318 
1319  if(maModel.maColorScaleType == "min")
1320  pEntry->SetType(COLORSCALE_MIN);
1321  else if (maModel.maColorScaleType == "max")
1322  pEntry->SetType(COLORSCALE_MAX);
1323  else if (maModel.maColorScaleType == "autoMin")
1324  pEntry->SetType(COLORSCALE_AUTO);
1325  else if (maModel.maColorScaleType == "autoMax")
1326  pEntry->SetType(COLORSCALE_AUTO);
1327  else if (maModel.maColorScaleType == "percentile")
1328  pEntry->SetType(COLORSCALE_PERCENTILE);
1329  else if (maModel.maColorScaleType == "percent")
1330  pEntry->SetType(COLORSCALE_PERCENT);
1331  else if (maModel.maColorScaleType == "formula")
1332  pEntry->SetType(COLORSCALE_FORMULA);
1333  break;
1334  }
1335  case UNKNOWN: // nothing to do
1336  default:
1337  break;
1338  }
1339 }
1340 
1342 {
1343  mnRuleType = DATABAR;
1344  maModel.mbGradient = rAttribs.getBool( XML_gradient, true );
1345  maModel.maAxisPosition = rAttribs.getString( XML_axisPosition, "automatic" );
1346 }
1347 
1349 {
1351  ThemeBuffer& rThemeBuffer = getTheme();
1352  GraphicHelper& rGraphicHelper = getBaseFilter().getGraphicHelper();
1353  ::Color aColor = importOOXColor(rAttribs, rThemeBuffer, rGraphicHelper);
1354  maModel.mnNegativeColor = aColor;
1355 }
1356 
1358 {
1360  ThemeBuffer& rThemeBuffer = getTheme();
1361  GraphicHelper& rGraphicHelper = getBaseFilter().getGraphicHelper();
1362  ::Color aColor = importOOXColor(rAttribs, rThemeBuffer, rGraphicHelper);
1363  maModel.mnAxisColor = aColor;
1364 }
1365 
1367 {
1368  mnRuleType = CFVO;
1369  maModel.maColorScaleType = rAttribs.getString( XML_type, OUString() );
1370 }
1371 
1372 ExtCfCondFormat::ExtCfCondFormat(const ScRangeList& rRange, std::vector< std::unique_ptr<ScFormatEntry> >& rEntries,
1373  const std::vector<sal_Int32>* pPriorities):
1374  maRange(rRange)
1375 {
1376  maEntries.swap(rEntries);
1377  if (pPriorities)
1378  maPriorities = *pPriorities;
1379  else
1380  maPriorities.resize(maEntries.size(), -1);
1381 }
1382 
1384 {
1385 }
1386 
1388 {
1389  return maRange;
1390 }
1391 
1392 const std::vector< std::unique_ptr<ScFormatEntry> >& ExtCfCondFormat::getEntries() const
1393 {
1394  return maEntries;
1395 }
1396 
1397 } // namespace oox
1398 
1399 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void setBiffOperator(sal_Int32 nOperator)
Sets the passed BIFF operator for condition type cellIs.
CondFormatBuffer(const WorksheetHelper &rHelper)
CondFormatModel()
Conditional formatting belongs to pivot table.
std::unique_ptr< ScIconSetFormatData > mxFormatData
sal_Int32 nIndex
CondFormatRef importConditionalFormatting(const AttributeList &rAttribs)
Imports settings from the conditionalFormatting element.
ScAddress aStart
Definition: address.hxx:500
ColorScaleRule * getColorScale()
SharedString intern(const OUString &rStr)
void importIcon(const AttributeList &rAttribs)
SC_DLLPUBLIC svl::SharedStringPool & GetSharedStringPool()
Definition: documen2.cxx:563
static sal_Int32 convertToApiOperator(sal_Int32 nToken)
Converts an OOXML condition operator token to the API constant.
bool mbBottom
True = stop evaluating rules, if this rule is true.
virtual void skip(sal_Int32 nBytes, size_t nAtomSize=1) override
void importColor(const AttributeList &rAttribs)
std::vector< sal_Int32 > maPriorities
void AddEntry(ScFormatEntry *pNew)
Definition: conditio.cxx:1747
OptValue< bool > getBool(sal_Int32 nAttrToken) const
sal_Int32 mnStdDev
In top-10 rules: True = bottom, false = top.
sal_Int32 mnRank
In time-period rules: Type of time period.
std::vector< std::unique_ptr< ExtCfCondFormat > > maExtCondFormats
All external conditional formatting rules in a sheet.
bool getType(BSTR name, Type &type)
A 2D cell range address list for binary filters.
OptValue< sal_Int32 > getInteger(sal_Int32 nAttrToken) const
OUString maText
Formulas for rule conditions.
sal_uIntPtr sal_uLong
void importCondFormatting(SequenceInputStream &rStrm)
Imports settings from the CONDFORMATTING record.
const sal_uInt32 UNSIGNED_RGB_TRANSPARENT
void importConditionalFormatting(const AttributeList &rAttribs)
Imports settings from the conditionalFormatting element.
IconSetRule(const WorksheetHelper &rParent)
std::unique_ptr< ScDataBarFormatData > mxFormat
sal_Int32 mnCol
void finalizeImport()
Creates a conditional formatting rule in the Calc document.
ApiTokenSequenceVector maFormulas
void importCfvo(const AttributeList &rAttribs)
SC_DLLPUBLIC sal_uLong AddCondFormat(std::unique_ptr< ScConditionalFormat > pNew, SCTAB nTab)
Definition: documen4.cxx:705
const CondFormat & mrCondFormat
OptValue< double > getDouble(sal_Int32 nAttrToken) const
void finalizeImport()
Creates the conditional formatting in the Calc document.
#define STATIC_ARRAY_SELECT(array, index, def)
OptValue< OUString > getString(sal_Int32 nAttrToken) const
ScCondFormatDateType
Definition: conditio.hxx:485
OUString createDxfStyle(sal_Int32 nDxfId) const
Creates the style sheet described by the DXF with the passed identifier.
bool mbEqualAverage
In average rules: True = above average, false = below.
static const ScIconSetMap g_IconSetMap[]
Definition: colorscale.hxx:381
sal_Int32 mnTimePeriod
In cell-is rules: Comparison operator.
StylesBuffer & getStyles() const
Returns all cell formatting objects read from the styles substream.
void setBiff12TextType(sal_Int32 nOperator)
Sets the passed BIFF12 text comparison type and operator.
std::unique_ptr< IconSetRule > mpIconSet
std::vector< std::unique_ptr< ExtCfCondFormat > > & importExtCondFormat()
GraphicHelper & getGraphicHelper() const
sal_uInt16 readuInt16()
SC_DLLPUBLIC ScConditionalFormatList * GetCondFormList(SCTAB nTab) const
Definition: documen4.cxx:857
SC_DLLPUBLIC void CheckLinkFormulaNeedingCheck(const ScTokenArray &rCode)
Check token array and set link check if ocDde/ocWebservice is contained.
Definition: documen8.cxx:1152
sal_Int32 mnPriority
Text for 'contains' rules.
void SetFormula(const OUString &rFormula, ScDocument *pDoc, const ScAddress &rAddr, formula::FormulaGrammar::Grammar eGrammar=formula::FormulaGrammar::GRAM_DEFAULT)
Definition: colorscale.cxx:206
sal_uInt32 getUnsignedHex(sal_Int32 nAttrToken, sal_uInt32 nDefault) const
CondFormatRuleModel()
In average rules: True = include average, false = exclude.
void AddEntries(ScColorScaleFormat *pFormat, ScDocument *pDoc, const ScAddress &rAddr)
bool hasAttribute(sal_Int32 nAttrToken) const
::oox::core::FilterBase & getBaseFilter() const
Returns the base filter object (base class of all filters).
ScConditionMode
Definition: conditio.hxx:61
SCTAB Tab() const
Definition: address.hxx:271
T * mpTarget
Represents a conditional formatting object with a list of affected cell ranges.
ExtCfDataBarRuleVec maCfRules
All conditional formatting in a sheet.
UNKNOWN
sal_Int32 mnType
Priority of this rule.
bool mbPercent
In top-10 rules: True = bottom, false = top.
OptValue< sal_uInt32 > getUnsigned(sal_Int32 nAttrToken) const
void insertRule(CondFormatRuleRef const &xRule)
void importColor(const AttributeList &rAttribs)
ApiTokenSequence importFormula(const ScAddress &rBaseAddr, const OUString &rFormulaString) const
Converts an OOXML formula string.
bool getFlag(Type nBitField, Type nMask)
ScAddress GetTopLeftCorner() const
Definition: rangelst.cxx:1157
Color maAxisColor
Color of the axis if used Default color is black.
Definition: colorscale.hxx:146
std::unique_ptr< ColorScaleRuleModelEntry > mpUpperLimit
std::unique_ptr< ColorScaleRule > mpColor
ExtCfCondFormat(const ScRangeList &aRange, std::vector< std::unique_ptr< ScFormatEntry > > &rEntries, const std::vector< sal_Int32 > *pPriorities=nullptr)
void SetRange(const ScRangeList &rRanges)
Definition: conditio.cxx:1741
double mnPriority
CondFormatRuleMap maRules
Model of this conditional formatting.
std::shared_ptr< CondFormatRule > CondFormatRuleRef
std::vector< ColorScaleRuleModelEntry > maColorScaleRuleEntries
sal_uInt16 char * pName
Definition: callform.cxx:58
bool mbStopIfTrue
Differential formatting identifier.
int i
sal_Int32 rStyleIdx
void AddEntry(ScColorScaleEntry *pEntry)
Definition: colorscale.cxx:401
sal_Int32 mnType
::Color getColor(const GraphicHelper &rGraphicHelper,::Color nPhClr=API_RGB_TRANSPARENT) const
const ScRangeList & getRanges() const
Returns the cell ranges this conditional formatting belongs to.
bool mbGradient
Paint the bars with gradient.
Definition: colorscale.hxx:153
Shared formula definition.
size_t size() const
Definition: rangelst.hxx:90
ExtCfDataBarRule(ScDataBarFormatData *pTarget, const WorksheetHelper &rParent)
std::shared_ptr< CondFormat > CondFormatRef
void importNegativeFillColor(const AttributeList &rAttribs)
void convertToCellRangeList(ScRangeList &orRanges, const OUString &rString, sal_Int16 nSheet, bool bTrackOverflow)
Tries to convert the passed string to a cell range list.
XML_TOKEN_INVALID
ExtCfDataBarRuleRef createExtCfDataBarRule(ScDataBarFormatData *pTarget)
static SC_DLLPUBLIC bool ConvertToTokenArray(ScDocument &rDoc, ScTokenArray &rTokenArray, const css::uno::Sequence< css::sheet::FormulaToken > &rSequence)
Definition: tokenuno.cxx:348
virtual ScFormatEntry * Clone(ScDocument *pDoc) const =0
ScIconSetType eType
Definition: colorscale.hxx:217
void importAttribs(const AttributeList &rAttribs)
static ScConditionMode convertToInternalOperator(sal_Int32 nToken)
bool mbPivot
Cell ranges for this conditional format.
css::uno::Sequence< ApiToken > ApiTokenSequence
sal_Int32 mnDxfId
In average rules: Number of std deviations.
CondFormatRule(const CondFormat &rCondFormat, ScConditionalFormat *pFormat)
void importAttribs(const AttributeList &rAttribs)
DefTokenId nToken
Definition: qproform.cxx:400
CondFormatRuleRef createRule()
ScDataBarFormatData * mpTarget
sal_Int64 getRemaining() const
void setSrgbClr(sal_Int32 nRgb)
const ScRangeList & getRange() const
void importCfvo(const AttributeList &rAttribs)
databar::ScAxisPosition meAxisPosition
Paint negative values into the same direction as positive values If false we will set the mid point a...
Definition: colorscale.hxx:168
CondFormatRuleModel maModel
FormulaToken * AddString(const svl::SharedString &rStr)
#define SAL_WARN_IF(condition, area, stream)
std::unique_ptr< Color > mpNegativeColor
Specifies the color for negative values.
Definition: colorscale.hxx:141
std::unique_ptr< ScColorScaleEntry, o3tl::default_delete< ScColorScaleEntry > > mpUpperLimit
Definition: colorscale.hxx:185
void SetIconSetData(ScIconSetFormatData *pData)
::Color getColorByIndex(size_t nIndex) const
Definition: themebuffer.cxx:46
void appendFormula(const OUString &rFormula)
Appends a new condition formula string.
ColorScaleRule(const CondFormat &rFormat)
bool mbAboveAverage
In top-10 rules: True = percent, false = rank.
static OUString generateAddress2dString(const ScAddress &rAddress, bool bAbsolute)
Generates a cell address string in A1 notation from the passed cell address.
ScConditionalFormat * mpFormat
SCTAB getSheetIndex() const
Returns the index of the current sheet.
void importCfRule(const AttributeList &rAttribs)
Imports rule settings from the cfRule element.
std::unique_ptr< ColorScaleRuleModelEntry > mpLowerLimit
void SetDateType(condformat::ScCondFormatDateType eType)
Definition: conditio.cxx:1672
const std::vector< std::unique_ptr< ScFormatEntry > > & getEntries() const
QPRO_FUNC_TYPE nType
Definition: qproform.cxx:401
void SetData(ScDataBarFormat *pFormat, ScDocument *pDoc, const ScAddress &rAddr)
std::unique_ptr< DataBarRule > mpDataBar
const char * pName
Definition: colorscale.hxx:216
void addExcelTintTransformation(double fTint)
CondFormat(const WorksheetHelper &rHelper)
void importCfvo(const AttributeList &rAttribs)
ScIconSetType
Definition: colorscale.hxx:189
std::shared_ptr< ExtCfDataBarRule > ExtCfDataBarRuleRef
FormulaParser & getFormulaParser() const
Returns a shared import formula parser (import filter only!).
CondFormatRuleRef importCfRule(const AttributeList &rAttribs)
Imports a conditional formatting rule from the cfRule element.
ScRange & front()
Definition: rangelst.hxx:93
sal_Int32 mnOperator
Type of the rule.
ThemeBuffer & getTheme() const
Returns the office theme object read from the theme substorage.
#define SAL_WARN(area, stream)
void setFormatEntry(sal_Int32 nPriority, ScFormatEntry *pEntry)
Directly set a ScFormatEntry with a priority ready for finalizeImport().
void SetType(ScColorScaleEntryType eType)
Definition: colorscale.cxx:323
void importCfvo(const AttributeList &rAttribs)
std::unique_ptr< ScColorScaleEntry, o3tl::default_delete< ScColorScaleEntry > > mpLowerLimit
Definition: colorscale.hxx:186
void importFormula(const OUString &rFormula)
CondFormatRef importCondFormatting(SequenceInputStream &rStrm)
Imports settings from the CONDFORMATTING record.
ScConditionalFormat * mpFormat
Maps formatting rules by priority.
std::vector< std::unique_ptr< ScFormatEntry > > maEntries
void importAxisColor(const AttributeList &rAttribs)
DataBarRule(const CondFormat &rFormat)
void SetData(ScIconSetFormat *pFormat, ScDocument *pDoc, const ScAddress &rAddr)
void forEachMem(FuncType pFunc) const
void importDataBar(const AttributeList &rAttribs)
void SetStyleName(const OUString &rStyleName)
Definition: conditio.cxx:1677
bool mbNeg
Use different color for negative values.
Definition: colorscale.hxx:160
void SetDataBarData(ScDataBarFormatData *pData)
Definition: colorscale.cxx:720
B2DRange maRange
OptValue< sal_Int32 > getToken(sal_Int32 nAttrToken) const
sal_Int16 SCTAB
Definition: types.hxx:23
SC_DLLPUBLIC void AddCondFormatData(const ScRangeList &rRange, SCTAB nTab, sal_uInt32 nIndex)
Definition: document.cxx:4809
AddressConverter & getAddressConverter() const
Returns the converter for string to cell address/range conversion.
std::vector< ColorScaleRuleModelEntry > maEntries
FormulaToken * AddDouble(double fVal)