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