LCOV - code coverage report
Current view: top level - sc/source/ui/vba - vbanames.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 58 122 47.5 %
Date: 2014-11-03 Functions: 8 16 50.0 %
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 <vbahelper/helperdecl.hxx>
      20             : 
      21             : #include <com/sun/star/table/XCellRange.hpp>
      22             : #include <com/sun/star/sheet/XCellRangeAddressable.hpp>
      23             : 
      24             : #include "vbanames.hxx"
      25             : #include "vbaname.hxx"
      26             : #include "vbarange.hxx"
      27             : #include "vbaglobals.hxx"
      28             : #include <vector>
      29             : #include <rangenam.hxx>
      30             : #include <vcl/msgbox.hxx>
      31             : #include "tabvwsh.hxx"
      32             : #include "viewdata.hxx"
      33             : #include "compiler.hxx"
      34             : #include "tokenarray.hxx"
      35             : #include "cellsuno.hxx"
      36             : 
      37             : #include <boost/scoped_ptr.hpp>
      38             : 
      39             : using namespace ::ooo::vba;
      40             : using namespace ::com::sun::star;
      41             : 
      42           0 : class NamesEnumeration : public EnumerationHelperImpl
      43             : {
      44             :     uno::Reference< frame::XModel > m_xModel;
      45             :     uno::WeakReference< XHelperInterface > m_xParent;
      46             :     uno::Reference< sheet::XNamedRanges > m_xNames;
      47             : public:
      48           0 :     NamesEnumeration( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< container::XEnumeration >& xEnumeration,  const uno::Reference< frame::XModel >& xModel , const uno::Reference< sheet::XNamedRanges >& xNames ) throw ( uno::RuntimeException ) : EnumerationHelperImpl( xParent, xContext, xEnumeration ), m_xModel( xModel ), m_xParent( xParent ), m_xNames( xNames ) {}
      49             : 
      50           0 :     virtual uno::Any SAL_CALL nextElement(  ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception) SAL_OVERRIDE
      51             :     {
      52           0 :         uno::Reference< sheet::XNamedRange > xNamed( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
      53           0 :         return uno::makeAny( uno::Reference< excel::XName > ( new ScVbaName( m_xParent, m_xContext, xNamed ,m_xNames , m_xModel ) ) );
      54             :     }
      55             : 
      56             : };
      57             : 
      58          44 : ScVbaNames::ScVbaNames(const css::uno::Reference< ov::XHelperInterface >& xParent,
      59             :             const css::uno::Reference< css::uno::XComponentContext >& xContext,
      60             :             const css::uno::Reference< css::sheet::XNamedRanges >& xNames,
      61             :             const css::uno::Reference< css::frame::XModel >& xModel ):
      62             :             ScVbaNames_BASE(  xParent , xContext , uno::Reference< container::XIndexAccess >( xNames, uno::UNO_QUERY ) ),
      63             :             mxModel( xModel ),
      64          44 :             mxNames( xNames )
      65             : {
      66          44 :     m_xNameAccess.set( xNames, uno::UNO_QUERY_THROW );
      67          44 : }
      68             : 
      69          88 : ScVbaNames::~ScVbaNames()
      70             : {
      71          88 : }
      72             : 
      73             : ScDocument *
      74          10 : ScVbaNames::getScDocument()
      75             : {
      76          10 :     uno::Reference< frame::XModel > xModel( getModel() , uno::UNO_QUERY_THROW );
      77          10 :     ScTabViewShell * pTabViewShell = excel::getBestViewShell( xModel );
      78          10 :     if ( !pTabViewShell )
      79           0 :         throw uno::RuntimeException( "No ViewShell available" );
      80          10 :     ScViewData& rViewData = pTabViewShell->GetViewData();
      81          10 :     return rViewData.GetDocument();
      82             : }
      83             : 
      84             : css::uno::Any
      85          10 : ScVbaNames::Add( const css::uno::Any& Name ,
      86             :                                         const css::uno::Any& RefersTo,
      87             :                                         const css::uno::Any& /*Visible*/,
      88             :                                         const css::uno::Any& /*MacroType*/,
      89             :                                         const css::uno::Any& /*ShoutcutKey*/,
      90             :                                         const css::uno::Any& /*Category*/,
      91             :                                         const css::uno::Any& NameLocal,
      92             :                                         const css::uno::Any& /*RefersToLocal*/,
      93             :                                         const css::uno::Any& /*CategoryLocal*/,
      94             :                                         const css::uno::Any& RefersToR1C1,
      95             :                                         const css::uno::Any& RefersToR1C1Local )
      96             :     throw (css::uno::RuntimeException, std::exception)
      97             : {
      98          10 :     OUString sName;
      99          20 :     uno::Reference< excel::XRange > xRange;
     100          10 :     if ( Name.hasValue() )
     101          10 :         Name >>= sName;
     102           0 :     else if ( NameLocal.hasValue() )
     103           0 :         NameLocal >>= sName;
     104          10 :     if ( !sName.isEmpty() )
     105             :     {
     106          10 :         if ( !ScRangeData::IsNameValid( sName , getScDocument() ) )
     107             :         {
     108           0 :             OUString sResult ;
     109           0 :             sal_Int32 nToken = 0;
     110           0 :             sal_Int32 nIndex = 0;
     111           0 :             sResult = sName.getToken( nToken , '!' , nIndex );
     112           0 :             if ( -1 == nIndex )
     113           0 :                 sResult = sName;
     114             :             else
     115           0 :                 sResult = sName.copy( nIndex );
     116           0 :             sName = sResult ;
     117           0 :             if ( !ScRangeData::IsNameValid( sName , getScDocument() ) )
     118           0 :                 throw uno::RuntimeException( "This Name is not valid ." );
     119             :         }
     120             :     }
     121          20 :     uno::Reference< table::XCellRange > xUnoRange;
     122          10 :     if ( RefersTo.hasValue() || RefersToR1C1.hasValue() || RefersToR1C1Local.hasValue() )
     123             :     {
     124          10 :         OUString sFormula;
     125             : 
     126          10 :         formula::FormulaGrammar::Grammar eGram = formula::FormulaGrammar::GRAM_NATIVE_XL_A1;
     127          10 :         if ( RefersTo.hasValue() )
     128             :         {
     129          10 :             if ( RefersTo.getValueTypeClass() == uno::TypeClass_STRING )
     130           0 :                 RefersTo >>= sFormula;
     131             :             else
     132          10 :                 RefersTo >>= xRange;
     133             :         }
     134          10 :         if ( RefersToR1C1.hasValue() )
     135             :         {
     136           0 :             if ( RefersToR1C1.getValueTypeClass() == uno::TypeClass_STRING )
     137             :             {
     138           0 :                 RefersToR1C1 >>= sFormula;
     139           0 :                 eGram = formula::FormulaGrammar::GRAM_NATIVE_XL_R1C1;
     140             :             }
     141             :             else
     142           0 :                 RefersToR1C1 >>= xRange;
     143             :         }
     144          10 :         if ( RefersToR1C1Local.hasValue() )
     145             :         {
     146           0 :             if ( RefersToR1C1Local.getValueTypeClass() == uno::TypeClass_STRING )
     147             :             {
     148           0 :                 RefersToR1C1Local >>= sFormula;
     149           0 :                 eGram = formula::FormulaGrammar::GRAM_NATIVE_XL_R1C1;
     150             :             }
     151             :             else
     152           0 :                 RefersToR1C1Local >>= xRange;
     153             :         }
     154          10 :         if ( !xRange.is() && !sFormula.isEmpty() )
     155             :         {
     156           0 :             ScAddress aBlank;
     157           0 :             ScCompiler aComp( getScDocument(), aBlank );
     158           0 :             aComp.SetGrammar( eGram );
     159           0 :             boost::scoped_ptr<ScTokenArray> pTokens(aComp.CompileString(sFormula));
     160           0 :             if ( pTokens )
     161             :             {
     162           0 :                 ScRange aRange;
     163           0 :                 ScDocShell* pDocSh = excel::getDocShell(getModel());
     164           0 :                 if (pTokens->IsValidReference(aRange, aBlank))
     165           0 :                     xUnoRange =  new ScCellRangeObj( pDocSh, aRange );
     166             :                 else
     167             :                 {
     168             :                     // assume it's an address try strip the '=' if it's there
     169             :                     // and try and create a range ( must be a better way )
     170           0 :                     if ( sFormula.startsWith("=") )
     171           0 :                         sFormula = sFormula.copy(1);
     172           0 :                     ScRangeList aCellRanges;
     173           0 :                     sal_uInt16 nFlags = 0;
     174             : 
     175           0 :                     formula::FormulaGrammar::AddressConvention eConv = ( eGram == formula::FormulaGrammar::GRAM_NATIVE_XL_A1 ) ? formula::FormulaGrammar::CONV_XL_A1 : formula::FormulaGrammar::CONV_XL_R1C1;
     176           0 :                     if ( ScVbaRange::getCellRangesForAddress( nFlags, sFormula, pDocSh, aCellRanges, eConv , ',' ) )
     177             :                     {
     178           0 :                         if ( aCellRanges.size() == 1 )
     179           0 :                             xUnoRange =  new ScCellRangeObj( pDocSh, *aCellRanges.front() );
     180             :                         else
     181             :                         {
     182           0 :                             uno::Reference< sheet::XSheetCellRangeContainer > xRanges( new ScCellRangesObj( pDocSh, aCellRanges ) );
     183           0 :                             xRange = new ScVbaRange( mxParent, mxContext, xRanges );
     184             :                         }
     185           0 :                     }
     186             : 
     187             :                 }
     188           0 :             }
     189          10 :         }
     190             :     }
     191             : 
     192          10 :     if ( xRange.is() || xUnoRange.is() )
     193             :     {
     194          10 :         if ( !xRange.is() )
     195           0 :             xRange = new ScVbaRange( mxParent, mxContext, xUnoRange );
     196             : 
     197          10 :         uno::Reference< excel::XRange > xArea( xRange->Areas( uno::makeAny( sal_Int32(1) ) ), uno::UNO_QUERY );
     198             : 
     199          10 :         uno::Any xAny = xArea->getCellRange() ;
     200             : 
     201          10 :         uno::Reference< sheet::XCellRangeAddressable > thisRangeAdd( xAny, ::uno::UNO_QUERY_THROW);
     202             : 
     203          10 :         table::CellRangeAddress aAddr = thisRangeAdd->getRangeAddress();
     204          10 :         ScAddress aPos( static_cast< SCCOL >( aAddr.StartColumn ) , static_cast< SCROW >( aAddr.StartRow ) , static_cast< SCTAB >(aAddr.Sheet ) );
     205          10 :         uno::Any xAny2 ;
     206          10 :         if ( mxNames.is() )
     207             :         {
     208          10 :             RangeType nType = RT_NAME;
     209          10 :             table::CellAddress aCellAddr( aAddr.Sheet , aAddr.StartColumn , aAddr.StartRow );
     210          10 :             if ( mxNames->hasByName( sName ) )
     211           0 :                 mxNames->removeByName(sName);
     212          10 :             OUString sTmp = "$";
     213          20 :             uno::Reference< ov::XCollection > xCol( xRange->Areas( uno::Any() ), uno::UNO_QUERY );
     214          22 :             for ( sal_Int32 nArea = 1; nArea <= xCol->getCount(); ++nArea )
     215             :             {
     216          12 :                 xArea.set( xRange->Areas( uno::makeAny( nArea ) ), uno::UNO_QUERY_THROW );
     217             : 
     218          12 :                 OUString sRangeAdd = xArea->Address( xAny2, xAny2 , xAny2 , xAny2, xAny2 );
     219          12 :                 if ( nArea > 1 )
     220           2 :                     sTmp += ",";
     221          12 :                 sTmp = sTmp + "'" + xRange->getWorksheet()->getName() + "'." + sRangeAdd;
     222          12 :             }
     223          10 :             mxNames->addNewByName( sName , sTmp , aCellAddr , (sal_Int32)nType);
     224          20 :             return Item( uno::makeAny( sName ), uno::Any() );
     225           0 :         }
     226             :     }
     227          10 :     return css::uno::Any();
     228             : }
     229             : 
     230             : // XEnumerationAccess
     231             : css::uno::Type
     232           0 : ScVbaNames::getElementType() throw( css::uno::RuntimeException )
     233             : {
     234           0 :     return cppu::UnoType<ov::excel::XName>::get();
     235             : }
     236             : 
     237             : uno::Reference< container::XEnumeration >
     238           0 : ScVbaNames::createEnumeration() throw (uno::RuntimeException)
     239             : {
     240           0 :     uno::Reference< container::XEnumerationAccess > xEnumAccess( mxNames, uno::UNO_QUERY_THROW );
     241           0 :     return new NamesEnumeration( getParent(), mxContext, xEnumAccess->createEnumeration(), mxModel , mxNames );
     242             : }
     243             : 
     244             : uno::Any
     245          24 : ScVbaNames::createCollectionObject( const uno::Any& aSource )
     246             : {
     247          24 :     uno::Reference< sheet::XNamedRange > xName( aSource, uno::UNO_QUERY );
     248          24 :     return uno::makeAny( uno::Reference< excel::XName > ( new ScVbaName( getParent(), mxContext, xName, mxNames , mxModel ) ) );
     249             : }
     250             : 
     251             : OUString
     252           0 : ScVbaNames::getServiceImplName()
     253             : {
     254           0 :     return OUString( "ScVbaNames" );
     255             : }
     256             : 
     257             : css::uno::Sequence<OUString>
     258           0 : ScVbaNames::getServiceNames()
     259             : {
     260           0 :     static uno::Sequence< OUString > aServiceNames;
     261           0 :     if ( aServiceNames.getLength() == 0 )
     262             :     {
     263           0 :         aServiceNames.realloc( 1 );
     264           0 :         aServiceNames[ 0 ] = "ooo.vba.excel.NamedRanges";
     265             :     }
     266           0 :     return aServiceNames;
     267          12 : }
     268             : 
     269             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10