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