1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #include <vbahelper/helperdecl.hxx>
21 #include <com/sun/star/table/XCellRange.hpp>
22 #include <com/sun/star/sheet/XCellRangeAddressable.hpp>
24 #include "vbanames.hxx"
25 #include "vbaname.hxx"
26 #include "vbarange.hxx"
27 #include "vbaglobals.hxx"
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"
37 #include <boost/scoped_ptr.hpp>
39 using namespace ::ooo::vba
;
40 using namespace ::com::sun::star
;
42 class NamesEnumeration
: public EnumerationHelperImpl
44 uno::Reference
< frame::XModel
> m_xModel
;
45 uno::WeakReference
< XHelperInterface
> m_xParent
;
46 uno::Reference
< sheet::XNamedRanges
> m_xNames
;
48 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
) {}
50 virtual uno::Any SAL_CALL
nextElement( ) throw (container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
, std::exception
) SAL_OVERRIDE
52 uno::Reference
< sheet::XNamedRange
> xNamed( m_xEnumeration
->nextElement(), uno::UNO_QUERY_THROW
);
53 return uno::makeAny( uno::Reference
< excel::XName
> ( new ScVbaName( m_xParent
, m_xContext
, xNamed
,m_xNames
, m_xModel
) ) );
58 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
) ),
66 m_xNameAccess
.set( xNames
, uno::UNO_QUERY_THROW
);
69 ScVbaNames::~ScVbaNames()
74 ScVbaNames::getScDocument()
76 uno::Reference
< frame::XModel
> xModel( getModel() , uno::UNO_QUERY_THROW
);
77 ScTabViewShell
* pTabViewShell
= excel::getBestViewShell( xModel
);
79 throw uno::RuntimeException( "No ViewShell available" );
80 ScViewData
& rViewData
= pTabViewShell
->GetViewData();
81 return rViewData
.GetDocument();
85 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
)
99 uno::Reference
< excel::XRange
> xRange
;
100 if ( Name
.hasValue() )
102 else if ( NameLocal
.hasValue() )
104 if ( !sName
.isEmpty() )
106 if ( !ScRangeData::IsNameValid( sName
, getScDocument() ) )
109 sal_Int32 nToken
= 0;
110 sal_Int32 nIndex
= 0;
111 sResult
= sName
.getToken( nToken
, '!' , nIndex
);
115 sResult
= sName
.copy( nIndex
);
117 if ( !ScRangeData::IsNameValid( sName
, getScDocument() ) )
118 throw uno::RuntimeException( "This Name is not valid ." );
121 uno::Reference
< table::XCellRange
> xUnoRange
;
122 if ( RefersTo
.hasValue() || RefersToR1C1
.hasValue() || RefersToR1C1Local
.hasValue() )
126 formula::FormulaGrammar::Grammar eGram
= formula::FormulaGrammar::GRAM_NATIVE_XL_A1
;
127 if ( RefersTo
.hasValue() )
129 if ( RefersTo
.getValueTypeClass() == uno::TypeClass_STRING
)
130 RefersTo
>>= sFormula
;
134 if ( RefersToR1C1
.hasValue() )
136 if ( RefersToR1C1
.getValueTypeClass() == uno::TypeClass_STRING
)
138 RefersToR1C1
>>= sFormula
;
139 eGram
= formula::FormulaGrammar::GRAM_NATIVE_XL_R1C1
;
142 RefersToR1C1
>>= xRange
;
144 if ( RefersToR1C1Local
.hasValue() )
146 if ( RefersToR1C1Local
.getValueTypeClass() == uno::TypeClass_STRING
)
148 RefersToR1C1Local
>>= sFormula
;
149 eGram
= formula::FormulaGrammar::GRAM_NATIVE_XL_R1C1
;
152 RefersToR1C1Local
>>= xRange
;
154 if ( !xRange
.is() && !sFormula
.isEmpty() )
157 ScCompiler
aComp( getScDocument(), aBlank
);
158 aComp
.SetGrammar( eGram
);
159 boost::scoped_ptr
<ScTokenArray
> pTokens(aComp
.CompileString(sFormula
));
163 ScDocShell
* pDocSh
= excel::getDocShell(getModel());
164 if (pTokens
->IsValidReference(aRange
, aBlank
))
165 xUnoRange
= new ScCellRangeObj( pDocSh
, aRange
);
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 if ( sFormula
.startsWith("=") )
171 sFormula
= sFormula
.copy(1);
172 ScRangeList aCellRanges
;
173 sal_uInt16 nFlags
= 0;
175 formula::FormulaGrammar::AddressConvention eConv
= ( eGram
== formula::FormulaGrammar::GRAM_NATIVE_XL_A1
) ? formula::FormulaGrammar::CONV_XL_A1
: formula::FormulaGrammar::CONV_XL_R1C1
;
176 if ( ScVbaRange::getCellRangesForAddress( nFlags
, sFormula
, pDocSh
, aCellRanges
, eConv
, ',' ) )
178 if ( aCellRanges
.size() == 1 )
179 xUnoRange
= new ScCellRangeObj( pDocSh
, *aCellRanges
.front() );
182 uno::Reference
< sheet::XSheetCellRangeContainer
> xRanges( new ScCellRangesObj( pDocSh
, aCellRanges
) );
183 xRange
= new ScVbaRange( mxParent
, mxContext
, xRanges
);
192 if ( xRange
.is() || xUnoRange
.is() )
195 xRange
= new ScVbaRange( mxParent
, mxContext
, xUnoRange
);
197 uno::Reference
< excel::XRange
> xArea( xRange
->Areas( uno::makeAny( sal_Int32(1) ) ), uno::UNO_QUERY
);
199 uno::Any xAny
= xArea
->getCellRange() ;
201 uno::Reference
< sheet::XCellRangeAddressable
> thisRangeAdd( xAny
, ::uno::UNO_QUERY_THROW
);
203 table::CellRangeAddress aAddr
= thisRangeAdd
->getRangeAddress();
204 ScAddress
aPos( static_cast< SCCOL
>( aAddr
.StartColumn
) , static_cast< SCROW
>( aAddr
.StartRow
) , static_cast< SCTAB
>(aAddr
.Sheet
) );
208 RangeType nType
= RT_NAME
;
209 table::CellAddress
aCellAddr( aAddr
.Sheet
, aAddr
.StartColumn
, aAddr
.StartRow
);
210 if ( mxNames
->hasByName( sName
) )
211 mxNames
->removeByName(sName
);
213 uno::Reference
< ov::XCollection
> xCol( xRange
->Areas( uno::Any() ), uno::UNO_QUERY
);
214 for ( sal_Int32 nArea
= 1; nArea
<= xCol
->getCount(); ++nArea
)
216 xArea
.set( xRange
->Areas( uno::makeAny( nArea
) ), uno::UNO_QUERY_THROW
);
218 OUString sRangeAdd
= xArea
->Address( xAny2
, xAny2
, xAny2
, xAny2
, xAny2
);
221 sTmp
= sTmp
+ "'" + xRange
->getWorksheet()->getName() + "'." + sRangeAdd
;
223 mxNames
->addNewByName( sName
, sTmp
, aCellAddr
, (sal_Int32
)nType
);
224 return Item( uno::makeAny( sName
), uno::Any() );
227 return css::uno::Any();
230 // XEnumerationAccess
232 ScVbaNames::getElementType() throw( css::uno::RuntimeException
)
234 return cppu::UnoType
<ov::excel::XName
>::get();
237 uno::Reference
< container::XEnumeration
>
238 ScVbaNames::createEnumeration() throw (uno::RuntimeException
)
240 uno::Reference
< container::XEnumerationAccess
> xEnumAccess( mxNames
, uno::UNO_QUERY_THROW
);
241 return new NamesEnumeration( getParent(), mxContext
, xEnumAccess
->createEnumeration(), mxModel
, mxNames
);
245 ScVbaNames::createCollectionObject( const uno::Any
& aSource
)
247 uno::Reference
< sheet::XNamedRange
> xName( aSource
, uno::UNO_QUERY
);
248 return uno::makeAny( uno::Reference
< excel::XName
> ( new ScVbaName( getParent(), mxContext
, xName
, mxNames
, mxModel
) ) );
252 ScVbaNames::getServiceImplName()
254 return OUString( "ScVbaNames" );
257 css::uno::Sequence
<OUString
>
258 ScVbaNames::getServiceNames()
260 static uno::Sequence
< OUString
> aServiceNames
;
261 if ( aServiceNames
.getLength() == 0 )
263 aServiceNames
.realloc( 1 );
264 aServiceNames
[ 0 ] = "ooo.vba.excel.NamedRanges";
266 return aServiceNames
;
269 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */