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 : #include <com/sun/star/sheet/XCellRangeReferrer.hpp>
24 :
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 "nameuno.hxx"
34 : #include "compiler.hxx"
35 : #include "tokenarray.hxx"
36 :
37 : #include <boost/scoped_ptr.hpp>
38 :
39 : using namespace ::ooo::vba;
40 : using namespace ::com::sun::star;
41 :
42 24 : ScVbaName::ScVbaName(const css::uno::Reference< ov::XHelperInterface >& xParent,
43 : const css::uno::Reference< css::uno::XComponentContext >& xContext,
44 : const css::uno::Reference< css::sheet::XNamedRange >& xName,
45 : const css::uno::Reference< css::sheet::XNamedRanges >& xNames,
46 : const css::uno::Reference< css::frame::XModel >& xModel ):
47 : NameImpl_BASE( xParent , xContext ),
48 : mxModel( xModel ),
49 : mxNamedRange( xName ),
50 24 : mxNames( xNames )
51 : {
52 24 : }
53 :
54 48 : ScVbaName::~ScVbaName()
55 : {
56 48 : }
57 :
58 : css::uno::Reference< ov::excel::XWorksheet >
59 0 : ScVbaName::getWorkSheet() throw (css::uno::RuntimeException)
60 : {
61 0 : uno::Reference< excel::XApplication > xApplication( Application(), uno::UNO_QUERY_THROW );
62 0 : return xApplication->getActiveSheet();
63 : }
64 :
65 : OUString
66 6 : ScVbaName::getName() throw (css::uno::RuntimeException, std::exception)
67 : {
68 6 : return mxNamedRange->getName();
69 : }
70 :
71 : void
72 0 : ScVbaName::setName( const OUString & rName ) throw (css::uno::RuntimeException, std::exception)
73 : {
74 0 : mxNamedRange->setName( rName );
75 0 : }
76 :
77 : OUString
78 0 : ScVbaName::getNameLocal() throw (css::uno::RuntimeException, std::exception)
79 : {
80 0 : return getName();
81 : }
82 :
83 : void
84 0 : ScVbaName::setNameLocal( const OUString & rName ) throw (css::uno::RuntimeException, std::exception)
85 : {
86 0 : setName( rName );
87 0 : }
88 :
89 : sal_Bool
90 0 : ScVbaName::getVisible() throw (css::uno::RuntimeException, std::exception)
91 : {
92 0 : return true;
93 : }
94 :
95 : void
96 0 : ScVbaName::setVisible( sal_Bool /*bVisible*/ ) throw (css::uno::RuntimeException, std::exception)
97 : {
98 0 : }
99 :
100 14 : OUString ScVbaName::getContent( const formula::FormulaGrammar::Grammar eGrammar, bool bPrependEquals )
101 : {
102 14 : ScNamedRangeObj* pNamedRange = dynamic_cast< ScNamedRangeObj* >( mxNamedRange.get() );
103 14 : OUString aContent;
104 14 : if ( pNamedRange )
105 : {
106 14 : ScRangeData* pData = pNamedRange->GetRangeData_Impl();
107 14 : if (pData)
108 14 : pData->GetSymbol( aContent, eGrammar );
109 : }
110 14 : if ( bPrependEquals )
111 : {
112 14 : if (aContent.indexOf('=') != 0)
113 14 : aContent = "=" + aContent;
114 : }
115 14 : return aContent;
116 : }
117 :
118 2 : void ScVbaName::setContent( const OUString& rContent, const formula::FormulaGrammar::Grammar eGrammar, bool bRemoveEquals )
119 : {
120 2 : OUString sContent( rContent );
121 2 : if ( bRemoveEquals )
122 : {
123 2 : if (sContent.startsWith("="))
124 2 : sContent = sContent.copy(1);
125 : }
126 2 : ScNamedRangeObj* pNamedRange = dynamic_cast< ScNamedRangeObj* >( mxNamedRange.get() );
127 :
128 : // We should be able to do the below by just setting calling SetCode on pNamedRange
129 : // right?
130 2 : if ( pNamedRange && pNamedRange->pDocShell )
131 : {
132 :
133 2 : ScDocument& rDoc = pNamedRange->pDocShell->GetDocument();
134 2 : ScRangeData* pOldData = pNamedRange->GetRangeData_Impl();
135 2 : if (pOldData)
136 : {
137 : // Shorter way of doing this ?
138 2 : ScCompiler aComp( &rDoc, pOldData->GetPos() );
139 2 : aComp.SetGrammar( eGrammar );
140 4 : boost::scoped_ptr<ScTokenArray> pArray(aComp.CompileString(sContent));
141 4 : pOldData->SetCode(*pArray);
142 : }
143 2 : }
144 2 : }
145 :
146 : OUString
147 8 : ScVbaName::getValue() throw (css::uno::RuntimeException, std::exception)
148 : {
149 8 : rtl::OUString sResult = getContent( formula::FormulaGrammar::GRAM_NATIVE_XL_A1, true );
150 :
151 8 : return sResult;
152 : }
153 :
154 : void
155 0 : ScVbaName::setValue( const OUString & rValue ) throw (css::uno::RuntimeException, std::exception)
156 : {
157 0 : setContent( rValue, formula::FormulaGrammar::GRAM_NATIVE_XL_A1, true );
158 0 : }
159 :
160 : OUString
161 8 : ScVbaName::getRefersTo() throw (css::uno::RuntimeException, std::exception)
162 : {
163 8 : return getValue();
164 : }
165 :
166 : void
167 0 : ScVbaName::setRefersTo( const OUString & rRefersTo ) throw (css::uno::RuntimeException, std::exception)
168 : {
169 0 : setValue( rRefersTo );
170 0 : }
171 :
172 : OUString
173 0 : ScVbaName::getRefersToLocal() throw (css::uno::RuntimeException, std::exception)
174 : {
175 0 : return getRefersTo();
176 : }
177 :
178 : void
179 0 : ScVbaName::setRefersToLocal( const OUString & rRefersTo ) throw (css::uno::RuntimeException, std::exception)
180 : {
181 0 : setRefersTo( rRefersTo );
182 0 : }
183 :
184 : OUString
185 6 : ScVbaName::getRefersToR1C1() throw (css::uno::RuntimeException, std::exception)
186 : {
187 6 : rtl::OUString sResult = getContent( formula::FormulaGrammar::GRAM_NATIVE_XL_R1C1, true );
188 6 : return sResult;
189 : }
190 :
191 : void
192 2 : ScVbaName::setRefersToR1C1( const OUString & rRefersTo ) throw (css::uno::RuntimeException, std::exception)
193 : {
194 2 : setContent( rRefersTo, formula::FormulaGrammar::GRAM_NATIVE_XL_R1C1, true );
195 2 : }
196 :
197 : OUString
198 0 : ScVbaName::getRefersToR1C1Local() throw (css::uno::RuntimeException, std::exception)
199 : {
200 0 : return getRefersToR1C1();
201 : }
202 :
203 : void
204 0 : ScVbaName::setRefersToR1C1Local( const OUString & rRefersTo ) throw (css::uno::RuntimeException, std::exception)
205 : {
206 0 : setRefersTo( rRefersTo );
207 0 : }
208 :
209 : css::uno::Reference< ov::excel::XRange >
210 6 : ScVbaName::getRefersToRange() throw (css::uno::RuntimeException, std::exception)
211 : {
212 : uno::Reference< ov::excel::XRange > xRange = ScVbaRange::getRangeObjectForName(
213 6 : mxContext, mxNamedRange->getName(), excel::getDocShell( mxModel ), formula::FormulaGrammar::CONV_XL_R1C1 );
214 6 : return xRange;
215 : }
216 :
217 : void
218 0 : ScVbaName::setRefersToRange( const css::uno::Reference< ov::excel::XRange > /*rRange*/ ) throw (css::uno::RuntimeException)
219 : {
220 0 : }
221 :
222 : void
223 10 : ScVbaName::Delete() throw (css::uno::RuntimeException, std::exception)
224 : {
225 10 : mxNames->removeByName( mxNamedRange->getName() );
226 10 : }
227 :
228 : OUString
229 0 : ScVbaName::getServiceImplName()
230 : {
231 0 : return OUString( "ScVbaName" );
232 : }
233 :
234 : uno::Sequence< OUString >
235 0 : ScVbaName::getServiceNames()
236 : {
237 0 : static uno::Sequence< OUString > aServiceNames;
238 0 : if ( aServiceNames.getLength() == 0 )
239 : {
240 0 : aServiceNames.realloc( 1 );
241 0 : aServiceNames[ 0 ] = "ooo.vba.excel.Name";
242 : }
243 0 : return aServiceNames;
244 12 : }
245 :
246 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|