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