1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
27 #include <unonames.hxx>
28 #include "vbaformatconditions.hxx"
29 #include "vbaformatcondition.hxx"
30 #include "vbastyles.hxx"
32 using namespace ::ooo::vba
;
33 using namespace ::com::sun::star
;
36 ScVbaFormatConditions::Delete( )
40 ScVbaStyles
* pStyles
= mxStyles
.get();
42 DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED
, {} );
43 sal_Int32 nCount
= mxSheetConditionalEntries
->getCount();
44 for (sal_Int32 i
= nCount
- 1; i
>= 0; i
--)
46 uno::Reference
< sheet::XSheetConditionalEntry
> xSheetConditionalEntry( mxSheetConditionalEntries
->getByIndex(i
), uno::UNO_QUERY_THROW
);
47 pStyles
->Delete(xSheetConditionalEntry
->getStyleName());
48 mxSheetConditionalEntries
->removeByIndex(i
);
52 catch (uno::Exception
& )
54 DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED
, {});
59 ScVbaFormatConditions::getElementType()
61 return cppu::UnoType
<excel::XFormatCondition
>::get();
64 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
)
66 uno::Reference
< sheet::XSheetConditionalEntry
> xSheetConditionalEntry
;
67 aObject
>>= xSheetConditionalEntry
;
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
);
75 ScVbaFormatConditions::createCollectionObject(const uno::Any
& aObject
)
77 return xSheetConditionToFormatCondition( uno::Reference
< XHelperInterface
>( mxRangeParent
, uno::UNO_QUERY_THROW
), mxContext
, mxStyles
, this, mxParentRangePropertySet
, aObject
);
82 class EnumWrapper
: public EnumerationHelper_BASE
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
;
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
96 return ( nIndex
< m_xIndexAccess
->getCount() );
99 virtual uno::Any SAL_CALL
nextElement( ) override
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
++ ) );
106 catch (const container::NoSuchElementException
&)
110 catch (const lang::WrappedTargetException
&)
114 catch (const uno::RuntimeException
&)
118 catch (const uno::Exception
& e
)
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
);
125 throw container::NoSuchElementException();
131 uno::Reference
< excel::XFormatCondition
> SAL_CALL
132 ScVbaFormatConditions::Add( ::sal_Int32 _nType
, const uno::Any
& _aOperator
, const uno::Any
& _aFormula1
, const uno::Any
& _aFormula2
)
134 return Add( _nType
, _aOperator
, _aFormula1
, _aFormula2
, uno::Reference
< excel::XStyle
>() );
137 uno::Reference
< excel::XFormatCondition
>
138 ScVbaFormatConditions::Add( ::sal_Int32 _nType
, const uno::Any
& _aOperator
, const uno::Any
& _aFormula1
, const uno::Any
& _aFormula2
, const css::uno::Reference
< excel::XStyle
>& _xStyle
)
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
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
;
154 sStyleName
= getStyleName();
155 xStyle
= mxStyles
->Add(sStyleName
, uno::Any() );
159 sStyleName
= xStyle
->getName();
162 std::vector
< beans::PropertyValue
> aPropertyValueVector
;
163 sheet::ConditionOperator aType
= ScVbaFormatCondition::retrieveAPIType(_nType
, uno::Reference
< sheet::XSheetCondition
>() );
166 if ( aType
== sheet::ConditionOperator_FORMULA
)
167 aValue
<<= sheet::ConditionOperator_FORMULA
;
169 aValue
<<= ScVbaFormatCondition::retrieveAPIOperator(_aOperator
);
171 beans::PropertyValue
aProperty( u
"Operator"_ustr
, 0, aValue
, beans::PropertyState_DIRECT_VALUE
);
172 aPropertyValueVector
.push_back( aProperty
);
174 if ( _aFormula1
.hasValue() )
176 beans::PropertyValue
aProp( u
"Formula1"_ustr
, 0, uno::Any( getA1Formula( _aFormula1
) ), beans::PropertyState_DIRECT_VALUE
);
177 aPropertyValueVector
.push_back( aProp
);
179 if ( _aFormula2
.hasValue() )
181 beans::PropertyValue
aProp( u
"Formula2"_ustr
, 0, uno::Any( getA1Formula( _aFormula2
) ), beans::PropertyState_DIRECT_VALUE
);
182 aPropertyValueVector
.push_back( aProp
);
184 aProperty
.Name
= "StyleName";
185 aProperty
.Value
<<= sStyleName
;
187 mxSheetConditionalEntries
->addNew(comphelper::containerToSequence(aPropertyValueVector
));
188 for (sal_Int32 i
= mxSheetConditionalEntries
->getCount()-1; i
>= 0; i
--)
190 uno::Reference
< sheet::XSheetConditionalEntry
> xSheetConditionalEntry( mxSheetConditionalEntries
->getByIndex(i
), uno::UNO_QUERY_THROW
);
191 if (xSheetConditionalEntry
->getStyleName() == sStyleName
)
193 xFormatCondition
= new ScVbaFormatCondition(uno::Reference
< XHelperInterface
>( mxRangeParent
, uno::UNO_QUERY_THROW
), mxContext
, xSheetConditionalEntry
, xStyle
, this, mxParentRangePropertySet
);
195 return xFormatCondition
;
199 catch (uno::Exception
& )
202 DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED
, {} );
203 return xFormatCondition
;
206 uno::Reference
< container::XEnumeration
> SAL_CALL
207 ScVbaFormatConditions::createEnumeration()
209 return new EnumWrapper( m_xIndexAccess
, mxRangeParent
, mxContext
, mxStyles
, this, mxParentRangePropertySet
);
213 ScVbaFormatConditions::notifyRange()
217 mxParentRangePropertySet
->setPropertyValue(SC_UNONAME_CONDFMT
, uno::Any( mxSheetConditionalEntries
));
219 catch (uno::Exception
& )
221 DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED
, {});
226 ScVbaFormatConditions::getA1Formula(const css::uno::Any
& _aFormula
)
228 // #TODO, #FIXME hook-in proper formula conversion detection & logic
230 if ( !( _aFormula
>>= sFormula
) )
231 DebugHelper::basicexception(ERRCODE_BASIC_BAD_PARAMETER
, {} );
236 ScVbaFormatConditions::getStyleName()
238 ScVbaStyles
* pStyles
= mxStyles
.get();
240 DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED
, {} );
241 uno::Sequence
< OUString
> sCellStyleNames
= pStyles
->getStyleNames();
242 return ContainerUtilities::getUniqueName(sCellStyleNames
, u
"Excel_CondFormat"_ustr
, u
"_");
246 ScVbaFormatConditions::removeFormatCondition( const OUString
& _sStyleName
, bool _bRemoveStyle
)
250 sal_Int32 nElems
= mxSheetConditionalEntries
->getCount();
251 for (sal_Int32 i
= 0; i
< nElems
; i
++)
253 uno::Reference
< sheet::XSheetConditionalEntry
> xSheetConditionalEntry( mxSheetConditionalEntries
->getByIndex(i
), uno::UNO_QUERY_THROW
);
254 if (_sStyleName
== xSheetConditionalEntry
->getStyleName())
256 mxSheetConditionalEntries
->removeByIndex(i
);
259 ScVbaStyles
* pStyles
= mxStyles
.get();
261 DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED
, {});
262 pStyles
->Delete( _sStyleName
);
268 catch (const uno::Exception
&)
270 DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED
, {});
275 ScVbaFormatConditions::getServiceImplName()
277 return u
"ScVbaFormatConditions"_ustr
;
280 uno::Sequence
< OUString
>
281 ScVbaFormatConditions::getServiceNames()
283 static uno::Sequence
< OUString
> const aServiceNames
285 u
"ooo.vba.excel.FormatConditions"_ustr
287 return aServiceNames
;
290 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */