nss: upgrade to release 3.73
[LibreOffice.git] / sc / source / ui / vba / vbanames.cxx
blob79a7a80ab68c59926538cc736ef0611fa1807bc9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
35 #include <memory>
37 using namespace ::ooo::vba;
38 using namespace ::com::sun::star;
40 namespace {
42 class NamesEnumeration : public EnumerationHelperImpl
44 uno::Reference< frame::XModel > m_xModel;
45 uno::Reference< sheet::XNamedRanges > m_xNames;
46 public:
47 /// @throws uno::RuntimeException
48 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 ) {}
50 virtual uno::Any SAL_CALL nextElement( ) override
52 uno::Reference< sheet::XNamedRange > xNamed( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
53 return uno::makeAny( uno::Reference< excel::XName > ( new ScVbaName( m_xParent, m_xContext, xNamed ,m_xNames , m_xModel ) ) );
60 ScVbaNames::ScVbaNames(const css::uno::Reference< ov::XHelperInterface >& xParent,
61 const css::uno::Reference< css::uno::XComponentContext >& xContext,
62 const css::uno::Reference< css::sheet::XNamedRanges >& xNames,
63 const css::uno::Reference< css::frame::XModel >& xModel ):
64 ScVbaNames_BASE( xParent , xContext , uno::Reference< container::XIndexAccess >( xNames, uno::UNO_QUERY ) ),
65 mxModel( xModel ),
66 mxNames( xNames )
68 m_xNameAccess.set( xNames, uno::UNO_QUERY_THROW );
71 ScVbaNames::~ScVbaNames()
75 ScDocument&
76 ScVbaNames::getScDocument()
78 uno::Reference< frame::XModel > xModel( getModel() , uno::UNO_SET_THROW );
79 ScTabViewShell * pTabViewShell = excel::getBestViewShell( xModel );
80 if ( !pTabViewShell )
81 throw uno::RuntimeException( "No ViewShell available" );
82 ScViewData& rViewData = pTabViewShell->GetViewData();
83 return rViewData.GetDocument();
86 css::uno::Any
87 ScVbaNames::Add( const css::uno::Any& Name ,
88 const css::uno::Any& RefersTo,
89 const css::uno::Any& /*Visible*/,
90 const css::uno::Any& /*MacroType*/,
91 const css::uno::Any& /*ShoutcutKey*/,
92 const css::uno::Any& /*Category*/,
93 const css::uno::Any& NameLocal,
94 const css::uno::Any& /*RefersToLocal*/,
95 const css::uno::Any& /*CategoryLocal*/,
96 const css::uno::Any& RefersToR1C1,
97 const css::uno::Any& RefersToR1C1Local )
99 OUString sName;
100 uno::Reference< excel::XRange > xRange;
101 if ( Name.hasValue() )
102 Name >>= sName;
103 else if ( NameLocal.hasValue() )
104 NameLocal >>= sName;
105 if ( !sName.isEmpty() )
107 if ( ScRangeData::IsNameValid( sName, getScDocument() ) != ScRangeData::NAME_VALID )
109 const sal_Int32 nIndex{ sName.indexOf('!') };
110 if (nIndex>=0)
111 sName = sName.copy(nIndex+1);
112 if ( ScRangeData::IsNameValid( sName, getScDocument() ) != ScRangeData::NAME_VALID )
113 throw uno::RuntimeException( "This Name is not valid ." );
116 uno::Reference< table::XCellRange > xUnoRange;
117 if ( RefersTo.hasValue() || RefersToR1C1.hasValue() || RefersToR1C1Local.hasValue() )
119 OUString sFormula;
121 formula::FormulaGrammar::Grammar eGram = formula::FormulaGrammar::GRAM_NATIVE_XL_A1;
122 if ( RefersTo.hasValue() )
124 if ( RefersTo.getValueTypeClass() == uno::TypeClass_STRING )
125 RefersTo >>= sFormula;
126 else
127 RefersTo >>= xRange;
129 if ( RefersToR1C1.hasValue() )
131 if ( RefersToR1C1.getValueTypeClass() == uno::TypeClass_STRING )
133 RefersToR1C1 >>= sFormula;
134 eGram = formula::FormulaGrammar::GRAM_NATIVE_XL_R1C1;
136 else
137 RefersToR1C1 >>= xRange;
139 if ( RefersToR1C1Local.hasValue() )
141 if ( RefersToR1C1Local.getValueTypeClass() == uno::TypeClass_STRING )
143 RefersToR1C1Local >>= sFormula;
144 eGram = formula::FormulaGrammar::GRAM_NATIVE_XL_R1C1;
146 else
147 RefersToR1C1Local >>= xRange;
149 if ( !xRange.is() && !sFormula.isEmpty() )
151 ScAddress aBlank;
152 ScCompiler aComp( getScDocument(), aBlank, eGram );
153 std::unique_ptr<ScTokenArray> pTokens(aComp.CompileString(sFormula));
154 if ( pTokens )
156 ScRange aRange;
157 ScDocShell* pDocSh = excel::getDocShell(getModel());
158 if (pTokens->IsValidReference(aRange, aBlank))
159 xUnoRange = new ScCellRangeObj( pDocSh, aRange );
160 else
162 // assume it's an address try strip the '=' if it's there
163 // and try and create a range ( must be a better way )
164 if ( sFormula.startsWith("=") )
165 sFormula = sFormula.copy(1);
166 ScRangeList aCellRanges;
167 ScRefFlags nFlags = ScRefFlags::ZERO;
168 formula::FormulaGrammar::AddressConvention eConv = ( eGram == formula::FormulaGrammar::GRAM_NATIVE_XL_A1 ) ? formula::FormulaGrammar::CONV_XL_A1 : formula::FormulaGrammar::CONV_XL_R1C1;
169 if ( ScVbaRange::getCellRangesForAddress( nFlags, sFormula, pDocSh, aCellRanges, eConv , ',' ) )
171 if ( aCellRanges.size() == 1 )
172 xUnoRange = new ScCellRangeObj( pDocSh, aCellRanges.front() );
173 else
175 uno::Reference< sheet::XSheetCellRangeContainer > xRanges( new ScCellRangesObj( pDocSh, aCellRanges ) );
176 xRange = new ScVbaRange( mxParent, mxContext, xRanges );
185 if ( xRange.is() || xUnoRange.is() )
187 if ( !xRange.is() )
188 xRange = new ScVbaRange( mxParent, mxContext, xUnoRange );
190 uno::Reference< excel::XRange > xArea( xRange->Areas( uno::makeAny( sal_Int32(1) ) ), uno::UNO_QUERY );
192 uno::Any aAny = xArea->getCellRange() ;
194 uno::Reference< sheet::XCellRangeAddressable > thisRangeAdd( aAny, ::uno::UNO_QUERY_THROW);
196 table::CellRangeAddress aAddr = thisRangeAdd->getRangeAddress();
197 uno::Any aAny2;
198 if ( mxNames.is() )
200 table::CellAddress aCellAddr( aAddr.Sheet , aAddr.StartColumn , aAddr.StartRow );
201 if ( mxNames->hasByName( sName ) )
202 mxNames->removeByName(sName);
203 OUStringBuffer sTmp = "$";
204 uno::Reference< ov::XCollection > xCol( xRange->Areas( uno::Any() ), uno::UNO_QUERY );
205 for ( sal_Int32 nArea = 1; nArea <= xCol->getCount(); ++nArea )
207 xArea.set( xRange->Areas( uno::makeAny( nArea ) ), uno::UNO_QUERY_THROW );
209 OUString sRangeAdd = xArea->Address( aAny2, aAny2 , aAny2 , aAny2, aAny2 );
210 if ( nArea > 1 )
211 sTmp.append(",");
212 sTmp.append("'").append(xRange->getWorksheet()->getName()).append("'.").append(sRangeAdd);
214 mxNames->addNewByName( sName, sTmp.makeStringAndClear(), aCellAddr, 0/*nUnoType*/);
215 return Item( uno::makeAny( sName ), uno::Any() );
218 return css::uno::Any();
221 // XEnumerationAccess
222 css::uno::Type
223 ScVbaNames::getElementType()
225 return cppu::UnoType<ov::excel::XName>::get();
228 uno::Reference< container::XEnumeration >
229 ScVbaNames::createEnumeration()
231 uno::Reference< container::XEnumerationAccess > xEnumAccess( mxNames, uno::UNO_QUERY_THROW );
232 return new NamesEnumeration( getParent(), mxContext, xEnumAccess->createEnumeration(), mxModel , mxNames );
235 uno::Any
236 ScVbaNames::createCollectionObject( const uno::Any& aSource )
238 uno::Reference< sheet::XNamedRange > xName( aSource, uno::UNO_QUERY );
239 return uno::makeAny( uno::Reference< excel::XName > ( new ScVbaName( getParent(), mxContext, xName, mxNames , mxModel ) ) );
242 OUString
243 ScVbaNames::getServiceImplName()
245 return "ScVbaNames";
248 css::uno::Sequence<OUString>
249 ScVbaNames::getServiceNames()
251 static uno::Sequence< OUString > const aServiceNames
253 "ooo.vba.excel.NamedRanges"
255 return aServiceNames;
258 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */