LibreOffice Module sc (master)  1
vbaformatconditions.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 <ooo/vba/excel/XRange.hpp>
21 #include <com/sun/star/sheet/XSheetConditionalEntry.hpp>
22 #include <basic/sberrors.hxx>
23 #include <comphelper/sequence.hxx>
24 #include <cppuhelper/exc_hlp.hxx>
25 #include <vector>
26 #include <unonames.hxx>
27 #include "vbaformatconditions.hxx"
28 #include "vbaformatcondition.hxx"
29 #include "vbastyles.hxx"
30 
31 using namespace ::ooo::vba;
32 using namespace ::com::sun::star;
33 
34 void SAL_CALL
36 {
37  try
38  {
39  ScVbaStyles* pStyles = static_cast< ScVbaStyles* >( mxStyles.get() );
40  if ( !pStyles )
41  DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED, {} );
42  sal_Int32 nCount = mxSheetConditionalEntries->getCount();
43  for (sal_Int32 i = nCount - 1; i >= 0; i--)
44  {
45  uno::Reference< sheet::XSheetConditionalEntry > xSheetConditionalEntry( mxSheetConditionalEntries->getByIndex(i), uno::UNO_QUERY_THROW );
46  pStyles->Delete(xSheetConditionalEntry->getStyleName());
47  mxSheetConditionalEntries->removeByIndex(i);
48  }
49  notifyRange();
50  }
51  catch (uno::Exception& )
52  {
53  DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED, {});
54  }
55 }
56 
57 uno::Type SAL_CALL
59 {
61 }
62 
63 static uno::Any xSheetConditionToFormatCondition( const uno::Reference< XHelperInterface >& xRangeParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< excel::XStyles >& xStyles, const uno::Reference< excel::XFormatConditions >& xFormatConditions, const uno::Reference< beans::XPropertySet >& xRangeProps, const uno::Any& aObject )
64 {
65  uno::Reference< sheet::XSheetConditionalEntry > xSheetConditionalEntry;
66  aObject >>= xSheetConditionalEntry;
67 
68  uno::Reference< excel::XStyle > xStyle( xStyles->Item( uno::makeAny( xSheetConditionalEntry->getStyleName() ), uno::Any() ), uno::UNO_QUERY_THROW );
69  uno::Reference< excel::XFormatCondition > xCondition = new ScVbaFormatCondition( xRangeParent, xContext, xSheetConditionalEntry, xStyle, xFormatConditions, xRangeProps );
70  return uno::makeAny( xCondition );
71 }
72 
75 {
76  return xSheetConditionToFormatCondition( uno::Reference< XHelperInterface >( mxRangeParent, uno::UNO_QUERY_THROW ), mxContext, mxStyles, this, mxParentRangePropertySet, aObject );
77 }
78 
79 namespace {
80 
81 class EnumWrapper : public EnumerationHelper_BASE
82 {
83  uno::Reference<container::XIndexAccess > m_xIndexAccess;
84  uno::Reference<excel::XRange > m_xParentRange;
85  uno::Reference<uno::XComponentContext > m_xContext;
86  uno::Reference<excel::XStyles > m_xStyles;
87  uno::Reference<excel::XFormatConditions > m_xParentCollection;
88  uno::Reference<beans::XPropertySet > m_xProps;
89 
90  sal_Int32 nIndex;
91 public:
92  EnumWrapper( const uno::Reference< container::XIndexAccess >& xIndexAccess, const uno::Reference<excel::XRange >& xRange, const uno::Reference<uno::XComponentContext >& xContext, const uno::Reference<excel::XStyles >& xStyles, const uno::Reference< excel::XFormatConditions >& xCollection, const uno::Reference<beans::XPropertySet >& xProps ) : m_xIndexAccess( xIndexAccess ), m_xParentRange( xRange ), m_xContext( xContext ), m_xStyles( xStyles ), m_xParentCollection( xCollection ), m_xProps( xProps ), nIndex( 0 ) {}
93  virtual sal_Bool SAL_CALL hasMoreElements( ) override
94  {
95  return ( nIndex < m_xIndexAccess->getCount() );
96  }
97 
98  virtual uno::Any SAL_CALL nextElement( ) override
99  {
100  try
101  {
102  if ( nIndex < m_xIndexAccess->getCount() )
103  return xSheetConditionToFormatCondition( uno::Reference< XHelperInterface >( m_xParentRange, uno::UNO_QUERY_THROW ), m_xContext, m_xStyles, m_xParentCollection, m_xProps, m_xIndexAccess->getByIndex( nIndex++ ) );
104  }
105  catch (const container::NoSuchElementException&)
106  {
107  throw;
108  }
109  catch (const lang::WrappedTargetException&)
110  {
111  throw;
112  }
113  catch (const uno::RuntimeException&)
114  {
115  throw;
116  }
117  catch (const uno::Exception& e)
118  {
119  css::uno::Any a(cppu::getCaughtException());
120  throw css::lang::WrappedTargetException(
121  "wrapped Exception " + e.Message,
122  css::uno::Reference<css::uno::XInterface>(), a);
123  }
124  throw container::NoSuchElementException();
125  }
126 };
127 
128 }
129 
130 uno::Reference< excel::XFormatCondition > SAL_CALL
131 ScVbaFormatConditions::Add( ::sal_Int32 _nType, const uno::Any& _aOperator, const uno::Any& _aFormula1, const uno::Any& _aFormula2 )
132 {
133  return Add( _nType, _aOperator, _aFormula1, _aFormula2, uno::Reference< excel::XStyle >() );
134 }
135 
136 uno::Reference< excel::XFormatCondition >
137 ScVbaFormatConditions::Add( ::sal_Int32 _nType, const uno::Any& _aOperator, const uno::Any& _aFormula1, const uno::Any& _aFormula2, const css::uno::Reference< excel::XStyle >& _xStyle )
138 {
139  // #TODO
140  // #FIXME
141  // This method will NOT handle r1c1 formulas [*]and only assumes that
142  // the formulas are _xlA1 based ( need to hook into calc work this should
143  // address this )
144  // [*] reason: getA1Formula method below is just a hook and just
145  // returns what it gets ( e.g. doesn't convert anything )
146  uno::Reference< excel::XStyle > xStyle( _xStyle );
147  uno::Reference< excel::XFormatCondition > xFormatCondition;
148  try
149  {
150  OUString sStyleName;
151  if ( !xStyle.is() )
152  {
153  sStyleName = getStyleName();
154  xStyle = mxStyles->Add(sStyleName, uno::Any() );
155  }
156  else
157  {
158  sStyleName = xStyle->getName();
159  }
160 
161  std::vector< beans::PropertyValue > aPropertyValueVector;
162  sheet::ConditionOperator aType = ScVbaFormatCondition::retrieveAPIType(_nType, uno::Reference< sheet::XSheetCondition >() );
163  uno::Any aValue;
164 
165  if ( aType == sheet::ConditionOperator_FORMULA)
166  aValue <<= sheet::ConditionOperator_FORMULA;
167  else
168  aValue <<= ScVbaFormatCondition::retrieveAPIOperator(_aOperator);
169 
170  beans::PropertyValue aProperty( "Operator", 0, aValue, beans::PropertyState_DIRECT_VALUE );
171  aPropertyValueVector.push_back( aProperty );
172 
173  if ( _aFormula1.hasValue() )
174  {
175  beans::PropertyValue aProp( "Formula1", 0, uno::makeAny( getA1Formula( _aFormula1 ) ), beans::PropertyState_DIRECT_VALUE );
176  aPropertyValueVector.push_back( aProp );
177  }
178  if ( _aFormula2.hasValue() )
179  {
180  beans::PropertyValue aProp( "Formula2", 0, uno::makeAny( getA1Formula( _aFormula2 ) ), beans::PropertyState_DIRECT_VALUE );
181  aPropertyValueVector.push_back( aProp );
182  }
183  aProperty.Name = "StyleName";
184  aProperty.Value <<= sStyleName;
185 
186  mxSheetConditionalEntries->addNew(comphelper::containerToSequence(aPropertyValueVector));
187  for (sal_Int32 i = mxSheetConditionalEntries->getCount()-1; i >= 0; i--)
188  {
189  uno::Reference< sheet::XSheetConditionalEntry > xSheetConditionalEntry( mxSheetConditionalEntries->getByIndex(i), uno::UNO_QUERY_THROW );
190  if (xSheetConditionalEntry->getStyleName() == sStyleName)
191  {
192  xFormatCondition = new ScVbaFormatCondition(uno::Reference< XHelperInterface >( mxRangeParent, uno::UNO_QUERY_THROW ), mxContext, xSheetConditionalEntry, xStyle, this, mxParentRangePropertySet);
193  notifyRange();
194  return xFormatCondition;
195  }
196  }
197  }
198  catch (uno::Exception& )
199  {
200  }
201  DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED, {} );
202  return xFormatCondition;
203 }
204 
205 uno::Reference< container::XEnumeration > SAL_CALL
207 {
208  return new EnumWrapper( m_xIndexAccess, mxRangeParent, mxContext, mxStyles, this, mxParentRangePropertySet );
209 }
210 
211 void
213 {
214  try
215  {
216  mxParentRangePropertySet->setPropertyValue(SC_UNONAME_CONDFMT, uno::makeAny( mxSheetConditionalEntries ));
217  }
218  catch (uno::Exception& )
219  {
220  DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED, {});
221  }
222 }
223 
224 OUString
225 ScVbaFormatConditions::getA1Formula(const css::uno::Any& _aFormula)
226 {
227  // #TODO, #FIXME hook-in proper formula conversion detection & logic
228  OUString sFormula;
229  if ( !( _aFormula >>= sFormula ) )
230  DebugHelper::basicexception(ERRCODE_BASIC_BAD_PARAMETER, {} );
231  return sFormula;
232 }
233 
234 OUString
236 {
237  ScVbaStyles* pStyles = static_cast< ScVbaStyles* >( mxStyles.get() );
238  if ( !pStyles )
239  DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED, {} );
240  uno::Sequence< OUString > sCellStyleNames = pStyles->getStyleNames();
241  return ContainerUtilities::getUniqueName(sCellStyleNames, "Excel_CondFormat", u"_");
242 }
243 
244 void
245 ScVbaFormatConditions::removeFormatCondition( const OUString& _sStyleName, bool _bRemoveStyle)
246 {
247  try
248  {
249  sal_Int32 nElems = mxSheetConditionalEntries->getCount();
250  for (sal_Int32 i = 0; i < nElems; i++)
251  {
252  uno::Reference< sheet::XSheetConditionalEntry > xSheetConditionalEntry( mxSheetConditionalEntries->getByIndex(i), uno::UNO_QUERY_THROW );
253  if (_sStyleName == xSheetConditionalEntry->getStyleName())
254  {
255  mxSheetConditionalEntries->removeByIndex(i);
256  if (_bRemoveStyle)
257  {
258  ScVbaStyles* pStyles = static_cast< ScVbaStyles* >( mxStyles.get() );
259  if ( !pStyles )
260  DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED, {});
261  pStyles->Delete( _sStyleName );
262  }
263  return;
264  }
265  }
266  }
267  catch (const uno::Exception&)
268  {
269  DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED, {});
270  }
271 }
272 
273 OUString
275 {
276  return "ScVbaFormatConditions";
277 }
278 
279 uno::Sequence< OUString >
281 {
282  static uno::Sequence< OUString > const aServiceNames
283  {
284  "ooo.vba.excel.FormatConditions"
285  };
286  return aServiceNames;
287 }
288 
289 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static css::sheet::ConditionOperator retrieveAPIOperator(const css::uno::Any &_aOperator)
css::uno::Reference< css::beans::XPropertySet > mxParentRangePropertySet
bool hasValue()
sal_Int32 nIndex
css::uno::Reference< ov::excel::XFormatCondition > Add(::sal_Int32 Type, const css::uno::Any &Operator, const css::uno::Any &Formula1, const css::uno::Any &Formula2, const css::uno::Reference< ov::excel::XStyle > &_xCalcStyle)
virtual OUString getServiceImplName() override
#define SC_UNONAME_CONDFMT
Definition: unonames.hxx:203
Sequence< OUString > aServiceNames
void Delete(const OUString &_sStyleName)
Definition: vbastyles.cxx:170
virtual css::uno::Sequence< OUString > getServiceNames() override
::cppu::WeakImplHelper< css::container::XEnumeration > EnumerationHelper_BASE
void removeFormatCondition(const OUString &_sStyleName, bool _bRemoveStyle)
Any SAL_CALL getCaughtException()
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
int nCount
css::uno::Sequence< OUString > getStyleNames()
Definition: vbastyles.cxx:58
css::uno::Reference< ov::excel::XRange > mxRangeParent
virtual css::uno::Any createCollectionObject(const css::uno::Any &) override
int i
virtual css::uno::Type SAL_CALL getElementType() override
uno_Any a
virtual void SAL_CALL Delete() override
float u
unsigned char sal_Bool
css::uno::Type const & get()
#define ERRCODE_BASIC_BAD_PARAMETER
css::uno::Reference< ov::excel::XStyles > mxStyles
css::uno::Reference< css::sheet::XSheetConditionalEntries > mxSheetConditionalEntries
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
Reference< XPropertySet > m_xProps
#define ERRCODE_BASIC_METHOD_FAILED
static css::sheet::ConditionOperator retrieveAPIType(sal_Int32 _nVBAType, const css::uno::Reference< css::sheet::XSheetCondition > &_xSheetCondition)
Reference< XComponentContext > m_xContext
static OUString getA1Formula(const css::uno::Any &_aFormula)
static uno::Any xSheetConditionToFormatCondition(const uno::Reference< XHelperInterface > &xRangeParent, const uno::Reference< uno::XComponentContext > &xContext, const uno::Reference< excel::XStyles > &xStyles, const uno::Reference< excel::XFormatConditions > &xFormatConditions, const uno::Reference< beans::XPropertySet > &xRangeProps, const uno::Any &aObject)