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 #include <boost/scoped_ptr.hpp>
39 using namespace ::ooo::vba
;
40 using namespace ::com::sun::star
;
42 ScVbaName::ScVbaName(const css::uno::Reference
< ov::XHelperInterface
>& xParent
,
43 const css::uno::Reference
< css::uno::XComponentContext
>& xContext
,
44 const css::uno::Reference
< css::sheet::XNamedRange
>& xName
,
45 const css::uno::Reference
< css::sheet::XNamedRanges
>& xNames
,
46 const css::uno::Reference
< css::frame::XModel
>& xModel
):
47 NameImpl_BASE( xParent
, xContext
),
49 mxNamedRange( xName
),
54 ScVbaName::~ScVbaName()
59 ScVbaName::getName() throw (css::uno::RuntimeException
, std::exception
)
61 return mxNamedRange
->getName();
65 ScVbaName::setName( const OUString
& rName
) throw (css::uno::RuntimeException
, std::exception
)
67 mxNamedRange
->setName( rName
);
71 ScVbaName::getNameLocal() throw (css::uno::RuntimeException
, std::exception
)
77 ScVbaName::setNameLocal( const OUString
& rName
) throw (css::uno::RuntimeException
, std::exception
)
83 ScVbaName::getVisible() throw (css::uno::RuntimeException
, std::exception
)
89 ScVbaName::setVisible( sal_Bool
/*bVisible*/ ) throw (css::uno::RuntimeException
, std::exception
)
93 OUString
ScVbaName::getContent( const formula::FormulaGrammar::Grammar eGrammar
, bool bPrependEquals
)
95 ScNamedRangeObj
* pNamedRange
= dynamic_cast< ScNamedRangeObj
* >( mxNamedRange
.get() );
99 ScRangeData
* pData
= pNamedRange
->GetRangeData_Impl();
101 pData
->GetSymbol( aContent
, eGrammar
);
103 if ( bPrependEquals
)
105 if (aContent
.indexOf('=') != 0)
106 aContent
= "=" + aContent
;
111 void ScVbaName::setContent( const OUString
& rContent
, const formula::FormulaGrammar::Grammar eGrammar
, bool bRemoveEquals
)
113 OUString
sContent( rContent
);
116 if (sContent
.startsWith("="))
117 sContent
= sContent
.copy(1);
119 ScNamedRangeObj
* pNamedRange
= dynamic_cast< ScNamedRangeObj
* >( mxNamedRange
.get() );
121 // We should be able to do the below by just setting calling SetCode on pNamedRange
123 if ( pNamedRange
&& pNamedRange
->pDocShell
)
126 ScDocument
& rDoc
= pNamedRange
->pDocShell
->GetDocument();
127 ScRangeData
* pOldData
= pNamedRange
->GetRangeData_Impl();
130 // Shorter way of doing this ?
131 ScCompiler
aComp( &rDoc
, pOldData
->GetPos() );
132 aComp
.SetGrammar( eGrammar
);
133 boost::scoped_ptr
<ScTokenArray
> pArray(aComp
.CompileString(sContent
));
134 pOldData
->SetCode(*pArray
);
140 ScVbaName::getValue() throw (css::uno::RuntimeException
, std::exception
)
142 rtl::OUString sResult
= getContent( formula::FormulaGrammar::GRAM_NATIVE_XL_A1
, true );
148 ScVbaName::setValue( const OUString
& rValue
) throw (css::uno::RuntimeException
, std::exception
)
150 setContent( rValue
, formula::FormulaGrammar::GRAM_NATIVE_XL_A1
, true );
154 ScVbaName::getRefersTo() throw (css::uno::RuntimeException
, std::exception
)
160 ScVbaName::setRefersTo( const OUString
& rRefersTo
) throw (css::uno::RuntimeException
, std::exception
)
162 setValue( rRefersTo
);
166 ScVbaName::getRefersToLocal() throw (css::uno::RuntimeException
, std::exception
)
168 return getRefersTo();
172 ScVbaName::setRefersToLocal( const OUString
& rRefersTo
) throw (css::uno::RuntimeException
, std::exception
)
174 setRefersTo( rRefersTo
);
178 ScVbaName::getRefersToR1C1() throw (css::uno::RuntimeException
, std::exception
)
180 rtl::OUString sResult
= getContent( formula::FormulaGrammar::GRAM_NATIVE_XL_R1C1
, true );
185 ScVbaName::setRefersToR1C1( const OUString
& rRefersTo
) throw (css::uno::RuntimeException
, std::exception
)
187 setContent( rRefersTo
, formula::FormulaGrammar::GRAM_NATIVE_XL_R1C1
, true );
191 ScVbaName::getRefersToR1C1Local() throw (css::uno::RuntimeException
, std::exception
)
193 return getRefersToR1C1();
197 ScVbaName::setRefersToR1C1Local( const OUString
& rRefersTo
) throw (css::uno::RuntimeException
, std::exception
)
199 setRefersTo( rRefersTo
);
202 css::uno::Reference
< ov::excel::XRange
>
203 ScVbaName::getRefersToRange() throw (css::uno::RuntimeException
, std::exception
)
205 uno::Reference
< ov::excel::XRange
> xRange
= ScVbaRange::getRangeObjectForName(
206 mxContext
, mxNamedRange
->getName(), excel::getDocShell( mxModel
), formula::FormulaGrammar::CONV_XL_R1C1
);
211 ScVbaName::Delete() throw (css::uno::RuntimeException
, std::exception
)
213 mxNames
->removeByName( mxNamedRange
->getName() );
217 ScVbaName::getServiceImplName()
219 return OUString( "ScVbaName" );
222 uno::Sequence
< OUString
>
223 ScVbaName::getServiceNames()
225 static uno::Sequence
< OUString
> aServiceNames
;
226 if ( aServiceNames
.getLength() == 0 )
228 aServiceNames
.realloc( 1 );
229 aServiceNames
[ 0 ] = "ooo.vba.excel.Name";
231 return aServiceNames
;
234 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */