1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 #include "vbaworksheets.hxx" 28 29 #include <sfx2/dispatch.hxx> 30 #include <sfx2/app.hxx> 31 #include <sfx2/bindings.hxx> 32 #include <sfx2/request.hxx> 33 #include <sfx2/viewfrm.hxx> 34 #include <sfx2/itemwrapper.hxx> 35 #include <svl/itemset.hxx> 36 #include <svl/eitem.hxx> 37 38 #include <comphelper/processfactory.hxx> 39 #include <cppuhelper/implbase3.hxx> 40 41 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp> 42 #include <com/sun/star/container/XEnumerationAccess.hpp> 43 #include <com/sun/star/sheet/XSpreadsheetView.hpp> 44 #include <com/sun/star/container/XNamed.hpp> 45 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp> 46 #include <com/sun/star/beans/XPropertySet.hpp> 47 48 #include <ooo/vba/excel/XApplication.hpp> 49 #include <tools/string.hxx> 50 #include "tabvwsh.hxx" 51 52 #include "vbaglobals.hxx" 53 #include "vbaworksheet.hxx" 54 #include "vbaworkbook.hxx" 55 #include "unonames.hxx" 56 57 using namespace ::ooo::vba; 58 using namespace ::com::sun::star; 59 60 61 typedef ::cppu::WeakImplHelper1< container::XEnumeration > SheetEnumeration_BASE; 62 typedef ::cppu::WeakImplHelper3< container::XNameAccess, container::XIndexAccess, container::XEnumerationAccess > SheetCollectionHelper_BASE; 63 // a map ( or hashmap ) wont do as we need also to preserve the order 64 // (as added ) of the items 65 typedef std::vector< uno::Reference< sheet::XSpreadsheet > > SheetMap; 66 67 68 // #FIXME #TODO the implementation of the Sheets collections sucks, 69 // e.g. there is no support for tracking sheets added/removed from the collection 70 71 class WorkSheetsEnumeration : public SheetEnumeration_BASE 72 { 73 SheetMap mSheetMap; 74 SheetMap::iterator mIt; 75 public: 76 WorkSheetsEnumeration( const SheetMap& sMap ) : mSheetMap( sMap ), mIt( mSheetMap.begin() ) {} 77 virtual ::sal_Bool SAL_CALL hasMoreElements( ) throw (uno::RuntimeException) 78 { 79 return ( mIt != mSheetMap.end() ); 80 } 81 virtual uno::Any SAL_CALL nextElement( ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException) 82 { 83 if ( !hasMoreElements() ) 84 throw container::NoSuchElementException(); 85 uno::Reference< sheet::XSpreadsheet > xSheet( *mIt++ ); 86 return uno::makeAny( xSheet ) ; 87 } 88 }; 89 90 class SheetCollectionHelper : public SheetCollectionHelper_BASE 91 { 92 SheetMap mSheetMap; 93 SheetMap::iterator cachePos; 94 public: 95 SheetCollectionHelper( const SheetMap& sMap ) : mSheetMap( sMap ), cachePos(mSheetMap.begin()) {} 96 // XElementAccess 97 virtual uno::Type SAL_CALL getElementType( ) throw (uno::RuntimeException) { return sheet::XSpreadsheet::static_type(0); } 98 virtual ::sal_Bool SAL_CALL hasElements( ) throw (uno::RuntimeException) { return ( mSheetMap.size() > 0 ); } 99 // XNameAcess 100 virtual uno::Any SAL_CALL getByName( const ::rtl::OUString& aName ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException) 101 { 102 if ( !hasByName(aName) ) 103 throw container::NoSuchElementException(); 104 return uno::makeAny( *cachePos ); 105 } 106 virtual uno::Sequence< ::rtl::OUString > SAL_CALL getElementNames( ) throw (uno::RuntimeException) 107 { 108 uno::Sequence< rtl::OUString > sNames( mSheetMap.size() ); 109 rtl::OUString* pString = sNames.getArray(); 110 SheetMap::iterator it = mSheetMap.begin(); 111 SheetMap::iterator it_end = mSheetMap.end(); 112 113 for ( ; it != it_end; ++it, ++pString ) 114 { 115 uno::Reference< container::XNamed > xName( *it, uno::UNO_QUERY_THROW ); 116 *pString = xName->getName(); 117 } 118 return sNames; 119 } 120 virtual ::sal_Bool SAL_CALL hasByName( const ::rtl::OUString& aName ) throw (uno::RuntimeException) 121 { 122 cachePos = mSheetMap.begin(); 123 SheetMap::iterator it_end = mSheetMap.end(); 124 for ( ; cachePos != it_end; ++cachePos ) 125 { 126 uno::Reference< container::XNamed > xName( *cachePos, uno::UNO_QUERY_THROW ); 127 if ( aName.equals( xName->getName() ) ) 128 break; 129 } 130 return ( cachePos != it_end ); 131 } 132 133 // XElementAccess 134 virtual ::sal_Int32 SAL_CALL getCount( ) throw (uno::RuntimeException) { return mSheetMap.size(); } 135 virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException ) 136 { 137 if ( Index < 0 || Index >= getCount() ) 138 throw lang::IndexOutOfBoundsException(); 139 140 return uno::makeAny( mSheetMap[ Index ] ); 141 142 } 143 // XEnumerationAccess 144 virtual uno::Reference< container::XEnumeration > SAL_CALL createEnumeration( ) throw (uno::RuntimeException) 145 { 146 return new WorkSheetsEnumeration( mSheetMap ); 147 } 148 }; 149 150 class SheetsEnumeration : public EnumerationHelperImpl 151 { 152 uno::Reference< frame::XModel > m_xModel; 153 public: 154 SheetsEnumeration( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< container::XEnumeration >& xEnumeration, const uno::Reference< frame::XModel >& xModel ) throw ( uno::RuntimeException ) : EnumerationHelperImpl( xParent, xContext, xEnumeration ), m_xModel( xModel ) {} 155 156 virtual uno::Any SAL_CALL nextElement( ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException) 157 { 158 uno::Reference< sheet::XSpreadsheet > xSheet( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW ); 159 uno::Reference< XHelperInterface > xIf = excel::getUnoSheetModuleObj( xSheet ); 160 uno::Any aRet; 161 if ( !xIf.is() ) 162 { 163 // if the Sheet is in a document created by the api unfortunately ( at the 164 // moment, it actually wont have the special Document modules 165 uno::Reference< excel::XWorksheet > xNewSheet( new ScVbaWorksheet( m_xParent, m_xContext, xSheet, m_xModel ) ); 166 aRet <<= xNewSheet; 167 } 168 else 169 aRet <<= xIf; 170 return aRet; 171 } 172 173 }; 174 175 ScVbaWorksheets::ScVbaWorksheets( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< ::com::sun::star::uno::XComponentContext > & xContext, const uno::Reference< container::XIndexAccess >& xSheets, const uno::Reference< frame::XModel >& xModel ): ScVbaWorksheets_BASE( xParent, xContext, xSheets ), mxModel( xModel ), m_xSheets( uno::Reference< sheet::XSpreadsheets >( xSheets, uno::UNO_QUERY ) ) 176 { 177 } 178 179 ScVbaWorksheets::ScVbaWorksheets( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< ::com::sun::star::uno::XComponentContext > & xContext, const uno::Reference< container::XEnumerationAccess >& xEnumAccess, const uno::Reference< frame::XModel >& xModel ): ScVbaWorksheets_BASE( xParent, xContext, uno::Reference< container::XIndexAccess >( xEnumAccess, uno::UNO_QUERY ) ), mxModel(xModel) 180 { 181 } 182 183 // XEnumerationAccess 184 uno::Type 185 ScVbaWorksheets::getElementType() throw (uno::RuntimeException) 186 { 187 return excel::XWorksheet::static_type(0); 188 } 189 190 uno::Reference< container::XEnumeration > 191 ScVbaWorksheets::createEnumeration() throw (uno::RuntimeException) 192 { 193 if ( !m_xSheets.is() ) 194 { 195 uno::Reference< container::XEnumerationAccess > xAccess( m_xIndexAccess, uno::UNO_QUERY_THROW ); 196 return xAccess->createEnumeration(); 197 } 198 uno::Reference< container::XEnumerationAccess > xEnumAccess( m_xSheets, uno::UNO_QUERY_THROW ); 199 return new SheetsEnumeration( this, mxContext, xEnumAccess->createEnumeration(), mxModel ); 200 } 201 202 uno::Any 203 ScVbaWorksheets::createCollectionObject( const uno::Any& aSource ) 204 { 205 uno::Reference< sheet::XSpreadsheet > xSheet( aSource, uno::UNO_QUERY ); 206 uno::Reference< XHelperInterface > xIf = excel::getUnoSheetModuleObj( xSheet ); 207 uno::Any aRet; 208 if ( !xIf.is() ) 209 { 210 // if the Sheet is in a document created by the api unfortunately ( at the 211 // moment, it actually wont have the special Document modules 212 uno::Reference< excel::XWorksheet > xNewSheet( new ScVbaWorksheet( getParent(), mxContext, xSheet, mxModel ) ); 213 aRet <<= xNewSheet; 214 } 215 else 216 aRet <<= xIf; 217 return aRet; 218 } 219 220 // XWorksheets 221 uno::Any 222 ScVbaWorksheets::Add( const uno::Any& Before, const uno::Any& After, 223 const uno::Any& Count, const uno::Any& Type ) throw (uno::RuntimeException) 224 { 225 if ( isSelectedSheets() ) 226 return uno::Any(); // or should we throw? 227 228 rtl::OUString aStringSheet; 229 sal_Bool bBefore(sal_True); 230 SCTAB nSheetIndex = 0; 231 SCTAB nNewSheets = 1, nType = 0; 232 Count >>= nNewSheets; 233 Type >>= nType; 234 SCTAB nCount = 0; 235 236 uno::Reference< excel::XWorksheet > xBeforeAfterSheet; 237 238 if ( Before.hasValue() ) 239 { 240 if ( Before >>= xBeforeAfterSheet ) 241 aStringSheet = xBeforeAfterSheet->getName(); 242 else 243 Before >>= aStringSheet; 244 } 245 246 if (!aStringSheet.getLength() && After.hasValue() ) 247 { 248 if ( After >>= xBeforeAfterSheet ) 249 aStringSheet = xBeforeAfterSheet->getName(); 250 else 251 After >>= aStringSheet; 252 bBefore = sal_False; 253 } 254 if (!aStringSheet.getLength()) 255 { 256 uno::Reference< excel::XApplication > xApplication( Application(), uno::UNO_QUERY_THROW ); 257 aStringSheet = xApplication->getActiveWorkbook()->getActiveSheet()->getName(); 258 bBefore = sal_True; 259 } 260 nCount = static_cast< SCTAB >( m_xIndexAccess->getCount() ); 261 for (SCTAB i=0; i < nCount; i++) 262 { 263 uno::Reference< sheet::XSpreadsheet > xSheet(m_xIndexAccess->getByIndex(i), uno::UNO_QUERY); 264 uno::Reference< container::XNamed > xNamed( xSheet, uno::UNO_QUERY_THROW ); 265 if (xNamed->getName() == aStringSheet) 266 { 267 nSheetIndex = i; 268 break; 269 } 270 } 271 272 if(!bBefore) 273 nSheetIndex++; 274 275 SCTAB nSheetName = nCount + 1L; 276 String aStringBase( RTL_CONSTASCII_USTRINGPARAM("Sheet") ); 277 uno::Any result; 278 for (SCTAB i=0; i < nNewSheets; i++, nSheetName++) 279 { 280 String aStringName = aStringBase; 281 aStringName += String::CreateFromInt32(nSheetName); 282 while (m_xNameAccess->hasByName(aStringName)) 283 { 284 nSheetName++; 285 aStringName = aStringBase; 286 aStringName += String::CreateFromInt32(nSheetName); 287 } 288 m_xSheets->insertNewByName(aStringName, nSheetIndex + i); 289 result = getItemByStringIndex( aStringName ); 290 } 291 uno::Reference< excel::XWorksheet > xNewSheet( result, uno::UNO_QUERY ); 292 if ( xNewSheet.is() ) 293 xNewSheet->Activate(); 294 return result; 295 } 296 297 void 298 ScVbaWorksheets::Delete() throw (uno::RuntimeException) 299 { 300 // #TODO #INVESTIGATE 301 // mmm this method could be trouble if the underlying 302 // uno objects ( the m_xIndexAccess etc ) aren't aware of the 303 // contents that are deleted 304 sal_Int32 nElems = getCount(); 305 for ( sal_Int32 nItem = 1; nItem <= nElems; ++nItem ) 306 { 307 uno::Reference< excel::XWorksheet > xSheet( Item( uno::makeAny( nItem ), uno::Any() ), uno::UNO_QUERY_THROW ); 308 xSheet->Delete(); 309 } 310 } 311 312 bool 313 ScVbaWorksheets::isSelectedSheets() 314 { 315 return !m_xSheets.is(); 316 } 317 318 void SAL_CALL 319 ScVbaWorksheets::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 ) throw (uno::RuntimeException) 320 { 321 sal_Int32 nTo = 0; 322 sal_Int32 nFrom = 0; 323 sal_Int16 nCopies = 1; 324 sal_Bool bCollate = sal_False; 325 sal_Bool bSelection = sal_False; 326 From >>= nFrom; 327 To >>= nTo; 328 Copies >>= nCopies; 329 if ( nCopies > 1 ) // Collate only useful when more that 1 copy 330 Collate >>= bCollate; 331 332 if ( !( nFrom || nTo ) ) 333 if ( isSelectedSheets() ) 334 bSelection = sal_True; 335 336 PrintOutHelper( excel::getBestViewShell( mxModel ), From, To, Copies, Preview, ActivePrinter, PrintToFile, Collate, PrToFileName, bSelection ); 337 } 338 339 uno::Any SAL_CALL 340 ScVbaWorksheets::getVisible() throw (uno::RuntimeException) 341 { 342 sal_Bool bVisible = sal_True; 343 uno::Reference< container::XEnumeration > xEnum( createEnumeration(), uno::UNO_QUERY_THROW ); 344 while ( xEnum->hasMoreElements() ) 345 { 346 uno::Reference< excel::XWorksheet > xSheet( xEnum->nextElement(), uno::UNO_QUERY_THROW ); 347 if ( xSheet->getVisible() == sal_False ) 348 { 349 bVisible = sal_False; 350 break; 351 } 352 } 353 return uno::makeAny( bVisible ); 354 } 355 356 void SAL_CALL 357 ScVbaWorksheets::setVisible( const uno::Any& _visible ) throw (uno::RuntimeException) 358 { 359 sal_Bool bState = sal_False; 360 if ( _visible >>= bState ) 361 { 362 uno::Reference< container::XEnumeration > xEnum( createEnumeration(), uno::UNO_QUERY_THROW ); 363 while ( xEnum->hasMoreElements() ) 364 { 365 uno::Reference< excel::XWorksheet > xSheet( xEnum->nextElement(), uno::UNO_QUERY_THROW ); 366 xSheet->setVisible( bState ); 367 } 368 } 369 else 370 throw uno::RuntimeException( rtl::OUString( 371 RTL_CONSTASCII_USTRINGPARAM( "Visible property doesn't support non boolean #FIXME" ) ), uno::Reference< uno::XInterface >() ); 372 } 373 374 void SAL_CALL 375 ScVbaWorksheets::Select( const uno::Any& Replace ) throw (uno::RuntimeException) 376 { 377 ScTabViewShell* pViewShell = excel::getBestViewShell( mxModel ); 378 if ( !pViewShell ) 379 throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Cannot obtain view shell" ) ), uno::Reference< uno::XInterface >() ); 380 381 ScMarkData& rMarkData = pViewShell->GetViewData()->GetMarkData(); 382 sal_Bool bReplace = sal_True; 383 Replace >>= bReplace; 384 // Replace is defaulted to True, meanining this current collection 385 // becomes the Selection, if it were false then the current selection would 386 // be extended 387 bool bSelectSingle = bReplace; 388 sal_Int32 nElems = getCount(); 389 for ( sal_Int32 nItem = 1; nItem <= nElems; ++nItem ) 390 { 391 uno::Reference< excel::XWorksheet > xSheet( Item( uno::makeAny( nItem ), uno::Any() ), uno::UNO_QUERY_THROW ); 392 ScVbaWorksheet* pSheet = dynamic_cast< ScVbaWorksheet* >( xSheet.get() ); 393 if ( pSheet ) 394 { 395 if ( bSelectSingle ) 396 { 397 rMarkData.SelectOneTable( static_cast< SCTAB >( pSheet->getSheetID() ) ); 398 bSelectSingle = false; 399 } 400 else 401 rMarkData.SelectTable( static_cast< SCTAB >( pSheet->getSheetID() ), sal_True ); 402 403 } 404 } 405 406 407 } 408 409 //ScVbaCollectionBaseImpl 410 uno::Any SAL_CALL 411 ScVbaWorksheets::Item( const uno::Any& Index, const uno::Any& Index2 ) throw (uno::RuntimeException) 412 { 413 if ( Index.getValueTypeClass() == uno::TypeClass_SEQUENCE ) 414 { 415 uno::Reference< script::XTypeConverter > xConverter = getTypeConverter(mxContext); 416 uno::Any aConverted; 417 aConverted = xConverter->convertTo( Index, getCppuType((uno::Sequence< uno::Any >*)0) ); 418 SheetMap mSheets; 419 uno::Sequence< uno::Any > sIndices; 420 aConverted >>= sIndices; 421 sal_Int32 nElems = sIndices.getLength(); 422 for( sal_Int32 index = 0; index < nElems; ++index ) 423 { 424 uno::Reference< excel::XWorksheet > xWorkSheet( ScVbaWorksheets_BASE::Item( sIndices[ index ], Index2 ), uno::UNO_QUERY_THROW ); 425 ScVbaWorksheet* pWorkSheet = dynamic_cast< ScVbaWorksheet* >( xWorkSheet.get() ); 426 if ( pWorkSheet ) 427 { 428 uno::Reference< sheet::XSpreadsheet > xSheet( pWorkSheet->getSheet() , uno::UNO_QUERY_THROW ); 429 uno::Reference< container::XNamed > xName( xSheet, uno::UNO_QUERY_THROW ); 430 mSheets.push_back( xSheet ); 431 } 432 } 433 uno::Reference< container::XIndexAccess > xIndexAccess = new SheetCollectionHelper( mSheets ); 434 uno::Reference< XCollection > xSelectedSheets( new ScVbaWorksheets( this->getParent(), mxContext, xIndexAccess, mxModel ) ); 435 return uno::makeAny( xSelectedSheets ); 436 } 437 return ScVbaWorksheets_BASE::Item( Index, Index2 ); 438 } 439 440 uno::Any 441 ScVbaWorksheets::getItemByStringIndex( const rtl::OUString& sIndex ) throw (uno::RuntimeException) 442 { 443 return ScVbaWorksheets_BASE::getItemByStringIndex( sIndex ); 444 } 445 446 rtl::OUString& 447 ScVbaWorksheets::getServiceImplName() 448 { 449 static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaWorksheets") ); 450 return sImplName; 451 } 452 453 css::uno::Sequence<rtl::OUString> 454 ScVbaWorksheets::getServiceNames() 455 { 456 static uno::Sequence< rtl::OUString > sNames; 457 if ( sNames.getLength() == 0 ) 458 { 459 sNames.realloc( 1 ); 460 sNames[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.Worksheets") ); 461 } 462 return sNames; 463 } 464 465 /*static*/ bool ScVbaWorksheets::nameExists( uno::Reference <sheet::XSpreadsheetDocument>& xSpreadDoc, const ::rtl::OUString & name, SCTAB& nTab ) throw ( lang::IllegalArgumentException ) 466 { 467 if (!xSpreadDoc.is()) 468 throw lang::IllegalArgumentException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "nameExists() xSpreadDoc is null" ) ), uno::Reference< uno::XInterface >(), 1 ); 469 uno::Reference <container::XIndexAccess> xIndex( xSpreadDoc->getSheets(), uno::UNO_QUERY ); 470 if ( xIndex.is() ) 471 { 472 SCTAB nCount = static_cast< SCTAB >( xIndex->getCount() ); 473 for (SCTAB i=0; i < nCount; i++) 474 { 475 uno::Reference< container::XNamed > xNamed( xIndex->getByIndex(i), uno::UNO_QUERY_THROW ); 476 if (xNamed->getName() == name) 477 { 478 nTab = i; 479 return true; 480 } 481 } 482 } 483 return false; 484 } 485