1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: vbaname.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #include <vbahelper/helperdecl.hxx>
32 #include <com/sun/star/table/XCellRange.hpp>
33 #include <com/sun/star/sheet/XCellRangeAddressable.hpp>
34 #include <com/sun/star/sheet/XCellRangeReferrer.hpp>
36 #include "vbaname.hxx"
37 #include "vbarange.hxx"
38 #include "vbaglobals.hxx"
40 #include <rangenam.hxx>
41 #include <vcl/msgbox.hxx>
42 #include "tabvwsh.hxx"
43 #include "viewdata.hxx"
45 using namespace ::ooo::vba
;
46 using namespace ::com::sun::star
;
48 ScVbaName::ScVbaName(const css::uno::Reference
< ov::XHelperInterface
>& xParent
,
49 const css::uno::Reference
< css::uno::XComponentContext
>& xContext
,
50 const css::uno::Reference
< css::sheet::XNamedRange
>& xName
,
51 const css::uno::Reference
< css::sheet::XNamedRanges
>& xNames
,
52 const css::uno::Reference
< css::frame::XModel
>& xModel
):
53 NameImpl_BASE( xParent
, xContext
),
55 mxNamedRange( xName
),
60 ScVbaName::~ScVbaName()
64 css::uno::Reference
< ov::excel::XWorksheet
>
65 ScVbaName::getWorkSheet() throw (css::uno::RuntimeException
)
67 uno::Reference
< excel::XApplication
> xApplication( Application(), uno::UNO_QUERY_THROW
);
68 return xApplication
->getActiveSheet();
72 ScVbaName::getName() throw (css::uno::RuntimeException
)
75 sName
+= UniString( getWorkSheet()->getName());
76 sName
+= String::CreateFromAscii("!");
77 sName
+= UniString ( mxNamedRange
->getName() );
78 return ::rtl::OUString( sName
);
82 ScVbaName::setName( const ::rtl::OUString
& rName
) throw (css::uno::RuntimeException
)
84 mxNamedRange
->setName( rName
);
88 ScVbaName::getNameLocal() throw (css::uno::RuntimeException
)
94 ScVbaName::setNameLocal( const ::rtl::OUString
& rName
) throw (css::uno::RuntimeException
)
100 ScVbaName::getVisible() throw (css::uno::RuntimeException
)
106 ScVbaName::setVisible( sal_Bool
/*bVisible*/ ) throw (css::uno::RuntimeException
)
111 ScVbaName::getValue() throw (css::uno::RuntimeException
)
113 ::rtl::OUString sValue
= mxNamedRange
->getContent();
114 ::rtl::OUString sSheetName
= getWorkSheet()->getName();
115 ::rtl::OUString sSegmentation
= ::rtl::OUString::createFromAscii( ";" );
116 ::rtl::OUString sNewSegmentation
= ::rtl::OUString::createFromAscii( "," );
117 ::rtl::OUString sResult
;
120 nTo
= sValue
.indexOf( sSegmentation
, nFrom
);
123 ::rtl::OUString sTmpValue
= sValue
.copy( nFrom
, nTo
- nFrom
);
124 if ( sTmpValue
.toChar() == '$' )
126 ::rtl::OUString sTmp
= sTmpValue
.copy( 1 );
127 sTmp
= sTmp
.replaceAt(0, (sSheetName
+ ::rtl::OUString::createFromAscii(".")).getLength(), sSheetName
+ ::rtl::OUString::createFromAscii("!"));
129 sResult
+= sNewSegmentation
;
132 nTo
= sValue
.indexOf( sSegmentation
, nFrom
);
134 ::rtl::OUString sTmpValue
= sValue
.copy( nFrom
);
135 if ( sTmpValue
.toChar() == '$' )
137 ::rtl::OUString sTmp
= sTmpValue
.copy(1);
138 sTmp
= sTmp
.replaceAt(0, (sSheetName
+ ::rtl::OUString::createFromAscii(".")).getLength(), sSheetName
+ ::rtl::OUString::createFromAscii("!"));
141 if (sResult
.indexOf('=') != 0)
143 sResult
= ::rtl::OUString::createFromAscii("=") + sResult
;
149 ScVbaName::setValue( const ::rtl::OUString
& rValue
) throw (css::uno::RuntimeException
)
151 ::rtl::OUString sSheetName
= getWorkSheet()->getName();
152 ::rtl::OUString sValue
= rValue
;
153 ::rtl::OUString sSegmentation
= ::rtl::OUString::createFromAscii( "," );
154 ::rtl::OUString sNewSegmentation
= ::rtl::OUString::createFromAscii( ";" );
155 ::rtl::OUString sResult
;
158 if (sValue
.indexOf('=') == 0)
160 ::rtl::OUString sTmp
= sValue
.copy(1);
163 nTo
= sValue
.indexOf( sSegmentation
, nFrom
);
166 ::rtl::OUString sTmpValue
= sValue
.copy( nFrom
, nTo
- nFrom
);
167 sTmpValue
= sTmpValue
.replaceAt(0, (sSheetName
+ ::rtl::OUString::createFromAscii("!")).getLength(), sSheetName
+ ::rtl::OUString::createFromAscii("."));
168 if (sTmpValue
.copy(0, sSheetName
.getLength()).equals(sSheetName
))
170 sTmpValue
= ::rtl::OUString::createFromAscii("$") + sTmpValue
;
172 sTmpValue
+= sNewSegmentation
;
173 sResult
+= sTmpValue
;
175 nTo
= sValue
.indexOf( sSegmentation
, nFrom
);
177 ::rtl::OUString sTmpValue
= sValue
.copy( nFrom
);
178 sTmpValue
= sTmpValue
.replaceAt(0, (sSheetName
+ ::rtl::OUString::createFromAscii("!")).getLength(), sSheetName
+ ::rtl::OUString::createFromAscii("."));
179 if (sTmpValue
.copy(0, sSheetName
.getLength()).equals(sSheetName
))
181 sTmpValue
= ::rtl::OUString::createFromAscii("$") + sTmpValue
;
183 sResult
+= sTmpValue
;
184 mxNamedRange
->setContent(sResult
);
188 ScVbaName::getRefersTo() throw (css::uno::RuntimeException
)
194 ScVbaName::setRefersTo( const ::rtl::OUString
& rRefersTo
) throw (css::uno::RuntimeException
)
196 setValue( rRefersTo
);
200 ScVbaName::getRefersToLocal() throw (css::uno::RuntimeException
)
202 return getRefersTo();
206 ScVbaName::setRefersToLocal( const ::rtl::OUString
& rRefersTo
) throw (css::uno::RuntimeException
)
208 setRefersTo( rRefersTo
);
212 ScVbaName::getRefersToR1C1() throw (css::uno::RuntimeException
)
214 return getRefersTo();
218 ScVbaName::setRefersToR1C1( const ::rtl::OUString
& rRefersTo
) throw (css::uno::RuntimeException
)
220 setRefersTo( rRefersTo
);
224 ScVbaName::getRefersToR1C1Local() throw (css::uno::RuntimeException
)
226 return getRefersTo();
230 ScVbaName::setRefersToR1C1Local( const ::rtl::OUString
& rRefersTo
) throw (css::uno::RuntimeException
)
232 setRefersTo( rRefersTo
);
235 css::uno::Reference
< ov::excel::XRange
>
236 ScVbaName::getRefersToRange() throw (css::uno::RuntimeException
)
238 uno::Reference
< ov::excel::XRange
> xRange
= ScVbaRange::getRangeObjectForName( mxContext
, mxNamedRange
->getName(), excel::getDocShell( mxModel
), formula::FormulaGrammar::CONV_XL_R1C1
);
243 ScVbaName::setRefersToRange( const css::uno::Reference
< ov::excel::XRange
> /*rRange*/ ) throw (css::uno::RuntimeException
)
248 ScVbaName::Delete() throw (css::uno::RuntimeException
)
250 mxNames
->removeByName( mxNamedRange
->getName() );
254 ScVbaName::getServiceImplName()
256 static rtl::OUString
sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaName") );
260 uno::Sequence
< rtl::OUString
>
261 ScVbaName::getServiceNames()
263 static uno::Sequence
< rtl::OUString
> aServiceNames
;
264 if ( aServiceNames
.getLength() == 0 )
266 aServiceNames
.realloc( 1 );
267 aServiceNames
[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.Name" ) );
269 return aServiceNames
;