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