xref: /trunk/main/sc/source/ui/vba/vbaname.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir #include <vbahelper/helperdecl.hxx>
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir #include <com/sun/star/table/XCellRange.hpp>
30*cdf0e10cSrcweir #include <com/sun/star/sheet/XCellRangeAddressable.hpp>
31*cdf0e10cSrcweir #include <com/sun/star/sheet/XCellRangeReferrer.hpp>
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include "vbaname.hxx"
34*cdf0e10cSrcweir #include "vbarange.hxx"
35*cdf0e10cSrcweir #include "vbaglobals.hxx"
36*cdf0e10cSrcweir #include <vector>
37*cdf0e10cSrcweir #include <rangenam.hxx>
38*cdf0e10cSrcweir #include <vcl/msgbox.hxx>
39*cdf0e10cSrcweir #include "tabvwsh.hxx"
40*cdf0e10cSrcweir #include "viewdata.hxx"
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir using namespace ::ooo::vba;
43*cdf0e10cSrcweir using namespace ::com::sun::star;
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir ScVbaName::ScVbaName(const css::uno::Reference< ov::XHelperInterface >& xParent,
46*cdf0e10cSrcweir             const css::uno::Reference< css::uno::XComponentContext >& xContext,
47*cdf0e10cSrcweir             const css::uno::Reference< css::sheet::XNamedRange >& xName,
48*cdf0e10cSrcweir             const css::uno::Reference< css::sheet::XNamedRanges >& xNames,
49*cdf0e10cSrcweir             const css::uno::Reference< css::frame::XModel >& xModel ):
50*cdf0e10cSrcweir             NameImpl_BASE(  xParent , xContext ),
51*cdf0e10cSrcweir             mxModel( xModel ),
52*cdf0e10cSrcweir             mxNamedRange( xName ),
53*cdf0e10cSrcweir             mxNames( xNames )
54*cdf0e10cSrcweir {
55*cdf0e10cSrcweir }
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir ScVbaName::~ScVbaName()
58*cdf0e10cSrcweir {
59*cdf0e10cSrcweir }
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir css::uno::Reference< ov::excel::XWorksheet >
62*cdf0e10cSrcweir ScVbaName::getWorkSheet() throw (css::uno::RuntimeException)
63*cdf0e10cSrcweir {
64*cdf0e10cSrcweir     uno::Reference< excel::XApplication > xApplication( Application(), uno::UNO_QUERY_THROW );
65*cdf0e10cSrcweir     return xApplication->getActiveSheet();
66*cdf0e10cSrcweir }
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir ::rtl::OUString
69*cdf0e10cSrcweir ScVbaName::getName() throw (css::uno::RuntimeException)
70*cdf0e10cSrcweir {
71*cdf0e10cSrcweir     String sName;
72*cdf0e10cSrcweir     sName += UniString( getWorkSheet()->getName());
73*cdf0e10cSrcweir     sName += String::CreateFromAscii("!");
74*cdf0e10cSrcweir     sName += UniString ( mxNamedRange->getName() );
75*cdf0e10cSrcweir     return ::rtl::OUString( sName );
76*cdf0e10cSrcweir }
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir void
79*cdf0e10cSrcweir ScVbaName::setName( const ::rtl::OUString & rName ) throw (css::uno::RuntimeException)
80*cdf0e10cSrcweir {
81*cdf0e10cSrcweir     mxNamedRange->setName( rName );
82*cdf0e10cSrcweir }
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir ::rtl::OUString
85*cdf0e10cSrcweir ScVbaName::getNameLocal() throw (css::uno::RuntimeException)
86*cdf0e10cSrcweir {
87*cdf0e10cSrcweir     return getName();
88*cdf0e10cSrcweir }
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir void
91*cdf0e10cSrcweir ScVbaName::setNameLocal( const ::rtl::OUString & rName ) throw (css::uno::RuntimeException)
92*cdf0e10cSrcweir {
93*cdf0e10cSrcweir     setName( rName );
94*cdf0e10cSrcweir }
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir sal_Bool
97*cdf0e10cSrcweir ScVbaName::getVisible() throw (css::uno::RuntimeException)
98*cdf0e10cSrcweir {
99*cdf0e10cSrcweir     return true;
100*cdf0e10cSrcweir }
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir void
103*cdf0e10cSrcweir ScVbaName::setVisible( sal_Bool /*bVisible*/ ) throw (css::uno::RuntimeException)
104*cdf0e10cSrcweir {
105*cdf0e10cSrcweir }
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir ::rtl::OUString
108*cdf0e10cSrcweir ScVbaName::getValue() throw (css::uno::RuntimeException)
109*cdf0e10cSrcweir {
110*cdf0e10cSrcweir     ::rtl::OUString sValue = mxNamedRange->getContent();
111*cdf0e10cSrcweir     ::rtl::OUString sSheetName = getWorkSheet()->getName();
112*cdf0e10cSrcweir     ::rtl::OUString sSegmentation = ::rtl::OUString::createFromAscii( ";" );
113*cdf0e10cSrcweir     ::rtl::OUString sNewSegmentation = ::rtl::OUString::createFromAscii( "," );
114*cdf0e10cSrcweir     ::rtl::OUString sResult;
115*cdf0e10cSrcweir     sal_Int32 nFrom = 0;
116*cdf0e10cSrcweir     sal_Int32 nTo = 0;
117*cdf0e10cSrcweir     nTo = sValue.indexOf( sSegmentation, nFrom );
118*cdf0e10cSrcweir     while ( nTo != -1 )
119*cdf0e10cSrcweir     {
120*cdf0e10cSrcweir         ::rtl::OUString sTmpValue = sValue.copy( nFrom, nTo - nFrom );
121*cdf0e10cSrcweir         if ( sTmpValue.toChar() == '$' )
122*cdf0e10cSrcweir         {
123*cdf0e10cSrcweir             ::rtl::OUString sTmp = sTmpValue.copy( 1 );
124*cdf0e10cSrcweir             sTmp = sTmp.replaceAt(0, (sSheetName + ::rtl::OUString::createFromAscii(".")).getLength(), sSheetName + ::rtl::OUString::createFromAscii("!"));
125*cdf0e10cSrcweir             sResult += sTmp;
126*cdf0e10cSrcweir             sResult += sNewSegmentation;
127*cdf0e10cSrcweir         }
128*cdf0e10cSrcweir         nFrom = nTo + 1;
129*cdf0e10cSrcweir         nTo = sValue.indexOf( sSegmentation, nFrom );
130*cdf0e10cSrcweir     }
131*cdf0e10cSrcweir     ::rtl::OUString sTmpValue = sValue.copy( nFrom );
132*cdf0e10cSrcweir     if ( sTmpValue.toChar() == '$' )
133*cdf0e10cSrcweir     {
134*cdf0e10cSrcweir         ::rtl::OUString sTmp = sTmpValue.copy(1);
135*cdf0e10cSrcweir         sTmp = sTmp.replaceAt(0, (sSheetName + ::rtl::OUString::createFromAscii(".")).getLength(), sSheetName + ::rtl::OUString::createFromAscii("!"));
136*cdf0e10cSrcweir         sResult += sTmp;
137*cdf0e10cSrcweir     }
138*cdf0e10cSrcweir     if (sResult.indexOf('=') != 0)
139*cdf0e10cSrcweir     {
140*cdf0e10cSrcweir         sResult = ::rtl::OUString::createFromAscii("=") + sResult;
141*cdf0e10cSrcweir     }
142*cdf0e10cSrcweir     return sResult;
143*cdf0e10cSrcweir }
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir void
146*cdf0e10cSrcweir ScVbaName::setValue( const ::rtl::OUString & rValue ) throw (css::uno::RuntimeException)
147*cdf0e10cSrcweir {
148*cdf0e10cSrcweir     ::rtl::OUString sSheetName = getWorkSheet()->getName();
149*cdf0e10cSrcweir     ::rtl::OUString sValue = rValue;
150*cdf0e10cSrcweir     ::rtl::OUString sSegmentation = ::rtl::OUString::createFromAscii( "," );
151*cdf0e10cSrcweir     ::rtl::OUString sNewSegmentation = ::rtl::OUString::createFromAscii( ";" );
152*cdf0e10cSrcweir     ::rtl::OUString sResult;
153*cdf0e10cSrcweir     sal_Int32 nFrom = 0;
154*cdf0e10cSrcweir     sal_Int32 nTo = 0;
155*cdf0e10cSrcweir     if (sValue.indexOf('=') == 0)
156*cdf0e10cSrcweir     {
157*cdf0e10cSrcweir         ::rtl::OUString sTmp = sValue.copy(1);
158*cdf0e10cSrcweir         sValue = sTmp;
159*cdf0e10cSrcweir     }
160*cdf0e10cSrcweir     nTo = sValue.indexOf( sSegmentation, nFrom );
161*cdf0e10cSrcweir     while ( nTo != -1 )
162*cdf0e10cSrcweir     {
163*cdf0e10cSrcweir         ::rtl::OUString sTmpValue = sValue.copy( nFrom, nTo - nFrom );
164*cdf0e10cSrcweir         sTmpValue = sTmpValue.replaceAt(0, (sSheetName + ::rtl::OUString::createFromAscii("!")).getLength(), sSheetName + ::rtl::OUString::createFromAscii("."));
165*cdf0e10cSrcweir         if (sTmpValue.copy(0, sSheetName.getLength()).equals(sSheetName))
166*cdf0e10cSrcweir         {
167*cdf0e10cSrcweir             sTmpValue = ::rtl::OUString::createFromAscii("$") + sTmpValue;
168*cdf0e10cSrcweir         }
169*cdf0e10cSrcweir         sTmpValue += sNewSegmentation;
170*cdf0e10cSrcweir         sResult += sTmpValue;
171*cdf0e10cSrcweir         nFrom = nTo + 1;
172*cdf0e10cSrcweir         nTo = sValue.indexOf( sSegmentation, nFrom );
173*cdf0e10cSrcweir     }
174*cdf0e10cSrcweir     ::rtl::OUString sTmpValue = sValue.copy( nFrom );
175*cdf0e10cSrcweir     sTmpValue = sTmpValue.replaceAt(0, (sSheetName + ::rtl::OUString::createFromAscii("!")).getLength(), sSheetName + ::rtl::OUString::createFromAscii("."));
176*cdf0e10cSrcweir     if (sTmpValue.copy(0, sSheetName.getLength()).equals(sSheetName))
177*cdf0e10cSrcweir     {
178*cdf0e10cSrcweir         sTmpValue = ::rtl::OUString::createFromAscii("$") + sTmpValue;
179*cdf0e10cSrcweir     }
180*cdf0e10cSrcweir     sResult += sTmpValue;
181*cdf0e10cSrcweir     mxNamedRange->setContent(sResult);
182*cdf0e10cSrcweir }
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir ::rtl::OUString
185*cdf0e10cSrcweir ScVbaName::getRefersTo() throw (css::uno::RuntimeException)
186*cdf0e10cSrcweir {
187*cdf0e10cSrcweir     return getValue();
188*cdf0e10cSrcweir }
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir void
191*cdf0e10cSrcweir ScVbaName::setRefersTo( const ::rtl::OUString & rRefersTo ) throw (css::uno::RuntimeException)
192*cdf0e10cSrcweir {
193*cdf0e10cSrcweir     setValue( rRefersTo );
194*cdf0e10cSrcweir }
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir ::rtl::OUString
197*cdf0e10cSrcweir ScVbaName::getRefersToLocal() throw (css::uno::RuntimeException)
198*cdf0e10cSrcweir {
199*cdf0e10cSrcweir     return getRefersTo();
200*cdf0e10cSrcweir }
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir void
203*cdf0e10cSrcweir ScVbaName::setRefersToLocal( const ::rtl::OUString & rRefersTo ) throw (css::uno::RuntimeException)
204*cdf0e10cSrcweir {
205*cdf0e10cSrcweir     setRefersTo( rRefersTo );
206*cdf0e10cSrcweir }
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir ::rtl::OUString
209*cdf0e10cSrcweir ScVbaName::getRefersToR1C1() throw (css::uno::RuntimeException)
210*cdf0e10cSrcweir {
211*cdf0e10cSrcweir     return getRefersTo();
212*cdf0e10cSrcweir }
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir void
215*cdf0e10cSrcweir ScVbaName::setRefersToR1C1( const ::rtl::OUString & rRefersTo ) throw (css::uno::RuntimeException)
216*cdf0e10cSrcweir {
217*cdf0e10cSrcweir     setRefersTo( rRefersTo );
218*cdf0e10cSrcweir }
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir ::rtl::OUString
221*cdf0e10cSrcweir ScVbaName::getRefersToR1C1Local() throw (css::uno::RuntimeException)
222*cdf0e10cSrcweir {
223*cdf0e10cSrcweir     return getRefersTo();
224*cdf0e10cSrcweir }
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir void
227*cdf0e10cSrcweir ScVbaName::setRefersToR1C1Local( const ::rtl::OUString & rRefersTo ) throw (css::uno::RuntimeException)
228*cdf0e10cSrcweir {
229*cdf0e10cSrcweir     setRefersTo( rRefersTo );
230*cdf0e10cSrcweir }
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir css::uno::Reference< ov::excel::XRange >
233*cdf0e10cSrcweir ScVbaName::getRefersToRange() throw (css::uno::RuntimeException)
234*cdf0e10cSrcweir {
235*cdf0e10cSrcweir     uno::Reference< ov::excel::XRange > xRange = ScVbaRange::getRangeObjectForName(
236*cdf0e10cSrcweir         mxContext, mxNamedRange->getName(), excel::getDocShell( mxModel ), formula::FormulaGrammar::CONV_XL_R1C1 );
237*cdf0e10cSrcweir     return xRange;
238*cdf0e10cSrcweir }
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir void
241*cdf0e10cSrcweir ScVbaName::setRefersToRange( const css::uno::Reference< ov::excel::XRange > /*rRange*/ ) throw (css::uno::RuntimeException)
242*cdf0e10cSrcweir {
243*cdf0e10cSrcweir }
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir void
246*cdf0e10cSrcweir ScVbaName::Delete() throw (css::uno::RuntimeException)
247*cdf0e10cSrcweir {
248*cdf0e10cSrcweir     mxNames->removeByName( mxNamedRange->getName() );
249*cdf0e10cSrcweir }
250*cdf0e10cSrcweir 
251*cdf0e10cSrcweir rtl::OUString&
252*cdf0e10cSrcweir ScVbaName::getServiceImplName()
253*cdf0e10cSrcweir {
254*cdf0e10cSrcweir     static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaName") );
255*cdf0e10cSrcweir     return sImplName;
256*cdf0e10cSrcweir }
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir uno::Sequence< rtl::OUString >
259*cdf0e10cSrcweir ScVbaName::getServiceNames()
260*cdf0e10cSrcweir {
261*cdf0e10cSrcweir     static uno::Sequence< rtl::OUString > aServiceNames;
262*cdf0e10cSrcweir     if ( aServiceNames.getLength() == 0 )
263*cdf0e10cSrcweir     {
264*cdf0e10cSrcweir         aServiceNames.realloc( 1 );
265*cdf0e10cSrcweir         aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.Name" ) );
266*cdf0e10cSrcweir     }
267*cdf0e10cSrcweir     return aServiceNames;
268*cdf0e10cSrcweir }
269*cdf0e10cSrcweir 
270