xref: /aoo41x/main/sc/source/ui/vba/vbaname.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 #include <com/sun/star/sheet/XCellRangeReferrer.hpp>
28 
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 
ScVbaName(const css::uno::Reference<ov::XHelperInterface> & xParent,const css::uno::Reference<css::uno::XComponentContext> & xContext,const css::uno::Reference<css::sheet::XNamedRange> & xName,const css::uno::Reference<css::sheet::XNamedRanges> & xNames,const css::uno::Reference<css::frame::XModel> & xModel)41 ScVbaName::ScVbaName(const css::uno::Reference< ov::XHelperInterface >& xParent,
42 			const css::uno::Reference< css::uno::XComponentContext >& xContext,
43 			const css::uno::Reference< css::sheet::XNamedRange >& xName,
44 			const css::uno::Reference< css::sheet::XNamedRanges >& xNames,
45 			const css::uno::Reference< css::frame::XModel >& xModel ):
46 			NameImpl_BASE(  xParent , xContext ),
47 			mxModel( xModel ),
48 			mxNamedRange( xName ),
49 			mxNames( xNames )
50 {
51 }
52 
~ScVbaName()53 ScVbaName::~ScVbaName()
54 {
55 }
56 
57 css::uno::Reference< ov::excel::XWorksheet >
getWorkSheet()58 ScVbaName::getWorkSheet() throw (css::uno::RuntimeException)
59 {
60 	uno::Reference< excel::XApplication > xApplication( Application(), uno::UNO_QUERY_THROW );
61 	return xApplication->getActiveSheet();
62 }
63 
64 ::rtl::OUString
getName()65 ScVbaName::getName() throw (css::uno::RuntimeException)
66 {
67 	String sName;
68 	sName += UniString( getWorkSheet()->getName());
69 	sName += String::CreateFromAscii("!");
70 	sName += UniString ( mxNamedRange->getName() );
71 	return ::rtl::OUString( sName );
72 }
73 
74 void
setName(const::rtl::OUString & rName)75 ScVbaName::setName( const ::rtl::OUString & rName ) throw (css::uno::RuntimeException)
76 {
77 	mxNamedRange->setName( rName );
78 }
79 
80 ::rtl::OUString
getNameLocal()81 ScVbaName::getNameLocal() throw (css::uno::RuntimeException)
82 {
83 	return getName();
84 }
85 
86 void
setNameLocal(const::rtl::OUString & rName)87 ScVbaName::setNameLocal( const ::rtl::OUString & rName ) throw (css::uno::RuntimeException)
88 {
89 	setName( rName );
90 }
91 
92 sal_Bool
getVisible()93 ScVbaName::getVisible() throw (css::uno::RuntimeException)
94 {
95 	return true;
96 }
97 
98 void
setVisible(sal_Bool)99 ScVbaName::setVisible( sal_Bool /*bVisible*/ ) throw (css::uno::RuntimeException)
100 {
101 }
102 
103 ::rtl::OUString
getValue()104 ScVbaName::getValue() throw (css::uno::RuntimeException)
105 {
106 	::rtl::OUString sValue = mxNamedRange->getContent();
107 	::rtl::OUString sSheetName = getWorkSheet()->getName();
108     ::rtl::OUString sSegmentation = ::rtl::OUString::createFromAscii( ";" );
109     ::rtl::OUString sNewSegmentation = ::rtl::OUString::createFromAscii( "," );
110     ::rtl::OUString sResult;
111     sal_Int32 nFrom = 0;
112     sal_Int32 nTo = 0;
113     nTo = sValue.indexOf( sSegmentation, nFrom );
114     while ( nTo != -1 )
115 	{
116         ::rtl::OUString sTmpValue = sValue.copy( nFrom, nTo - nFrom );
117         if ( sTmpValue.toChar() == '$' )
118         {
119             ::rtl::OUString sTmp = sTmpValue.copy( 1 );
120             sTmp = sTmp.replaceAt(0, (sSheetName + ::rtl::OUString::createFromAscii(".")).getLength(), sSheetName + ::rtl::OUString::createFromAscii("!"));
121             sResult += sTmp;
122             sResult += sNewSegmentation;
123         }
124         nFrom = nTo + 1;
125         nTo = sValue.indexOf( sSegmentation, nFrom );
126     }
127     ::rtl::OUString sTmpValue = sValue.copy( nFrom );
128 	if ( sTmpValue.toChar() == '$' )
129 	{
130 		::rtl::OUString sTmp = sTmpValue.copy(1);
131         sTmp = sTmp.replaceAt(0, (sSheetName + ::rtl::OUString::createFromAscii(".")).getLength(), sSheetName + ::rtl::OUString::createFromAscii("!"));
132 		sResult += sTmp;
133 	}
134 	if (sResult.indexOf('=') != 0)
135 	{
136 		sResult = ::rtl::OUString::createFromAscii("=") + sResult;
137 	}
138 	return sResult;
139 }
140 
141 void
setValue(const::rtl::OUString & rValue)142 ScVbaName::setValue( const ::rtl::OUString & rValue ) throw (css::uno::RuntimeException)
143 {
144 	::rtl::OUString sSheetName = getWorkSheet()->getName();
145 	::rtl::OUString sValue = rValue;
146     ::rtl::OUString sSegmentation = ::rtl::OUString::createFromAscii( "," );
147     ::rtl::OUString sNewSegmentation = ::rtl::OUString::createFromAscii( ";" );
148     ::rtl::OUString sResult;
149     sal_Int32 nFrom = 0;
150     sal_Int32 nTo = 0;
151 	if (sValue.indexOf('=') == 0)
152 	{
153 		::rtl::OUString sTmp = sValue.copy(1);
154 		sValue = sTmp;
155 	}
156     nTo = sValue.indexOf( sSegmentation, nFrom );
157     while ( nTo != -1 )
158     {
159         ::rtl::OUString sTmpValue = sValue.copy( nFrom, nTo - nFrom );
160         sTmpValue = sTmpValue.replaceAt(0, (sSheetName + ::rtl::OUString::createFromAscii("!")).getLength(), sSheetName + ::rtl::OUString::createFromAscii("."));
161         if (sTmpValue.copy(0, sSheetName.getLength()).equals(sSheetName))
162         {
163             sTmpValue = ::rtl::OUString::createFromAscii("$") + sTmpValue;
164         }
165         sTmpValue += sNewSegmentation;
166         sResult += sTmpValue;
167         nFrom = nTo + 1;
168         nTo = sValue.indexOf( sSegmentation, nFrom );
169     }
170     ::rtl::OUString sTmpValue = sValue.copy( nFrom );
171 	sTmpValue = sTmpValue.replaceAt(0, (sSheetName + ::rtl::OUString::createFromAscii("!")).getLength(), sSheetName + ::rtl::OUString::createFromAscii("."));
172 	if (sTmpValue.copy(0, sSheetName.getLength()).equals(sSheetName))
173 	{
174 		sTmpValue = ::rtl::OUString::createFromAscii("$") + sTmpValue;
175 	}
176     sResult += sTmpValue;
177 	mxNamedRange->setContent(sResult);
178 }
179 
180 ::rtl::OUString
getRefersTo()181 ScVbaName::getRefersTo() throw (css::uno::RuntimeException)
182 {
183 	return getValue();
184 }
185 
186 void
setRefersTo(const::rtl::OUString & rRefersTo)187 ScVbaName::setRefersTo( const ::rtl::OUString & rRefersTo ) throw (css::uno::RuntimeException)
188 {
189 	setValue( rRefersTo );
190 }
191 
192 ::rtl::OUString
getRefersToLocal()193 ScVbaName::getRefersToLocal() throw (css::uno::RuntimeException)
194 {
195 	return getRefersTo();
196 }
197 
198 void
setRefersToLocal(const::rtl::OUString & rRefersTo)199 ScVbaName::setRefersToLocal( const ::rtl::OUString & rRefersTo ) throw (css::uno::RuntimeException)
200 {
201 	setRefersTo( rRefersTo );
202 }
203 
204 ::rtl::OUString
getRefersToR1C1()205 ScVbaName::getRefersToR1C1() throw (css::uno::RuntimeException)
206 {
207 	return getRefersTo();
208 }
209 
210 void
setRefersToR1C1(const::rtl::OUString & rRefersTo)211 ScVbaName::setRefersToR1C1( const ::rtl::OUString & rRefersTo ) throw (css::uno::RuntimeException)
212 {
213 	setRefersTo( rRefersTo );
214 }
215 
216 ::rtl::OUString
getRefersToR1C1Local()217 ScVbaName::getRefersToR1C1Local() throw (css::uno::RuntimeException)
218 {
219 	return getRefersTo();
220 }
221 
222 void
setRefersToR1C1Local(const::rtl::OUString & rRefersTo)223 ScVbaName::setRefersToR1C1Local( const ::rtl::OUString & rRefersTo ) throw (css::uno::RuntimeException)
224 {
225 	setRefersTo( rRefersTo );
226 }
227 
228 css::uno::Reference< ov::excel::XRange >
getRefersToRange()229 ScVbaName::getRefersToRange() throw (css::uno::RuntimeException)
230 {
231     uno::Reference< ov::excel::XRange > xRange = ScVbaRange::getRangeObjectForName(
232         mxContext, mxNamedRange->getName(), excel::getDocShell( mxModel ), formula::FormulaGrammar::CONV_XL_R1C1 );
233 	return xRange;
234 }
235 
236 void
setRefersToRange(const css::uno::Reference<ov::excel::XRange>)237 ScVbaName::setRefersToRange( const css::uno::Reference< ov::excel::XRange > /*rRange*/ ) throw (css::uno::RuntimeException)
238 {
239 }
240 
241 void
Delete()242 ScVbaName::Delete() throw (css::uno::RuntimeException)
243 {
244 	mxNames->removeByName( mxNamedRange->getName() );
245 }
246 
247 rtl::OUString&
getServiceImplName()248 ScVbaName::getServiceImplName()
249 {
250 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaName") );
251 	return sImplName;
252 }
253 
254 uno::Sequence< rtl::OUString >
getServiceNames()255 ScVbaName::getServiceNames()
256 {
257 	static uno::Sequence< rtl::OUString > aServiceNames;
258 	if ( aServiceNames.getLength() == 0 )
259 	{
260 		aServiceNames.realloc( 1 );
261 		aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.Name" ) );
262 	}
263 	return aServiceNames;
264 }
265 
266