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

Generated by: LCOV version 1.10