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

Generated by: LCOV version 1.10