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