Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : : #include <vbahelper/helperdecl.hxx>
29 : :
30 : : #include <com/sun/star/table/XCellRange.hpp>
31 : : #include <com/sun/star/sheet/XCellRangeAddressable.hpp>
32 : :
33 : : #include "vbanames.hxx"
34 : : #include "vbaname.hxx"
35 : : #include "vbarange.hxx"
36 : : #include "vbaglobals.hxx"
37 : : #include <vector>
38 : : #include <rangenam.hxx>
39 : : #include <vcl/msgbox.hxx>
40 : : #include "tabvwsh.hxx"
41 : : #include "viewdata.hxx"
42 : :
43 : : using namespace ::ooo::vba;
44 : : using namespace ::com::sun::star;
45 : :
46 [ # # ][ # # ]: 0 : class NamesEnumeration : public EnumerationHelperImpl
47 : : {
48 : : uno::Reference< frame::XModel > m_xModel;
49 : : uno::WeakReference< XHelperInterface > m_xParent;
50 : : uno::Reference< sheet::XNamedRanges > m_xNames;
51 : : public:
52 [ # # ]: 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 ) {}
53 : :
54 : 0 : virtual uno::Any SAL_CALL nextElement( ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
55 : : {
56 [ # # ][ # # ]: 0 : uno::Reference< sheet::XNamedRange > xNamed( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
[ # # ]
57 [ # # ][ # # ]: 0 : return uno::makeAny( uno::Reference< excel::XName > ( new ScVbaName( m_xParent, m_xContext, xNamed ,m_xNames , m_xModel ) ) );
[ # # ][ # # ]
[ # # ]
58 : : }
59 : :
60 : : };
61 : :
62 : :
63 : 0 : ScVbaNames::ScVbaNames(const css::uno::Reference< ov::XHelperInterface >& xParent,
64 : : const css::uno::Reference< css::uno::XComponentContext >& xContext,
65 : : const css::uno::Reference< css::sheet::XNamedRanges >& xNames,
66 : : const css::uno::Reference< css::frame::XModel >& xModel ):
67 : : ScVbaNames_BASE( xParent , xContext , uno::Reference< container::XIndexAccess >( xNames, uno::UNO_QUERY ) ),
68 : : mxModel( xModel ),
69 [ # # ]: 0 : mxNames( xNames )
70 : : {
71 [ # # ]: 0 : m_xNameAccess.set( xNames, uno::UNO_QUERY_THROW );
72 : 0 : }
73 : :
74 : 0 : ScVbaNames::~ScVbaNames()
75 : : {
76 [ # # ]: 0 : }
77 : :
78 : : ScDocument *
79 : 0 : ScVbaNames::getScDocument()
80 : : {
81 [ # # ][ # # ]: 0 : uno::Reference< frame::XModel > xModel( getModel() , uno::UNO_QUERY_THROW );
82 [ # # ]: 0 : ScTabViewShell * pTabViewShell = excel::getBestViewShell( xModel );
83 [ # # ]: 0 : if ( !pTabViewShell )
84 [ # # ][ # # ]: 0 : throw uno::RuntimeException( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("No ViewShell available")), uno::Reference< uno::XInterface >() );
85 : 0 : ScViewData* pViewData = pTabViewShell->GetViewData();
86 [ # # ]: 0 : if ( !pViewData )
87 [ # # ][ # # ]: 0 : throw uno::RuntimeException( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("No ViewData available")), uno::Reference< uno::XInterface >() );
88 [ # # ]: 0 : return pViewData->GetDocument();
89 : : }
90 : :
91 : 0 : void GetRangeOrRefersTo( const css::uno::Any& RefersTo, const uno::Reference< uno::XComponentContext >& xContext, css::uno::Reference< excel::XRange >& xRange, rtl::OUString& sRefersTo )
92 : : {
93 [ # # ]: 0 : if ( RefersTo.getValueTypeClass() == uno::TypeClass_STRING )
94 : : {
95 : 0 : RefersTo >>= sRefersTo;
96 : : }
97 [ # # ]: 0 : else if ( RefersTo.getValueTypeClass() == uno::TypeClass_INTERFACE )
98 : : {
99 : 0 : RefersTo >>= xRange;
100 : : }
101 [ # # ]: 0 : else if ( RefersTo.hasValue() )
102 : : {
103 [ # # ]: 0 : uno::Reference< script::XTypeConverter > xConverter = getTypeConverter( xContext );
104 : : try
105 : : {
106 [ # # ]: 0 : if ( xConverter.is() )
107 : : {
108 [ # # ][ # # ]: 0 : uno::Any aConverted = xConverter->convertTo( RefersTo, getCppuType((rtl::OUString*)0) );
[ # # ]
109 : 0 : aConverted >>= sRefersTo;
110 : : }
111 : : }
112 [ # # ]: 0 : catch( uno::Exception& )
113 : : {
114 : 0 : }
115 : : }
116 [ # # ]: 0 : }
117 : :
118 : : css::uno::Any
119 : 0 : ScVbaNames::Add( const css::uno::Any& Name ,
120 : : const css::uno::Any& RefersTo,
121 : : const css::uno::Any& /*Visible*/,
122 : : const css::uno::Any& /*MacroType*/,
123 : : const css::uno::Any& /*ShoutcutKey*/,
124 : : const css::uno::Any& /*Category*/,
125 : : const css::uno::Any& NameLocal,
126 : : const css::uno::Any& /*RefersToLocal*/,
127 : : const css::uno::Any& /*CategoryLocal*/,
128 : : const css::uno::Any& RefersToR1C1,
129 : : const css::uno::Any& RefersToR1C1Local ) throw (css::uno::RuntimeException)
130 : : {
131 : 0 : rtl::OUString sSheetName;
132 : 0 : rtl::OUString sName;
133 : 0 : rtl::OUString sRefersTo;
134 : 0 : uno::Reference< excel::XRange > xRange;
135 [ # # ]: 0 : if ( Name.hasValue() )
136 : 0 : Name >>= sName;
137 [ # # ]: 0 : else if ( NameLocal.hasValue() )
138 : 0 : NameLocal >>= sName;
139 [ # # ]: 0 : if ( !sName.isEmpty() )
140 : : {
141 : 0 : sal_Int32 nTokenIndex = sName.indexOf('!');
142 [ # # ]: 0 : if ( nTokenIndex >= 0 )
143 : : {
144 : 0 : sSheetName = sName.copy( 0, nTokenIndex );
145 : 0 : sName = sName.copy( nTokenIndex + 1 );
146 : : }
147 [ # # ][ # # ]: 0 : if ( !ScRangeData::IsNameValid( sName , getScDocument() ) )
[ # # ][ # # ]
[ # # ]
148 : : {
149 : 0 : ::rtl::OUString sResult ;
150 : 0 : sal_Int32 nToken = 0;
151 : 0 : sal_Int32 nIndex = 0;
152 : 0 : sResult = sName.getToken( nToken , '!' , nIndex );
153 [ # # ]: 0 : if ( -1 == nIndex )
154 : 0 : sResult = sName;
155 : : else
156 : 0 : sResult = sName.copy( nIndex );
157 : 0 : sName = sResult ;
158 [ # # ][ # # ]: 0 : if ( !ScRangeData::IsNameValid( sName , getScDocument() ) )
[ # # ][ # # ]
[ # # ]
159 [ # # ][ # # ]: 0 : throw uno::RuntimeException( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("This Name is a invalid .")), uno::Reference< uno::XInterface >() );
160 : : }
161 : : }
162 [ # # ][ # # ]: 0 : if ( RefersTo.hasValue() || RefersToR1C1.hasValue() || RefersToR1C1Local.hasValue() )
[ # # ][ # # ]
163 : : {
164 [ # # ]: 0 : if ( RefersTo.hasValue() )
165 [ # # ]: 0 : GetRangeOrRefersTo( RefersTo, mxContext, xRange, sRefersTo );
166 [ # # ]: 0 : if ( RefersToR1C1.hasValue() )
167 [ # # ]: 0 : GetRangeOrRefersTo( RefersToR1C1, mxContext, xRange, sRefersTo );
168 [ # # ]: 0 : if ( RefersToR1C1Local.hasValue() )
169 [ # # ]: 0 : GetRangeOrRefersTo( RefersToR1C1Local, mxContext, xRange, sRefersTo );
170 : : }
171 : :
172 [ # # ]: 0 : String aContent;
173 : 0 : table::CellAddress aPosition;
174 : 0 : RangeType nType = RT_NAME;
175 [ # # ]: 0 : if ( xRange.is() )
176 : : {
177 [ # # ][ # # ]: 0 : ScVbaRange* pRange = dynamic_cast< ScVbaRange* >( xRange.get() );
178 : 0 : uno::Reference< table::XCellRange > thisRange ;
179 [ # # ]: 0 : uno::Any xAny = pRange->getCellRange() ;
180 [ # # ]: 0 : if ( xAny.hasValue() )
181 [ # # ]: 0 : xAny >>= thisRange;
182 [ # # ]: 0 : uno::Reference< sheet::XCellRangeAddressable > thisRangeAdd( thisRange, ::uno::UNO_QUERY_THROW);
183 [ # # ][ # # ]: 0 : table::CellRangeAddress aAddr = thisRangeAdd->getRangeAddress();
184 : 0 : ScAddress aPos( static_cast< SCCOL >( aAddr.StartColumn ) , static_cast< SCROW >( aAddr.StartRow ) , static_cast< SCTAB >(aAddr.Sheet ) );
185 : 0 : uno::Any xAny2 ;
186 [ # # ][ # # ]: 0 : String sRangeAdd = xRange->Address( xAny2, xAny2 , xAny2 , xAny2, xAny2 );
[ # # ]
187 [ # # ]: 0 : aContent += rtl::OUString("$");
188 [ # # ][ # # ]: 0 : aContent += UniString(xRange->getWorksheet()->getName());
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
189 [ # # ]: 0 : aContent += rtl::OUString(".");
190 [ # # ]: 0 : aContent += sRangeAdd;
191 [ # # ]: 0 : aPosition = table::CellAddress( aAddr.Sheet , aAddr.StartColumn , aAddr.StartRow );
192 : : }
193 : : else
194 : : {
195 [ # # ]: 0 : ScDocShell* pDocShell = excel::getDocShell( mxModel );
196 [ # # ]: 0 : ScDocument* pDoc = pDocShell ? pDocShell->GetDocument() : NULL;
197 [ # # ][ # # ]: 0 : excel::CompileExcelFormulaToODF( pDoc, sRefersTo, aContent );
[ # # ]
198 [ # # ]: 0 : if ( aContent.Len() == 0 )
199 : : {
200 [ # # ]: 0 : aContent = sRefersTo;
201 : : }
202 : : }
203 : :
204 : 0 : uno::Reference< sheet::XNamedRange > xNewNamedRange;
205 [ # # ]: 0 : if ( mxNames.is() )
206 : : {
207 [ # # ][ # # ]: 0 : if ( mxNames->hasByName( sName ) )
[ # # ]
208 : : {
209 [ # # ][ # # ]: 0 : mxNames->removeByName( sName );
210 : : }
211 [ # # ][ # # ]: 0 : mxNames->addNewByName( sName, rtl::OUString( aContent ), aPosition, (sal_Int32) nType );
[ # # ]
212 [ # # ][ # # ]: 0 : xNewNamedRange = uno::Reference< sheet::XNamedRange >( mxNames->getByName( sName ), uno::UNO_QUERY );
[ # # ][ # # ]
213 : : }
214 [ # # ]: 0 : if ( xNewNamedRange.is() )
215 : : {
216 [ # # ][ # # ]: 0 : return uno::makeAny( uno::Reference< excel::XName >( new ScVbaName( mxParent, mxContext, xNewNamedRange ,mxNames , mxModel ) ) );
[ # # ][ # # ]
[ # # ]
217 : : }
218 [ # # ]: 0 : return css::uno::Any();
219 : : }
220 : :
221 : : // XEnumerationAccess
222 : : css::uno::Type
223 : 0 : ScVbaNames::getElementType() throw( css::uno::RuntimeException )
224 : : {
225 : 0 : return ov::excel::XName::static_type(0);
226 : : }
227 : :
228 : : uno::Reference< container::XEnumeration >
229 : 0 : ScVbaNames::createEnumeration() throw (uno::RuntimeException)
230 : : {
231 [ # # ]: 0 : uno::Reference< container::XEnumerationAccess > xEnumAccess( mxNames, uno::UNO_QUERY_THROW );
232 [ # # ][ # # ]: 0 : return new NamesEnumeration( getParent(), mxContext, xEnumAccess->createEnumeration(), mxModel , mxNames );
[ # # ][ # # ]
[ # # ][ # # ]
233 : : }
234 : :
235 : : uno::Any
236 : 0 : ScVbaNames::createCollectionObject( const uno::Any& aSource )
237 : : {
238 [ # # ]: 0 : uno::Reference< sheet::XNamedRange > xName( aSource, uno::UNO_QUERY );
239 [ # # ][ # # ]: 0 : return uno::makeAny( uno::Reference< excel::XName > ( new ScVbaName( getParent(), mxContext, xName, mxNames , mxModel ) ) );
[ # # ][ # # ]
[ # # ]
240 : : }
241 : :
242 : : rtl::OUString
243 : 0 : ScVbaNames::getServiceImplName()
244 : : {
245 : 0 : return rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ScVbaNames"));
246 : : }
247 : :
248 : : css::uno::Sequence<rtl::OUString>
249 : 0 : ScVbaNames::getServiceNames()
250 : : {
251 [ # # ][ # # ]: 0 : static uno::Sequence< rtl::OUString > aServiceNames;
[ # # ][ # # ]
252 [ # # ]: 0 : if ( aServiceNames.getLength() == 0 )
253 : : {
254 : 0 : aServiceNames.realloc( 1 );
255 [ # # ]: 0 : aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.NamedRanges" ) );
256 : : }
257 : 0 : return aServiceNames;
258 : : }
259 : :
260 : :
261 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|