1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #include <vbahelper/helperdecl.hxx>
21 #include <com/sun/star/table/XCellRange.hpp>
22 #include <com/sun/star/sheet/XCellRangeAddressable.hpp>
23 #include <com/sun/star/sheet/XCellRangeReferrer.hpp>
25 #include "vbaname.hxx"
26 #include "vbarange.hxx"
27 #include "vbaglobals.hxx"
29 #include <rangenam.hxx>
30 #include <vcl/msgbox.hxx>
31 #include "tabvwsh.hxx"
32 #include "viewdata.hxx"
33 #include "nameuno.hxx"
34 #include "compiler.hxx"
35 #include "tokenarray.hxx"
37 using namespace ::ooo::vba
;
38 using namespace ::com::sun::star
;
40 ScVbaName::ScVbaName(const css::uno::Reference
< ov::XHelperInterface
>& xParent
,
41 const css::uno::Reference
< css::uno::XComponentContext
>& xContext
,
42 const css::uno::Reference
< css::sheet::XNamedRange
>& xName
,
43 const css::uno::Reference
< css::sheet::XNamedRanges
>& xNames
,
44 const css::uno::Reference
< css::frame::XModel
>& xModel
):
45 NameImpl_BASE( xParent
, xContext
),
47 mxNamedRange( xName
),
52 ScVbaName::~ScVbaName()
56 css::uno::Reference
< ov::excel::XWorksheet
>
57 ScVbaName::getWorkSheet() throw (css::uno::RuntimeException
)
59 uno::Reference
< excel::XApplication
> xApplication( Application(), uno::UNO_QUERY_THROW
);
60 return xApplication
->getActiveSheet();
64 ScVbaName::getName() throw (css::uno::RuntimeException
)
66 return mxNamedRange
->getName();
70 ScVbaName::setName( const OUString
& rName
) throw (css::uno::RuntimeException
)
72 mxNamedRange
->setName( rName
);
76 ScVbaName::getNameLocal() throw (css::uno::RuntimeException
)
82 ScVbaName::setNameLocal( const OUString
& rName
) throw (css::uno::RuntimeException
)
88 ScVbaName::getVisible() throw (css::uno::RuntimeException
)
94 ScVbaName::setVisible( sal_Bool
/*bVisible*/ ) throw (css::uno::RuntimeException
)
98 OUString
ScVbaName::getContent( const formula::FormulaGrammar::Grammar eGrammar
, bool bPrependEquals
)
100 ScNamedRangeObj
* pNamedRange
= dynamic_cast< ScNamedRangeObj
* >( mxNamedRange
.get() );
104 ScRangeData
* pData
= pNamedRange
->GetRangeData_Impl();
106 pData
->GetSymbol( aContent
, eGrammar
);
108 if ( bPrependEquals
)
110 if (aContent
.indexOf('=') != 0)
111 aContent
= OUString::createFromAscii("=") + aContent
;
116 void ScVbaName::setContent( const OUString
& rContent
, const formula::FormulaGrammar::Grammar eGrammar
, bool bRemoveEquals
)
118 OUString
sContent( rContent
);
121 if (sContent
.indexOf('=') == 0)
122 sContent
= sContent
.copy(1);
124 ScNamedRangeObj
* pNamedRange
= dynamic_cast< ScNamedRangeObj
* >( mxNamedRange
.get() );
126 // We should be able to do the below by just setting calling SetCode on pNamedRange
128 if ( pNamedRange
&& pNamedRange
->pDocShell
)
131 ScDocument
* pDoc
= pNamedRange
->pDocShell
->GetDocument();
132 ScRangeData
* pOldData
= pNamedRange
->GetRangeData_Impl();
135 // Shorter way of doing this ?
136 ScCompiler
aComp( pDoc
, pOldData
->GetPos() );
137 aComp
.SetGrammar( eGrammar
);
138 ScTokenArray
aArray(*aComp
.CompileString( sContent
) );
139 pOldData
->SetCode( aArray
);
145 ScVbaName::getValue() throw (css::uno::RuntimeException
)
147 rtl::OUString sResult
= getContent( formula::FormulaGrammar::GRAM_NATIVE_XL_A1
, true );
153 ScVbaName::setValue( const OUString
& rValue
) throw (css::uno::RuntimeException
)
155 setContent( rValue
, formula::FormulaGrammar::GRAM_NATIVE_XL_A1
, true );
159 ScVbaName::getRefersTo() throw (css::uno::RuntimeException
)
165 ScVbaName::setRefersTo( const OUString
& rRefersTo
) throw (css::uno::RuntimeException
)
167 setValue( rRefersTo
);
171 ScVbaName::getRefersToLocal() throw (css::uno::RuntimeException
)
173 return getRefersTo();
177 ScVbaName::setRefersToLocal( const OUString
& rRefersTo
) throw (css::uno::RuntimeException
)
179 setRefersTo( rRefersTo
);
183 ScVbaName::getRefersToR1C1() throw (css::uno::RuntimeException
)
185 rtl::OUString sResult
= getContent( formula::FormulaGrammar::GRAM_NATIVE_XL_R1C1
, true );
190 ScVbaName::setRefersToR1C1( const OUString
& rRefersTo
) throw (css::uno::RuntimeException
)
192 setContent( rRefersTo
, formula::FormulaGrammar::GRAM_NATIVE_XL_R1C1
, true );
196 ScVbaName::getRefersToR1C1Local() throw (css::uno::RuntimeException
)
198 return getRefersToR1C1();
202 ScVbaName::setRefersToR1C1Local( const OUString
& rRefersTo
) throw (css::uno::RuntimeException
)
204 setRefersTo( rRefersTo
);
207 css::uno::Reference
< ov::excel::XRange
>
208 ScVbaName::getRefersToRange() throw (css::uno::RuntimeException
)
210 uno::Reference
< ov::excel::XRange
> xRange
= ScVbaRange::getRangeObjectForName(
211 mxContext
, mxNamedRange
->getName(), excel::getDocShell( mxModel
), formula::FormulaGrammar::CONV_XL_R1C1
);
216 ScVbaName::setRefersToRange( const css::uno::Reference
< ov::excel::XRange
> /*rRange*/ ) throw (css::uno::RuntimeException
)
221 ScVbaName::Delete() throw (css::uno::RuntimeException
)
223 mxNames
->removeByName( mxNamedRange
->getName() );
227 ScVbaName::getServiceImplName()
229 return OUString( "ScVbaName" );
232 uno::Sequence
< OUString
>
233 ScVbaName::getServiceNames()
235 static uno::Sequence
< OUString
> aServiceNames
;
236 if ( aServiceNames
.getLength() == 0 )
238 aServiceNames
.realloc( 1 );
239 aServiceNames
[ 0 ] = OUString( "ooo.vba.excel.Name" );
241 return aServiceNames
;
244 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */