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