LCOV - code coverage report
Current view: top level - sc/source/ui/vba - vbawsfunction.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 1 127 0.8 %
Date: 2015-06-13 12:38:46 Functions: 2 14 14.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : #include <com/sun/star/beans/XPropertySet.hpp>
      20             : #include <com/sun/star/table/XCell.hpp>
      21             : #include <com/sun/star/table/XColumnRowRange.hpp>
      22             : #include <com/sun/star/beans/XIntrospection.hpp>
      23             : #include <com/sun/star/beans/XIntrospectionAccess.hpp>
      24             : #include <com/sun/star/sheet/XFunctionAccess.hpp>
      25             : #include <com/sun/star/sheet/XCellRangesQuery.hpp>
      26             : #include <com/sun/star/sheet/XCellRangeAddressable.hpp>
      27             : #include <com/sun/star/sheet/CellFlags.hpp>
      28             : #include <com/sun/star/reflection/XIdlMethod.hpp>
      29             : #include <com/sun/star/beans/MethodConcept.hpp>
      30             : #include <comphelper/processfactory.hxx>
      31             : #include <cppuhelper/queryinterface.hxx>
      32             : #include <comphelper/anytostring.hxx>
      33             : 
      34             : #include "vbawsfunction.hxx"
      35             : #include "compiler.hxx"
      36             : 
      37             : using namespace com::sun::star;
      38             : using namespace ooo::vba;
      39             : 
      40             : namespace {
      41             : 
      42           0 : void lclConvertDoubleToBoolean( uno::Any& rAny )
      43             : {
      44           0 :     if( rAny.has< double >() )
      45             :     {
      46           0 :         double fValue = rAny.get< double >();
      47           0 :         if( fValue == 0.0 )
      48           0 :             rAny <<= false;
      49           0 :         else if( fValue == 1.0 )
      50           0 :             rAny <<= true;
      51             :         // do nothing for other values or types
      52             :     }
      53           0 : }
      54             : 
      55           0 : void lclConvertBooleanToDouble( uno::Any& rAny )
      56             : {
      57           0 :     bool bValue( false );
      58           0 :     if ( rAny >>= bValue )
      59             :     {
      60           0 :         if ( bValue )
      61           0 :            rAny <<= double( 1.0 );
      62             :         else
      63           0 :            rAny <<= double( 0.0 );
      64             :     }
      65           0 : }
      66             : 
      67             : } // namespace
      68             : 
      69           0 : ScVbaWSFunction::ScVbaWSFunction( const uno::Reference< XHelperInterface >& xParent, const css::uno::Reference< css::uno::XComponentContext >& xContext ) :
      70           0 :     ScVbaWSFunction_BASE( xParent, xContext )
      71             : {
      72           0 : }
      73             : 
      74             : uno::Reference< beans::XIntrospectionAccess >
      75           0 : ScVbaWSFunction::getIntrospection()  throw(uno::RuntimeException, std::exception)
      76             : {
      77           0 :     return uno::Reference<beans::XIntrospectionAccess>();
      78             : }
      79             : 
      80             : uno::Any SAL_CALL
      81           0 : ScVbaWSFunction::invoke(const 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, std::exception)
      82             : {
      83             :     // create copy of parameters, replace Excel range objects with UNO range objects
      84           0 :     uno::Sequence< uno::Any > aParamTemp( Params );
      85           0 :     if( aParamTemp.hasElements() )
      86             :     {
      87           0 :         uno::Any* pArray = aParamTemp.getArray();
      88           0 :         uno::Any* pArrayEnd = pArray + aParamTemp.getLength();
      89           0 :         for( ; pArray < pArrayEnd; ++pArray )
      90             :         {
      91           0 :             switch( pArray->getValueType().getTypeClass()  )
      92             :             {
      93             :                 case uno::TypeClass_BOOLEAN:
      94           0 :                     lclConvertBooleanToDouble( *pArray );
      95           0 :                     break;
      96             :                 case uno::TypeClass_INTERFACE:
      97             :                 {
      98           0 :                     uno::Reference< excel::XRange > myRange( *pArray, uno::UNO_QUERY );
      99           0 :                     if( myRange.is() )
     100           0 :                         *pArray = myRange->getCellRange();
     101             :                 }
     102           0 :                     break;
     103             :                 case uno::TypeClass_SEQUENCE:
     104             :                 {
     105             :                     // the sheet.FunctionAccess service doesn't deal with Sequences, only Sequences of Sequence
     106           0 :                     uno::Type aType = pArray->getValueType();
     107           0 :                     if ( aType.equals( cppu::UnoType<uno::Sequence<sal_Int16>>::get() ) )
     108             :                     {
     109           0 :                         uno::Sequence< uno::Sequence< sal_Int16 > >  aTmp(1);
     110           0 :                         (*pArray) >>= aTmp[ 0 ];
     111           0 :                         (*pArray) <<= aTmp;
     112             :                     }
     113           0 :                     else if ( aType.equals( cppu::UnoType<uno::Sequence<sal_Int32>>::get() ) )
     114             :                     {
     115           0 :                         uno::Sequence< uno::Sequence< sal_Int32 > > aTmp(1);
     116           0 :                         (*pArray) >>= aTmp[ 0 ];
     117           0 :                         (*pArray) <<= aTmp;
     118             :                     }
     119           0 :                     else if ( aType.equals( cppu::UnoType<uno::Sequence<double>>::get() ) )
     120             :                     {
     121           0 :                         uno::Sequence< uno::Sequence< double > > aTmp(1);
     122           0 :                         (*pArray) >>= aTmp[ 0 ];
     123           0 :                         (*pArray) <<= aTmp;
     124             :                     }
     125           0 :                     else if ( aType.equals( cppu::UnoType<uno::Sequence<OUString>>::get() ) )
     126             :                     {
     127           0 :                         uno::Sequence< uno::Sequence< OUString > > aTmp(1);
     128           0 :                         (*pArray) >>= aTmp[ 0 ];
     129           0 :                         (*pArray) <<= aTmp;
     130             :                     }
     131           0 :                     else if ( aType.equals( cppu::UnoType<uno::Sequence<uno::Any>>::get() ) )
     132             :                     {
     133           0 :                         uno::Sequence< uno::Sequence<uno::Any > > aTmp(1);
     134           0 :                         (*pArray) >>= aTmp[ 0 ];
     135           0 :                         (*pArray) <<= aTmp;
     136           0 :                     }
     137             :                 }
     138           0 :                     break;
     139             :                 default:
     140           0 :                     break;
     141             :             }
     142             :             OSL_TRACE("Param[%d] is %s", (int)(pArray - aParamTemp.getConstArray()), OUStringToOString( comphelper::anyToString( *pArray ), RTL_TEXTENCODING_UTF8 ).getStr() );
     143             :         }
     144             :     }
     145             : 
     146           0 :     uno::Any aRet;
     147           0 :     bool bAsArray = true;
     148             : 
     149             :     // special handing for some functions that don't work correctly in FunctionAccess
     150           0 :     ScCompiler aCompiler( 0, ScAddress() );
     151           0 :     OpCode eOpCode = aCompiler.GetEnglishOpCode( FunctionName.toAsciiUpperCase() );
     152           0 :     switch( eOpCode )
     153             :     {
     154             :         // ISLOGICAL does not work in array formula mode
     155             :         case ocIsLogical:
     156             :         {
     157           0 :             if( aParamTemp.getLength() != 1 )
     158           0 :                 throw lang::IllegalArgumentException();
     159           0 :             const uno::Any& rParam = aParamTemp[ 0 ];
     160           0 :             if( rParam.has< bool >() )
     161             :             {
     162           0 :                 aRet <<= true;
     163             :             }
     164           0 :             else if( rParam.has< uno::Reference< table::XCellRange > >() ) try
     165             :             {
     166           0 :                 uno::Reference< sheet::XCellRangeAddressable > xRangeAddr( rParam, uno::UNO_QUERY_THROW );
     167           0 :                 table::CellRangeAddress aRangeAddr = xRangeAddr->getRangeAddress();
     168           0 :                 bAsArray = (aRangeAddr.StartColumn != aRangeAddr.EndColumn) || (aRangeAddr.StartRow != aRangeAddr.EndRow);
     169             :             }
     170           0 :             catch( uno::Exception& )
     171             :             {
     172             :             }
     173             :         }
     174           0 :         break;
     175             :         default:;
     176             :     }
     177             : 
     178           0 :     if( !aRet.hasValue() )
     179             :     {
     180           0 :         uno::Reference< lang::XMultiComponentFactory > xSMgr( mxContext->getServiceManager(), uno::UNO_QUERY_THROW );
     181           0 :         uno::Reference< sheet::XFunctionAccess > xFunctionAccess( xSMgr->createInstanceWithContext(
     182           0 :             OUString( "com.sun.star.sheet.FunctionAccess" ), mxContext ),
     183           0 :             uno::UNO_QUERY_THROW );
     184           0 :         uno::Reference< beans::XPropertySet > xPropSet( xFunctionAccess, uno::UNO_QUERY_THROW );
     185           0 :         xPropSet->setPropertyValue("IsArrayFunction", uno::Any( bAsArray ) );
     186           0 :         aRet = xFunctionAccess->callFunction( FunctionName, aParamTemp );
     187             :     }
     188             : 
     189             :     /*  Convert return value from double to Boolean for some functions that
     190             :         return Booleans. */
     191             :     typedef uno::Sequence< uno::Sequence< uno::Any > > AnySeqSeq;
     192           0 :     if( (eOpCode == ocIsEmpty) || (eOpCode == ocIsString) || (eOpCode == ocIsNonString) || (eOpCode == ocIsLogical) ||
     193           0 :         (eOpCode == ocIsRef) || (eOpCode == ocIsValue) || (eOpCode == ocIsFormula) || (eOpCode == ocIsNA) ||
     194           0 :         (eOpCode == ocIsErr) || (eOpCode == ocIsError) || (eOpCode == ocIsEven) || (eOpCode == ocIsOdd) ||
     195           0 :         (eOpCode == ocAnd) || (eOpCode == ocOr) || (eOpCode == ocXor) || (eOpCode == ocNot) || (eOpCode == ocTrue) || (eOpCode == ocFalse) )
     196             :     {
     197           0 :         if( aRet.has< AnySeqSeq >() )
     198             :         {
     199           0 :             AnySeqSeq aAnySeqSeq = aRet.get< AnySeqSeq >();
     200           0 :             for( sal_Int32 nRow = 0; nRow < aAnySeqSeq.getLength(); ++nRow )
     201           0 :                 for( sal_Int32 nCol = 0; nCol < aAnySeqSeq[ nRow ].getLength(); ++nCol )
     202           0 :                     lclConvertDoubleToBoolean( aAnySeqSeq[ nRow ][ nCol ] );
     203           0 :             aRet <<= aAnySeqSeq;
     204             :         }
     205             :         else
     206             :         {
     207           0 :             lclConvertDoubleToBoolean( aRet );
     208             :         }
     209             :     }
     210             : 
     211             :     /*  Hack/workaround (?): shorten single-row matrix to simple array, shorten
     212             :         1x1 matrix to single value. */
     213           0 :     if( aRet.has< AnySeqSeq >() )
     214             :     {
     215           0 :         AnySeqSeq aAnySeqSeq = aRet.get< AnySeqSeq >();
     216           0 :         if( aAnySeqSeq.getLength() == 1 )
     217             :         {
     218           0 :             if( aAnySeqSeq[ 0 ].getLength() == 1 )
     219           0 :                 aRet = aAnySeqSeq[ 0 ][ 0 ];
     220             :             else
     221           0 :                 aRet <<= aAnySeqSeq[ 0 ];
     222           0 :         }
     223             :     }
     224             : 
     225             : #if 0
     226             :     // 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?
     227             :     // To fix this issue in safe, current solution is to convert this array to a double value just for MATCH function.
     228             :     OUString aUpper( FunctionName.toAsciiUpperCase() );
     229             :     ScCompiler aCompiler( NULL, ScAddress() );
     230             :     OpCode eOp = aCompiler.GetEnglishOpCode( aUpper );
     231             :     if( eOp == ocMatch )
     232             :     {
     233             :         double fVal = 0.0;
     234             :         if( aRet >>= fVal )
     235             :             return aRet;
     236             :         uno::Sequence< uno::Sequence< uno::Any > > aSequence;
     237             :         if( !( ( aRet >>= aSequence ) && ( aSequence.getLength() > 0 ) &&
     238             :             ( aSequence[0].getLength() > 0 ) && ( aSequence[0][0] >>= fVal ) ) )
     239             :                 throw uno::RuntimeException();
     240             :         aRet <<= fVal;
     241             :     }
     242             : #endif
     243             : 
     244           0 :     return aRet;
     245             : }
     246             : 
     247             : void SAL_CALL
     248           0 : ScVbaWSFunction::setValue(const OUString& /*PropertyName*/, const uno::Any& /*Value*/) throw(beans::UnknownPropertyException, script::CannotConvertException, reflection::InvocationTargetException, uno::RuntimeException, std::exception)
     249             : {
     250           0 :     throw beans::UnknownPropertyException();
     251             : }
     252             : 
     253             : uno::Any SAL_CALL
     254           0 : ScVbaWSFunction::getValue(const OUString& /*PropertyName*/) throw(beans::UnknownPropertyException, uno::RuntimeException, std::exception)
     255             : {
     256           0 :     throw beans::UnknownPropertyException();
     257             : }
     258             : 
     259             : sal_Bool SAL_CALL
     260           0 : ScVbaWSFunction::hasMethod(const OUString& Name)  throw(uno::RuntimeException, std::exception)
     261             : {
     262           0 :     bool bIsFound = false;
     263             :     try
     264             :     {
     265             :     // the function name contained in the com.sun.star.sheet.FunctionDescription service is alwayse localized.
     266             :         // but the function name used in WorksheetFunction is a programmatic name (seems English).
     267             :         // So m_xNameAccess->hasByName( Name ) may fail to find name when a function name has a localized name.
     268           0 :         ScCompiler aCompiler( NULL, ScAddress() );
     269           0 :         if( aCompiler.IsEnglishSymbol( Name ) )
     270           0 :             bIsFound = true;
     271             :     }
     272           0 :     catch( uno::Exception& /*e*/ )
     273             :     {
     274             :         // failed to find name
     275             :     }
     276           0 :     return bIsFound;
     277             : }
     278             : 
     279             : sal_Bool SAL_CALL
     280           0 : ScVbaWSFunction::hasProperty(const OUString& /*Name*/)  throw(uno::RuntimeException, std::exception)
     281             : {
     282           0 :      return false;
     283             : }
     284             : 
     285             : OUString SAL_CALL
     286           0 : ScVbaWSFunction::getExactName( const OUString& aApproximateName ) throw (css::uno::RuntimeException, std::exception)
     287             : {
     288           0 :     OUString sName = aApproximateName.toAsciiUpperCase();
     289           0 :     if ( !hasMethod( sName ) )
     290           0 :         return OUString();
     291           0 :     return sName;
     292             : }
     293             : 
     294             : OUString
     295           0 : ScVbaWSFunction::getServiceImplName()
     296             : {
     297           0 :     return OUString("ScVbaWSFunction");
     298             : }
     299             : 
     300             : uno::Sequence< OUString >
     301           0 : ScVbaWSFunction::getServiceNames()
     302             : {
     303           0 :     static uno::Sequence< OUString > aServiceNames;
     304           0 :     if ( aServiceNames.getLength() == 0 )
     305             :     {
     306           0 :         aServiceNames.realloc( 1 );
     307           0 :         aServiceNames[ 0 ] = "ooo.vba.excel.WorksheetFunction";
     308             :     }
     309           0 :     return aServiceNames;
     310           9 : }
     311             : 
     312             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11