xref: /AOO41X/main/sc/source/ui/vba/vbanames.cxx (revision b3f79822e811ac3493b185030a72c3c5a51f32d8)
1*b3f79822SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*b3f79822SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*b3f79822SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*b3f79822SAndrew Rist  * distributed with this work for additional information
6*b3f79822SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*b3f79822SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*b3f79822SAndrew Rist  * "License"); you may not use this file except in compliance
9*b3f79822SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*b3f79822SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*b3f79822SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*b3f79822SAndrew Rist  * software distributed under the License is distributed on an
15*b3f79822SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b3f79822SAndrew Rist  * KIND, either express or implied.  See the License for the
17*b3f79822SAndrew Rist  * specific language governing permissions and limitations
18*b3f79822SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*b3f79822SAndrew Rist  *************************************************************/
21*b3f79822SAndrew Rist 
22*b3f79822SAndrew Rist 
23cdf0e10cSrcweir #include <vbahelper/helperdecl.hxx>
24cdf0e10cSrcweir 
25cdf0e10cSrcweir #include <com/sun/star/table/XCellRange.hpp>
26cdf0e10cSrcweir #include <com/sun/star/sheet/XCellRangeAddressable.hpp>
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include "vbanames.hxx"
29cdf0e10cSrcweir #include "vbaname.hxx"
30cdf0e10cSrcweir #include "vbarange.hxx"
31cdf0e10cSrcweir #include "vbaglobals.hxx"
32cdf0e10cSrcweir #include <vector>
33cdf0e10cSrcweir #include <rangenam.hxx>
34cdf0e10cSrcweir #include <vcl/msgbox.hxx>
35cdf0e10cSrcweir #include "tabvwsh.hxx"
36cdf0e10cSrcweir #include "viewdata.hxx"
37cdf0e10cSrcweir 
38cdf0e10cSrcweir using namespace ::ooo::vba;
39cdf0e10cSrcweir using namespace ::com::sun::star;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir class NamesEnumeration : public EnumerationHelperImpl
42cdf0e10cSrcweir {
43cdf0e10cSrcweir 	uno::Reference< frame::XModel > m_xModel;
44cdf0e10cSrcweir 	uno::WeakReference< XHelperInterface > m_xParent;
45cdf0e10cSrcweir 	uno::Reference< sheet::XNamedRanges > m_xNames;
46cdf0e10cSrcweir public:
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)47cdf0e10cSrcweir     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 ) {}
48cdf0e10cSrcweir 
nextElement()49cdf0e10cSrcweir 	virtual uno::Any SAL_CALL nextElement(  ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
50cdf0e10cSrcweir 	{
51cdf0e10cSrcweir 		uno::Reference< sheet::XNamedRange > xNamed( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
52cdf0e10cSrcweir 		return uno::makeAny( uno::Reference< excel::XName > ( new ScVbaName( m_xParent, m_xContext, xNamed ,m_xNames , m_xModel ) ) );
53cdf0e10cSrcweir 	}
54cdf0e10cSrcweir 
55cdf0e10cSrcweir };
56cdf0e10cSrcweir 
57cdf0e10cSrcweir 
ScVbaNames(const css::uno::Reference<ov::XHelperInterface> & xParent,const css::uno::Reference<css::uno::XComponentContext> & xContext,const css::uno::Reference<css::sheet::XNamedRanges> & xNames,const css::uno::Reference<css::frame::XModel> & xModel)58cdf0e10cSrcweir ScVbaNames::ScVbaNames(const css::uno::Reference< ov::XHelperInterface >& xParent,
59cdf0e10cSrcweir 			const css::uno::Reference< css::uno::XComponentContext >& xContext,
60cdf0e10cSrcweir 			const css::uno::Reference< css::sheet::XNamedRanges >& xNames,
61cdf0e10cSrcweir 			const css::uno::Reference< css::frame::XModel >& xModel ):
62cdf0e10cSrcweir 			ScVbaNames_BASE(  xParent , xContext , uno::Reference< container::XIndexAccess >( xNames, uno::UNO_QUERY ) ),
63cdf0e10cSrcweir 			mxModel( xModel ),
64cdf0e10cSrcweir 			mxNames( xNames )
65cdf0e10cSrcweir {
66cdf0e10cSrcweir     m_xNameAccess.set( xNames, uno::UNO_QUERY_THROW );
67cdf0e10cSrcweir }
68cdf0e10cSrcweir 
~ScVbaNames()69cdf0e10cSrcweir ScVbaNames::~ScVbaNames()
70cdf0e10cSrcweir {
71cdf0e10cSrcweir }
72cdf0e10cSrcweir 
73cdf0e10cSrcweir ScDocument *
getScDocument()74cdf0e10cSrcweir ScVbaNames::getScDocument()
75cdf0e10cSrcweir {
76cdf0e10cSrcweir 	uno::Reference< frame::XModel > xModel( getModel() , uno::UNO_QUERY_THROW );
77cdf0e10cSrcweir 	ScTabViewShell * pTabViewShell = excel::getBestViewShell( xModel );
78cdf0e10cSrcweir 	if ( !pTabViewShell )
79cdf0e10cSrcweir 		throw uno::RuntimeException( rtl::OUString::createFromAscii("No ViewShell available"), uno::Reference< uno::XInterface >() );
80cdf0e10cSrcweir 	ScViewData* pViewData = pTabViewShell->GetViewData();
81cdf0e10cSrcweir 	if ( !pViewData )
82cdf0e10cSrcweir 		throw uno::RuntimeException( rtl::OUString::createFromAscii("No ViewData available"), uno::Reference< uno::XInterface >() );
83cdf0e10cSrcweir 	return pViewData->GetDocument();
84cdf0e10cSrcweir }
85cdf0e10cSrcweir 
86cdf0e10cSrcweir css::uno::Any
Add(const css::uno::Any & Name,const css::uno::Any & RefersTo,const css::uno::Any &,const css::uno::Any &,const css::uno::Any &,const css::uno::Any &,const css::uno::Any & NameLocal,const css::uno::Any &,const css::uno::Any &,const css::uno::Any & RefersToR1C1,const css::uno::Any & RefersToR1C1Local)87cdf0e10cSrcweir ScVbaNames::Add( const css::uno::Any& Name ,
88cdf0e10cSrcweir                                         const css::uno::Any& RefersTo,
89cdf0e10cSrcweir                                         const css::uno::Any& /*Visible*/,
90cdf0e10cSrcweir                                         const css::uno::Any& /*MacroType*/,
91cdf0e10cSrcweir                                         const css::uno::Any& /*ShoutcutKey*/,
92cdf0e10cSrcweir                                         const css::uno::Any& /*Category*/,
93cdf0e10cSrcweir                                         const css::uno::Any& NameLocal,
94cdf0e10cSrcweir                                         const css::uno::Any& /*RefersToLocal*/,
95cdf0e10cSrcweir                                         const css::uno::Any& /*CategoryLocal*/,
96cdf0e10cSrcweir                                         const css::uno::Any& RefersToR1C1,
97cdf0e10cSrcweir                                         const css::uno::Any& RefersToR1C1Local ) throw (css::uno::RuntimeException)
98cdf0e10cSrcweir {
99cdf0e10cSrcweir 	rtl::OUString sName;
100cdf0e10cSrcweir 	uno::Reference< excel::XRange > xRange;
101cdf0e10cSrcweir 	if ( Name.hasValue() )
102cdf0e10cSrcweir 		Name >>= sName;
103cdf0e10cSrcweir 	else if ( NameLocal.hasValue() )
104cdf0e10cSrcweir 		NameLocal >>= sName;
105cdf0e10cSrcweir 	if ( sName.getLength() != 0 )
106cdf0e10cSrcweir 	{
107cdf0e10cSrcweir 		if ( !ScRangeData::IsNameValid( sName , getScDocument() ) )
108cdf0e10cSrcweir 		{
109cdf0e10cSrcweir 			::rtl::OUString sResult ;
110cdf0e10cSrcweir 			sal_Int32 nToken = 0;
111cdf0e10cSrcweir 			sal_Int32 nIndex = 0;
112cdf0e10cSrcweir 			sResult = sName.getToken( nToken , '!' , nIndex );
113cdf0e10cSrcweir 			if ( -1 == nIndex )
114cdf0e10cSrcweir 				sResult = sName;
115cdf0e10cSrcweir 			else
116cdf0e10cSrcweir 				sResult = sName.copy( nIndex );
117cdf0e10cSrcweir 			sName = sResult ;
118cdf0e10cSrcweir 			if ( !ScRangeData::IsNameValid( sName , getScDocument() ) )
119cdf0e10cSrcweir 				throw uno::RuntimeException( rtl::OUString::createFromAscii("This Name is a valid ."), uno::Reference< uno::XInterface >() );
120cdf0e10cSrcweir 		}
121cdf0e10cSrcweir 	}
122cdf0e10cSrcweir 	if ( RefersTo.hasValue() || RefersToR1C1.hasValue() || RefersToR1C1Local.hasValue() )
123cdf0e10cSrcweir 	{
124cdf0e10cSrcweir 		if ( RefersTo.hasValue() )
125cdf0e10cSrcweir 			RefersTo >>= xRange;
126cdf0e10cSrcweir 		if ( RefersToR1C1.hasValue() )
127cdf0e10cSrcweir 			RefersToR1C1 >>= xRange;
128cdf0e10cSrcweir 		if ( RefersToR1C1Local.hasValue() )
129cdf0e10cSrcweir 			RefersToR1C1Local >>= xRange;
130cdf0e10cSrcweir 	}
131cdf0e10cSrcweir 
132cdf0e10cSrcweir 	if ( xRange.is() )
133cdf0e10cSrcweir 	{
134cdf0e10cSrcweir 		ScVbaRange* pRange = dynamic_cast< ScVbaRange* >( xRange.get() );
135cdf0e10cSrcweir 		uno::Reference< table::XCellRange > thisRange ;
136cdf0e10cSrcweir 		uno::Any xAny = pRange->getCellRange() ;
137cdf0e10cSrcweir 		if ( xAny.hasValue() )
138cdf0e10cSrcweir 			xAny >>= thisRange;
139cdf0e10cSrcweir 		uno::Reference< sheet::XCellRangeAddressable > thisRangeAdd( thisRange, ::uno::UNO_QUERY_THROW);
140cdf0e10cSrcweir 		table::CellRangeAddress aAddr = thisRangeAdd->getRangeAddress();
141cdf0e10cSrcweir 		ScAddress aPos( static_cast< SCCOL >( aAddr.StartColumn ) , static_cast< SCROW >( aAddr.StartRow ) , static_cast< SCTAB >(aAddr.Sheet ) );
142cdf0e10cSrcweir 		uno::Any xAny2 ;
143cdf0e10cSrcweir 		String sRangeAdd = xRange->Address( xAny2, xAny2 , xAny2 , xAny2, xAny2 );
144cdf0e10cSrcweir 		String sTmp;
145cdf0e10cSrcweir 		sTmp += String::CreateFromAscii("$");
146cdf0e10cSrcweir 		sTmp += UniString(xRange->getWorksheet()->getName());
147cdf0e10cSrcweir 		sTmp += String::CreateFromAscii(".");
148cdf0e10cSrcweir 		sTmp += sRangeAdd;
149cdf0e10cSrcweir 		if ( mxNames.is() )
150cdf0e10cSrcweir 		{
151cdf0e10cSrcweir 			RangeType nType = RT_NAME;
152cdf0e10cSrcweir 			table::CellAddress aCellAddr( aAddr.Sheet , aAddr.StartColumn , aAddr.StartRow );
153cdf0e10cSrcweir 			if ( mxNames->hasByName( sName ) )
154cdf0e10cSrcweir 				mxNames->removeByName(sName);
155cdf0e10cSrcweir 			mxNames->addNewByName( sName , rtl::OUString(sTmp) , aCellAddr , (sal_Int32)nType);
156cdf0e10cSrcweir 			uno::Reference< sheet::XNamedRange > xName( mxNames->getByName( sName ), uno::UNO_QUERY_THROW );
157cdf0e10cSrcweir             return uno::Any( uno::Reference< excel::XName >( new ScVbaName( getParent(), mxContext, xName, mxNames, mxModel ) ) );
158cdf0e10cSrcweir 		}
159cdf0e10cSrcweir 	}
160cdf0e10cSrcweir 	return css::uno::Any();
161cdf0e10cSrcweir }
162cdf0e10cSrcweir 
163cdf0e10cSrcweir // XEnumerationAccess
164cdf0e10cSrcweir css::uno::Type
getElementType()165cdf0e10cSrcweir ScVbaNames::getElementType() throw( css::uno::RuntimeException )
166cdf0e10cSrcweir {
167cdf0e10cSrcweir 	return ov::excel::XName::static_type(0);
168cdf0e10cSrcweir }
169cdf0e10cSrcweir 
170cdf0e10cSrcweir uno::Reference< container::XEnumeration >
createEnumeration()171cdf0e10cSrcweir ScVbaNames::createEnumeration() throw (uno::RuntimeException)
172cdf0e10cSrcweir {
173cdf0e10cSrcweir 	uno::Reference< container::XEnumerationAccess > xEnumAccess( mxNames, uno::UNO_QUERY_THROW );
174cdf0e10cSrcweir 	return new NamesEnumeration( getParent(), mxContext, xEnumAccess->createEnumeration(), mxModel , mxNames );
175cdf0e10cSrcweir }
176cdf0e10cSrcweir 
177cdf0e10cSrcweir uno::Any
createCollectionObject(const uno::Any & aSource)178cdf0e10cSrcweir ScVbaNames::createCollectionObject( const uno::Any& aSource )
179cdf0e10cSrcweir {
180cdf0e10cSrcweir 	uno::Reference< sheet::XNamedRange > xName( aSource, uno::UNO_QUERY );
181cdf0e10cSrcweir 	return uno::makeAny( uno::Reference< excel::XName > ( new ScVbaName( getParent(), mxContext, xName, mxNames , mxModel ) ) );
182cdf0e10cSrcweir }
183cdf0e10cSrcweir 
184cdf0e10cSrcweir rtl::OUString&
getServiceImplName()185cdf0e10cSrcweir ScVbaNames::getServiceImplName()
186cdf0e10cSrcweir {
187cdf0e10cSrcweir 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaNames") );
188cdf0e10cSrcweir 	return sImplName;
189cdf0e10cSrcweir }
190cdf0e10cSrcweir 
191cdf0e10cSrcweir css::uno::Sequence<rtl::OUString>
getServiceNames()192cdf0e10cSrcweir ScVbaNames::getServiceNames()
193cdf0e10cSrcweir {
194cdf0e10cSrcweir 	static uno::Sequence< rtl::OUString > aServiceNames;
195cdf0e10cSrcweir 	if ( aServiceNames.getLength() == 0 )
196cdf0e10cSrcweir 	{
197cdf0e10cSrcweir 		aServiceNames.realloc( 1 );
198cdf0e10cSrcweir 		aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.NamedRanges" ) );
199cdf0e10cSrcweir 	}
200cdf0e10cSrcweir 	return aServiceNames;
201cdf0e10cSrcweir }
202cdf0e10cSrcweir 
203cdf0e10cSrcweir 
204