xref: /aoo42x/main/sc/source/ui/vba/vbawsfunction.cxx (revision b3f79822)
1*b3f79822SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*b3f79822SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*b3f79822SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*b3f79822SAndrew Rist  * distributed with this work for additional information
6*b3f79822SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*b3f79822SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*b3f79822SAndrew Rist  * "License"); you may not use this file except in compliance
9*b3f79822SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*b3f79822SAndrew Rist  *
11*b3f79822SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*b3f79822SAndrew Rist  *
13*b3f79822SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*b3f79822SAndrew Rist  * software distributed under the License is distributed on an
15*b3f79822SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b3f79822SAndrew Rist  * KIND, either express or implied.  See the License for the
17*b3f79822SAndrew Rist  * specific language governing permissions and limitations
18*b3f79822SAndrew Rist  * under the License.
19*b3f79822SAndrew Rist  *
20*b3f79822SAndrew Rist  *************************************************************/
21*b3f79822SAndrew Rist 
22*b3f79822SAndrew Rist 
23cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
24cdf0e10cSrcweir #include <com/sun/star/table/XCell.hpp>
25cdf0e10cSrcweir #include <com/sun/star/table/XColumnRowRange.hpp>
26cdf0e10cSrcweir #include <com/sun/star/beans/XIntrospection.hpp>
27cdf0e10cSrcweir #include <com/sun/star/beans/XIntrospectionAccess.hpp>
28cdf0e10cSrcweir #include <com/sun/star/sheet/XFunctionAccess.hpp>
29cdf0e10cSrcweir #include <com/sun/star/sheet/XCellRangesQuery.hpp>
30cdf0e10cSrcweir #include <com/sun/star/sheet/XCellRangeAddressable.hpp>
31cdf0e10cSrcweir #include <com/sun/star/sheet/CellFlags.hpp>
32cdf0e10cSrcweir #include <com/sun/star/reflection/XIdlMethod.hpp>
33cdf0e10cSrcweir #include <com/sun/star/beans/MethodConcept.hpp>
34cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
35cdf0e10cSrcweir #include <cppuhelper/queryinterface.hxx>
36cdf0e10cSrcweir #include <comphelper/anytostring.hxx>
37cdf0e10cSrcweir 
38cdf0e10cSrcweir #include "vbawsfunction.hxx"
39cdf0e10cSrcweir #include "compiler.hxx"
40cdf0e10cSrcweir 
41cdf0e10cSrcweir using namespace com::sun::star;
42cdf0e10cSrcweir using namespace ooo::vba;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir namespace {
45cdf0e10cSrcweir 
lclConvertDoubleToBoolean(uno::Any & rAny)46cdf0e10cSrcweir void lclConvertDoubleToBoolean( uno::Any& rAny )
47cdf0e10cSrcweir {
48cdf0e10cSrcweir     if( rAny.has< double >() )
49cdf0e10cSrcweir     {
50cdf0e10cSrcweir         double fValue = rAny.get< double >();
51cdf0e10cSrcweir         if( fValue == 0.0 )
52cdf0e10cSrcweir             rAny <<= false;
53cdf0e10cSrcweir         else if( fValue == 1.0 )
54cdf0e10cSrcweir             rAny <<= true;
55cdf0e10cSrcweir         // do nothing for other values or types
56cdf0e10cSrcweir     }
57cdf0e10cSrcweir }
58cdf0e10cSrcweir 
59cdf0e10cSrcweir } // namespace
60cdf0e10cSrcweir 
ScVbaWSFunction(const uno::Reference<XHelperInterface> & xParent,const css::uno::Reference<css::uno::XComponentContext> & xContext)61cdf0e10cSrcweir ScVbaWSFunction::ScVbaWSFunction( const uno::Reference< XHelperInterface >& xParent, const css::uno::Reference< css::uno::XComponentContext >& xContext ) :
62cdf0e10cSrcweir     ScVbaWSFunction_BASE( xParent, xContext )
63cdf0e10cSrcweir {
64cdf0e10cSrcweir }
65cdf0e10cSrcweir 
66cdf0e10cSrcweir uno::Reference< beans::XIntrospectionAccess >
getIntrospection(void)67cdf0e10cSrcweir ScVbaWSFunction::getIntrospection(void)  throw(uno::RuntimeException)
68cdf0e10cSrcweir {
69cdf0e10cSrcweir 	return uno::Reference<beans::XIntrospectionAccess>();
70cdf0e10cSrcweir }
71cdf0e10cSrcweir 
72cdf0e10cSrcweir uno::Any SAL_CALL
invoke(const rtl::OUString & FunctionName,const uno::Sequence<uno::Any> & Params,uno::Sequence<sal_Int16> &,uno::Sequence<uno::Any> &)73cdf0e10cSrcweir ScVbaWSFunction::invoke(const rtl::OUString& FunctionName, const uno::Sequence< uno::Any >& Params, uno::Sequence< sal_Int16 >& /*OutParamIndex*/, uno::Sequence< uno::Any >& /*OutParam*/) throw(lang::IllegalArgumentException, script::CannotConvertException, reflection::InvocationTargetException, uno::RuntimeException)
74cdf0e10cSrcweir {
75cdf0e10cSrcweir     // create copy of parameters, replace Excel range objects with UNO range objects
76cdf0e10cSrcweir     uno::Sequence< uno::Any > aParamTemp( Params );
77cdf0e10cSrcweir     if( aParamTemp.hasElements() )
78cdf0e10cSrcweir     {
79cdf0e10cSrcweir         uno::Any* pArray = aParamTemp.getArray();
80cdf0e10cSrcweir         uno::Any* pArrayEnd = pArray + aParamTemp.getLength();
81cdf0e10cSrcweir         for( ; pArray < pArrayEnd; ++pArray )
82cdf0e10cSrcweir         {
83cdf0e10cSrcweir             uno::Reference< excel::XRange > myRange( *pArray, uno::UNO_QUERY );
84cdf0e10cSrcweir             if( myRange.is() )
85cdf0e10cSrcweir                 *pArray = myRange->getCellRange();
86cdf0e10cSrcweir             OSL_TRACE("Param[%d] is %s", (int)(pArray - aParamTemp.getConstArray()), rtl::OUStringToOString( comphelper::anyToString( *pArray ), RTL_TEXTENCODING_UTF8 ).getStr() );
87cdf0e10cSrcweir         }
88cdf0e10cSrcweir     }
89cdf0e10cSrcweir 
90cdf0e10cSrcweir     uno::Any aRet;
91cdf0e10cSrcweir     bool bAsArray = true;
92cdf0e10cSrcweir 
93cdf0e10cSrcweir     // special handing for some functions that don't work correctly in FunctionAccess
94cdf0e10cSrcweir 	ScCompiler aCompiler( 0, ScAddress() );
95cdf0e10cSrcweir 	OpCode eOpCode = aCompiler.GetEnglishOpCode( FunctionName.toAsciiUpperCase() );
96cdf0e10cSrcweir 	switch( eOpCode )
97cdf0e10cSrcweir 	{
98cdf0e10cSrcweir         // ISLOGICAL does not work in array formula mode
99cdf0e10cSrcweir         case ocIsLogical:
100cdf0e10cSrcweir         {
101cdf0e10cSrcweir             if( aParamTemp.getLength() != 1 )
102cdf0e10cSrcweir                 throw lang::IllegalArgumentException();
103cdf0e10cSrcweir             const uno::Any& rParam = aParamTemp[ 0 ];
104cdf0e10cSrcweir             if( rParam.has< bool >() )
105cdf0e10cSrcweir             {
106cdf0e10cSrcweir                 aRet <<= true;
107cdf0e10cSrcweir             }
108cdf0e10cSrcweir             else if( rParam.has< uno::Reference< table::XCellRange > >() ) try
109cdf0e10cSrcweir             {
110cdf0e10cSrcweir                 uno::Reference< sheet::XCellRangeAddressable > xRangeAddr( rParam, uno::UNO_QUERY_THROW );
111cdf0e10cSrcweir                 table::CellRangeAddress aRangeAddr = xRangeAddr->getRangeAddress();
112cdf0e10cSrcweir                 bAsArray = (aRangeAddr.StartColumn != aRangeAddr.EndColumn) || (aRangeAddr.StartRow != aRangeAddr.EndRow);
113cdf0e10cSrcweir             }
114cdf0e10cSrcweir             catch( uno::Exception& )
115cdf0e10cSrcweir             {
116cdf0e10cSrcweir             }
117cdf0e10cSrcweir         }
118cdf0e10cSrcweir         break;
119cdf0e10cSrcweir         default:;
120cdf0e10cSrcweir     }
121cdf0e10cSrcweir 
122cdf0e10cSrcweir     if( !aRet.hasValue() )
123cdf0e10cSrcweir     {
124cdf0e10cSrcweir     	uno::Reference< lang::XMultiComponentFactory > xSMgr( mxContext->getServiceManager(), uno::UNO_QUERY_THROW );
125cdf0e10cSrcweir     	uno::Reference< sheet::XFunctionAccess > xFunctionAccess( xSMgr->createInstanceWithContext(
126cdf0e10cSrcweir             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.sheet.FunctionAccess" ) ), mxContext ),
127cdf0e10cSrcweir             uno::UNO_QUERY_THROW );
128cdf0e10cSrcweir     	uno::Reference< beans::XPropertySet > xPropSet( xFunctionAccess, uno::UNO_QUERY_THROW );
129cdf0e10cSrcweir     	xPropSet->setPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "IsArrayFunction" ) ), uno::Any( bAsArray ) );
130cdf0e10cSrcweir         aRet = xFunctionAccess->callFunction( FunctionName, aParamTemp );
131cdf0e10cSrcweir     }
132cdf0e10cSrcweir 
133cdf0e10cSrcweir     /*  Convert return value from double to to Boolean for some functions that
134cdf0e10cSrcweir         return Booleans. */
135cdf0e10cSrcweir     typedef uno::Sequence< uno::Sequence< uno::Any > > AnySeqSeq;
136cdf0e10cSrcweir     if( (eOpCode == ocIsEmpty) || (eOpCode == ocIsString) || (eOpCode == ocIsNonString) || (eOpCode == ocIsLogical) ||
137cdf0e10cSrcweir         (eOpCode == ocIsRef) || (eOpCode == ocIsValue) || (eOpCode == ocIsFormula) || (eOpCode == ocIsNA) ||
138cdf0e10cSrcweir         (eOpCode == ocIsErr) || (eOpCode == ocIsError) || (eOpCode == ocIsEven) || (eOpCode == ocIsOdd) ||
139cdf0e10cSrcweir         (eOpCode == ocAnd) || (eOpCode == ocOr) || (eOpCode == ocNot) || (eOpCode == ocTrue) || (eOpCode == ocFalse) )
140cdf0e10cSrcweir     {
141cdf0e10cSrcweir         if( aRet.has< AnySeqSeq >() )
142cdf0e10cSrcweir         {
143cdf0e10cSrcweir             AnySeqSeq aAnySeqSeq = aRet.get< AnySeqSeq >();
144cdf0e10cSrcweir             for( sal_Int32 nRow = 0; nRow < aAnySeqSeq.getLength(); ++nRow )
145cdf0e10cSrcweir                 for( sal_Int32 nCol = 0; nCol < aAnySeqSeq[ nRow ].getLength(); ++nCol )
146cdf0e10cSrcweir                     lclConvertDoubleToBoolean( aAnySeqSeq[ nRow ][ nCol ] );
147cdf0e10cSrcweir             aRet <<= aAnySeqSeq;
148cdf0e10cSrcweir         }
149cdf0e10cSrcweir         else
150cdf0e10cSrcweir         {
151cdf0e10cSrcweir             lclConvertDoubleToBoolean( aRet );
152cdf0e10cSrcweir         }
153cdf0e10cSrcweir     }
154cdf0e10cSrcweir 
155cdf0e10cSrcweir     /*  Hack/workaround (?): shorten single-row matrix to simple array, shorten
156cdf0e10cSrcweir         1x1 matrix to single value. */
157cdf0e10cSrcweir     if( aRet.has< AnySeqSeq >() )
158cdf0e10cSrcweir     {
159cdf0e10cSrcweir         AnySeqSeq aAnySeqSeq = aRet.get< AnySeqSeq >();
160cdf0e10cSrcweir         if( aAnySeqSeq.getLength() == 1 )
161cdf0e10cSrcweir         {
162cdf0e10cSrcweir             if( aAnySeqSeq[ 0 ].getLength() == 1 )
163cdf0e10cSrcweir                 aRet = aAnySeqSeq[ 0 ][ 0 ];
164cdf0e10cSrcweir             else
165cdf0e10cSrcweir                 aRet <<= aAnySeqSeq[ 0 ];
166cdf0e10cSrcweir         }
167cdf0e10cSrcweir     }
168cdf0e10cSrcweir 
169cdf0e10cSrcweir #if 0
170cdf0e10cSrcweir 	// MATCH function should alwayse return a double value, but currently if the first argument is XCellRange, MATCH function returns an array instead of a double value. Don't know why?
171cdf0e10cSrcweir 	// To fix this issue in safe, current solution is to convert this array to a double value just for MATCH function.
172cdf0e10cSrcweir 	String aUpper( FunctionName );
173cdf0e10cSrcweir 	ScCompiler aCompiler( NULL, ScAddress() );
174cdf0e10cSrcweir 	OpCode eOp = aCompiler.GetEnglishOpCode( aUpper.ToUpperAscii() );
175cdf0e10cSrcweir 	if( eOp == ocMatch )
176cdf0e10cSrcweir 	{
177cdf0e10cSrcweir 		double fVal = 0.0;
178cdf0e10cSrcweir 		if( aRet >>= fVal )
179cdf0e10cSrcweir 			return aRet;
180cdf0e10cSrcweir 		uno::Sequence< uno::Sequence< uno::Any > > aSequence;
181cdf0e10cSrcweir 		if( !( ( aRet >>= aSequence ) && ( aSequence.getLength() > 0 ) &&
182cdf0e10cSrcweir 			( aSequence[0].getLength() > 0 ) && ( aSequence[0][0] >>= fVal ) ) )
183cdf0e10cSrcweir 				throw uno::RuntimeException();
184cdf0e10cSrcweir 		aRet <<= fVal;
185cdf0e10cSrcweir 	}
186cdf0e10cSrcweir #endif
187cdf0e10cSrcweir 
188cdf0e10cSrcweir 	return aRet;
189cdf0e10cSrcweir }
190cdf0e10cSrcweir 
191cdf0e10cSrcweir void SAL_CALL
setValue(const rtl::OUString &,const uno::Any &)192cdf0e10cSrcweir ScVbaWSFunction::setValue(const rtl::OUString& /*PropertyName*/, const uno::Any& /*Value*/) throw(beans::UnknownPropertyException, script::CannotConvertException, reflection::InvocationTargetException, uno::RuntimeException)
193cdf0e10cSrcweir {
194cdf0e10cSrcweir 	throw beans::UnknownPropertyException();
195cdf0e10cSrcweir }
196cdf0e10cSrcweir 
197cdf0e10cSrcweir uno::Any SAL_CALL
getValue(const rtl::OUString &)198cdf0e10cSrcweir ScVbaWSFunction::getValue(const rtl::OUString& /*PropertyName*/) throw(beans::UnknownPropertyException, uno::RuntimeException)
199cdf0e10cSrcweir {
200cdf0e10cSrcweir 	throw beans::UnknownPropertyException();
201cdf0e10cSrcweir }
202cdf0e10cSrcweir 
203cdf0e10cSrcweir sal_Bool SAL_CALL
hasMethod(const rtl::OUString & Name)204cdf0e10cSrcweir ScVbaWSFunction::hasMethod(const rtl::OUString& Name)  throw(uno::RuntimeException)
205cdf0e10cSrcweir {
206cdf0e10cSrcweir 	sal_Bool bIsFound = sal_False;
207cdf0e10cSrcweir 	try
208cdf0e10cSrcweir 	{
209cdf0e10cSrcweir 	// the function name contained in the com.sun.star.sheet.FunctionDescription service is alwayse localized.
210cdf0e10cSrcweir 		// but the function name used in WorksheetFunction is a programmatic name (seems English).
211cdf0e10cSrcweir 		// So m_xNameAccess->hasByName( Name ) may fail to find name when a function name has a localized name.
212cdf0e10cSrcweir 		ScCompiler aCompiler( NULL, ScAddress() );
213cdf0e10cSrcweir 		if( aCompiler.IsEnglishSymbol( Name ) )
214cdf0e10cSrcweir 			bIsFound = sal_True;
215cdf0e10cSrcweir 	}
216cdf0e10cSrcweir 	catch( uno::Exception& /*e*/ )
217cdf0e10cSrcweir 	{
218cdf0e10cSrcweir 		// failed to find name
219cdf0e10cSrcweir 	}
220cdf0e10cSrcweir 	return bIsFound;
221cdf0e10cSrcweir }
222cdf0e10cSrcweir 
223cdf0e10cSrcweir sal_Bool SAL_CALL
hasProperty(const rtl::OUString &)224cdf0e10cSrcweir ScVbaWSFunction::hasProperty(const rtl::OUString& /*Name*/)  throw(uno::RuntimeException)
225cdf0e10cSrcweir {
226cdf0e10cSrcweir 	 return sal_False;
227cdf0e10cSrcweir }
228cdf0e10cSrcweir 
229cdf0e10cSrcweir ::rtl::OUString SAL_CALL
getExactName(const::rtl::OUString & aApproximateName)230cdf0e10cSrcweir ScVbaWSFunction::getExactName( const ::rtl::OUString& aApproximateName ) throw (css::uno::RuntimeException)
231cdf0e10cSrcweir {
232cdf0e10cSrcweir 	rtl::OUString sName = aApproximateName.toAsciiUpperCase();
233cdf0e10cSrcweir 	if ( !hasMethod( sName ) )
234cdf0e10cSrcweir 		return rtl::OUString();
235cdf0e10cSrcweir 	return sName;
236cdf0e10cSrcweir }
237cdf0e10cSrcweir 
238cdf0e10cSrcweir rtl::OUString&
getServiceImplName()239cdf0e10cSrcweir ScVbaWSFunction::getServiceImplName()
240cdf0e10cSrcweir {
241cdf0e10cSrcweir 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaWSFunction") );
242cdf0e10cSrcweir 	return sImplName;
243cdf0e10cSrcweir }
244cdf0e10cSrcweir 
245cdf0e10cSrcweir uno::Sequence< rtl::OUString >
getServiceNames()246cdf0e10cSrcweir ScVbaWSFunction::getServiceNames()
247cdf0e10cSrcweir {
248cdf0e10cSrcweir 	static uno::Sequence< rtl::OUString > aServiceNames;
249cdf0e10cSrcweir 	if ( aServiceNames.getLength() == 0 )
250cdf0e10cSrcweir 	{
251cdf0e10cSrcweir 		aServiceNames.realloc( 1 );
252cdf0e10cSrcweir 		aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.WorksheetFunction" ) );
253cdf0e10cSrcweir 	}
254cdf0e10cSrcweir 	return aServiceNames;
255cdf0e10cSrcweir }
256