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 .
20 #include <com/sun/star/table/XCellRange.hpp>
21 #include <com/sun/star/sheet/XCellRangeAddressable.hpp>
22 #include <com/sun/star/sheet/XNamedRange.hpp>
23 #include <com/sun/star/sheet/XNamedRanges.hpp>
25 #include "excelvbahelper.hxx"
26 #include "vbanames.hxx"
27 #include "vbaname.hxx"
28 #include "vbarange.hxx"
29 #include <tabvwsh.hxx>
30 #include <viewdata.hxx>
31 #include <compiler.hxx>
32 #include <tokenarray.hxx>
33 #include <cellsuno.hxx>
37 using namespace ::ooo::vba
;
38 using namespace ::com::sun::star
;
40 class NamesEnumeration
: public EnumerationHelperImpl
42 uno::Reference
< frame::XModel
> m_xModel
;
43 uno::Reference
< sheet::XNamedRanges
> m_xNames
;
45 /// @throws uno::RuntimeException
46 NamesEnumeration( const uno::Reference
< XHelperInterface
>& xParent
, const uno::Reference
< uno::XComponentContext
>& xContext
, const uno::Reference
< container::XEnumeration
>& xEnumeration
, const uno::Reference
< frame::XModel
>& xModel
, const uno::Reference
< sheet::XNamedRanges
>& xNames
) : EnumerationHelperImpl( xParent
, xContext
, xEnumeration
), m_xModel( xModel
), m_xNames( xNames
) {}
48 virtual uno::Any SAL_CALL
nextElement( ) override
50 uno::Reference
< sheet::XNamedRange
> xNamed( m_xEnumeration
->nextElement(), uno::UNO_QUERY_THROW
);
51 return uno::makeAny( uno::Reference
< excel::XName
> ( new ScVbaName( m_xParent
, m_xContext
, xNamed
,m_xNames
, m_xModel
) ) );
56 ScVbaNames::ScVbaNames(const css::uno::Reference
< ov::XHelperInterface
>& xParent
,
57 const css::uno::Reference
< css::uno::XComponentContext
>& xContext
,
58 const css::uno::Reference
< css::sheet::XNamedRanges
>& xNames
,
59 const css::uno::Reference
< css::frame::XModel
>& xModel
):
60 ScVbaNames_BASE( xParent
, xContext
, uno::Reference
< container::XIndexAccess
>( xNames
, uno::UNO_QUERY
) ),
64 m_xNameAccess
.set( xNames
, uno::UNO_QUERY_THROW
);
67 ScVbaNames::~ScVbaNames()
72 ScVbaNames::getScDocument()
74 uno::Reference
< frame::XModel
> xModel( getModel() , uno::UNO_SET_THROW
);
75 ScTabViewShell
* pTabViewShell
= excel::getBestViewShell( xModel
);
77 throw uno::RuntimeException( "No ViewShell available" );
78 ScViewData
& rViewData
= pTabViewShell
->GetViewData();
79 return rViewData
.GetDocument();
83 ScVbaNames::Add( const css::uno::Any
& Name
,
84 const css::uno::Any
& RefersTo
,
85 const css::uno::Any
& /*Visible*/,
86 const css::uno::Any
& /*MacroType*/,
87 const css::uno::Any
& /*ShoutcutKey*/,
88 const css::uno::Any
& /*Category*/,
89 const css::uno::Any
& NameLocal
,
90 const css::uno::Any
& /*RefersToLocal*/,
91 const css::uno::Any
& /*CategoryLocal*/,
92 const css::uno::Any
& RefersToR1C1
,
93 const css::uno::Any
& RefersToR1C1Local
)
96 uno::Reference
< excel::XRange
> xRange
;
97 if ( Name
.hasValue() )
99 else if ( NameLocal
.hasValue() )
101 if ( !sName
.isEmpty() )
103 if ( ScRangeData::IsNameValid( sName
, getScDocument() ) != ScRangeData::NAME_VALID
)
105 const sal_Int32 nIndex
{ sName
.indexOf('!') };
107 sName
= sName
.copy(nIndex
+1);
108 if ( ScRangeData::IsNameValid( sName
, getScDocument() ) != ScRangeData::NAME_VALID
)
109 throw uno::RuntimeException( "This Name is not valid ." );
112 uno::Reference
< table::XCellRange
> xUnoRange
;
113 if ( RefersTo
.hasValue() || RefersToR1C1
.hasValue() || RefersToR1C1Local
.hasValue() )
117 formula::FormulaGrammar::Grammar eGram
= formula::FormulaGrammar::GRAM_NATIVE_XL_A1
;
118 if ( RefersTo
.hasValue() )
120 if ( RefersTo
.getValueTypeClass() == uno::TypeClass_STRING
)
121 RefersTo
>>= sFormula
;
125 if ( RefersToR1C1
.hasValue() )
127 if ( RefersToR1C1
.getValueTypeClass() == uno::TypeClass_STRING
)
129 RefersToR1C1
>>= sFormula
;
130 eGram
= formula::FormulaGrammar::GRAM_NATIVE_XL_R1C1
;
133 RefersToR1C1
>>= xRange
;
135 if ( RefersToR1C1Local
.hasValue() )
137 if ( RefersToR1C1Local
.getValueTypeClass() == uno::TypeClass_STRING
)
139 RefersToR1C1Local
>>= sFormula
;
140 eGram
= formula::FormulaGrammar::GRAM_NATIVE_XL_R1C1
;
143 RefersToR1C1Local
>>= xRange
;
145 if ( !xRange
.is() && !sFormula
.isEmpty() )
148 ScCompiler
aComp( getScDocument(), aBlank
, eGram
);
149 std::unique_ptr
<ScTokenArray
> pTokens(aComp
.CompileString(sFormula
));
153 ScDocShell
* pDocSh
= excel::getDocShell(getModel());
154 if (pTokens
->IsValidReference(aRange
, aBlank
))
155 xUnoRange
= new ScCellRangeObj( pDocSh
, aRange
);
158 // assume it's an address try strip the '=' if it's there
159 // and try and create a range ( must be a better way )
160 if ( sFormula
.startsWith("=") )
161 sFormula
= sFormula
.copy(1);
162 ScRangeList aCellRanges
;
163 ScRefFlags nFlags
= ScRefFlags::ZERO
;
164 formula::FormulaGrammar::AddressConvention eConv
= ( eGram
== formula::FormulaGrammar::GRAM_NATIVE_XL_A1
) ? formula::FormulaGrammar::CONV_XL_A1
: formula::FormulaGrammar::CONV_XL_R1C1
;
165 if ( ScVbaRange::getCellRangesForAddress( nFlags
, sFormula
, pDocSh
, aCellRanges
, eConv
, ',' ) )
167 if ( aCellRanges
.size() == 1 )
168 xUnoRange
= new ScCellRangeObj( pDocSh
, aCellRanges
.front() );
171 uno::Reference
< sheet::XSheetCellRangeContainer
> xRanges( new ScCellRangesObj( pDocSh
, aCellRanges
) );
172 xRange
= new ScVbaRange( mxParent
, mxContext
, xRanges
);
181 if ( xRange
.is() || xUnoRange
.is() )
184 xRange
= new ScVbaRange( mxParent
, mxContext
, xUnoRange
);
186 uno::Reference
< excel::XRange
> xArea( xRange
->Areas( uno::makeAny( sal_Int32(1) ) ), uno::UNO_QUERY
);
188 uno::Any aAny
= xArea
->getCellRange() ;
190 uno::Reference
< sheet::XCellRangeAddressable
> thisRangeAdd( aAny
, ::uno::UNO_QUERY_THROW
);
192 table::CellRangeAddress aAddr
= thisRangeAdd
->getRangeAddress();
196 table::CellAddress
aCellAddr( aAddr
.Sheet
, aAddr
.StartColumn
, aAddr
.StartRow
);
197 if ( mxNames
->hasByName( sName
) )
198 mxNames
->removeByName(sName
);
199 OUStringBuffer sTmp
= "$";
200 uno::Reference
< ov::XCollection
> xCol( xRange
->Areas( uno::Any() ), uno::UNO_QUERY
);
201 for ( sal_Int32 nArea
= 1; nArea
<= xCol
->getCount(); ++nArea
)
203 xArea
.set( xRange
->Areas( uno::makeAny( nArea
) ), uno::UNO_QUERY_THROW
);
205 OUString sRangeAdd
= xArea
->Address( aAny2
, aAny2
, aAny2
, aAny2
, aAny2
);
208 sTmp
.append("'").append(xRange
->getWorksheet()->getName()).append("'.").append(sRangeAdd
);
210 mxNames
->addNewByName( sName
, sTmp
.makeStringAndClear(), aCellAddr
, 0/*nUnoType*/);
211 return Item( uno::makeAny( sName
), uno::Any() );
214 return css::uno::Any();
217 // XEnumerationAccess
219 ScVbaNames::getElementType()
221 return cppu::UnoType
<ov::excel::XName
>::get();
224 uno::Reference
< container::XEnumeration
>
225 ScVbaNames::createEnumeration()
227 uno::Reference
< container::XEnumerationAccess
> xEnumAccess( mxNames
, uno::UNO_QUERY_THROW
);
228 return new NamesEnumeration( getParent(), mxContext
, xEnumAccess
->createEnumeration(), mxModel
, mxNames
);
232 ScVbaNames::createCollectionObject( const uno::Any
& aSource
)
234 uno::Reference
< sheet::XNamedRange
> xName( aSource
, uno::UNO_QUERY
);
235 return uno::makeAny( uno::Reference
< excel::XName
> ( new ScVbaName( getParent(), mxContext
, xName
, mxNames
, mxModel
) ) );
239 ScVbaNames::getServiceImplName()
244 css::uno::Sequence
<OUString
>
245 ScVbaNames::getServiceNames()
247 static uno::Sequence
< OUString
> const aServiceNames
249 "ooo.vba.excel.NamedRanges"
251 return aServiceNames
;
254 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */