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