merge the formfield patch from ooo-build
[ooovba.git] / sc / source / ui / vba / vbaworksheet.cxx
blob0f52b14fbb312b1400f6f196ddc82ea73d61e8f3
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: vbaworksheet.cxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #include <vbahelper/helperdecl.hxx>
31 #include <cppuhelper/queryinterface.hxx>
33 #include <com/sun/star/beans/XPropertySet.hpp>
34 #include <com/sun/star/beans/XIntrospectionAccess.hpp>
35 #include <com/sun/star/beans/XIntrospection.hpp>
36 #include <com/sun/star/container/XNamed.hpp>
37 #include <com/sun/star/util/XProtectable.hpp>
38 #include <com/sun/star/table/XCellRange.hpp>
39 #include <com/sun/star/sheet/XSpreadsheetView.hpp>
40 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
41 #include <com/sun/star/sheet/XCalculatable.hpp>
42 #include <com/sun/star/sheet/XCellRangeAddressable.hpp>
43 #include <com/sun/star/sheet/XSheetCellRange.hpp>
44 #include <com/sun/star/sheet/XSheetCellCursor.hpp>
45 #include <com/sun/star/sheet/XSheetAnnotationsSupplier.hpp>
46 #include <com/sun/star/sheet/XUsedAreaCursor.hpp>
47 #include <com/sun/star/sheet/XSpreadsheets.hpp>
48 #include <com/sun/star/sheet/XSheetPastable.hpp>
49 #include <com/sun/star/sheet/XCellAddressable.hpp>
50 #include <com/sun/star/sheet/XSheetOutline.hpp>
51 #include <com/sun/star/sheet/XSheetPageBreak.hpp>
52 #include <com/sun/star/sheet/XDataPilotTablesSupplier.hpp>
53 #include <com/sun/star/util/XURLTransformer.hpp>
54 #include <com/sun/star/frame/XDispatchProvider.hpp>
55 #include <com/sun/star/frame/XComponentLoader.hpp>
56 #include <com/sun/star/table/XColumnRowRange.hpp>
57 #include <com/sun/star/table/XTableChartsSupplier.hpp>
58 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
59 #include <com/sun/star/drawing/XControlShape.hpp>
60 #include <com/sun/star/form/FormComponentType.hpp>
61 #include <com/sun/star/form/XFormsSupplier.hpp>
62 #include <ooo/vba/excel/XlEnableSelection.hpp>
63 #include <ooo/vba/XControlProvider.hpp>
65 #include <comphelper/processfactory.hxx>
67 #include <tools/string.hxx>
69 //zhangyun showdataform
70 #include <sfx2/sfxdlg.hxx>
71 #include <scabstdlg.hxx>
72 #include <tabvwsh.hxx>
73 #include <scitems.hxx>
75 #include <svx/svdouno.hxx>
76 #include <svx/svdpage.hxx>
78 #include "cellsuno.hxx"
79 #include "drwlayer.hxx"
80 #ifdef CWSSCSHEETPROTECTION
81 #include "tabprotection.hxx"
82 #endif
83 #include "scextopt.hxx"
84 #include "vbaoutline.hxx"
85 #include "vbarange.hxx"
86 #include "vbacomments.hxx"
87 #include "vbaworksheet.hxx"
88 #include "vbachartobjects.hxx"
89 #include "vbapivottables.hxx"
90 #include "vbaoleobject.hxx"
91 #include "vbaoleobjects.hxx"
92 #include <vbahelper/vbashapes.hxx>
93 #include "vbapagesetup.hxx"
94 #include "vbapagebreaks.hxx"
96 #define STANDARDWIDTH 2267
97 #define STANDARDHEIGHT 427
98 #define DOESNOTEXIST -1
99 using namespace com::sun::star;
100 using namespace ooo::vba;
101 bool
102 nameExists( uno::Reference <sheet::XSpreadsheetDocument>& xSpreadDoc, ::rtl::OUString & name, SCTAB& nTab ) throw ( lang::IllegalArgumentException )
104 if (!xSpreadDoc.is())
105 throw lang::IllegalArgumentException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "nameExists() xSpreadDoc is null" ) ), uno::Reference< uno::XInterface >(), 1 );
106 uno::Reference <sheet::XSpreadsheets> xSheets = xSpreadDoc->getSheets();
107 uno::Reference <container::XIndexAccess> xIndex( xSheets, uno::UNO_QUERY );
108 if ( xIndex.is() )
110 SCTAB nCount = static_cast< SCTAB >( xIndex->getCount() );
111 for (SCTAB i=0; i < nCount; i++)
113 uno::Reference< sheet::XSpreadsheet > xSheet(xIndex->getByIndex(i), uno::UNO_QUERY);
114 uno::Reference< container::XNamed > xNamed( xSheet, uno::UNO_QUERY_THROW );
115 if (xNamed->getName() == name)
117 nTab = i;
118 return true;
122 return false;
125 static void getNewSpreadsheetName (rtl::OUString &aNewName, rtl::OUString aOldName, uno::Reference <sheet::XSpreadsheetDocument>& xSpreadDoc )
127 if (!xSpreadDoc.is())
128 throw lang::IllegalArgumentException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "getNewSpreadsheetName() xSpreadDoc is null" ) ), uno::Reference< uno::XInterface >(), 1 );
129 static rtl::OUString aUnderScre( RTL_CONSTASCII_USTRINGPARAM( "_" ) );
130 int currentNum =2;
131 aNewName = aOldName + aUnderScre+ String::CreateFromInt32(currentNum) ;
132 SCTAB nTab = 0;
133 while ( nameExists(xSpreadDoc,aNewName, nTab ) )
135 aNewName = aOldName + aUnderScre +
136 String::CreateFromInt32(++currentNum) ;
140 static void removeAllSheets( uno::Reference <sheet::XSpreadsheetDocument>& xSpreadDoc, rtl::OUString aSheetName)
142 if (!xSpreadDoc.is())
143 throw lang::IllegalArgumentException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "removeAllSheets() xSpreadDoc is null" ) ), uno::Reference< uno::XInterface >(), 1 );
144 uno::Reference<sheet::XSpreadsheets> xSheets = xSpreadDoc->getSheets();
145 uno::Reference <container::XIndexAccess> xIndex( xSheets, uno::UNO_QUERY );
147 if ( xIndex.is() )
149 uno::Reference<container::XNameContainer> xNameContainer(xSheets,uno::UNO_QUERY_THROW);
150 for (sal_Int32 i = xIndex->getCount() -1; i>= 1; i--)
152 uno::Reference< sheet::XSpreadsheet > xSheet(xIndex->getByIndex(i), uno::UNO_QUERY);
153 uno::Reference< container::XNamed > xNamed( xSheet, uno::UNO_QUERY_THROW );
154 if (xNamed.is())
156 xNameContainer->removeByName(xNamed->getName());
160 uno::Reference< sheet::XSpreadsheet > xSheet(xIndex->getByIndex(0), uno::UNO_QUERY); uno::Reference< container::XNamed > xNamed( xSheet, uno::UNO_QUERY_THROW );
161 if (xNamed.is())
163 xNamed->setName(aSheetName);
168 static uno::Reference<frame::XModel>
169 openNewDoc(rtl::OUString aSheetName )
171 uno::Reference<frame::XModel> xModel;
174 uno::Reference< beans::XPropertySet > xProps( ::comphelper::getProcessServiceFactory(), uno::UNO_QUERY_THROW );
175 uno::Reference< uno::XComponentContext > xContext( xProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DefaultContext" ))), uno::UNO_QUERY_THROW );
176 uno::Reference<lang::XMultiComponentFactory > xServiceManager(
177 xContext->getServiceManager(), uno::UNO_QUERY_THROW );
179 uno::Reference <frame::XComponentLoader > xComponentLoader(
180 xServiceManager->createInstanceWithContext(
181 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.Desktop" ) ),
182 xContext ), uno::UNO_QUERY_THROW );
184 uno::Reference<lang::XComponent > xComponent( xComponentLoader->loadComponentFromURL(
185 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "private:factory/scalc" ) ),
186 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "_blank" ) ), 0,
187 uno::Sequence < ::com::sun::star::beans::PropertyValue >() ) );
188 uno::Reference <sheet::XSpreadsheetDocument> xSpreadDoc( xComponent, uno::UNO_QUERY_THROW );
189 if ( xSpreadDoc.is() )
191 removeAllSheets(xSpreadDoc,aSheetName);
193 xModel.set(xSpreadDoc,uno::UNO_QUERY_THROW);
195 catch ( uno::Exception & /*e*/ )
198 return xModel;
201 ScVbaWorksheet::ScVbaWorksheet( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext ) : WorksheetImpl_BASE( xParent, xContext )
204 ScVbaWorksheet::ScVbaWorksheet(const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext,
205 const uno::Reference< sheet::XSpreadsheet >& xSheet,
206 const uno::Reference< frame::XModel >& xModel ) throw (uno::RuntimeException) : WorksheetImpl_BASE( xParent, xContext ), mxSheet( xSheet ), mxModel(xModel)
210 ScVbaWorksheet::ScVbaWorksheet( uno::Sequence< uno::Any> const & args,
211 uno::Reference< uno::XComponentContext> const & xContext ) throw ( lang::IllegalArgumentException ) : WorksheetImpl_BASE( getXSomethingFromArgs< XHelperInterface >( args, 0 ), xContext ), mxModel( getXSomethingFromArgs< frame::XModel >( args, 1 ) )
213 if ( args.getLength() < 2 )
214 throw lang::IllegalArgumentException();
216 rtl::OUString sSheetName;
217 args[2] >>= sSheetName;
219 uno::Reference< sheet::XSpreadsheetDocument > xSpreadDoc( mxModel, uno::UNO_QUERY_THROW );
220 uno::Reference< container::XNameAccess > xNameAccess( xSpreadDoc->getSheets(), uno::UNO_QUERY_THROW );
221 mxSheet.set( xNameAccess->getByName( sSheetName ), uno::UNO_QUERY_THROW );
224 ::rtl::OUString
225 ScVbaWorksheet::getName() throw (uno::RuntimeException)
227 uno::Reference< container::XNamed > xNamed( getSheet(), uno::UNO_QUERY_THROW );
228 return xNamed->getName();
231 void
232 ScVbaWorksheet::setName(const ::rtl::OUString &rName ) throw (uno::RuntimeException)
234 uno::Reference< container::XNamed > xNamed( getSheet(), uno::UNO_QUERY_THROW );
235 xNamed->setName( rName );
238 sal_Bool
239 ScVbaWorksheet::getVisible() throw (uno::RuntimeException)
241 uno::Reference< beans::XPropertySet > xProps( getSheet(), uno::UNO_QUERY_THROW );
242 uno::Any aValue = xProps->getPropertyValue
243 (rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "IsVisible" ) ) );
244 sal_Bool bRet = false;
245 aValue >>= bRet;
246 return bRet;
249 void
250 ScVbaWorksheet::setVisible( sal_Bool bVisible ) throw (uno::RuntimeException)
252 uno::Reference< beans::XPropertySet > xProps( getSheet(), uno::UNO_QUERY_THROW );
253 uno::Any aValue( bVisible );
254 xProps->setPropertyValue
255 (rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "IsVisible" ) ), aValue);
258 sal_Int16
259 ScVbaWorksheet::getIndex() throw (uno::RuntimeException)
261 return getSheetID() + 1;
264 sal_Int32
265 ScVbaWorksheet::getEnableSelection() throw (uno::RuntimeException)
267 uno::Reference <sheet::XSpreadsheetDocument> xSpreadDoc( getModel(), uno::UNO_QUERY_THROW );
268 SCTAB nTab = 0;
269 rtl::OUString aSheetName = getName();
270 bool bSheetExists = nameExists (xSpreadDoc, aSheetName, nTab);
271 if ( bSheetExists )
273 uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_QUERY_THROW );
274 ScDocument* pDoc = excel::getDocShell( xModel )->GetDocument();
275 ScTableProtection* pProtect = pDoc->GetTabProtection(nTab);
276 sal_Bool bLockedCells = sal_False;
277 sal_Bool bUnlockedCells = sal_False;
278 if( pProtect )
280 bLockedCells = pProtect->isOptionEnabled(ScTableProtection::SELECT_LOCKED_CELLS);
281 bUnlockedCells = pProtect->isOptionEnabled(ScTableProtection::SELECT_UNLOCKED_CELLS);
283 if( bLockedCells )
284 return excel::XlEnableSelection::xlNoRestrictions;
285 if( bUnlockedCells )
286 return excel::XlEnableSelection::xlUnlockedCells;
287 return excel::XlEnableSelection::xlNoSelection;
289 else
290 throw uno::RuntimeException(::rtl::OUString(
291 RTL_CONSTASCII_USTRINGPARAM( "Sheet Name does not exist. ") ),
292 uno::Reference< XInterface >() );
293 return excel::XlEnableSelection::xlNoSelection;
297 void
298 ScVbaWorksheet::setEnableSelection( sal_Int32 nSelection ) throw (uno::RuntimeException)
300 if( (nSelection != excel::XlEnableSelection::xlNoRestrictions) &&
301 (nSelection != excel::XlEnableSelection::xlUnlockedCells) &&
302 (nSelection != excel::XlEnableSelection::xlNoSelection) )
304 DebugHelper::exception(SbERR_BAD_PARAMETER, rtl::OUString() );
307 uno::Reference <sheet::XSpreadsheetDocument> xSpreadDoc( getModel(), uno::UNO_QUERY_THROW );
308 SCTAB nTab = 0;
309 rtl::OUString aSheetName = getName();
310 bool bSheetExists = nameExists (xSpreadDoc, aSheetName, nTab);
311 if ( bSheetExists )
313 uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_QUERY_THROW );
314 ScDocument* pDoc = excel::getDocShell( xModel )->GetDocument();
315 ScTableProtection* pProtect = pDoc->GetTabProtection(nTab);
316 // default is xlNoSelection
317 sal_Bool bLockedCells = sal_False;
318 sal_Bool bUnlockedCells = sal_False;
319 if( nSelection == excel::XlEnableSelection::xlNoRestrictions )
321 bLockedCells = sal_True;
322 bUnlockedCells = sal_True;
324 else if( nSelection == excel::XlEnableSelection::xlUnlockedCells )
326 bUnlockedCells = sal_True;
328 if( pProtect )
330 pProtect->setOption( ScTableProtection::SELECT_LOCKED_CELLS, bLockedCells );
331 pProtect->setOption( ScTableProtection::SELECT_UNLOCKED_CELLS, bUnlockedCells );
334 else
335 throw uno::RuntimeException(::rtl::OUString(
336 RTL_CONSTASCII_USTRINGPARAM( "Sheet Name does not exist. ") ),
337 uno::Reference< XInterface >() );
341 uno::Reference< excel::XRange >
342 ScVbaWorksheet::getUsedRange() throw (uno::RuntimeException)
344 uno::Reference< sheet::XSheetCellRange > xSheetCellRange(getSheet(), uno::UNO_QUERY_THROW );
345 uno::Reference< sheet::XSheetCellCursor > xSheetCellCursor( getSheet()->createCursorByRange( xSheetCellRange ), uno::UNO_QUERY_THROW );
346 uno::Reference<sheet::XUsedAreaCursor> xUsedCursor(xSheetCellCursor,uno::UNO_QUERY_THROW);
347 xUsedCursor->gotoStartOfUsedArea( false );
348 xUsedCursor->gotoEndOfUsedArea( true );
349 uno::Reference< table::XCellRange > xRange( xSheetCellCursor, uno::UNO_QUERY);
350 return new ScVbaRange(this, mxContext, xRange);
353 uno::Reference< excel::XOutline >
354 ScVbaWorksheet::Outline( ) throw (uno::RuntimeException)
356 uno::Reference<sheet::XSheetOutline> xOutline(getSheet(),uno::UNO_QUERY_THROW);
357 return new ScVbaOutline( this, mxContext, xOutline);
360 uno::Reference< excel::XPageSetup >
361 ScVbaWorksheet::PageSetup( ) throw (uno::RuntimeException)
363 return new ScVbaPageSetup( this, mxContext, getSheet(), getModel() );
366 uno::Any
367 ScVbaWorksheet::HPageBreaks( const uno::Any& aIndex ) throw (uno::RuntimeException)
369 uno::Reference< sheet::XSheetPageBreak > xSheetPageBreak(getSheet(),uno::UNO_QUERY_THROW);
370 uno::Reference< excel::XHPageBreaks > xHPageBreaks( new ScVbaHPageBreaks( this, mxContext, xSheetPageBreak));
371 if ( aIndex.hasValue() )
372 return xHPageBreaks->Item( aIndex, uno::Any());
373 return uno::makeAny( xHPageBreaks );
376 uno::Any
377 ScVbaWorksheet::VPageBreaks( const uno::Any& aIndex ) throw ( uno::RuntimeException )
379 uno::Reference< sheet::XSheetPageBreak > xSheetPageBreak( getSheet(), uno::UNO_QUERY_THROW );
380 uno::Reference< excel::XVPageBreaks > xVPageBreaks( new ScVbaVPageBreaks( this, mxContext, xSheetPageBreak ) );
381 if( aIndex.hasValue() )
382 return xVPageBreaks->Item( aIndex, uno::Any());
383 return uno::makeAny( xVPageBreaks );
386 sal_Int32
387 ScVbaWorksheet::getStandardWidth() throw (uno::RuntimeException)
389 return STANDARDWIDTH ;
392 sal_Int32
393 ScVbaWorksheet::getStandardHeight() throw (uno::RuntimeException)
395 return STANDARDHEIGHT;
398 sal_Bool
399 ScVbaWorksheet::getProtectionMode() throw (uno::RuntimeException)
401 return false;
404 sal_Bool
405 ScVbaWorksheet::getProtectContents()throw (uno::RuntimeException)
407 uno::Reference<util::XProtectable > xProtectable(getSheet(), uno::UNO_QUERY_THROW);
408 return xProtectable->isProtected();
411 sal_Bool
412 ScVbaWorksheet::getProtectDrawingObjects() throw (uno::RuntimeException)
414 #ifdef CWSSCSHEETPROTECTION
415 SCTAB nTab = 0;
416 rtl::OUString aSheetName = getName();
417 uno::Reference <sheet::XSpreadsheetDocument> xSpreadDoc( getModel(), uno::UNO_QUERY_THROW );
418 bool bSheetExists = nameExists (xSpreadDoc, aSheetName, nTab);
419 if ( bSheetExists )
421 uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_QUERY_THROW );
422 ScDocument* pDoc = excel::getDocShell( xModel )->GetDocument();
423 ScTableProtection* pProtect = pDoc->GetTabProtection(nTab);
424 if ( pProtect )
425 return pProtect->getOption( ScTableProtection::OBJECTS );
427 #else
428 return getProtectContents(); // fallback
429 #endif
430 return false;
433 void
434 ScVbaWorksheet::Activate() throw (uno::RuntimeException)
436 uno::Reference< sheet::XSpreadsheetView > xSpreadsheet(
437 getModel()->getCurrentController(), uno::UNO_QUERY_THROW );
438 xSpreadsheet->setActiveSheet(getSheet());
441 void
442 ScVbaWorksheet::Select() throw (uno::RuntimeException)
444 Activate();
447 void
448 ScVbaWorksheet::Move( const uno::Any& Before, const uno::Any& After ) throw (uno::RuntimeException)
450 rtl::OUString aSheetName;
451 uno::Reference<excel::XWorksheet> xSheet;
452 rtl::OUString aCurrSheetName =getName();
454 if (!(Before >>= xSheet) && !(After >>=xSheet)&& !(Before.hasValue()) && !(After.hasValue()))
456 uno::Reference< sheet::XSheetCellCursor > xSheetCellCursor = getSheet()->createCursor( );
457 uno::Reference<sheet::XUsedAreaCursor> xUsedCursor(xSheetCellCursor,uno::UNO_QUERY_THROW);
458 uno::Reference< table::XCellRange > xRange1( xSheetCellCursor, uno::UNO_QUERY);
459 // #FIXME needs worksheet as parent
460 uno::Reference<excel::XRange> xRange = new ScVbaRange( this, mxContext, xRange1);
461 if (xRange.is())
462 xRange->Select();
463 excel::implnCopy(mxModel);
464 uno::Reference<frame::XModel> xModel = openNewDoc(aCurrSheetName);
465 if (xModel.is())
467 excel::implnPaste(xModel);
468 Delete();
470 return ;
473 uno::Reference <sheet::XSpreadsheetDocument> xSpreadDoc( getModel(), uno::UNO_QUERY_THROW );
474 SCTAB nDest = 0;
475 aSheetName = xSheet->getName();
476 bool bSheetExists = nameExists (xSpreadDoc, aSheetName, nDest);
477 if ( bSheetExists )
479 sal_Bool bAfter = After.hasValue();
480 if (bAfter)
481 nDest++;
482 uno::Reference<sheet::XSpreadsheets> xSheets = xSpreadDoc->getSheets();
483 xSheets->moveByName(aCurrSheetName,nDest);
487 void
488 ScVbaWorksheet::Copy( const uno::Any& Before, const uno::Any& After ) throw (uno::RuntimeException)
490 rtl::OUString aSheetName;
491 uno::Reference<excel::XWorksheet> xSheet;
492 rtl::OUString aCurrSheetName =getName();
493 if (!(Before >>= xSheet) && !(After >>=xSheet)&& !(Before.hasValue()) && !(After.hasValue()))
495 uno::Reference< sheet::XSheetCellCursor > xSheetCellCursor = getSheet()->createCursor( );
496 uno::Reference<sheet::XUsedAreaCursor> xUsedCursor(xSheetCellCursor,uno::UNO_QUERY_THROW);
497 uno::Reference< table::XCellRange > xRange1( xSheetCellCursor, uno::UNO_QUERY);
498 uno::Reference<excel::XRange> xRange = new ScVbaRange( this, mxContext, xRange1);
499 if (xRange.is())
500 xRange->Select();
501 excel::implnCopy(mxModel);
502 uno::Reference<frame::XModel> xModel = openNewDoc(aCurrSheetName);
503 if (xModel.is())
505 excel::implnPaste(xModel);
507 return;
510 uno::Reference <sheet::XSpreadsheetDocument> xSpreadDoc( getModel(), uno::UNO_QUERY );
511 SCTAB nDest = 0;
512 aSheetName = xSheet->getName();
513 bool bSheetExists = nameExists (xSpreadDoc, aSheetName, nDest );
515 if ( bSheetExists )
517 sal_Bool bAfter = After.hasValue();
518 if(bAfter)
519 nDest++;
520 uno::Reference<sheet::XSpreadsheets> xSheets = xSpreadDoc->getSheets();
521 getNewSpreadsheetName(aSheetName,aCurrSheetName,xSpreadDoc);
522 xSheets->copyByName(aCurrSheetName,aSheetName,nDest);
527 void
528 ScVbaWorksheet::Paste( const uno::Any& Destination, const uno::Any& /*Link*/ ) throw (uno::RuntimeException)
530 // #TODO# #FIXME# Link is not used
531 uno::Reference<excel::XRange> xRange( Destination, uno::UNO_QUERY );
532 if ( xRange.is() )
533 xRange->Select();
534 excel::implnPaste( mxModel );
537 void
538 ScVbaWorksheet::Delete() throw (uno::RuntimeException)
540 uno::Reference <sheet::XSpreadsheetDocument> xSpreadDoc( getModel(), uno::UNO_QUERY_THROW );
541 rtl::OUString aSheetName = getName();
542 if ( xSpreadDoc.is() )
544 SCTAB nTab = 0;
545 if (!nameExists(xSpreadDoc, aSheetName, nTab ))
547 return;
549 uno::Reference<sheet::XSpreadsheets> xSheets = xSpreadDoc->getSheets();
550 uno::Reference<container::XNameContainer> xNameContainer(xSheets,uno::UNO_QUERY_THROW);
551 xNameContainer->removeByName(aSheetName);
552 mxSheet.clear();
556 uno::Reference< excel::XWorksheet >
557 ScVbaWorksheet::getSheetAtOffset(SCTAB offset) throw (uno::RuntimeException)
559 uno::Reference <sheet::XSpreadsheetDocument> xSpreadDoc( getModel(), uno::UNO_QUERY_THROW );
560 uno::Reference <sheet::XSpreadsheets> xSheets( xSpreadDoc->getSheets(), uno::UNO_QUERY_THROW );
561 uno::Reference <container::XIndexAccess> xIndex( xSheets, uno::UNO_QUERY_THROW );
563 rtl::OUString aName = getName();
564 SCTAB nIdx = 0;
565 bool bSheetExists = nameExists (xSpreadDoc, aName, nIdx );
567 if ( !bSheetExists )
568 return uno::Reference< excel::XWorksheet >();
569 nIdx = nIdx + offset;
570 uno::Reference< sheet::XSpreadsheet > xSheet(xIndex->getByIndex(nIdx), uno::UNO_QUERY_THROW);
571 // parent will be the parent of 'this' worksheet
572 return new ScVbaWorksheet (getParent(), mxContext, xSheet, getModel());
575 uno::Reference< excel::XWorksheet >
576 ScVbaWorksheet::getNext() throw (uno::RuntimeException)
578 //VBA, minz@cn.ibm.com. catch the exception for index out of bound
579 try{
580 return getSheetAtOffset(static_cast<SCTAB>(1));
581 }catch( lang::IndexOutOfBoundsException& /*e*/ )
583 return NULL;
587 uno::Reference< excel::XWorksheet >
588 ScVbaWorksheet::getPrevious() throw (uno::RuntimeException)
590 //VBA, minz@cn.ibm.com. catch the exception for index out of bound
591 try{
592 return getSheetAtOffset(-1);
593 }catch( lang::IndexOutOfBoundsException& /*e*/ )
595 return NULL;
600 void
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)
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 );
611 void
612 ScVbaWorksheet::Unprotect( const uno::Any& Password ) throw (uno::RuntimeException)
614 uno::Reference<util::XProtectable > xProtectable(getSheet(), uno::UNO_QUERY_THROW);
615 ::rtl::OUString aPasswd;
616 Password >>= aPasswd;
617 xProtectable->unprotect( aPasswd );
620 void
621 ScVbaWorksheet::Calculate() throw (uno::RuntimeException)
623 uno::Reference <sheet::XCalculatable> xReCalculate(getModel(), uno::UNO_QUERY_THROW);
624 xReCalculate->calculate();
627 uno::Reference< excel::XRange >
628 ScVbaWorksheet::Range( const ::uno::Any& Cell1, const ::uno::Any& Cell2 ) throw (uno::RuntimeException)
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 );
635 void
636 ScVbaWorksheet::CheckSpelling( const uno::Any& /*CustomDictionary*/,const uno::Any& /*IgnoreUppercase*/,const uno::Any& /*AlwaysSuggest*/, const uno::Any& /*SpellingLang*/ ) throw (uno::RuntimeException)
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);
644 uno::Reference< excel::XRange >
645 ScVbaWorksheet::getSheetRange() throw (uno::RuntimeException)
647 uno::Reference< table::XCellRange > xRange( getSheet(),uno::UNO_QUERY_THROW );
648 return uno::Reference< excel::XRange >( new ScVbaRange( this, mxContext, xRange ) );
651 // These are hacks - we prolly (somehow) need to inherit
652 // the vbarange functionality here ...
653 uno::Reference< excel::XRange >
654 ScVbaWorksheet::Cells( const ::uno::Any &nRow, const ::uno::Any &nCol )
655 throw (uno::RuntimeException)
657 return getSheetRange()->Cells( nRow, nCol );
660 uno::Reference< excel::XRange >
661 ScVbaWorksheet::Rows(const uno::Any& aIndex ) throw (uno::RuntimeException)
663 return getSheetRange()->Rows( aIndex );
666 uno::Reference< excel::XRange >
667 ScVbaWorksheet::Columns( const uno::Any& aIndex ) throw (uno::RuntimeException)
669 return getSheetRange()->Columns( aIndex );
672 uno::Any SAL_CALL
673 ScVbaWorksheet::ChartObjects( const uno::Any& Index ) throw (uno::RuntimeException)
675 if ( !mxCharts.is() )
677 uno::Reference< table::XTableChartsSupplier > xChartSupplier( getSheet(), uno::UNO_QUERY_THROW );
678 uno::Reference< table::XTableCharts > xTableCharts = xChartSupplier->getCharts();
680 uno::Reference< drawing::XDrawPageSupplier > xDrawPageSupplier( getSheet(), uno::UNO_QUERY_THROW ); //VBA, minz@cn.ibm.com.
681 mxCharts = new ScVbaChartObjects( this, mxContext, xTableCharts, xDrawPageSupplier );
683 if ( Index.hasValue() )
685 uno::Reference< XCollection > xColl( mxCharts, uno::UNO_QUERY_THROW );
686 return xColl->Item( Index, uno::Any() );
688 else
689 return uno::makeAny( mxCharts );
693 uno::Any SAL_CALL
694 ScVbaWorksheet::PivotTables( const uno::Any& Index ) throw (uno::RuntimeException)
696 uno::Reference< css::sheet::XSpreadsheet > xSheet = getSheet();
697 uno::Reference< sheet::XDataPilotTablesSupplier > xTables(xSheet, uno::UNO_QUERY_THROW ) ;
698 uno::Reference< container::XIndexAccess > xIndexAccess( xTables->getDataPilotTables(), uno::UNO_QUERY_THROW );
700 uno::Reference< XCollection > xColl( new ScVbaPivotTables( this, mxContext, xIndexAccess ) );
701 if ( Index.hasValue() )
702 return xColl->Item( Index, uno::Any() );
703 return uno::makeAny( xColl );
706 uno::Any SAL_CALL
707 ScVbaWorksheet::Comments( const uno::Any& Index ) throw (uno::RuntimeException)
709 uno::Reference< css::sheet::XSpreadsheet > xSheet = getSheet();
710 uno::Reference< sheet::XSheetAnnotationsSupplier > xAnnosSupp( xSheet, uno::UNO_QUERY_THROW );
711 uno::Reference< sheet::XSheetAnnotations > xAnnos( xAnnosSupp->getAnnotations(), uno::UNO_QUERY_THROW );
712 uno::Reference< container::XIndexAccess > xIndexAccess( xAnnos, uno::UNO_QUERY_THROW );
713 uno::Reference< XCollection > xColl( new ScVbaComments( this, mxContext, xIndexAccess ) );
714 if ( Index.hasValue() )
715 return xColl->Item( Index, uno::Any() );
716 return uno::makeAny( xColl );
719 uno::Any SAL_CALL
720 ScVbaWorksheet::OLEObjects( const uno::Any& Index ) throw (uno::RuntimeException)
722 ScVbaOLEObjects* aOleObjects;
723 uno::Reference< sheet::XSpreadsheet > xSpreadsheet( getSheet(), uno::UNO_QUERY_THROW );
724 uno::Reference< drawing::XDrawPageSupplier > xDrawPageSupplier( xSpreadsheet, uno::UNO_QUERY_THROW );
725 uno::Reference< drawing::XDrawPage > xDrawPage( xDrawPageSupplier->getDrawPage(), uno::UNO_QUERY_THROW );
726 uno::Reference< container::XIndexAccess > xIndexAccess( xDrawPage, uno::UNO_QUERY_THROW );
727 aOleObjects = new ScVbaOLEObjects( this, mxContext, xIndexAccess );
729 if( Index.hasValue() )
731 return aOleObjects->Item( Index, uno::Any() );
733 else
735 return uno::makeAny( uno::Reference< excel::XOLEObjects> ( aOleObjects ) );
738 uno::Any SAL_CALL
739 ScVbaWorksheet::Shapes( const uno::Any& aIndex ) throw (uno::RuntimeException)
741 uno::Reference< sheet::XSpreadsheet > xSpreadsheet( getSheet(), uno::UNO_QUERY_THROW );
742 uno::Reference< drawing::XDrawPageSupplier > xDrawPageSupplier( xSpreadsheet, uno::UNO_QUERY_THROW );
743 uno::Reference< drawing::XShapes > xShapes( xDrawPageSupplier->getDrawPage(), uno::UNO_QUERY_THROW );
744 uno::Reference< container::XIndexAccess > xIndexAccess( xShapes, uno::UNO_QUERY_THROW );
746 uno::Reference< msforms::XShapes> xVbaShapes( new ScVbaShapes( this, mxContext, xIndexAccess, getModel() ) );
747 if ( aIndex.hasValue() )
748 return xVbaShapes->Item( aIndex, uno::Any() );
749 return uno::makeAny( xVbaShapes );
752 void SAL_CALL
753 ScVbaWorksheet::ShowDataForm( ) throw (uno::RuntimeException)
755 #ifdef VBA_OOBUILD_HACK
756 uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_QUERY_THROW );
757 ScTabViewShell* pTabViewShell = excel::getBestViewShell( xModel );
759 ScAbstractDialogFactory* pFact = ScAbstractDialogFactory::Create();
760 DBG_ASSERT(pFact, "ScAbstractFactory create fail!");//CHINA001
762 AbstractScDataFormDlg* pDlg = pFact->CreateScDataFormDlg( pTabViewShell->GetDialogParent(),RID_SCDLG_DATAFORM, pTabViewShell);
763 DBG_ASSERT(pDlg, "Dialog create fail!");//CHINA001
765 pDlg->Execute();
766 #else
767 throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Not implemented") ), uno::Reference< uno::XInterface >() );
768 #endif
771 uno::Any SAL_CALL
772 ScVbaWorksheet::Evaluate( const ::rtl::OUString& Name ) throw (uno::RuntimeException)
774 // #TODO Evaluate allows other things to be evaluated, e.g. functions
775 // I think ( like SIN(3) etc. ) need to investigate that
776 // named Ranges also? e.g. [MyRange] if so need a list of named ranges
777 uno::Any aVoid;
778 return uno::Any( Range( uno::Any( Name ), aVoid ) );
782 uno::Reference< beans::XIntrospectionAccess > SAL_CALL
783 ScVbaWorksheet::getIntrospection( ) throw (uno::RuntimeException)
785 return uno::Reference< beans::XIntrospectionAccess >();
788 uno::Any SAL_CALL
789 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)
791 OSL_TRACE("** ScVbaWorksheet::invoke( %s ), will barf",
792 rtl::OUStringToOString( aFunctionName, RTL_TEXTENCODING_UTF8 ).getStr() );
794 throw uno::RuntimeException(); // unsupported operation
797 void SAL_CALL
798 ScVbaWorksheet::setValue( const ::rtl::OUString& aPropertyName, const uno::Any& aValue ) throw (beans::UnknownPropertyException, script::CannotConvertException, reflection::InvocationTargetException, uno::RuntimeException)
800 setDefaultPropByIntrospection( uno::makeAny( getValue( aPropertyName ) ), aValue );
802 uno::Any SAL_CALL
803 ScVbaWorksheet::getValue( const ::rtl::OUString& aPropertyName ) throw (beans::UnknownPropertyException, uno::RuntimeException)
805 uno::Reference< drawing::XControlShape > xControlShape( getControlShape( aPropertyName ), uno::UNO_QUERY_THROW );
807 uno::Reference<lang::XMultiComponentFactory > xServiceManager( mxContext->getServiceManager(), uno::UNO_QUERY_THROW );
808 uno::Reference< XControlProvider > xControlProvider( xServiceManager->createInstanceWithContext( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ooo.vba.ControlProvider" ) ), mxContext ), uno::UNO_QUERY_THROW );
809 uno::Reference< msforms::XControl > xControl( xControlProvider->createControl( xControlShape, getModel() ) );
810 return uno::makeAny( xControl );
813 ::sal_Bool SAL_CALL
814 ScVbaWorksheet::hasMethod( const ::rtl::OUString& /*aName*/ ) throw (uno::RuntimeException)
816 return sal_False;
819 uno::Reference< container::XNameAccess >
820 ScVbaWorksheet::getFormControls()
822 uno::Reference< container::XNameAccess > xFormControls;
825 uno::Reference< sheet::XSpreadsheet > xSpreadsheet( getSheet(), uno::UNO_QUERY_THROW );
826 uno::Reference< drawing::XDrawPageSupplier > xDrawPageSupplier( xSpreadsheet, uno::UNO_QUERY_THROW );
827 uno::Reference< form::XFormsSupplier > xFormSupplier( xDrawPageSupplier->getDrawPage(), uno::UNO_QUERY_THROW );
828 uno::Reference< container::XIndexAccess > xIndexAccess( xFormSupplier->getForms(), uno::UNO_QUERY_THROW );
829 // get the www-standard container ( maybe we should access the
830 // 'www-standard' by name rather than index, this seems an
831 // implementation detail
832 xFormControls.set( xIndexAccess->getByIndex(0), uno::UNO_QUERY_THROW );
835 catch( uno::Exception& )
838 return xFormControls;
841 ::sal_Bool SAL_CALL
842 ScVbaWorksheet::hasProperty( const ::rtl::OUString& aName ) throw (uno::RuntimeException)
844 uno::Reference< container::XNameAccess > xFormControls( getFormControls() );
845 if ( xFormControls.is() )
846 return xFormControls->hasByName( aName );
847 return sal_False;
850 uno::Any
851 ScVbaWorksheet::getControlShape( const ::rtl::OUString& sName )
853 // ideally we would get an XControl object but it appears an XControl
854 // implementation only exists for a Control implementation optained from the
855 // view ( e.g. in basic you would get this from
856 // thiscomponent.currentcontroller.getControl( controlModel ) )
857 // and the thing to realise is that it is only possible to get an XControl
858 // for a currently displayed control :-( often we would want to modify
859 // a control not on the active sheet. But.. you can always access the
860 // XControlShape from the DrawPage whether that is the active drawpage or not
862 uno::Reference< drawing::XDrawPageSupplier > xDrawPageSupplier( getSheet(), uno::UNO_QUERY_THROW );
863 uno::Reference< container::XIndexAccess > xIndexAccess( xDrawPageSupplier->getDrawPage(), uno::UNO_QUERY_THROW );
865 sal_Int32 nCount = xIndexAccess->getCount();
866 for( int index = 0; index < nCount; index++ )
868 uno::Any aUnoObj = xIndexAccess->getByIndex( index );
869 // It seems there are some drawing objects that can not query into Control shapes?
870 uno::Reference< drawing::XControlShape > xControlShape( aUnoObj, uno::UNO_QUERY );
871 if( xControlShape.is() )
873 uno::Reference< container::XNamed > xNamed( xControlShape->getControl(), uno::UNO_QUERY_THROW );
874 if( sName.equals( xNamed->getName() ))
876 return aUnoObj;
880 return uno::Any();
884 rtl::OUString&
885 ScVbaWorksheet::getServiceImplName()
887 static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaWorksheet") );
888 return sImplName;
890 void SAL_CALL
891 ScVbaWorksheet::setEnableCalculation( ::sal_Bool bEnableCalculation ) throw ( script::BasicErrorException, uno::RuntimeException)
893 uno::Reference <sheet::XCalculatable> xCalculatable(getModel(), uno::UNO_QUERY_THROW);
894 xCalculatable->enableAutomaticCalculation( bEnableCalculation);
896 ::sal_Bool SAL_CALL
897 ScVbaWorksheet::getEnableCalculation( ) throw (css::script::BasicErrorException, css::uno::RuntimeException)
899 uno::Reference <sheet::XCalculatable> xCalculatable(getModel(), uno::UNO_QUERY_THROW);
900 return xCalculatable->isAutomaticCalculationEnabled();
903 uno::Sequence< rtl::OUString >
904 ScVbaWorksheet::getServiceNames()
906 static uno::Sequence< rtl::OUString > aServiceNames;
907 if ( aServiceNames.getLength() == 0 )
909 aServiceNames.realloc( 1 );
910 aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.Worksheet" ) );
912 return aServiceNames;
915 rtl::OUString SAL_CALL
916 ScVbaWorksheet::getCodeName() throw (css::uno::RuntimeException)
918 #if 0
919 uno::Reference <sheet::XSpreadsheetDocument> xSpreadDoc( getModel(), uno::UNO_QUERY_THROW );
920 SCTAB nTab = 0;
921 rtl::OUString aSheetName = getName();
922 bool bSheetExists = nameExists (xSpreadDoc, aSheetName, nTab);
923 if ( bSheetExists )
925 uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_QUERY_THROW );
926 ScDocument* pDoc = getDocShell( xModel )->GetDocument();
927 ScExtDocOptions* pExtOptions = pDoc->GetExtDocOptions();
928 rtl::OUString sCodeName = pExtOptions->GetCodeName( nTab );
929 return sCodeName;
931 else
932 throw uno::RuntimeException(::rtl::OUString(
933 RTL_CONSTASCII_USTRINGPARAM( "Sheet Name does not exist. ") ),
934 uno::Reference< XInterface >() );
935 #else
936 throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Not implemented") ), uno::Reference< uno::XInterface >() );
937 #endif
939 #if 0
940 void SAL_CALL
941 ScVbaWorksheet::setCodeName( const rtl::OUString& sCodeName ) throw (css::uno::RuntimeException)
943 uno::Reference <sheet::XSpreadsheetDocument> xSpreadDoc( getModel(), uno::UNO_QUERY_THROW );
944 SCTAB nTab = 0;
945 rtl::OUString aSheetName = getName();
946 bool bSheetExists = nameExists (xSpreadDoc, aSheetName, nTab);
947 if ( bSheetExists )
949 uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_QUERY_THROW );
950 ScDocument* pDoc = getDocShell( xModel )->GetDocument();
951 ScExtDocOptions* pExtOptions = pDoc->GetExtDocOptions();
952 pExtOptions->SetCodeName( sCodeName, nTab );
954 else
955 throw uno::RuntimeException(::rtl::OUString(
956 RTL_CONSTASCII_USTRINGPARAM( "Sheet Name does not exist. ") ),
957 uno::Reference< XInterface >() );
958 #else
959 void SAL_CALL
960 ScVbaWorksheet::setCodeName( const rtl::OUString& ) throw (css::uno::RuntimeException)
962 throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Not implemented") ), uno::Reference< uno::XInterface >() );
963 #endif
966 sal_Int16
967 ScVbaWorksheet::getSheetID() throw (uno::RuntimeException)
969 uno::Reference< sheet::XCellRangeAddressable > xAddressable( getSheet(), uno::UNO_QUERY_THROW ); //VBA. minz@cn.ibm.com. if ActiveSheet, mxSheet is null.
970 return xAddressable->getRangeAddress().Sheet;
973 void SAL_CALL
974 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)
976 sal_Int32 nTo = 0;
977 sal_Int32 nFrom = 0;
978 sal_Int16 nCopies = 1;
979 sal_Bool bCollate = sal_False;
980 sal_Bool bSelection = sal_False;
981 sal_Bool bIgnorePrintAreas = sal_False;
982 From >>= nFrom;
983 To >>= nTo;
984 Copies >>= nCopies;
985 IgnorePrintAreas >>= bIgnorePrintAreas;
986 if ( nCopies > 1 ) // Collate only useful when more that 1 copy
987 Collate >>= bCollate;
989 if ( !( nFrom || nTo ) )
990 bSelection = sal_True;
992 uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_QUERY_THROW );
993 PrintOutHelper( excel::getBestViewShell( xModel ), From, To, Copies, Preview, ActivePrinter, PrintToFile, Collate, PrToFileName, bSelection );
996 namespace worksheet
998 namespace sdecl = comphelper::service_decl;
999 sdecl::vba_service_class_<ScVbaWorksheet, sdecl::with_args<true> > serviceImpl;
1000 extern sdecl::ServiceDecl const serviceDecl(
1001 serviceImpl,
1002 "ScVbaWorksheet",
1003 "ooo.vba.excel.Worksheet" );