1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include "excelvbahelper.hxx"
#include <basic/basmgr.hxx>
#include <comphelper/processfactory.hxx>
#include <com/sun/star/sheet/XSheetCellRange.hpp>
#include <com/sun/star/sheet/GlobalSheetSettings.hpp>
#include "docuno.hxx"
#include "tabvwsh.hxx"
#include "transobj.hxx"
#include "scmod.hxx"
#include "cellsuno.hxx"
#include <gridwin.hxx>
#include <com/sun/star/script/vba/VBAEventId.hpp>
#include <com/sun/star/script/vba/XVBACompatibility.hpp>
#include <com/sun/star/script/vba/XVBAEventProcessor.hpp>
#include <com/sun/star/script/vba/XVBAModuleInfo.hpp>
#include <com/sun/star/script/ModuleInfo.hpp>
#include <com/sun/star/script/ModuleType.hpp>
using namespace ::com::sun::star;
using namespace ::ooo::vba;
namespace ooo {
namespace vba {
namespace excel {
uno::Reference< sheet::XUnnamedDatabaseRanges >
GetUnnamedDataBaseRanges( const ScDocShell* pShell )
{
uno::Reference< frame::XModel > xModel;
if ( pShell )
xModel.set( pShell->GetModel(), uno::UNO_QUERY_THROW );
uno::Reference< beans::XPropertySet > xModelProps( xModel, uno::UNO_QUERY_THROW );
uno::Reference< sheet::XUnnamedDatabaseRanges > xUnnamedDBRanges( xModelProps->getPropertyValue("UnnamedDatabaseRanges"), uno::UNO_QUERY_THROW );
return xUnnamedDBRanges;
}
// returns the XDatabaseRange for the autofilter on sheet (nSheet)
// also populates sName with the name of range
uno::Reference< sheet::XDatabaseRange >
GetAutoFiltRange( const ScDocShell* pShell, sal_Int16 nSheet )
{
uno::Reference< sheet::XUnnamedDatabaseRanges > xUnnamedDBRanges( GetUnnamedDataBaseRanges( pShell ), uno::UNO_QUERY_THROW );
uno::Reference< sheet::XDatabaseRange > xDataBaseRange;
if (xUnnamedDBRanges->hasByTable( nSheet ) )
{
uno::Reference< sheet::XDatabaseRange > xDBRange( xUnnamedDBRanges->getByTable( nSheet ) , uno::UNO_QUERY_THROW );
bool bHasAuto = false;
uno::Reference< beans::XPropertySet > xProps( xDBRange, uno::UNO_QUERY_THROW );
xProps->getPropertyValue("AutoFilter") >>= bHasAuto;
if ( bHasAuto )
{
xDataBaseRange=xDBRange;
}
}
return xDataBaseRange;
}
ScDocShell* GetDocShellFromRange( const uno::Reference< uno::XInterface >& xRange )
{
ScCellRangesBase* pScCellRangesBase = ScCellRangesBase::getImplementation( xRange );
if ( !pScCellRangesBase )
{
throw uno::RuntimeException("Failed to access underlying doc shell uno range object" );
}
return pScCellRangesBase->GetDocShell();
}
uno::Reference< XHelperInterface >
getUnoSheetModuleObj( const uno::Reference< table::XCellRange >& xRange )
{
uno::Reference< sheet::XSheetCellRange > xSheetRange( xRange, uno::UNO_QUERY_THROW );
uno::Reference< sheet::XSpreadsheet > xSheet( xSheetRange->getSpreadsheet(), uno::UNO_SET_THROW );
return getUnoSheetModuleObj( xSheet );
}
void implSetZoom( const uno::Reference< frame::XModel >& xModel, sal_Int16 nZoom, std::vector< SCTAB >& nTabs )
{
ScTabViewShell* pViewSh = excel::getBestViewShell( xModel );
Fraction aFract( nZoom, 100 );
pViewSh->GetViewData().SetZoom( aFract, aFract, nTabs );
pViewSh->RefreshZoom();
}
const OUString REPLACE_CELLS_WARNING( "ReplaceCellsWarning");
class PasteCellsWarningReseter
{
private:
bool bInitialWarningState;
/// @throws uno::RuntimeException
static uno::Reference< sheet::XGlobalSheetSettings > const & getGlobalSheetSettings()
{
static uno::Reference< sheet::XGlobalSheetSettings > xProps = sheet::GlobalSheetSettings::create( comphelper::getProcessComponentContext() );
return xProps;
}
/// @throws uno::RuntimeException
static bool getReplaceCellsWarning()
{
return getGlobalSheetSettings()->getReplaceCellsWarning();
}
/// @throws uno::RuntimeException
static void setReplaceCellsWarning( bool bState )
{
getGlobalSheetSettings()->setReplaceCellsWarning( bState );
}
public:
/// @throws uno::RuntimeException
PasteCellsWarningReseter()
{
bInitialWarningState = getReplaceCellsWarning();
if ( bInitialWarningState )
setReplaceCellsWarning( false );
}
~PasteCellsWarningReseter()
{
if ( bInitialWarningState )
{
// don't allow dtor to throw
try
{
setReplaceCellsWarning( true );
}
catch ( uno::Exception& /*e*/ ){}
}
}
};
void
implnPaste( const uno::Reference< frame::XModel>& xModel )
{
PasteCellsWarningReseter resetWarningBox;
ScTabViewShell* pViewShell = getBestViewShell( xModel );
if ( pViewShell )
{
pViewShell->PasteFromSystem();
pViewShell->CellContentChanged();
}
}
void
implnCopy( const uno::Reference< frame::XModel>& xModel )
{
ScTabViewShell* pViewShell = getBestViewShell( xModel );
if ( pViewShell )
{
pViewShell->CopyToClip(nullptr,false,false,true);
// mark the copied transfer object so it is used in ScVbaRange::Insert
ScTransferObj* pClipObj = ScTransferObj::GetOwnClipboard( nullptr );
if (pClipObj)
pClipObj->SetUseInApi( true );
}
}
void
implnCut( const uno::Reference< frame::XModel>& xModel )
{
ScTabViewShell* pViewShell = getBestViewShell( xModel );
if ( pViewShell )
{
pViewShell->CutToClip();
// mark the copied transfer object so it is used in ScVbaRange::Insert
ScTransferObj* pClipObj = ScTransferObj::GetOwnClipboard( nullptr );
if (pClipObj)
pClipObj->SetUseInApi( true );
}
}
void implnPasteSpecial( const uno::Reference< frame::XModel>& xModel, InsertDeleteFlags nFlags, ScPasteFunc nFunction, bool bSkipEmpty, bool bTranspose)
{
PasteCellsWarningReseter resetWarningBox;
ScTabViewShell* pTabViewShell = getBestViewShell( xModel );
if ( pTabViewShell )
{
ScViewData& rView = pTabViewShell->GetViewData();
vcl::Window* pWin = rView.GetActiveWin();
if (pWin)
{
ScTransferObj* pOwnClip = ScTransferObj::GetOwnClipboard( pWin );
ScDocument* pDoc = nullptr;
if ( pOwnClip )
pDoc = pOwnClip->GetDocument();
pTabViewShell->PasteFromClip( nFlags, pDoc,
nFunction, bSkipEmpty, bTranspose, false,
INS_NONE, InsertDeleteFlags::NONE, true );
pTabViewShell->CellContentChanged();
}
}
}
ScDocShell*
getDocShell( const css::uno::Reference< css::frame::XModel>& xModel )
{
uno::Reference< uno::XInterface > xIf( xModel, uno::UNO_QUERY_THROW );
ScModelObj* pModel = dynamic_cast< ScModelObj* >( xIf.get() );
ScDocShell* pDocShell = nullptr;
if ( pModel )
pDocShell = static_cast<ScDocShell*>(pModel->GetEmbeddedObject());
return pDocShell;
}
ScTabViewShell*
getBestViewShell( const css::uno::Reference< css::frame::XModel>& xModel )
{
ScDocShell* pDocShell = getDocShell( xModel );
if ( pDocShell )
return pDocShell->GetBestViewShell();
return nullptr;
}
ScTabViewShell*
getCurrentBestViewShell( const uno::Reference< uno::XComponentContext >& xContext )
{
uno::Reference< frame::XModel > xModel = getCurrentExcelDoc( xContext );
return getBestViewShell( xModel );
}
SfxViewFrame*
getViewFrame( const uno::Reference< frame::XModel >& xModel )
{
ScTabViewShell* pViewShell = getBestViewShell( xModel );
if ( pViewShell )
return pViewShell->GetViewFrame();
return nullptr;
}
uno::Reference< XHelperInterface >
getUnoSheetModuleObj( const uno::Reference< sheet::XSpreadsheet >& xSheet )
{
uno::Reference< beans::XPropertySet > xProps( xSheet, uno::UNO_QUERY_THROW );
OUString sCodeName;
xProps->getPropertyValue("CodeName") >>= sCodeName;
// #TODO #FIXME ideally we should 'throw' here if we don't get a valid parent, but... it is possible
// to create a module ( and use 'Option VBASupport 1' ) for a calc document, in this scenario there
// are *NO* special document module objects ( of course being able to switch between vba/non vba mode at
// the document in the future could fix this, especially IF the switching of the vba mode takes care to
// create the special document module objects if they don't exist.
return getUnoDocModule( sCodeName, GetDocShellFromRange( xSheet ) );
}
uno::Reference< XHelperInterface >
getUnoSheetModuleObj( const uno::Reference< sheet::XSheetCellRangeContainer >& xRanges )
{
uno::Reference< container::XEnumerationAccess > xEnumAccess( xRanges, uno::UNO_QUERY_THROW );
uno::Reference< container::XEnumeration > xEnum = xEnumAccess->createEnumeration();
uno::Reference< table::XCellRange > xRange( xEnum->nextElement(), uno::UNO_QUERY_THROW );
return getUnoSheetModuleObj( xRange );
}
uno::Reference< XHelperInterface >
getUnoSheetModuleObj( const uno::Reference< table::XCell >& xCell )
{
uno::Reference< sheet::XSheetCellRange > xSheetRange( xCell, uno::UNO_QUERY_THROW );
uno::Reference< sheet::XSpreadsheet > xSheet( xSheetRange->getSpreadsheet(), uno::UNO_SET_THROW );
return getUnoSheetModuleObj( xSheet );
}
uno::Reference< XHelperInterface >
getUnoSheetModuleObj( const uno::Reference< frame::XModel >& xModel, SCTAB nTab )
{
uno::Reference< sheet::XSpreadsheetDocument > xDoc( xModel, uno::UNO_QUERY_THROW );
uno::Reference< container::XIndexAccess > xSheets( xDoc->getSheets(), uno::UNO_QUERY_THROW );
uno::Reference< sheet::XSpreadsheet > xSheet( xSheets->getByIndex( nTab ), uno::UNO_QUERY_THROW );
return getUnoSheetModuleObj( xSheet );
}
void setUpDocumentModules( const uno::Reference< sheet::XSpreadsheetDocument >& xDoc )
{
uno::Reference< frame::XModel > xModel( xDoc, uno::UNO_QUERY );
ScDocShell* pShell = excel::getDocShell( xModel );
if ( pShell )
{
OUString aPrjName( "Standard" );
pShell->GetBasicManager()->SetName( aPrjName );
/* Set library container to VBA compatibility mode. This will create
the VBA Globals object and store it in the Basic manager of the
document. */
uno::Reference<script::XLibraryContainer> xLibContainer = pShell->GetBasicContainer();
uno::Reference<script::vba::XVBACompatibility> xVBACompat( xLibContainer, uno::UNO_QUERY_THROW );
xVBACompat->setVBACompatibilityMode( true );
if( xLibContainer.is() )
{
if( !xLibContainer->hasByName( aPrjName ) )
xLibContainer->createLibrary( aPrjName );
uno::Any aLibAny = xLibContainer->getByName( aPrjName );
uno::Reference< container::XNameContainer > xLib;
aLibAny >>= xLib;
if( xLib.is() )
{
uno::Reference< script::vba::XVBAModuleInfo > xVBAModuleInfo( xLib, uno::UNO_QUERY_THROW );
uno::Reference< lang::XMultiServiceFactory> xSF( pShell->GetModel(), uno::UNO_QUERY_THROW);
uno::Reference< container::XNameAccess > xVBACodeNamedObjectAccess( xSF->createInstance("ooo.vba.VBAObjectModuleObjectProvider"), uno::UNO_QUERY_THROW );
// set up the module info for the workbook and sheets in the newly created
// spreadsheet
ScDocument& rDoc = pShell->GetDocument();
OUString sCodeName = rDoc.GetCodeName();
if ( sCodeName.isEmpty() )
{
sCodeName = "ThisWorkbook";
rDoc.SetCodeName( sCodeName );
}
std::vector< OUString > sDocModuleNames;
sDocModuleNames.push_back( sCodeName );
for ( SCTAB index = 0; index < rDoc.GetTableCount(); index++)
{
OUString aName;
rDoc.GetCodeName( index, aName );
sDocModuleNames.push_back( aName );
}
std::vector<OUString>::iterator it_end = sDocModuleNames.end();
for ( std::vector<OUString>::iterator it = sDocModuleNames.begin(); it != it_end; ++it )
{
script::ModuleInfo sModuleInfo;
uno::Any aName= xVBACodeNamedObjectAccess->getByName( *it );
sModuleInfo.ModuleObject.set( aName, uno::UNO_QUERY );
sModuleInfo.ModuleType = script::ModuleType::DOCUMENT;
xVBAModuleInfo->insertModuleInfo( *it, sModuleInfo );
if( xLib->hasByName( *it ) )
xLib->replaceByName( *it, uno::makeAny( OUString( "Option VBASupport 1\n") ) );
else
xLib->insertByName( *it, uno::makeAny( OUString( "Option VBASupport 1\n" ) ) );
}
}
}
/* Trigger the Workbook_Open event, event processor will register
itself as listener for specific events. */
try
{
uno::Reference< script::vba::XVBAEventProcessor > xVbaEvents( pShell->GetDocument().GetVbaEventProcessor(), uno::UNO_SET_THROW );
uno::Sequence< uno::Any > aArgs;
xVbaEvents->processVbaEvent( script::vba::VBAEventId::WORKBOOK_OPEN, aArgs );
}
catch( uno::Exception& )
{
}
}
}
SfxItemSet*
ScVbaCellRangeAccess::GetDataSet( ScCellRangesBase* pRangeObj )
{
return pRangeObj ? pRangeObj->GetCurrentDataSet( true ) : nullptr;
}
} // namespace excel
} // namespace vba
} // namespace ooo
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|