LCOV - code coverage report
Current view: top level - sc/source/ui/vba - vbanames.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 58 122 47.5 %
Date: 2015-06-13 12:38:46 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          22 : 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          22 :             mxNames( xNames )
      65             : {
      66          22 :     m_xNameAccess.set( xNames, uno::UNO_QUERY_THROW );
      67          22 : }
      68             : 
      69          44 : ScVbaNames::~ScVbaNames()
      70             : {
      71          44 : }
      72             : 
      73             : ScDocument *
      74           5 : ScVbaNames::getScDocument()
      75             : {
      76           5 :     uno::Reference< frame::XModel > xModel( getModel() , uno::UNO_QUERY_THROW );
      77           5 :     ScTabViewShell * pTabViewShell = excel::getBestViewShell( xModel );
      78           5 :     if ( !pTabViewShell )
      79           0 :         throw uno::RuntimeException( "No ViewShell available" );
      80           5 :     ScViewData& rViewData = pTabViewShell->GetViewData();
      81           5 :     return rViewData.GetDocument();
      82             : }
      83             : 
      84             : css::uno::Any
      85           5 : 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           5 :     OUString sName;
      99          10 :     uno::Reference< excel::XRange > xRange;
     100           5 :     if ( Name.hasValue() )
     101           5 :         Name >>= sName;
     102           0 :     else if ( NameLocal.hasValue() )
     103           0 :         NameLocal >>= sName;
     104           5 :     if ( !sName.isEmpty() )
     105             :     {
     106           5 :         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          10 :     uno::Reference< table::XCellRange > xUnoRange;
     122           5 :     if ( RefersTo.hasValue() || RefersToR1C1.hasValue() || RefersToR1C1Local.hasValue() )
     123             :     {
     124           5 :         OUString sFormula;
     125             : 
     126           5 :         formula::FormulaGrammar::Grammar eGram = formula::FormulaGrammar::GRAM_NATIVE_XL_A1;
     127           5 :         if ( RefersTo.hasValue() )
     128             :         {
     129           5 :             if ( RefersTo.getValueTypeClass() == uno::TypeClass_STRING )
     130           0 :                 RefersTo >>= sFormula;
     131             :             else
     132           5 :                 RefersTo >>= xRange;
     133             :         }
     134           5 :         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           5 :         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           5 :         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           5 :         }
     190             :     }
     191             : 
     192           5 :     if ( xRange.is() || xUnoRange.is() )
     193             :     {
     194           5 :         if ( !xRange.is() )
     195           0 :             xRange = new ScVbaRange( mxParent, mxContext, xUnoRange );
     196             : 
     197           5 :         uno::Reference< excel::XRange > xArea( xRange->Areas( uno::makeAny( sal_Int32(1) ) ), uno::UNO_QUERY );
     198             : 
     199           5 :         uno::Any xAny = xArea->getCellRange() ;
     200             : 
     201           5 :         uno::Reference< sheet::XCellRangeAddressable > thisRangeAdd( xAny, ::uno::UNO_QUERY_THROW);
     202             : 
     203           5 :         table::CellRangeAddress aAddr = thisRangeAdd->getRangeAddress();
     204           5 :         ScAddress aPos( static_cast< SCCOL >( aAddr.StartColumn ) , static_cast< SCROW >( aAddr.StartRow ) , static_cast< SCTAB >(aAddr.Sheet ) );
     205           5 :         uno::Any xAny2 ;
     206           5 :         if ( mxNames.is() )
     207             :         {
     208           5 :             RangeType nType = RT_NAME;
     209           5 :             table::CellAddress aCellAddr( aAddr.Sheet , aAddr.StartColumn , aAddr.StartRow );
     210           5 :             if ( mxNames->hasByName( sName ) )
     211           0 :                 mxNames->removeByName(sName);
     212           5 :             OUString sTmp = "$";
     213          10 :             uno::Reference< ov::XCollection > xCol( xRange->Areas( uno::Any() ), uno::UNO_QUERY );
     214          11 :             for ( sal_Int32 nArea = 1; nArea <= xCol->getCount(); ++nArea )
     215             :             {
     216           6 :                 xArea.set( xRange->Areas( uno::makeAny( nArea ) ), uno::UNO_QUERY_THROW );
     217             : 
     218           6 :                 OUString sRangeAdd = xArea->Address( xAny2, xAny2 , xAny2 , xAny2, xAny2 );
     219           6 :                 if ( nArea > 1 )
     220           1 :                     sTmp += ",";
     221           6 :                 sTmp = sTmp + "'" + xRange->getWorksheet()->getName() + "'." + sRangeAdd;
     222           6 :             }
     223           5 :             mxNames->addNewByName( sName , sTmp , aCellAddr , (sal_Int32)nType);
     224          10 :             return Item( uno::makeAny( sName ), uno::Any() );
     225           0 :         }
     226             :     }
     227           5 :     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          12 : ScVbaNames::createCollectionObject( const uno::Any& aSource )
     246             : {
     247          12 :     uno::Reference< sheet::XNamedRange > xName( aSource, uno::UNO_QUERY );
     248          12 :     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           9 : }
     268             : 
     269             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11