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>
31 #include <viewdata.hxx>
32 #include <compiler.hxx>
33 #include <tokenarray.hxx>
34 #include <cellsuno.hxx>
38 using namespace ::ooo::vba
;
39 using namespace ::com::sun::star
;
43 class NamesEnumeration
: public EnumerationHelperImpl
45 uno::Reference
< frame::XModel
> m_xModel
;
46 uno::Reference
< sheet::XNamedRanges
> m_xNames
;
48 /// @throws uno::RuntimeException
49 NamesEnumeration( const uno::Reference
< XHelperInterface
>& xParent
, const uno::Reference
< uno::XComponentContext
>& xContext
, const uno::Reference
< container::XEnumeration
>& xEnumeration
, uno::Reference
< frame::XModel
> xModel
, uno::Reference
< sheet::XNamedRanges
> xNames
) : EnumerationHelperImpl( xParent
, xContext
, xEnumeration
), m_xModel(std::move( xModel
)), m_xNames(std::move( xNames
)) {}
51 virtual uno::Any SAL_CALL
nextElement( ) override
53 uno::Reference
< sheet::XNamedRange
> xNamed( m_xEnumeration
->nextElement(), uno::UNO_QUERY_THROW
);
54 return uno::Any( uno::Reference
< excel::XName
> ( new ScVbaName( m_xParent
, m_xContext
, xNamed
,m_xNames
, m_xModel
) ) );
61 ScVbaNames::ScVbaNames(const css::uno::Reference
< ov::XHelperInterface
>& xParent
,
62 const css::uno::Reference
< css::uno::XComponentContext
>& xContext
,
63 const css::uno::Reference
< css::sheet::XNamedRanges
>& xNames
,
64 css::uno::Reference
< css::frame::XModel
> xModel
):
65 ScVbaNames_BASE( xParent
, xContext
, uno::Reference
< container::XIndexAccess
>( xNames
, uno::UNO_QUERY
) ),
66 mxModel(std::move( xModel
)),
69 m_xNameAccess
.set( xNames
, uno::UNO_QUERY_THROW
);
72 ScVbaNames::~ScVbaNames()
77 ScVbaNames::getScDocument()
79 uno::Reference
< frame::XModel
> xModel( getModel() , uno::UNO_SET_THROW
);
80 ScTabViewShell
* pTabViewShell
= excel::getBestViewShell( xModel
);
82 throw uno::RuntimeException( u
"No ViewShell available"_ustr
);
83 ScViewData
& rViewData
= pTabViewShell
->GetViewData();
84 return rViewData
.GetDocument();
88 ScVbaNames::Add( const css::uno::Any
& Name
,
89 const css::uno::Any
& RefersTo
,
90 const css::uno::Any
& /*Visible*/,
91 const css::uno::Any
& /*MacroType*/,
92 const css::uno::Any
& /*ShoutcutKey*/,
93 const css::uno::Any
& /*Category*/,
94 const css::uno::Any
& NameLocal
,
95 const css::uno::Any
& /*RefersToLocal*/,
96 const css::uno::Any
& /*CategoryLocal*/,
97 const css::uno::Any
& RefersToR1C1
,
98 const css::uno::Any
& RefersToR1C1Local
)
101 uno::Reference
< excel::XRange
> xRange
;
102 if ( Name
.hasValue() )
104 else if ( NameLocal
.hasValue() )
106 if ( !sName
.isEmpty() )
108 if (ScRangeData::IsNameValid(sName
, getScDocument())
109 != ScRangeData::IsNameValidType::NAME_VALID
)
111 const sal_Int32 nIndex
{ sName
.indexOf('!') };
113 sName
= sName
.copy(nIndex
+1);
114 if (ScRangeData::IsNameValid(sName
, getScDocument())
115 != ScRangeData::IsNameValidType::NAME_VALID
)
116 throw uno::RuntimeException( u
"This Name is not valid ."_ustr
);
119 uno::Reference
< table::XCellRange
> xUnoRange
;
120 if ( RefersTo
.hasValue() || RefersToR1C1
.hasValue() || RefersToR1C1Local
.hasValue() )
124 formula::FormulaGrammar::Grammar eGram
= formula::FormulaGrammar::GRAM_NATIVE_XL_A1
;
125 if ( RefersTo
.hasValue() )
127 if ( RefersTo
.getValueTypeClass() == uno::TypeClass_STRING
)
128 RefersTo
>>= sFormula
;
132 if ( RefersToR1C1
.hasValue() )
134 if ( RefersToR1C1
.getValueTypeClass() == uno::TypeClass_STRING
)
136 RefersToR1C1
>>= sFormula
;
137 eGram
= formula::FormulaGrammar::GRAM_NATIVE_XL_R1C1
;
140 RefersToR1C1
>>= xRange
;
142 if ( RefersToR1C1Local
.hasValue() )
144 if ( RefersToR1C1Local
.getValueTypeClass() == uno::TypeClass_STRING
)
146 RefersToR1C1Local
>>= sFormula
;
147 eGram
= formula::FormulaGrammar::GRAM_NATIVE_XL_R1C1
;
150 RefersToR1C1Local
>>= xRange
;
152 if ( !xRange
.is() && !sFormula
.isEmpty() )
155 ScCompiler
aComp( getScDocument(), aBlank
, eGram
);
156 std::unique_ptr
<ScTokenArray
> pTokens(aComp
.CompileString(sFormula
));
160 ScDocShell
* pDocSh
= excel::getDocShell(getModel());
161 if (pTokens
->IsValidReference(aRange
, aBlank
))
162 xUnoRange
= new ScCellRangeObj( pDocSh
, aRange
);
165 // assume it's an address try strip the '=' if it's there
166 // and try and create a range ( must be a better way )
167 if ( sFormula
.startsWith("=") )
168 sFormula
= sFormula
.copy(1);
169 ScRangeList aCellRanges
;
170 ScRefFlags nFlags
= ScRefFlags::ZERO
;
171 formula::FormulaGrammar::AddressConvention eConv
= ( eGram
== formula::FormulaGrammar::GRAM_NATIVE_XL_A1
) ? formula::FormulaGrammar::CONV_XL_A1
: formula::FormulaGrammar::CONV_XL_R1C1
;
172 if ( ScVbaRange::getCellRangesForAddress( nFlags
, sFormula
, pDocSh
, aCellRanges
, eConv
, ',' ) )
174 if ( aCellRanges
.size() == 1 )
175 xUnoRange
= new ScCellRangeObj( pDocSh
, aCellRanges
.front() );
178 uno::Reference
< sheet::XSheetCellRangeContainer
> xRanges( new ScCellRangesObj( pDocSh
, aCellRanges
) );
179 xRange
= new ScVbaRange( mxParent
, mxContext
, xRanges
);
188 if ( xRange
.is() || xUnoRange
.is() )
191 xRange
= new ScVbaRange( mxParent
, mxContext
, xUnoRange
);
193 uno::Reference
< excel::XRange
> xArea( xRange
->Areas( uno::Any( sal_Int32(1) ) ), uno::UNO_QUERY
);
195 uno::Any aAny
= xArea
->getCellRange() ;
197 uno::Reference
< sheet::XCellRangeAddressable
> thisRangeAdd( aAny
, ::uno::UNO_QUERY_THROW
);
199 table::CellRangeAddress aAddr
= thisRangeAdd
->getRangeAddress();
203 table::CellAddress
aCellAddr( aAddr
.Sheet
, aAddr
.StartColumn
, aAddr
.StartRow
);
204 if ( mxNames
->hasByName( sName
) )
205 mxNames
->removeByName(sName
);
206 OUStringBuffer sTmp
= "$";
207 uno::Reference
< ov::XCollection
> xCol( xRange
->Areas( uno::Any() ), uno::UNO_QUERY
);
208 for ( sal_Int32 nArea
= 1; nArea
<= xCol
->getCount(); ++nArea
)
210 xArea
.set( xRange
->Areas( uno::Any( nArea
) ), uno::UNO_QUERY_THROW
);
212 OUString sRangeAdd
= xArea
->Address( aAny2
, aAny2
, aAny2
, aAny2
, aAny2
);
215 sTmp
.append("'" + xRange
->getWorksheet()->getName() + "'." + sRangeAdd
);
217 mxNames
->addNewByName( sName
, sTmp
.makeStringAndClear(), aCellAddr
, 0/*nUnoType*/);
218 return Item( uno::Any( sName
), uno::Any() );
221 return css::uno::Any();
224 // XEnumerationAccess
226 ScVbaNames::getElementType()
228 return cppu::UnoType
<ov::excel::XName
>::get();
231 uno::Reference
< container::XEnumeration
>
232 ScVbaNames::createEnumeration()
234 uno::Reference
< container::XEnumerationAccess
> xEnumAccess( mxNames
, uno::UNO_QUERY_THROW
);
235 return new NamesEnumeration( getParent(), mxContext
, xEnumAccess
->createEnumeration(), mxModel
, mxNames
);
239 ScVbaNames::createCollectionObject( const uno::Any
& aSource
)
241 uno::Reference
< sheet::XNamedRange
> xName( aSource
, uno::UNO_QUERY
);
242 return uno::Any( uno::Reference
< excel::XName
> ( new ScVbaName( getParent(), mxContext
, xName
, mxNames
, mxModel
) ) );
246 ScVbaNames::getServiceImplName()
248 return u
"ScVbaNames"_ustr
;
251 css::uno::Sequence
<OUString
>
252 ScVbaNames::getServiceNames()
254 static uno::Sequence
< OUString
> const aServiceNames
256 u
"ooo.vba.excel.NamedRanges"_ustr
258 return aServiceNames
;
261 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */