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