LibreOffice Module sc (master) 1
vbanames.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
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>
24
25#include "excelvbahelper.hxx"
26#include "vbanames.hxx"
27#include "vbaname.hxx"
28#include "vbarange.hxx"
29#include <tabvwsh.hxx>
30#include <utility>
31#include <viewdata.hxx>
32#include <compiler.hxx>
33#include <tokenarray.hxx>
34#include <cellsuno.hxx>
35
36#include <memory>
37
38using namespace ::ooo::vba;
39using namespace ::com::sun::star;
40
41namespace {
42
43class NamesEnumeration : public EnumerationHelperImpl
44{
45 uno::Reference< frame::XModel > m_xModel;
46 uno::Reference< sheet::XNamedRanges > m_xNames;
47public:
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 )) {}
50
51 virtual uno::Any SAL_CALL nextElement( ) override
52 {
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 ) ) );
55 }
56
57};
58
59}
60
61ScVbaNames::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 )),
67 mxNames( xNames )
68{
69 m_xNameAccess.set( xNames, uno::UNO_QUERY_THROW );
70}
71
73{
74}
75
78{
79 uno::Reference< frame::XModel > xModel( getModel() , uno::UNO_SET_THROW );
81 if ( !pTabViewShell )
82 throw uno::RuntimeException( "No ViewShell available" );
83 ScViewData& rViewData = pTabViewShell->GetViewData();
84 return rViewData.GetDocument();
85}
86
87css::uno::Any
88ScVbaNames::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 )
99{
100 OUString sName;
101 uno::Reference< excel::XRange > xRange;
102 if ( Name.hasValue() )
103 Name >>= sName;
104 else if ( NameLocal.hasValue() )
105 NameLocal >>= sName;
106 if ( !sName.isEmpty() )
107 {
110 {
111 const sal_Int32 nIndex{ sName.indexOf('!') };
112 if (nIndex>=0)
113 sName = sName.copy(nIndex+1);
116 throw uno::RuntimeException( "This Name is not valid ." );
117 }
118 }
119 uno::Reference< table::XCellRange > xUnoRange;
120 if ( RefersTo.hasValue() || RefersToR1C1.hasValue() || RefersToR1C1Local.hasValue() )
121 {
122 OUString sFormula;
123
125 if ( RefersTo.hasValue() )
126 {
127 if ( RefersTo.getValueTypeClass() == uno::TypeClass_STRING )
128 RefersTo >>= sFormula;
129 else
130 RefersTo >>= xRange;
131 }
132 if ( RefersToR1C1.hasValue() )
133 {
134 if ( RefersToR1C1.getValueTypeClass() == uno::TypeClass_STRING )
135 {
136 RefersToR1C1 >>= sFormula;
138 }
139 else
140 RefersToR1C1 >>= xRange;
141 }
142 if ( RefersToR1C1Local.hasValue() )
143 {
144 if ( RefersToR1C1Local.getValueTypeClass() == uno::TypeClass_STRING )
145 {
146 RefersToR1C1Local >>= sFormula;
148 }
149 else
150 RefersToR1C1Local >>= xRange;
151 }
152 if ( !xRange.is() && !sFormula.isEmpty() )
153 {
154 ScAddress aBlank;
155 ScCompiler aComp( getScDocument(), aBlank, eGram );
156 std::unique_ptr<ScTokenArray> pTokens(aComp.CompileString(sFormula));
157 if ( pTokens )
158 {
159 ScRange aRange;
161 if (pTokens->IsValidReference(aRange, aBlank))
162 xUnoRange = new ScCellRangeObj( pDocSh, aRange );
163 else
164 {
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;
172 if ( ScVbaRange::getCellRangesForAddress( nFlags, sFormula, pDocSh, aCellRanges, eConv , ',' ) )
173 {
174 if ( aCellRanges.size() == 1 )
175 xUnoRange = new ScCellRangeObj( pDocSh, aCellRanges.front() );
176 else
177 {
178 uno::Reference< sheet::XSheetCellRangeContainer > xRanges( new ScCellRangesObj( pDocSh, aCellRanges ) );
179 xRange = new ScVbaRange( mxParent, mxContext, xRanges );
180 }
181 }
182
183 }
184 }
185 }
186 }
187
188 if ( xRange.is() || xUnoRange.is() )
189 {
190 if ( !xRange.is() )
191 xRange = new ScVbaRange( mxParent, mxContext, xUnoRange );
192
193 uno::Reference< excel::XRange > xArea( xRange->Areas( uno::Any( sal_Int32(1) ) ), uno::UNO_QUERY );
194
195 uno::Any aAny = xArea->getCellRange() ;
196
197 uno::Reference< sheet::XCellRangeAddressable > thisRangeAdd( aAny, ::uno::UNO_QUERY_THROW);
198
199 table::CellRangeAddress aAddr = thisRangeAdd->getRangeAddress();
200 uno::Any aAny2;
201 if ( mxNames.is() )
202 {
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 )
209 {
210 xArea.set( xRange->Areas( uno::Any( nArea ) ), uno::UNO_QUERY_THROW );
211
212 OUString sRangeAdd = xArea->Address( aAny2, aAny2 , aAny2 , aAny2, aAny2 );
213 if ( nArea > 1 )
214 sTmp.append(",");
215 sTmp.append("'" + xRange->getWorksheet()->getName() + "'." + sRangeAdd);
216 }
217 mxNames->addNewByName( sName, sTmp.makeStringAndClear(), aCellAddr, 0/*nUnoType*/);
218 return Item( uno::Any( sName ), uno::Any() );
219 }
220 }
221 return css::uno::Any();
222}
223
224// XEnumerationAccess
225css::uno::Type
227{
229}
230
231uno::Reference< container::XEnumeration >
233{
234 uno::Reference< container::XEnumerationAccess > xEnumAccess( mxNames, uno::UNO_QUERY_THROW );
235 return new NamesEnumeration( getParent(), mxContext, xEnumAccess->createEnumeration(), mxModel , mxNames );
236}
237
240{
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 ) ) );
243}
244
245OUString
247{
248 return "ScVbaNames";
249}
250
251css::uno::Sequence<OUString>
253{
254 static uno::Sequence< OUString > const aServiceNames
255 {
256 "ooo.vba.excel.NamedRanges"
257 };
258 return aServiceNames;
259}
260
261/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::frame::XModel2 > mxModel
ScRefFlags
Definition: address.hxx:158
css::uno::Reference< css::uno::XComponentContext > mxContext
css::uno::WeakReference< ov::XHelperInterface > mxParent
virtual css::uno::Reference< ov::XHelperInterface > SAL_CALL getParent() override
std::unique_ptr< ScTokenArray > CompileString(const OUString &rFormula)
Tokenize formula expression string into an array of tokens.
Definition: compiler.cxx:4691
static SC_DLLPUBLIC IsNameValidType IsNameValid(const OUString &rName, const ScDocument &rDoc)
Definition: rangenam.cxx:468
ScRange & front()
Definition: rangelst.hxx:92
size_t size() const
Definition: rangelst.hxx:89
ScViewData & GetViewData()
Definition: tabview.hxx:344
css::uno::Reference< css::container::XNameAccess > m_xNameAccess
virtual css::uno::Any SAL_CALL Item(const css::uno::Any &Index1, const css::uno::Any &) override
css::uno::Reference< css::sheet::XNamedRanges > mxNames
Definition: vbanames.hxx:33
ScVbaNames(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext, const css::uno::Reference< css::sheet::XNamedRanges > &xNames, css::uno::Reference< css::frame::XModel > xModel)
Definition: vbanames.cxx:61
virtual css::uno::Any createCollectionObject(const css::uno::Any &aSource) override
Definition: vbanames.cxx:239
css::uno::Reference< css::frame::XModel > mxModel
Definition: vbanames.hxx:32
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
Definition: vbanames.cxx:232
virtual css::uno::Sequence< OUString > getServiceNames() override
Definition: vbanames.cxx:252
ScDocument & getScDocument()
Definition: vbanames.cxx:77
virtual css::uno::Type SAL_CALL getElementType() override
Definition: vbanames.cxx:226
const css::uno::Reference< css::frame::XModel > & getModel() const
Definition: vbanames.hxx:35
virtual ~ScVbaNames() override
Definition: vbanames.cxx:72
virtual OUString getServiceImplName() override
Definition: vbanames.cxx:246
virtual css::uno::Any SAL_CALL Add(const css::uno::Any &aName, const css::uno::Any &aRefersTo, const css::uno::Any &aVisible, const css::uno::Any &aMacroType, const css::uno::Any &aShoutcutKey, const css::uno::Any &aCategory, const css::uno::Any &aNameLocal, const css::uno::Any &aRefersToLocal, const css::uno::Any &aCategoryLocal, const css::uno::Any &aRefersToR1C1, const css::uno::Any &aRefersToR1C1Local) override
Definition: vbanames.cxx:88
static bool getCellRangesForAddress(ScRefFlags &rResFlags, std::u16string_view sAddress, ScDocShell *pDocSh, ScRangeList &rCellRanges, formula::FormulaGrammar::AddressConvention eConv, char cDelimiter)
Definition: vbarange.cxx:1158
ScDocument & GetDocument() const
Definition: viewdata.hxx:380
css::uno::Type const & get()
Reference< frame::XModel > m_xModel
OString sFormula
Sequence< OUString > aServiceNames
OUString sName
sal_Int32 nIndex
Reference
ScDocShell * getDocShell(const css::uno::Reference< css::frame::XModel > &xModel)
ScTabViewShell * getBestViewShell(const css::uno::Reference< css::frame::XModel > &xModel)
Reference< XModel > xModel
OUString Name