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>
25#include <vector>
26#include <unonames.hxx>
29#include "vbastyles.hxx"
30
31using namespace ::ooo::vba;
32using namespace ::com::sun::star;
33
34void 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 }
50 }
51 catch (uno::Exception& )
52 {
53 DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED, {});
54 }
55}
56
57uno::Type SAL_CALL
59{
61}
62
63static 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::Any( 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::Any( xCondition );
71}
72
75{
76 return xSheetConditionToFormatCondition( uno::Reference< XHelperInterface >( mxRangeParent, uno::UNO_QUERY_THROW ), mxContext, mxStyles, this, mxParentRangePropertySet, aObject );
77}
78
79namespace {
80
81class 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;
91public:
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
130uno::Reference< excel::XFormatCondition > SAL_CALL
131ScVbaFormatConditions::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
136uno::Reference< excel::XFormatCondition >
137ScVbaFormatConditions::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::Any( getA1Formula( _aFormula1 ) ), beans::PropertyState_DIRECT_VALUE );
176 aPropertyValueVector.push_back( aProp );
177 }
178 if ( _aFormula2.hasValue() )
179 {
180 beans::PropertyValue aProp( "Formula2", 0, uno::Any( 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
205uno::Reference< container::XEnumeration > SAL_CALL
207{
208 return new EnumWrapper( m_xIndexAccess, mxRangeParent, mxContext, mxStyles, this, mxParentRangePropertySet );
209}
210
211void
213{
214 try
215 {
217 }
218 catch (uno::Exception& )
219 {
220 DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED, {});
221 }
222}
223
224OUString
225ScVbaFormatConditions::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
234OUString
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
244void
245ScVbaFormatConditions::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
273OUString
275{
276 return "ScVbaFormatConditions";
277}
278
279uno::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: */
Reference< XPropertySet > m_xProps
Reference< XComponentContext > m_xContext
css::uno::Reference< css::uno::XComponentContext > mxContext
css::uno::Reference< css::container::XIndexAccess > m_xIndexAccess
static css::sheet::ConditionOperator retrieveAPIOperator(const css::uno::Any &_aOperator)
static css::sheet::ConditionOperator retrieveAPIType(sal_Int32 _nVBAType, const css::uno::Reference< css::sheet::XSheetCondition > &_xSheetCondition)
virtual css::uno::Type SAL_CALL getElementType() override
virtual OUString getServiceImplName() override
css::uno::Reference< ov::excel::XRange > mxRangeParent
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)
static OUString getA1Formula(const css::uno::Any &_aFormula)
virtual css::uno::Sequence< OUString > getServiceNames() override
virtual css::uno::Any createCollectionObject(const css::uno::Any &) override
virtual void SAL_CALL Delete() override
css::uno::Reference< css::sheet::XSheetConditionalEntries > mxSheetConditionalEntries
css::uno::Reference< css::beans::XPropertySet > mxParentRangePropertySet
void removeFormatCondition(const OUString &_sStyleName, bool _bRemoveStyle)
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
css::uno::Reference< ov::excel::XStyles > mxStyles
css::uno::Sequence< OUString > getStyleNames()
Definition: vbastyles.cxx:58
void Delete(const OUString &_sStyleName)
Definition: vbastyles.cxx:170
css::uno::Type const & get()
int nCount
OString sFormula
float u
Sequence< OUString > aServiceNames
sal_Int32 nIndex
uno_Any a
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
Any SAL_CALL getCaughtException()
int i
#define ERRCODE_BASIC_BAD_PARAMETER
#define ERRCODE_BASIC_METHOD_FAILED
bool hasValue()
unsigned char sal_Bool
#define SC_UNONAME_CONDFMT
Definition: unonames.hxx:205
::cppu::WeakImplHelper< css::container::XEnumeration > EnumerationHelper_BASE
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)