Line data Source code
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 : #include <vbahelper/helperdecl.hxx>
20 :
21 : #include <com/sun/star/table/XCellRange.hpp>
22 : #include <com/sun/star/sheet/XCellRangeAddressable.hpp>
23 :
24 : #include "vbanames.hxx"
25 : #include "vbaname.hxx"
26 : #include "vbarange.hxx"
27 : #include "vbaglobals.hxx"
28 : #include <vector>
29 : #include <rangenam.hxx>
30 : #include <vcl/msgbox.hxx>
31 : #include "tabvwsh.hxx"
32 : #include "viewdata.hxx"
33 : #include "compiler.hxx"
34 : #include "tokenarray.hxx"
35 : #include "cellsuno.hxx"
36 :
37 : #include <boost/scoped_ptr.hpp>
38 :
39 : using namespace ::ooo::vba;
40 : using namespace ::com::sun::star;
41 :
42 0 : class NamesEnumeration : public EnumerationHelperImpl
43 : {
44 : uno::Reference< frame::XModel > m_xModel;
45 : uno::WeakReference< XHelperInterface > m_xParent;
46 : uno::Reference< sheet::XNamedRanges > m_xNames;
47 : public:
48 0 : 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 ) throw ( uno::RuntimeException ) : EnumerationHelperImpl( xParent, xContext, xEnumeration ), m_xModel( xModel ), m_xParent( xParent ), m_xNames( xNames ) {}
49 :
50 0 : virtual uno::Any SAL_CALL nextElement( ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception) SAL_OVERRIDE
51 : {
52 0 : uno::Reference< sheet::XNamedRange > xNamed( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
53 0 : return uno::makeAny( uno::Reference< excel::XName > ( new ScVbaName( m_xParent, m_xContext, xNamed ,m_xNames , m_xModel ) ) );
54 : }
55 :
56 : };
57 :
58 :
59 0 : ScVbaNames::ScVbaNames(const css::uno::Reference< ov::XHelperInterface >& xParent,
60 : const css::uno::Reference< css::uno::XComponentContext >& xContext,
61 : const css::uno::Reference< css::sheet::XNamedRanges >& xNames,
62 : const css::uno::Reference< css::frame::XModel >& xModel ):
63 : ScVbaNames_BASE( xParent , xContext , uno::Reference< container::XIndexAccess >( xNames, uno::UNO_QUERY ) ),
64 : mxModel( xModel ),
65 0 : mxNames( xNames )
66 : {
67 0 : m_xNameAccess.set( xNames, uno::UNO_QUERY_THROW );
68 0 : }
69 :
70 0 : ScVbaNames::~ScVbaNames()
71 : {
72 0 : }
73 :
74 : ScDocument *
75 0 : ScVbaNames::getScDocument()
76 : {
77 0 : uno::Reference< frame::XModel > xModel( getModel() , uno::UNO_QUERY_THROW );
78 0 : ScTabViewShell * pTabViewShell = excel::getBestViewShell( xModel );
79 0 : if ( !pTabViewShell )
80 0 : throw uno::RuntimeException( "No ViewShell available", uno::Reference< uno::XInterface >() );
81 0 : ScViewData* pViewData = pTabViewShell->GetViewData();
82 0 : if ( !pViewData )
83 0 : throw uno::RuntimeException( "No ViewData available", uno::Reference< uno::XInterface >() );
84 0 : return pViewData->GetDocument();
85 : }
86 :
87 :
88 : css::uno::Any
89 0 : ScVbaNames::Add( const css::uno::Any& Name ,
90 : const css::uno::Any& RefersTo,
91 : const css::uno::Any& /*Visible*/,
92 : const css::uno::Any& /*MacroType*/,
93 : const css::uno::Any& /*ShoutcutKey*/,
94 : const css::uno::Any& /*Category*/,
95 : const css::uno::Any& NameLocal,
96 : const css::uno::Any& /*RefersToLocal*/,
97 : const css::uno::Any& /*CategoryLocal*/,
98 : const css::uno::Any& RefersToR1C1,
99 : const css::uno::Any& RefersToR1C1Local )
100 : throw (css::uno::RuntimeException, std::exception)
101 : {
102 0 : OUString sName;
103 0 : uno::Reference< excel::XRange > xRange;
104 0 : if ( Name.hasValue() )
105 0 : Name >>= sName;
106 0 : else if ( NameLocal.hasValue() )
107 0 : NameLocal >>= sName;
108 0 : if ( !sName.isEmpty() )
109 : {
110 0 : if ( !ScRangeData::IsNameValid( sName , getScDocument() ) )
111 : {
112 0 : OUString sResult ;
113 0 : sal_Int32 nToken = 0;
114 0 : sal_Int32 nIndex = 0;
115 0 : sResult = sName.getToken( nToken , '!' , nIndex );
116 0 : if ( -1 == nIndex )
117 0 : sResult = sName;
118 : else
119 0 : sResult = sName.copy( nIndex );
120 0 : sName = sResult ;
121 0 : if ( !ScRangeData::IsNameValid( sName , getScDocument() ) )
122 : throw uno::RuntimeException( "This Name is not valid .",
123 0 : uno::Reference< uno::XInterface >() );
124 : }
125 : }
126 0 : uno::Reference< table::XCellRange > xUnoRange;
127 0 : if ( RefersTo.hasValue() || RefersToR1C1.hasValue() || RefersToR1C1Local.hasValue() )
128 : {
129 0 : OUString sFormula;
130 :
131 0 : formula::FormulaGrammar::Grammar eGram = formula::FormulaGrammar::GRAM_NATIVE_XL_A1;
132 0 : if ( RefersTo.hasValue() )
133 : {
134 0 : if ( RefersTo.getValueTypeClass() == uno::TypeClass_STRING )
135 0 : RefersTo >>= sFormula;
136 : else
137 0 : RefersTo >>= xRange;
138 : }
139 0 : if ( RefersToR1C1.hasValue() )
140 : {
141 0 : if ( RefersToR1C1.getValueTypeClass() == uno::TypeClass_STRING )
142 : {
143 0 : RefersToR1C1 >>= sFormula;
144 0 : eGram = formula::FormulaGrammar::GRAM_NATIVE_XL_R1C1;
145 : }
146 : else
147 0 : RefersToR1C1 >>= xRange;
148 : }
149 0 : if ( RefersToR1C1Local.hasValue() )
150 : {
151 0 : if ( RefersToR1C1Local.getValueTypeClass() == uno::TypeClass_STRING )
152 : {
153 0 : RefersToR1C1Local >>= sFormula;
154 0 : eGram = formula::FormulaGrammar::GRAM_NATIVE_XL_R1C1;
155 : }
156 : else
157 0 : RefersToR1C1Local >>= xRange;
158 : }
159 0 : if ( !xRange.is() && !sFormula.isEmpty() )
160 : {
161 0 : ScAddress aBlank;
162 0 : ScCompiler aComp( getScDocument(), aBlank );
163 0 : aComp.SetGrammar( eGram );
164 0 : boost::scoped_ptr<ScTokenArray> pTokens(aComp.CompileString(sFormula));
165 0 : if ( pTokens )
166 : {
167 0 : ScRange aRange;
168 0 : ScDocShell* pDocSh = excel::getDocShell(getModel());
169 0 : if (pTokens->IsValidReference(aRange, aBlank))
170 0 : xUnoRange = new ScCellRangeObj( pDocSh, aRange );
171 : else
172 : {
173 : // assume it's an address try strip the '=' if it's there
174 : // and try and create a range ( must be a better way )
175 0 : if ( sFormula.startsWith("=") )
176 0 : sFormula = sFormula.copy(1);
177 0 : ScRangeList aCellRanges;
178 0 : sal_uInt16 nFlags = 0;
179 :
180 0 : formula::FormulaGrammar::AddressConvention eConv = ( eGram == formula::FormulaGrammar::GRAM_NATIVE_XL_A1 ) ? formula::FormulaGrammar::CONV_XL_A1 : formula::FormulaGrammar::CONV_XL_R1C1;
181 0 : if ( ScVbaRange::getCellRangesForAddress( nFlags, sFormula, pDocSh, aCellRanges, eConv , ',' ) )
182 : {
183 0 : if ( aCellRanges.size() == 1 )
184 0 : xUnoRange = new ScCellRangeObj( pDocSh, *aCellRanges.front() );
185 : else
186 : {
187 0 : uno::Reference< sheet::XSheetCellRangeContainer > xRanges( new ScCellRangesObj( pDocSh, aCellRanges ) );
188 0 : xRange = new ScVbaRange( mxParent, mxContext, xRanges );
189 : }
190 0 : }
191 :
192 : }
193 0 : }
194 0 : }
195 : }
196 :
197 0 : if ( xRange.is() || xUnoRange.is() )
198 : {
199 0 : if ( !xRange.is() )
200 0 : xRange = new ScVbaRange( mxParent, mxContext, xUnoRange );
201 :
202 0 : uno::Reference< excel::XRange > xArea( xRange->Areas( uno::makeAny( sal_Int32(1) ) ), uno::UNO_QUERY );
203 :
204 0 : uno::Any xAny = xArea->getCellRange() ;
205 :
206 0 : uno::Reference< sheet::XCellRangeAddressable > thisRangeAdd( xAny, ::uno::UNO_QUERY_THROW);
207 :
208 0 : table::CellRangeAddress aAddr = thisRangeAdd->getRangeAddress();
209 0 : ScAddress aPos( static_cast< SCCOL >( aAddr.StartColumn ) , static_cast< SCROW >( aAddr.StartRow ) , static_cast< SCTAB >(aAddr.Sheet ) );
210 0 : uno::Any xAny2 ;
211 0 : if ( mxNames.is() )
212 : {
213 0 : RangeType nType = RT_NAME;
214 0 : table::CellAddress aCellAddr( aAddr.Sheet , aAddr.StartColumn , aAddr.StartRow );
215 0 : if ( mxNames->hasByName( sName ) )
216 0 : mxNames->removeByName(sName);
217 0 : OUString sTmp = "$";
218 0 : uno::Reference< ov::XCollection > xCol( xRange->Areas( uno::Any() ), uno::UNO_QUERY );
219 0 : for ( sal_Int32 nArea = 1; nArea <= xCol->getCount(); ++nArea )
220 : {
221 0 : xArea.set( xRange->Areas( uno::makeAny( nArea ) ), uno::UNO_QUERY_THROW );
222 :
223 0 : OUString sRangeAdd = xArea->Address( xAny2, xAny2 , xAny2 , xAny2, xAny2 );
224 0 : if ( nArea > 1 )
225 0 : sTmp += ",";
226 0 : sTmp = sTmp + "'" + xRange->getWorksheet()->getName() + "'." + sRangeAdd;
227 0 : }
228 0 : mxNames->addNewByName( sName , sTmp , aCellAddr , (sal_Int32)nType);
229 0 : return Item( uno::makeAny( sName ), uno::Any() );
230 0 : }
231 : }
232 0 : return css::uno::Any();
233 : }
234 :
235 : // XEnumerationAccess
236 : css::uno::Type
237 0 : ScVbaNames::getElementType() throw( css::uno::RuntimeException )
238 : {
239 0 : return cppu::UnoType<ov::excel::XName>::get();
240 : }
241 :
242 : uno::Reference< container::XEnumeration >
243 0 : ScVbaNames::createEnumeration() throw (uno::RuntimeException)
244 : {
245 0 : uno::Reference< container::XEnumerationAccess > xEnumAccess( mxNames, uno::UNO_QUERY_THROW );
246 0 : return new NamesEnumeration( getParent(), mxContext, xEnumAccess->createEnumeration(), mxModel , mxNames );
247 : }
248 :
249 : uno::Any
250 0 : ScVbaNames::createCollectionObject( const uno::Any& aSource )
251 : {
252 0 : uno::Reference< sheet::XNamedRange > xName( aSource, uno::UNO_QUERY );
253 0 : return uno::makeAny( uno::Reference< excel::XName > ( new ScVbaName( getParent(), mxContext, xName, mxNames , mxModel ) ) );
254 : }
255 :
256 : OUString
257 0 : ScVbaNames::getServiceImplName()
258 : {
259 0 : return OUString( "ScVbaNames" );
260 : }
261 :
262 : css::uno::Sequence<OUString>
263 0 : ScVbaNames::getServiceNames()
264 : {
265 0 : static uno::Sequence< OUString > aServiceNames;
266 0 : if ( aServiceNames.getLength() == 0 )
267 : {
268 0 : aServiceNames.realloc( 1 );
269 0 : aServiceNames[ 0 ] = "ooo.vba.excel.NamedRanges";
270 : }
271 0 : return aServiceNames;
272 0 : }
273 :
274 :
275 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|