xref: /aoo41x/main/sc/source/ui/vba/vbaworksheet.cxx (revision b3f79822)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include <vbahelper/helperdecl.hxx>
25 #include <cppuhelper/queryinterface.hxx>
26 
27 #include "vbaworksheet.hxx"
28 
29 #include <com/sun/star/beans/XPropertySet.hpp>
30 #include <com/sun/star/beans/XIntrospectionAccess.hpp>
31 #include <com/sun/star/beans/XIntrospection.hpp>
32 #include <com/sun/star/container/XNamed.hpp>
33 #include <com/sun/star/util/XProtectable.hpp>
34 #include <com/sun/star/table/XCellRange.hpp>
35 #include <com/sun/star/sheet/XSpreadsheetView.hpp>
36 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
37 #include <com/sun/star/sheet/XCalculatable.hpp>
38 #include <com/sun/star/sheet/XCellRangeAddressable.hpp>
39 #include <com/sun/star/sheet/XCellRangeReferrer.hpp>
40 #include <com/sun/star/sheet/XSheetCellRange.hpp>
41 #include <com/sun/star/sheet/XSheetCellCursor.hpp>
42 #include <com/sun/star/sheet/XSheetAnnotationsSupplier.hpp>
43 #include <com/sun/star/sheet/XUsedAreaCursor.hpp>
44 #include <com/sun/star/sheet/XSpreadsheets.hpp>
45 #include <com/sun/star/sheet/XSheetPastable.hpp>
46 #include <com/sun/star/sheet/XCellAddressable.hpp>
47 #include <com/sun/star/sheet/XSheetOutline.hpp>
48 #include <com/sun/star/sheet/XSheetPageBreak.hpp>
49 #include <com/sun/star/sheet/XDataPilotTablesSupplier.hpp>
50 #include <com/sun/star/sheet/XNamedRanges.hpp>
51 #include <com/sun/star/util/XURLTransformer.hpp>
52 #include <com/sun/star/frame/XDispatchProvider.hpp>
53 #include <com/sun/star/frame/XComponentLoader.hpp>
54 #include <com/sun/star/table/XColumnRowRange.hpp>
55 #include <com/sun/star/table/XTableChartsSupplier.hpp>
56 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
57 #include <com/sun/star/drawing/XControlShape.hpp>
58 #include <com/sun/star/form/FormComponentType.hpp>
59 #include <com/sun/star/form/XFormsSupplier.hpp>
60 #include <ooo/vba/excel/XlEnableSelection.hpp>
61 #include <ooo/vba/excel/XlSheetVisibility.hpp>
62 #include <ooo/vba/excel/XWorkbook.hpp>
63 #include <ooo/vba/XControlProvider.hpp>
64 
65 #include <comphelper/processfactory.hxx>
66 #include <vbahelper/vbashapes.hxx>
67 
68 #include <tools/string.hxx>
69 
70 //zhangyun showdataform
71 #include <sfx2/sfxdlg.hxx>
72 #include "scabstdlg.hxx"
73 #include "tabvwsh.hxx"
74 #include "scitems.hxx"
75 
76 #include <svx/svdouno.hxx>
77 #include <svx/svdpage.hxx>
78 
79 #include "cellsuno.hxx"
80 #include "drwlayer.hxx"
81 
82 #include "scextopt.hxx"
83 #include "vbaoutline.hxx"
84 #include "vbarange.hxx"
85 #include "vbacomments.hxx"
86 #include "vbachartobjects.hxx"
87 #include "vbapivottables.hxx"
88 #include "vbaoleobject.hxx"
89 #include "vbaoleobjects.hxx"
90 #include "vbapagesetup.hxx"
91 #include "vbapagebreaks.hxx"
92 #include "vbaworksheets.hxx"
93 #include "vbahyperlinks.hxx"
94 #include "vbasheetobjects.hxx"
95 #include "vbanames.hxx"
96 
97 #define STANDARDWIDTH 2267
98 #define STANDARDHEIGHT 427
99 #define DOESNOTEXIST -1
100 
101 using namespace com::sun::star;
102 using namespace ooo::vba;
103 
getNewSpreadsheetName(rtl::OUString & aNewName,rtl::OUString aOldName,uno::Reference<sheet::XSpreadsheetDocument> & xSpreadDoc)104 static void getNewSpreadsheetName (rtl::OUString &aNewName, rtl::OUString aOldName, uno::Reference <sheet::XSpreadsheetDocument>& xSpreadDoc )
105 {
106 	if (!xSpreadDoc.is())
107 		throw lang::IllegalArgumentException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "getNewSpreadsheetName() xSpreadDoc is null" ) ), uno::Reference< uno::XInterface  >(), 1 );
108 	static rtl::OUString aUnderScre( RTL_CONSTASCII_USTRINGPARAM( "_" ) );
109 	int currentNum =2;
110 	aNewName = aOldName + aUnderScre+ String::CreateFromInt32(currentNum) ;
111 	SCTAB nTab = 0;
112 	while ( ScVbaWorksheets::nameExists(xSpreadDoc,aNewName, nTab ) )
113 	{
114 		aNewName = aOldName + aUnderScre +
115 		String::CreateFromInt32(++currentNum) ;
116 	}
117 }
118 
removeAllSheets(uno::Reference<sheet::XSpreadsheetDocument> & xSpreadDoc,rtl::OUString aSheetName)119 static void removeAllSheets( uno::Reference <sheet::XSpreadsheetDocument>& xSpreadDoc, rtl::OUString aSheetName)
120 {
121 	if (!xSpreadDoc.is())
122 		throw lang::IllegalArgumentException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "removeAllSheets() xSpreadDoc is null" ) ), uno::Reference< uno::XInterface  >(), 1 );
123 	uno::Reference<sheet::XSpreadsheets> xSheets = xSpreadDoc->getSheets();
124 	uno::Reference <container::XIndexAccess> xIndex( xSheets, uno::UNO_QUERY );
125 
126 	if ( xIndex.is() )
127 	{
128 		uno::Reference<container::XNameContainer> xNameContainer(xSheets,uno::UNO_QUERY_THROW);
129 		for (sal_Int32 i = xIndex->getCount() -1; i>= 1; i--)
130 		{
131 			uno::Reference< sheet::XSpreadsheet > xSheet(xIndex->getByIndex(i), uno::UNO_QUERY);
132 			uno::Reference< container::XNamed > xNamed( xSheet, uno::UNO_QUERY_THROW );
133 			if (xNamed.is())
134 			{
135 				xNameContainer->removeByName(xNamed->getName());
136 			}
137 		}
138 
139 		uno::Reference< sheet::XSpreadsheet > xSheet(xIndex->getByIndex(0), uno::UNO_QUERY);
140         uno::Reference< container::XNamed > xNamed( xSheet, uno::UNO_QUERY_THROW );
141 		if (xNamed.is())
142 		{
143 			xNamed->setName(aSheetName);
144 		}
145 	}
146 }
147 
148 static uno::Reference<frame::XModel>
openNewDoc(rtl::OUString aSheetName)149 openNewDoc(rtl::OUString aSheetName )
150 {
151 	uno::Reference<frame::XModel> xModel;
152 	try
153 	{
154 		uno::Reference< beans::XPropertySet > xProps( ::comphelper::getProcessServiceFactory(), uno::UNO_QUERY_THROW );
155 		uno::Reference< uno::XComponentContext > xContext(  xProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DefaultContext" ))), uno::UNO_QUERY_THROW );
156 		uno::Reference<lang::XMultiComponentFactory > xServiceManager(
157 										xContext->getServiceManager(), uno::UNO_QUERY_THROW );
158 
159 		uno::Reference <frame::XComponentLoader > xComponentLoader(
160 						xServiceManager->createInstanceWithContext(
161 						rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.Desktop" ) ),
162 						xContext ), uno::UNO_QUERY_THROW );
163 
164 		uno::Reference<lang::XComponent > xComponent( xComponentLoader->loadComponentFromURL(
165 				rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "private:factory/scalc" ) ),
166 				rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "_blank" ) ), 0,
167 				uno::Sequence < ::com::sun::star::beans::PropertyValue >() ) );
168 		uno::Reference <sheet::XSpreadsheetDocument> xSpreadDoc( xComponent, uno::UNO_QUERY_THROW );
169 		if ( xSpreadDoc.is() )
170 		{
171 			removeAllSheets(xSpreadDoc,aSheetName);
172 		}
173 		xModel.set(xSpreadDoc,uno::UNO_QUERY_THROW);
174 	}
175 	catch ( uno::Exception & /*e*/ )
176 	{
177 	}
178 	return xModel;
179 }
180 
ScVbaWorksheet(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext)181 ScVbaWorksheet::ScVbaWorksheet( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext ) : WorksheetImpl_BASE( xParent, xContext ), mbVeryHidden( false )
182 {
183 }
184 
ScVbaWorksheet(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext,const uno::Reference<sheet::XSpreadsheet> & xSheet,const uno::Reference<frame::XModel> & xModel)185 ScVbaWorksheet::ScVbaWorksheet(const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext,
186 		const uno::Reference< sheet::XSpreadsheet >& xSheet,
187 		const uno::Reference< frame::XModel >& xModel ) throw (uno::RuntimeException) : WorksheetImpl_BASE( xParent, xContext ), mxSheet( xSheet ), mxModel(xModel), mbVeryHidden( false )
188 {
189 }
190 
ScVbaWorksheet(uno::Sequence<uno::Any> const & args,uno::Reference<uno::XComponentContext> const & xContext)191 ScVbaWorksheet::ScVbaWorksheet( uno::Sequence< uno::Any> const & args,
192     uno::Reference< uno::XComponentContext> const & xContext ) throw ( lang::IllegalArgumentException ) :  WorksheetImpl_BASE( getXSomethingFromArgs< XHelperInterface >( args, 0 ), xContext ), mxModel( getXSomethingFromArgs< frame::XModel >( args, 1 ) ), mbVeryHidden( false )
193 {
194 	if ( args.getLength() < 3 )
195 		throw lang::IllegalArgumentException();
196 
197 	rtl::OUString sSheetName;
198 	args[2] >>= sSheetName;
199 
200 	uno::Reference< sheet::XSpreadsheetDocument > xSpreadDoc( mxModel, uno::UNO_QUERY_THROW );
201 	uno::Reference< container::XNameAccess > xNameAccess( xSpreadDoc->getSheets(), uno::UNO_QUERY_THROW );
202 	mxSheet.set( xNameAccess->getByName( sSheetName ), uno::UNO_QUERY_THROW );
203 }
204 
~ScVbaWorksheet()205 ScVbaWorksheet::~ScVbaWorksheet()
206 {
207 }
208 
209 ::rtl::OUString
getName()210 ScVbaWorksheet::getName() throw (uno::RuntimeException)
211 {
212 	uno::Reference< container::XNamed > xNamed( getSheet(), uno::UNO_QUERY_THROW );
213 	return xNamed->getName();
214 }
215 
216 void
setName(const::rtl::OUString & rName)217 ScVbaWorksheet::setName(const ::rtl::OUString &rName ) throw (uno::RuntimeException)
218 {
219 	uno::Reference< container::XNamed > xNamed( getSheet(), uno::UNO_QUERY_THROW );
220 	xNamed->setName( rName );
221 }
222 
223 sal_Int32
getVisible()224 ScVbaWorksheet::getVisible() throw (uno::RuntimeException)
225 {
226 	uno::Reference< beans::XPropertySet > xProps( getSheet(), uno::UNO_QUERY_THROW );
227 	bool bVisible = false;
228 	xProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "IsVisible" ) ) ) >>= bVisible;
229 	using namespace ::ooo::vba::excel::XlSheetVisibility;
230 	return bVisible ? xlSheetVisible : (mbVeryHidden ? xlSheetVeryHidden : xlSheetHidden);
231 }
232 
233 void
setVisible(sal_Int32 nVisible)234 ScVbaWorksheet::setVisible( sal_Int32 nVisible ) throw (uno::RuntimeException)
235 {
236     using namespace ::ooo::vba::excel::XlSheetVisibility;
237     bool bVisible = true;
238     switch( nVisible )
239     {
240         case xlSheetVisible: case 1:  // Excel accepts -1 and 1 for visible sheets
241             bVisible = true;
242             mbVeryHidden = false;
243         break;
244         case xlSheetHidden:
245             bVisible = false;
246             mbVeryHidden = false;
247         break;
248         case xlSheetVeryHidden:
249             bVisible = false;
250             mbVeryHidden = true;
251         break;
252         default:
253             throw uno::RuntimeException();
254     }
255 	uno::Reference< beans::XPropertySet > xProps( getSheet(), uno::UNO_QUERY_THROW );
256 	xProps->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "IsVisible" ) ), uno::Any( bVisible ) );
257 }
258 
259 sal_Int16
getIndex()260 ScVbaWorksheet::getIndex() throw (uno::RuntimeException)
261 {
262 	return getSheetID() + 1;
263 }
264 
265 sal_Int32
getEnableSelection()266 ScVbaWorksheet::getEnableSelection() throw (uno::RuntimeException)
267 {
268     uno::Reference <sheet::XSpreadsheetDocument> xSpreadDoc( getModel(), uno::UNO_QUERY_THROW );
269     SCTAB nTab = 0;
270     if ( ScVbaWorksheets::nameExists(xSpreadDoc, getName(), nTab) )
271     {
272         uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_QUERY_THROW );
273         ScDocument* pDoc = excel::getDocShell( xModel )->GetDocument();
274         ScTableProtection* pProtect = pDoc->GetTabProtection(nTab);
275 		sal_Bool bLockedCells = sal_False;
276 		sal_Bool bUnlockedCells = sal_False;
277 		if( pProtect )
278 		{
279         	bLockedCells   = pProtect->isOptionEnabled(ScTableProtection::SELECT_LOCKED_CELLS);
280         	bUnlockedCells = pProtect->isOptionEnabled(ScTableProtection::SELECT_UNLOCKED_CELLS);
281 		}
282         if( bLockedCells )
283             return excel::XlEnableSelection::xlNoRestrictions;
284         if( bUnlockedCells )
285             return excel::XlEnableSelection::xlUnlockedCells;
286         return excel::XlEnableSelection::xlNoSelection;
287     }
288     else
289 		throw uno::RuntimeException(::rtl::OUString(
290                                 RTL_CONSTASCII_USTRINGPARAM( "Sheet Name does not exist. ") ),
291                                 uno::Reference< XInterface >() );
292     return excel::XlEnableSelection::xlNoSelection;
293 }
294 
295 
296 void
setEnableSelection(sal_Int32 nSelection)297 ScVbaWorksheet::setEnableSelection( sal_Int32 nSelection ) throw (uno::RuntimeException)
298 {
299     if( (nSelection != excel::XlEnableSelection::xlNoRestrictions) &&
300         (nSelection != excel::XlEnableSelection::xlUnlockedCells) &&
301         (nSelection != excel::XlEnableSelection::xlNoSelection) )
302     {
303         DebugHelper::exception(SbERR_BAD_PARAMETER, rtl::OUString() );
304     }
305 
306     uno::Reference <sheet::XSpreadsheetDocument> xSpreadDoc( getModel(), uno::UNO_QUERY_THROW );
307     SCTAB nTab = 0;
308     if ( ScVbaWorksheets::nameExists(xSpreadDoc, getName(), nTab) )
309     {
310         uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_QUERY_THROW );
311         ScDocument* pDoc = excel::getDocShell( xModel )->GetDocument();
312         ScTableProtection* pProtect = pDoc->GetTabProtection(nTab);
313         // default is xlNoSelection
314         sal_Bool bLockedCells = sal_False;
315         sal_Bool bUnlockedCells = sal_False;
316         if( nSelection == excel::XlEnableSelection::xlNoRestrictions )
317         {
318             bLockedCells = sal_True;
319             bUnlockedCells = sal_True;
320         }
321         else if( nSelection == excel::XlEnableSelection::xlUnlockedCells )
322         {
323             bUnlockedCells = sal_True;
324         }
325 		if( pProtect )
326 		{
327         	pProtect->setOption( ScTableProtection::SELECT_LOCKED_CELLS, bLockedCells );
328         	pProtect->setOption( ScTableProtection::SELECT_UNLOCKED_CELLS, bUnlockedCells );
329 		}
330     }
331     else
332 		throw uno::RuntimeException(::rtl::OUString(
333                                 RTL_CONSTASCII_USTRINGPARAM( "Sheet Name does not exist. ") ),
334                                 uno::Reference< XInterface >() );
335 
336 }
337 
getFirstDBRangeProperties()338 uno::Reference< beans::XPropertySet > ScVbaWorksheet::getFirstDBRangeProperties() throw (uno::RuntimeException)
339 {
340     uno::Reference< beans::XPropertySet > xModelProps( mxModel, uno::UNO_QUERY_THROW );
341     uno::Reference< container::XIndexAccess > xDBRangesIA( xModelProps->getPropertyValue(
342         ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DatabaseRanges" ) ) ), uno::UNO_QUERY_THROW );
343 
344     for( sal_Int32 nIndex = 0, nCount = xDBRangesIA->getCount(); nIndex < nCount; ++nIndex )
345     {
346         uno::Reference< sheet::XCellRangeReferrer > xDBRange( xDBRangesIA->getByIndex( nIndex ), uno::UNO_QUERY_THROW );
347         // check if the database area is on this sheet
348         uno::Reference< sheet::XCellRangeAddressable > xRangeAddr( xDBRange->getReferredCells(), uno::UNO_QUERY_THROW );
349         if( getSheetID() == xRangeAddr->getRangeAddress().Sheet )
350             return uno::Reference< beans::XPropertySet >( xDBRange, uno::UNO_QUERY_THROW );
351     }
352     return uno::Reference< beans::XPropertySet >();
353 }
354 
getAutoFilterMode()355 sal_Bool SAL_CALL ScVbaWorksheet::getAutoFilterMode() throw (uno::RuntimeException)
356 {
357     uno::Reference< beans::XPropertySet > xDBRangeProps = getFirstDBRangeProperties();
358     sal_Bool bAutoFilterMode = sal_False;
359     return
360         xDBRangeProps.is() &&
361         (xDBRangeProps->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "AutoFilter" ) ) ) >>= bAutoFilterMode) &&
362         bAutoFilterMode;
363 }
364 
setAutoFilterMode(sal_Bool bAutoFilterMode)365 void SAL_CALL ScVbaWorksheet::setAutoFilterMode( sal_Bool bAutoFilterMode ) throw (uno::RuntimeException)
366 {
367     uno::Reference< beans::XPropertySet > xDBRangeProps = getFirstDBRangeProperties();
368     if( xDBRangeProps.is() )
369         xDBRangeProps->setPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "AutoFilter" ) ), uno::Any( bAutoFilterMode ) );
370 }
371 
372 uno::Reference< excel::XRange >
getUsedRange()373 ScVbaWorksheet::getUsedRange() throw (uno::RuntimeException)
374 {
375  	uno::Reference< sheet::XSheetCellRange > xSheetCellRange(getSheet(), uno::UNO_QUERY_THROW );
376 	uno::Reference< sheet::XSheetCellCursor > xSheetCellCursor( getSheet()->createCursorByRange( xSheetCellRange ), uno::UNO_QUERY_THROW );
377 	uno::Reference<sheet::XUsedAreaCursor> xUsedCursor(xSheetCellCursor,uno::UNO_QUERY_THROW);
378 	xUsedCursor->gotoStartOfUsedArea( false );
379 	xUsedCursor->gotoEndOfUsedArea( true );
380 	uno::Reference< table::XCellRange > xRange( xSheetCellCursor, uno::UNO_QUERY);
381 	return new ScVbaRange(this, mxContext, xRange);
382 }
383 
384 uno::Reference< excel::XOutline >
Outline()385 ScVbaWorksheet::Outline( ) throw (uno::RuntimeException)
386 {
387 	uno::Reference<sheet::XSheetOutline> xOutline(getSheet(),uno::UNO_QUERY_THROW);
388 	return new ScVbaOutline( this, mxContext, xOutline);
389 }
390 
391 uno::Reference< excel::XPageSetup >
PageSetup()392 ScVbaWorksheet::PageSetup( ) throw (uno::RuntimeException)
393 {
394 	return new ScVbaPageSetup( this, mxContext, getSheet(), getModel() );
395 }
396 
397 uno::Any
HPageBreaks(const uno::Any & aIndex)398 ScVbaWorksheet::HPageBreaks( const uno::Any& aIndex ) throw (uno::RuntimeException)
399 {
400     uno::Reference< sheet::XSheetPageBreak > xSheetPageBreak(getSheet(),uno::UNO_QUERY_THROW);
401     uno::Reference< excel::XHPageBreaks > xHPageBreaks( new ScVbaHPageBreaks( this, mxContext, xSheetPageBreak));
402    if ( aIndex.hasValue() )
403       return xHPageBreaks->Item( aIndex, uno::Any());
404    return uno::makeAny( xHPageBreaks );
405 }
406 
407 uno::Any
VPageBreaks(const uno::Any & aIndex)408 ScVbaWorksheet::VPageBreaks( const uno::Any& aIndex ) throw ( uno::RuntimeException )
409 {
410 	uno::Reference< sheet::XSheetPageBreak > xSheetPageBreak( getSheet(), uno::UNO_QUERY_THROW );
411 	uno::Reference< excel::XVPageBreaks > xVPageBreaks( new ScVbaVPageBreaks( this, mxContext, xSheetPageBreak ) );
412 	if( aIndex.hasValue() )
413 		return xVPageBreaks->Item( aIndex, uno::Any());
414 	return uno::makeAny( xVPageBreaks );
415 }
416 
417 sal_Int32
getStandardWidth()418 ScVbaWorksheet::getStandardWidth() throw (uno::RuntimeException)
419 {
420 	return STANDARDWIDTH ;
421 }
422 
423 sal_Int32
getStandardHeight()424 ScVbaWorksheet::getStandardHeight() throw (uno::RuntimeException)
425 {
426 	return STANDARDHEIGHT;
427 }
428 
429 sal_Bool
getProtectionMode()430 ScVbaWorksheet::getProtectionMode() throw (uno::RuntimeException)
431 {
432 	return sal_False;
433 }
434 
435 sal_Bool
getProtectContents()436 ScVbaWorksheet::getProtectContents()throw (uno::RuntimeException)
437 {
438 	uno::Reference<util::XProtectable > xProtectable(getSheet(), uno::UNO_QUERY_THROW);
439 	return xProtectable->isProtected();
440 }
441 
442 sal_Bool
getProtectDrawingObjects()443 ScVbaWorksheet::getProtectDrawingObjects() throw (uno::RuntimeException)
444 {
445 	return sal_False;
446 }
447 
448 sal_Bool
getProtectScenarios()449 ScVbaWorksheet::getProtectScenarios() throw (uno::RuntimeException)
450 {
451 	return sal_False;
452 }
453 
454 void
Activate()455 ScVbaWorksheet::Activate() throw (uno::RuntimeException)
456 {
457 	uno::Reference< sheet::XSpreadsheetView > xSpreadsheet(
458         	getModel()->getCurrentController(), uno::UNO_QUERY_THROW );
459 	xSpreadsheet->setActiveSheet(getSheet());
460 }
461 
462 void
Select()463 ScVbaWorksheet::Select() throw (uno::RuntimeException)
464 {
465 	Activate();
466 }
467 
468 void
Move(const uno::Any & Before,const uno::Any & After)469 ScVbaWorksheet::Move( const uno::Any& Before, const uno::Any& After ) throw (uno::RuntimeException)
470 {
471 	uno::Reference<excel::XWorksheet> xSheet;
472 	rtl::OUString aCurrSheetName = getName();
473 
474 	if (!(Before >>= xSheet) && !(After >>=xSheet)&& !(Before.hasValue()) && !(After.hasValue()))
475 	{
476 		uno::Reference< sheet::XSheetCellCursor > xSheetCellCursor = getSheet()->createCursor( );
477 		uno::Reference<sheet::XUsedAreaCursor> xUsedCursor(xSheetCellCursor,uno::UNO_QUERY_THROW);
478         	uno::Reference< table::XCellRange > xRange1( xSheetCellCursor, uno::UNO_QUERY);
479 		// #FIXME needs worksheet as parent
480 		uno::Reference<excel::XRange> xRange =  new ScVbaRange( this, mxContext, xRange1);
481 		if (xRange.is())
482 			xRange->Select();
483 		excel::implnCopy(mxModel);
484 		uno::Reference<frame::XModel> xModel = openNewDoc(aCurrSheetName);
485 		if (xModel.is())
486 		{
487 			excel::implnPaste(xModel);
488 			Delete();
489 		}
490 		return ;
491 	}
492 
493 	uno::Reference <sheet::XSpreadsheetDocument> xSpreadDoc( getModel(), uno::UNO_QUERY_THROW );
494 	SCTAB nDest = 0;
495 	if ( ScVbaWorksheets::nameExists (xSpreadDoc, xSheet->getName(), nDest) )
496 	{
497 		sal_Bool bAfter = After.hasValue();
498 		if (bAfter)
499 			nDest++;
500 		uno::Reference<sheet::XSpreadsheets> xSheets = xSpreadDoc->getSheets();
501 		xSheets->moveByName(aCurrSheetName,nDest);
502 	}
503 }
504 
505 void
Copy(const uno::Any & Before,const uno::Any & After)506 ScVbaWorksheet::Copy( const uno::Any& Before, const uno::Any& After ) throw (uno::RuntimeException)
507 {
508 	uno::Reference<excel::XWorksheet> xSheet;
509 	rtl::OUString aCurrSheetName =getName();
510 	if (!(Before >>= xSheet) && !(After >>=xSheet)&& !(Before.hasValue()) && !(After.hasValue()))
511 	{
512 		uno::Reference< sheet::XSheetCellCursor > xSheetCellCursor = getSheet()->createCursor( );
513 		uno::Reference<sheet::XUsedAreaCursor> xUsedCursor(xSheetCellCursor,uno::UNO_QUERY_THROW);
514         	uno::Reference< table::XCellRange > xRange1( xSheetCellCursor, uno::UNO_QUERY);
515 		uno::Reference<excel::XRange> xRange =  new ScVbaRange( this, mxContext, xRange1);
516 		if (xRange.is())
517 			xRange->Select();
518 		excel::implnCopy(mxModel);
519 		uno::Reference<frame::XModel> xModel = openNewDoc(aCurrSheetName);
520 		if (xModel.is())
521 		{
522 			excel::implnPaste(xModel);
523 		}
524 		return;
525 	}
526 
527 	uno::Reference <sheet::XSpreadsheetDocument> xSpreadDoc( getModel(), uno::UNO_QUERY );
528 	SCTAB nDest = 0;
529 	rtl::OUString aSheetName = xSheet->getName();
530 	if ( ScVbaWorksheets::nameExists (xSpreadDoc, aSheetName, nDest ) )
531 	{
532 		sal_Bool bAfter = After.hasValue();
533 		if(bAfter)
534 			  nDest++;
535 		uno::Reference<sheet::XSpreadsheets> xSheets = xSpreadDoc->getSheets();
536 		getNewSpreadsheetName(aSheetName,aCurrSheetName,xSpreadDoc);
537 		xSheets->copyByName(aCurrSheetName,aSheetName,nDest);
538 	}
539 }
540 
541 
542 void
Paste(const uno::Any & Destination,const uno::Any &)543 ScVbaWorksheet::Paste( const uno::Any& Destination, const uno::Any& /*Link*/ ) throw (uno::RuntimeException)
544 {
545 	// #TODO# #FIXME# Link is not used
546 	uno::Reference<excel::XRange> xRange( Destination, uno::UNO_QUERY );
547 	if ( xRange.is() )
548 		xRange->Select();
549 	excel::implnPaste( mxModel );
550 }
551 
552 void
Delete()553 ScVbaWorksheet::Delete() throw (uno::RuntimeException)
554 {
555 	uno::Reference <sheet::XSpreadsheetDocument> xSpreadDoc( getModel(), uno::UNO_QUERY_THROW );
556 	rtl::OUString aSheetName = getName();
557 	if ( xSpreadDoc.is() )
558 	{
559 		SCTAB nTab = 0;
560 		if (!ScVbaWorksheets::nameExists(xSpreadDoc, aSheetName, nTab ))
561 		{
562 			return;
563 		}
564 		uno::Reference<sheet::XSpreadsheets> xSheets = xSpreadDoc->getSheets();
565 		uno::Reference<container::XNameContainer> xNameContainer(xSheets,uno::UNO_QUERY_THROW);
566 		xNameContainer->removeByName(aSheetName);
567         mxSheet.clear();
568 	}
569 }
570 
571 uno::Reference< excel::XWorksheet >
getSheetAtOffset(SCTAB offset)572 ScVbaWorksheet::getSheetAtOffset(SCTAB offset) throw (uno::RuntimeException)
573 {
574 	uno::Reference <sheet::XSpreadsheetDocument> xSpreadDoc( getModel(), uno::UNO_QUERY_THROW );
575 	uno::Reference <sheet::XSpreadsheets> xSheets( xSpreadDoc->getSheets(), uno::UNO_QUERY_THROW );
576 	uno::Reference <container::XIndexAccess> xIndex( xSheets, uno::UNO_QUERY_THROW );
577 
578 	SCTAB nIdx = 0;
579 	if ( !ScVbaWorksheets::nameExists (xSpreadDoc, getName(), nIdx ) )
580 		return uno::Reference< excel::XWorksheet >();
581 	nIdx = nIdx + offset;
582 	uno::Reference< sheet::XSpreadsheet > xSheet(xIndex->getByIndex(nIdx), uno::UNO_QUERY_THROW);
583 	// parent will be the parent of 'this' worksheet
584 	return new ScVbaWorksheet (getParent(), mxContext, xSheet, getModel());
585 }
586 
587 uno::Reference< excel::XWorksheet >
getNext()588 ScVbaWorksheet::getNext() throw (uno::RuntimeException)
589 {
590 	return getSheetAtOffset(static_cast<SCTAB>(1));
591 }
592 
593 uno::Reference< excel::XWorksheet >
getPrevious()594 ScVbaWorksheet::getPrevious() throw (uno::RuntimeException)
595 {
596 	return getSheetAtOffset(-1);
597 }
598 
599 
600 void
Protect(const uno::Any & Password,const uno::Any &,const uno::Any &,const uno::Any &,const uno::Any &)601 ScVbaWorksheet::Protect( const uno::Any& Password, const uno::Any& /*DrawingObjects*/, const uno::Any& /*Contents*/, const uno::Any& /*Scenarios*/, const uno::Any& /*UserInterfaceOnly*/ ) throw (uno::RuntimeException)
602 {
603 	// #TODO# #FIXME# is there anything we can do witht the unused param
604 	// can the implementation use anything else here
605 	uno::Reference<util::XProtectable > xProtectable(getSheet(), uno::UNO_QUERY_THROW);
606 	::rtl::OUString aPasswd;
607 	Password >>= aPasswd;
608 	xProtectable->protect( aPasswd );
609 }
610 
611 void
Unprotect(const uno::Any & Password)612 ScVbaWorksheet::Unprotect( const uno::Any& Password ) throw (uno::RuntimeException)
613 {
614 	uno::Reference<util::XProtectable > xProtectable(getSheet(), uno::UNO_QUERY_THROW);
615 	::rtl::OUString aPasswd;
616 	Password >>= aPasswd;
617 	xProtectable->unprotect( aPasswd );
618 }
619 
620 void
Calculate()621 ScVbaWorksheet::Calculate() throw (uno::RuntimeException)
622 {
623 	uno::Reference <sheet::XCalculatable> xReCalculate(getModel(), uno::UNO_QUERY_THROW);
624 	xReCalculate->calculate();
625 }
626 
627 uno::Reference< excel::XRange >
Range(const::uno::Any & Cell1,const::uno::Any & Cell2)628 ScVbaWorksheet::Range( const ::uno::Any& Cell1, const ::uno::Any& Cell2 ) throw (uno::RuntimeException)
629 {
630 	uno::Reference< excel::XRange > xSheetRange( new ScVbaRange( this, mxContext
631 , uno::Reference< table::XCellRange >( getSheet(), uno::UNO_QUERY_THROW ) ) );
632 	return xSheetRange->Range( Cell1, Cell2 );
633 }
634 
635 void
CheckSpelling(const uno::Any &,const uno::Any &,const uno::Any &,const uno::Any &)636 ScVbaWorksheet::CheckSpelling( const uno::Any& /*CustomDictionary*/,const uno::Any& /*IgnoreUppercase*/,const uno::Any& /*AlwaysSuggest*/, const uno::Any& /*SpellingLang*/ ) throw (uno::RuntimeException)
637 {
638 	// #TODO# #FIXME# unused params above, can we do anything with those
639 	rtl::OUString url = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".uno:SpellDialog"));
640 	uno::Reference< frame::XModel > xModel( getModel() );
641 	dispatchRequests(xModel,url);
642 }
643 
644 uno::Reference< excel::XRange >
getSheetRange()645 ScVbaWorksheet::getSheetRange() throw (uno::RuntimeException)
646 {
647 	uno::Reference< table::XCellRange > xRange( getSheet(),uno::UNO_QUERY_THROW );
648 	return uno::Reference< excel::XRange >( new ScVbaRange( this, mxContext, xRange ) );
649 }
650 
651 // These are hacks - we prolly (somehow) need to inherit
652 // the vbarange functionality here ...
653 uno::Reference< excel::XRange >
Cells(const::uno::Any & nRow,const::uno::Any & nCol)654 ScVbaWorksheet::Cells( const ::uno::Any &nRow, const ::uno::Any &nCol )
655 		throw (uno::RuntimeException)
656 {
657     // Performance optimization for often-called Cells method:
658     // Use a common helper method instead of creating a new ScVbaRange object
659     uno::Reference< table::XCellRange > xRange( getSheet(), uno::UNO_QUERY_THROW );
660     return ScVbaRange::CellsHelper( this, mxContext, xRange, nRow, nCol );
661 }
662 
663 uno::Reference< excel::XRange >
Rows(const uno::Any & aIndex)664 ScVbaWorksheet::Rows(const uno::Any& aIndex ) throw (uno::RuntimeException)
665 {
666 	return getSheetRange()->Rows( aIndex );
667 }
668 
669 uno::Reference< excel::XRange >
Columns(const uno::Any & aIndex)670 ScVbaWorksheet::Columns( const uno::Any& aIndex ) throw (uno::RuntimeException)
671 {
672 	return getSheetRange()->Columns( aIndex );
673 }
674 
675 uno::Any SAL_CALL
ChartObjects(const uno::Any & Index)676 ScVbaWorksheet::ChartObjects( const uno::Any& Index ) throw (uno::RuntimeException)
677 {
678 	if ( !mxCharts.is() )
679 	{
680 		uno::Reference< table::XTableChartsSupplier > xChartSupplier( getSheet(), uno::UNO_QUERY_THROW );
681 		uno::Reference< table::XTableCharts > xTableCharts = xChartSupplier->getCharts();
682 
683 		uno::Reference< drawing::XDrawPageSupplier > xDrawPageSupplier( mxSheet, uno::UNO_QUERY_THROW );
684 		mxCharts = new ScVbaChartObjects(  this, mxContext, xTableCharts, xDrawPageSupplier );
685 	}
686 	if ( Index.hasValue() )
687 	{
688 		uno::Reference< XCollection > xColl( mxCharts, uno::UNO_QUERY_THROW );
689 		return xColl->Item( Index, uno::Any() );
690 	}
691 	else
692 		return uno::makeAny( mxCharts );
693 
694 }
695 
696 uno::Any SAL_CALL
PivotTables(const uno::Any & Index)697 ScVbaWorksheet::PivotTables( const uno::Any& Index ) throw (uno::RuntimeException)
698 {
699 	uno::Reference< css::sheet::XSpreadsheet > xSheet = getSheet();
700 	uno::Reference< sheet::XDataPilotTablesSupplier > xTables(xSheet, uno::UNO_QUERY_THROW ) ;
701 	uno::Reference< container::XIndexAccess > xIndexAccess( xTables->getDataPilotTables(), uno::UNO_QUERY_THROW );
702 
703 	uno::Reference< XCollection > xColl(  new ScVbaPivotTables( this, mxContext, xIndexAccess ) );
704 	if ( Index.hasValue() )
705 		return xColl->Item( Index, uno::Any() );
706 	return uno::makeAny( xColl );
707 }
708 
709 uno::Any SAL_CALL
Comments(const uno::Any & Index)710 ScVbaWorksheet::Comments( const uno::Any& Index ) throw (uno::RuntimeException)
711 {
712 	uno::Reference< css::sheet::XSpreadsheet > xSheet = getSheet();
713 	uno::Reference< sheet::XSheetAnnotationsSupplier > xAnnosSupp( xSheet, uno::UNO_QUERY_THROW );
714 	uno::Reference< sheet::XSheetAnnotations > xAnnos( xAnnosSupp->getAnnotations(), uno::UNO_QUERY_THROW );
715 	uno::Reference< container::XIndexAccess > xIndexAccess( xAnnos, uno::UNO_QUERY_THROW );
716 	uno::Reference< XCollection > xColl(  new ScVbaComments( this, mxContext, mxModel, xIndexAccess ) );
717 	if ( Index.hasValue() )
718 		return xColl->Item( Index, uno::Any() );
719 	return uno::makeAny( xColl );
720 }
721 
722 uno::Any SAL_CALL
Hyperlinks(const uno::Any & aIndex)723 ScVbaWorksheet::Hyperlinks( const uno::Any& aIndex ) throw (uno::RuntimeException)
724 {
725     /*  The worksheet always returns the same Hyperlinks object.
726         See vbahyperlinks.hxx for more details. */
727     if( !mxHlinks.is() )
728         mxHlinks.set( new ScVbaHyperlinks( this, mxContext ) );
729 	if( aIndex.hasValue() )
730 		return uno::Reference< XCollection >( mxHlinks, uno::UNO_QUERY_THROW )->Item( aIndex, uno::Any() );
731     return uno::Any( mxHlinks );
732 }
733 
734 uno::Any SAL_CALL
Names(const css::uno::Any & aIndex)735 ScVbaWorksheet::Names( const css::uno::Any& aIndex ) throw (uno::RuntimeException)
736 {
737     // fake sheet-local names by returning all global names
738     // #163498# initialize Names object with correct parent (this worksheet)
739     // TODO: real sheet-local names...
740 	uno::Reference< beans::XPropertySet > xProps( mxModel, uno::UNO_QUERY_THROW );
741 	uno::Reference< sheet::XNamedRanges > xNamedRanges(  xProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("NamedRanges") ) ), uno::UNO_QUERY_THROW );
742 	uno::Reference< XCollection > xNames( new ScVbaNames( this, mxContext, xNamedRanges, mxModel ) );
743     if ( aIndex.hasValue() )
744         return uno::Any( xNames->Item( aIndex, uno::Any() ) );
745 	return uno::Any( xNames );
746 }
747 
748 uno::Any SAL_CALL
OLEObjects(const uno::Any & Index)749 ScVbaWorksheet::OLEObjects( const uno::Any& Index ) throw (uno::RuntimeException)
750 {
751     uno::Reference< sheet::XSpreadsheet > xSpreadsheet( getSheet(), uno::UNO_QUERY_THROW );
752     uno::Reference< drawing::XDrawPageSupplier > xDrawPageSupplier( xSpreadsheet, uno::UNO_QUERY_THROW );
753     uno::Reference< drawing::XDrawPage > xDrawPage( xDrawPageSupplier->getDrawPage(), uno::UNO_QUERY_THROW );
754     uno::Reference< container::XIndexAccess > xIndexAccess( xDrawPage, uno::UNO_QUERY_THROW );
755 
756     uno::Reference< excel::XOLEObjects >xOleObjects( new ScVbaOLEObjects( this, mxContext, xIndexAccess ) );
757     if( Index.hasValue() )
758         return xOleObjects->Item( Index, uno::Any() );
759     return uno::Any( xOleObjects );
760 }
761 
762 uno::Any SAL_CALL
Shapes(const uno::Any & aIndex)763 ScVbaWorksheet::Shapes( const uno::Any& aIndex ) throw (uno::RuntimeException)
764 {
765     uno::Reference< sheet::XSpreadsheet > xSpreadsheet( getSheet(), uno::UNO_QUERY_THROW );
766     uno::Reference< drawing::XDrawPageSupplier > xDrawPageSupplier( xSpreadsheet, uno::UNO_QUERY_THROW );
767     uno::Reference< drawing::XShapes > xShapes( xDrawPageSupplier->getDrawPage(), uno::UNO_QUERY_THROW );
768     uno::Reference< container::XIndexAccess > xIndexAccess( xShapes, uno::UNO_QUERY_THROW );
769 
770    uno::Reference< msforms::XShapes> xVbaShapes( new ScVbaShapes( this, mxContext, xIndexAccess, getModel() ) );
771    if ( aIndex.hasValue() )
772       return xVbaShapes->Item( aIndex, uno::Any() );
773    return uno::makeAny( xVbaShapes );
774 }
775 
776 uno::Any SAL_CALL
Buttons(const uno::Any & rIndex)777 ScVbaWorksheet::Buttons( const uno::Any& rIndex ) throw (uno::RuntimeException)
778 {
779     if( !mxButtons.is() )
780         mxButtons.set( new ScVbaButtons( this, mxContext, mxModel, mxSheet ) );
781     else
782         mxButtons->collectShapes();
783     if( rIndex.hasValue() )
784         return mxButtons->Item( rIndex, uno::Any() );
785     return uno::Any( uno::Reference< XCollection >( mxButtons.get() ) );
786 }
787 
788 uno::Any SAL_CALL
CheckBoxes(const uno::Any &)789 ScVbaWorksheet::CheckBoxes( const uno::Any& /*rIndex*/ ) throw (uno::RuntimeException)
790 {
791     throw uno::RuntimeException();
792 }
793 
794 uno::Any SAL_CALL
DropDowns(const uno::Any &)795 ScVbaWorksheet::DropDowns( const uno::Any& /*rIndex*/ ) throw (uno::RuntimeException)
796 {
797     throw uno::RuntimeException();
798 }
799 
800 uno::Any SAL_CALL
GroupBoxes(const uno::Any &)801 ScVbaWorksheet::GroupBoxes( const uno::Any& /*rIndex*/ ) throw (uno::RuntimeException)
802 {
803     throw uno::RuntimeException();
804 }
805 
806 uno::Any SAL_CALL
Labels(const uno::Any &)807 ScVbaWorksheet::Labels( const uno::Any& /*rIndex*/ ) throw (uno::RuntimeException)
808 {
809     throw uno::RuntimeException();
810 }
811 
812 uno::Any SAL_CALL
ListBoxes(const uno::Any &)813 ScVbaWorksheet::ListBoxes( const uno::Any& /*rIndex*/ ) throw (uno::RuntimeException)
814 {
815     throw uno::RuntimeException();
816 }
817 
818 uno::Any SAL_CALL
OptionButtons(const uno::Any &)819 ScVbaWorksheet::OptionButtons( const uno::Any& /*rIndex*/ ) throw (uno::RuntimeException)
820 {
821     throw uno::RuntimeException();
822 }
823 
824 uno::Any SAL_CALL
ScrollBars(const uno::Any &)825 ScVbaWorksheet::ScrollBars( const uno::Any& /*rIndex*/ ) throw (uno::RuntimeException)
826 {
827     throw uno::RuntimeException();
828 }
829 
830 uno::Any SAL_CALL
Spinners(const uno::Any &)831 ScVbaWorksheet::Spinners( const uno::Any& /*rIndex*/ ) throw (uno::RuntimeException)
832 {
833     throw uno::RuntimeException();
834 }
835 
836 void SAL_CALL
ShowDataForm()837 ScVbaWorksheet::ShowDataForm( ) throw (uno::RuntimeException)
838 {
839 #ifdef VBA_OOBUILD_HACK
840 	uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_QUERY_THROW );
841 	ScTabViewShell* pTabViewShell = excel::getBestViewShell( xModel );
842 
843 	ScAbstractDialogFactory* pFact = ScAbstractDialogFactory::Create();
844 	DBG_ASSERT(pFact, "ScAbstractFactory create fail!");//CHINA001
845 
846 	AbstractScDataFormDlg* pDlg = pFact->CreateScDataFormDlg( pTabViewShell->GetDialogParent(),RID_SCDLG_DATAFORM, pTabViewShell);
847 	DBG_ASSERT(pDlg, "Dialog create fail!");//CHINA001
848 
849 	pDlg->Execute();
850 #else
851 	throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Not implemented") ), uno::Reference< uno::XInterface >() );
852 #endif
853 }
854 
855 uno::Any SAL_CALL
Evaluate(const::rtl::OUString & Name)856 ScVbaWorksheet::Evaluate( const ::rtl::OUString& Name ) throw (uno::RuntimeException)
857 {
858 	// #TODO Evaluate allows other things to be evaluated, e.g. functions
859 	// I think ( like SIN(3) etc. ) need to investigate that
860 	// named Ranges also? e.g. [MyRange] if so need a list of named ranges
861 	uno::Any aVoid;
862 	return uno::Any( Range( uno::Any( Name ), aVoid ) );
863 }
864 
865 
866 uno::Reference< beans::XIntrospectionAccess > SAL_CALL
getIntrospection()867 ScVbaWorksheet::getIntrospection(  ) throw (uno::RuntimeException)
868 {
869 	return uno::Reference< beans::XIntrospectionAccess >();
870 }
871 
872 uno::Any SAL_CALL
invoke(const::rtl::OUString & aFunctionName,const uno::Sequence<uno::Any> &,uno::Sequence<::sal_Int16> &,uno::Sequence<uno::Any> &)873 ScVbaWorksheet::invoke( const ::rtl::OUString& aFunctionName, const uno::Sequence< uno::Any >& /*aParams*/, uno::Sequence< ::sal_Int16 >& /*aOutParamIndex*/, uno::Sequence< uno::Any >& /*aOutParam*/ ) throw (lang::IllegalArgumentException, script::CannotConvertException, reflection::InvocationTargetException, uno::RuntimeException)
874 {
875 	OSL_TRACE("** ScVbaWorksheet::invoke( %s ), will barf",
876 		rtl::OUStringToOString( aFunctionName, RTL_TEXTENCODING_UTF8 ).getStr() );
877 
878 	throw uno::RuntimeException(); // unsupported operation
879 }
880 
881 void SAL_CALL
setValue(const::rtl::OUString & aPropertyName,const uno::Any & aValue)882 ScVbaWorksheet::setValue( const ::rtl::OUString& aPropertyName, const uno::Any& aValue ) throw (beans::UnknownPropertyException, script::CannotConvertException, reflection::InvocationTargetException, uno::RuntimeException)
883 {
884     setDefaultPropByIntrospection( uno::makeAny( getValue( aPropertyName ) ), aValue );
885 }
886 uno::Any SAL_CALL
getValue(const::rtl::OUString & aPropertyName)887 ScVbaWorksheet::getValue( const ::rtl::OUString& aPropertyName ) throw (beans::UnknownPropertyException, uno::RuntimeException)
888 {
889     uno::Reference< drawing::XControlShape > xControlShape( getControlShape( aPropertyName ), uno::UNO_QUERY_THROW );
890 
891     uno::Reference<lang::XMultiComponentFactory > xServiceManager( mxContext->getServiceManager(), uno::UNO_QUERY_THROW );
892     uno::Reference< XControlProvider > xControlProvider( xServiceManager->createInstanceWithContext( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ooo.vba.ControlProvider" ) ), mxContext ), uno::UNO_QUERY_THROW );
893     uno::Reference< msforms::XControl > xControl( xControlProvider->createControl(  xControlShape, getModel() ) );
894     return uno::makeAny( xControl );
895 }
896 
897 ::sal_Bool SAL_CALL
hasMethod(const::rtl::OUString &)898 ScVbaWorksheet::hasMethod( const ::rtl::OUString& /*aName*/ ) throw (uno::RuntimeException)
899 {
900 	return sal_False;
901 }
902 
903 uno::Reference< container::XNameAccess >
getFormControls()904 ScVbaWorksheet::getFormControls()
905 {
906 	uno::Reference< container::XNameAccess > xFormControls;
907 	try
908 	{
909 		uno::Reference< sheet::XSpreadsheet > xSpreadsheet( getSheet(), uno::UNO_QUERY_THROW );
910 		uno::Reference< drawing::XDrawPageSupplier > xDrawPageSupplier( xSpreadsheet, uno::UNO_QUERY_THROW );
911 		uno::Reference< form::XFormsSupplier >  xFormSupplier( xDrawPageSupplier->getDrawPage(), uno::UNO_QUERY_THROW );
912     		uno::Reference< container::XIndexAccess > xIndexAccess( xFormSupplier->getForms(), uno::UNO_QUERY_THROW );
913 		// get the www-standard container ( maybe we should access the
914 		// 'www-standard' by name rather than index, this seems an
915 		// implementation detail
916 		if( xIndexAccess->hasElements() )
917 			xFormControls.set( xIndexAccess->getByIndex(0), uno::UNO_QUERY );
918 
919 	}
920 	catch( uno::Exception& )
921 	{
922 	}
923 	return xFormControls;
924 
925 				}
926 ::sal_Bool SAL_CALL
hasProperty(const::rtl::OUString & aName)927 ScVbaWorksheet::hasProperty( const ::rtl::OUString& aName ) throw (uno::RuntimeException)
928 {
929 	uno::Reference< container::XNameAccess > xFormControls( getFormControls() );
930 	if ( xFormControls.is() )
931 		return xFormControls->hasByName( aName );
932 	return sal_False;
933 }
934 
935 uno::Any
getControlShape(const::rtl::OUString & sName)936 ScVbaWorksheet::getControlShape( const ::rtl::OUString& sName )
937 {
938     // ideally we would get an XControl object but it appears an XControl
939     // implementation only exists for a Control implementation optained from the
940     // view ( e.g. in basic you would get this from
941     // thiscomponent.currentcontroller.getControl( controlModel ) )
942     // and the thing to realise is that it is only possible to get an XControl
943     // for a currently displayed control :-( often we would want to modify
944     // a control not on the active sheet. But.. you can always access the
945     // XControlShape from the DrawPage whether that is the active drawpage or not
946 
947     uno::Reference< drawing::XDrawPageSupplier > xDrawPageSupplier( getSheet(), uno::UNO_QUERY_THROW );
948     uno::Reference< container::XIndexAccess > xIndexAccess( xDrawPageSupplier->getDrawPage(), uno::UNO_QUERY_THROW );
949 
950     sal_Int32 nCount = xIndexAccess->getCount();
951     for( int index = 0; index < nCount; index++ )
952     {
953         uno::Any aUnoObj =  xIndexAccess->getByIndex( index );
954  		// It seems there are some drawing objects that can not query into Control shapes?
955         uno::Reference< drawing::XControlShape > xControlShape( aUnoObj, uno::UNO_QUERY );
956  		if( xControlShape.is() )
957  		{
958      	    uno::Reference< container::XNamed > xNamed( xControlShape->getControl(), uno::UNO_QUERY_THROW );
959         if( sName.equals( xNamed->getName() ))
960         {
961             return aUnoObj;
962         }
963  		}
964     }
965     return uno::Any();
966 }
967 
968 
969 rtl::OUString&
getServiceImplName()970 ScVbaWorksheet::getServiceImplName()
971 {
972 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaWorksheet") );
973 	return sImplName;
974 }
975 void SAL_CALL
setEnableCalculation(::sal_Bool bEnableCalculation)976 ScVbaWorksheet::setEnableCalculation( ::sal_Bool bEnableCalculation ) throw ( script::BasicErrorException, uno::RuntimeException)
977 {
978 	uno::Reference <sheet::XCalculatable> xCalculatable(getModel(), uno::UNO_QUERY_THROW);
979         xCalculatable->enableAutomaticCalculation( bEnableCalculation);
980 }
981 ::sal_Bool SAL_CALL
getEnableCalculation()982 ScVbaWorksheet::getEnableCalculation(  ) throw (css::script::BasicErrorException, css::uno::RuntimeException)
983 {
984 	uno::Reference <sheet::XCalculatable> xCalculatable(getModel(), uno::UNO_QUERY_THROW);
985 	return xCalculatable->isAutomaticCalculationEnabled();
986 }
987 
988 uno::Sequence< rtl::OUString >
getServiceNames()989 ScVbaWorksheet::getServiceNames()
990 {
991 	static uno::Sequence< rtl::OUString > aServiceNames;
992 	if ( aServiceNames.getLength() == 0 )
993 	{
994 		aServiceNames.realloc( 1 );
995 		aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.Worksheet" ) );
996 	}
997 	return aServiceNames;
998 }
999 
1000 rtl::OUString SAL_CALL
getCodeName()1001 ScVbaWorksheet::getCodeName() throw (css::uno::RuntimeException)
1002 {
1003     uno::Reference< beans::XPropertySet > xSheetProp( mxSheet, uno::UNO_QUERY_THROW );
1004     return xSheetProp->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CodeName" ) ) ).get< ::rtl::OUString >();
1005 }
1006 
1007 sal_Int16
getSheetID()1008 ScVbaWorksheet::getSheetID() throw (uno::RuntimeException)
1009 {
1010 	uno::Reference< sheet::XCellRangeAddressable > xAddressable( mxSheet, uno::UNO_QUERY_THROW );
1011 	return xAddressable->getRangeAddress().Sheet;
1012 }
1013 
1014 void SAL_CALL
PrintOut(const uno::Any & From,const uno::Any & To,const uno::Any & Copies,const uno::Any & Preview,const uno::Any & ActivePrinter,const uno::Any & PrintToFile,const uno::Any & Collate,const uno::Any & PrToFileName,const uno::Any & IgnorePrintAreas)1015 ScVbaWorksheet::PrintOut( const uno::Any& From, const uno::Any& To, const uno::Any& Copies, const uno::Any& Preview, const uno::Any& ActivePrinter, const uno::Any& PrintToFile, const uno::Any& Collate, const uno::Any& PrToFileName, const uno::Any& IgnorePrintAreas ) throw (uno::RuntimeException)
1016 {
1017 	sal_Int32 nTo = 0;
1018 	sal_Int32 nFrom = 0;
1019 	sal_Int16 nCopies = 1;
1020 	sal_Bool bCollate = sal_False;
1021 	sal_Bool bSelection = sal_False;
1022     sal_Bool bIgnorePrintAreas = sal_False;
1023 	From >>= nFrom;
1024 	To >>= nTo;
1025 	Copies >>= nCopies;
1026     IgnorePrintAreas >>= bIgnorePrintAreas;
1027 	if ( nCopies > 1 ) // Collate only useful when more that 1 copy
1028 		Collate >>= bCollate;
1029 
1030 	if ( !( nFrom || nTo ) )
1031 		bSelection = sal_True;
1032 
1033     uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_QUERY_THROW );
1034 	PrintOutHelper( excel::getBestViewShell( xModel ), From, To, Copies, Preview, ActivePrinter, PrintToFile, Collate, PrToFileName, bSelection );
1035 }
1036 
1037 namespace worksheet
1038 {
1039 namespace sdecl = comphelper::service_decl;
1040 sdecl::vba_service_class_<ScVbaWorksheet, sdecl::with_args<true> > serviceImpl;
1041 extern sdecl::ServiceDecl const serviceDecl(
1042     serviceImpl,
1043     "ScVbaWorksheet",
1044     "ooo.vba.excel.Worksheet" );
1045 }
1046