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