25 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
26 #include <com/sun/star/container/XEnumerationAccess.hpp>
27 #include <com/sun/star/sheet/XSpreadsheet.hpp>
28 #include <com/sun/star/container/XNamed.hpp>
29 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
30 #include <com/sun/star/script/XTypeConverter.hpp>
32 #include <ooo/vba/excel/XApplication.hpp>
41 #include <preview.hxx>
47 typedef std::vector< uno::Reference< sheet::XSpreadsheet > >
SheetMap;
54 class WorkSheetsEnumeration :
public ::cppu::WeakImplHelper< container::XEnumeration >
57 SheetMap::iterator mIt;
59 explicit WorkSheetsEnumeration(
const SheetMap& sMap ) : mSheetMap( sMap ), mIt( mSheetMap.begin() ) {}
60 virtual sal_Bool SAL_CALL hasMoreElements( )
override
62 return ( mIt != mSheetMap.end() );
64 virtual uno::Any SAL_CALL nextElement( )
override
66 if ( !hasMoreElements() )
67 throw container::NoSuchElementException();
68 uno::Reference< sheet::XSpreadsheet > xSheet( *mIt++ );
69 return uno::makeAny( xSheet ) ;
73 class SheetCollectionHelper :
public ::cppu::WeakImplHelper< container::XNameAccess,
74 container::XIndexAccess,
75 container::XEnumerationAccess >
78 SheetMap::iterator cachePos;
80 explicit SheetCollectionHelper(
const SheetMap& sMap ) : mSheetMap( sMap ), cachePos(mSheetMap.begin()) {}
83 virtual sal_Bool SAL_CALL hasElements( )
override {
return ( !mSheetMap.empty() ); }
85 virtual uno::Any SAL_CALL getByName(
const OUString&
aName )
override
87 if ( !hasByName(aName) )
88 throw container::NoSuchElementException();
89 return uno::makeAny( *cachePos );
91 virtual uno::Sequence< OUString > SAL_CALL getElementNames( )
override
93 uno::Sequence< OUString > sNames( mSheetMap.size() );
94 OUString* pString = sNames.getArray();
96 for (
const auto& rItem : mSheetMap )
98 uno::Reference< container::XNamed > xName( rItem, uno::UNO_QUERY_THROW );
99 *pString = xName->getName();
104 virtual sal_Bool SAL_CALL hasByName(
const OUString& aName )
override
106 cachePos = mSheetMap.begin();
107 SheetMap::iterator it_end = mSheetMap.end();
108 for ( ; cachePos != it_end; ++cachePos )
110 uno::Reference< container::XNamed > xName( *cachePos, uno::UNO_QUERY_THROW );
111 if ( aName == xName->getName() )
114 return ( cachePos != it_end );
118 virtual ::sal_Int32 SAL_CALL getCount( )
override {
return mSheetMap.size(); }
119 virtual uno::Any SAL_CALL getByIndex( ::sal_Int32
Index )
override
122 throw lang::IndexOutOfBoundsException();
124 return uno::makeAny( mSheetMap[ Index ] );
128 virtual uno::Reference< container::XEnumeration > SAL_CALL createEnumeration( )
override
130 return new WorkSheetsEnumeration( mSheetMap );
136 uno::Reference< frame::XModel >
m_xModel;
139 SheetsEnumeration(
const uno::Reference< XHelperInterface >& xParent,
const uno::Reference< uno::XComponentContext >& xContext,
const uno::Reference< container::XEnumeration >& xEnumeration,
const uno::Reference< frame::XModel >&
xModel ) :
EnumerationHelperImpl( xParent, xContext, xEnumeration ), m_xModel( xModel ) {}
141 virtual uno::Any SAL_CALL nextElement( )
override
143 uno::Reference< sheet::XSpreadsheet > xSheet( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
162 ScVbaWorksheets::ScVbaWorksheets(
const uno::Reference< XHelperInterface >& xParent,
const uno::Reference< css::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 ) )
177 uno::Reference< container::XEnumeration >
182 uno::Reference< container::XEnumerationAccess > xAccess(
m_xIndexAccess, uno::UNO_QUERY_THROW );
183 return xAccess->createEnumeration();
185 uno::Reference< container::XEnumerationAccess > xEnumAccess(
m_xSheets, uno::UNO_QUERY_THROW );
186 return new SheetsEnumeration(
this,
mxContext, xEnumAccess->createEnumeration(),
mxModel );
192 uno::Reference< sheet::XSpreadsheet > xSheet( aSource, uno::UNO_QUERY );
215 OUString aStringSheet;
217 SCTAB nSheetIndex = 0;
219 Count >>= nNewSheets;
223 uno::Reference< excel::XWorksheet > xBeforeAfterSheet;
227 if ( Before >>= xBeforeAfterSheet )
228 aStringSheet = xBeforeAfterSheet->getName();
230 Before >>= aStringSheet;
233 if (aStringSheet.isEmpty() && After.
hasValue() )
235 if ( After >>= xBeforeAfterSheet )
236 aStringSheet = xBeforeAfterSheet->getName();
238 After >>= aStringSheet;
241 if (aStringSheet.isEmpty())
243 uno::Reference< excel::XApplication > xApplication(
Application(), uno::UNO_QUERY_THROW );
244 aStringSheet = xApplication->getActiveWorkbook()->getActiveSheet()->getName();
250 uno::Reference< sheet::XSpreadsheet > xSheet(
m_xIndexAccess->getByIndex(
i), uno::UNO_QUERY);
251 uno::Reference< container::XNamed > xNamed( xSheet, uno::UNO_QUERY_THROW );
252 if (xNamed->getName() == aStringSheet)
262 SCTAB nSheetName = nCount + 1;
263 OUString aStringBase(
"Sheet" );
265 for (
SCTAB i=0;
i < nNewSheets;
i++, nSheetName++)
267 OUString aStringName = aStringBase + OUString::number(nSheetName);
271 aStringName = aStringBase + OUString::number(nSheetName);
273 m_xSheets->insertNewByName(aStringName, nSheetIndex +
i);
276 uno::Reference< excel::XWorksheet > xNewSheet( result, uno::UNO_QUERY );
277 if ( xNewSheet.is() )
278 xNewSheet->Activate();
290 for ( sal_Int32 nItem = 1; nItem <= nElems; ++nItem )
292 uno::Reference< excel::XWorksheet > xSheet(
Item( uno::makeAny( nItem ),
uno::Any() ), uno::UNO_QUERY_THROW );
308 bool bSelection =
false;
312 if ( !( nFrom || nTo ) )
323 uno::Reference< container::XEnumeration > xEnum(
createEnumeration(), uno::UNO_SET_THROW );
324 while ( xEnum->hasMoreElements() )
326 uno::Reference< excel::XWorksheet > xSheet( xEnum->nextElement(), uno::UNO_QUERY_THROW );
327 if ( xSheet->getVisible() == 0 )
333 return uno::makeAny( bVisible );
340 if ( !(_visible >>= bState) )
341 throw uno::RuntimeException(
"Visible property doesn't support non boolean #FIXME" );
343 uno::Reference< container::XEnumeration > xEnum(
createEnumeration(), uno::UNO_SET_THROW );
344 while ( xEnum->hasMoreElements() )
346 uno::Reference< excel::XWorksheet > xSheet( xEnum->nextElement(), uno::UNO_QUERY_THROW );
347 xSheet->setVisible( bState ? 1 : 0 );
357 throw uno::RuntimeException(
"Cannot obtain view shell" );
360 bool bReplace =
true;
361 Replace >>= bReplace;
365 bool bSelectSingle = bReplace;
367 for ( sal_Int32 nItem = 1; nItem <= nElems; ++nItem )
369 uno::Reference< excel::XWorksheet > xSheet(
Item( uno::makeAny( nItem ),
uno::Any() ), uno::UNO_QUERY_THROW );
370 ScVbaWorksheet* pSheet = excel::getImplFromDocModuleWrapper<ScVbaWorksheet>( xSheet );
374 bSelectSingle =
false;
385 uno::Reference<excel::XWorksheet> xSheet;
388 std::vector< uno::Reference< excel::XWorksheet > >
Sheets;
391 for ( nItem = 1; nItem <= nElems; ++nItem)
393 uno::Reference<excel::XWorksheet> xWorksheet(
Item( uno::makeAny( nItem ),
uno::Any() ), uno::UNO_QUERY_THROW );
394 Sheets.push_back(xWorksheet);
396 bool bNewDoc = (!(Before >>= xSheet) && !(After >>=xSheet)&& !(Before.
hasValue()) && !(After.
hasValue()));
398 uno::Reference< excel::XWorksheet > xSrcSheet;
402 xSrcSheet = Sheets.at(0);
403 ScVbaWorksheet* pSrcSheet = excel::getImplFromDocModuleWrapper<ScVbaWorksheet>( xSrcSheet );
412 for (; nItem < nElems; ++nItem )
414 xSrcSheet = Sheets[nItem];
415 ScVbaWorksheet* pSrcSheet = excel::getImplFromDocModuleWrapper<ScVbaWorksheet>( xSrcSheet );
427 if ( Index.getValueTypeClass() == uno::TypeClass_SEQUENCE )
430 uno::Any aConverted = xConverter->convertTo( Index,
cppu::UnoType<uno::Sequence< uno::Any >>::
get() );
432 uno::Sequence< uno::Any > sIndices;
433 aConverted >>= sIndices;
434 for(
const auto& rIndex : std::as_const(sIndices) )
437 ScVbaWorksheet* pWorkSheet = excel::getImplFromDocModuleWrapper<ScVbaWorksheet>( xWorkSheet );
438 uno::Reference< sheet::XSpreadsheet > xSheet( pWorkSheet->
getSheet() , uno::UNO_SET_THROW );
439 uno::Reference< container::XNamed > xName( xSheet, uno::UNO_QUERY_THROW );
440 aSheets.push_back( xSheet );
442 uno::Reference< container::XIndexAccess > xIndexAccess =
new SheetCollectionHelper( aSheets );
444 return uno::makeAny( xSelectedSheets );
452 return "ScVbaWorksheets";
455 css::uno::Sequence<OUString>
458 static uno::Sequence< OUString >
const sNames
460 "ooo.vba.excel.Worksheets"
467 if (!xSpreadDoc.is())
468 throw lang::IllegalArgumentException(
"nameExists() xSpreadDoc is null", uno::Reference< uno::XInterface >(), 1 );
469 uno::Reference <container::XIndexAccess> xIndex( xSpreadDoc->getSheets(), uno::UNO_QUERY );
475 uno::Reference< container::XNamed > xNamed( xIndex->getByIndex(
i), uno::UNO_QUERY_THROW );
476 if (xNamed->getName() == name)
507 ScPreview* pPrvView = pPrvShell->GetPreview();
511 for ( sal_Int32 nItem = 1; nItem <= nElems; ++nItem )
513 uno::Reference< excel::XWorksheet > xSheet(
Item( uno::makeAny( nItem ),
uno::Any() ), uno::UNO_QUERY_THROW );
514 ScVbaWorksheet* pSheet = excel::getImplFromDocModuleWrapper<ScVbaWorksheet>( xSheet );
516 aMarkData.SelectTable(static_cast< SCTAB >( pSheet->
getSheetID() ),
true );
ScMarkData & GetMarkData()
Reference< XTypeConverter > xConverter
SfxViewFrame * GetViewFrame() const
virtual css::uno::Any SAL_CALL Application() override
virtual css::uno::Any createCollectionObject(const css::uno::Any &aSource) override
void PrintOutHelper(SfxViewShell const *pViewShell, const uno::Any &From, const uno::Any &To, const uno::Any &Copies, const uno::Any &Preview, const uno::Any &, const uno::Any &, const uno::Any &Collate, const uno::Any &PrToFileName, bool bUseSelection)
Reference< frame::XModel > m_xModel
todo: It should be possible to have MarkArrays for each table, in order to enable "search all" across...
virtual void DataChanged(const DataChangedEvent &rDCEvt) override
css::uno::Reference< css::container::XIndexAccess > m_xIndexAccess
EnumerationHelperImpl(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext, const css::uno::Reference< css::container::XEnumeration > &xEnumeration)
virtual css::uno::Any SAL_CALL Add(const css::uno::Any &Before, const css::uno::Any &After, const css::uno::Any &Count, const css::uno::Any &Type) override
std::vector< uno::Reference< sheet::XSpreadsheet > > SheetMap
css::uno::Reference< css::frame::XModel2 > mxModel
virtual void SAL_CALL PrintPreview(const css::uno::Any &EnableChanges) override
ScDocument & GetDocument() const
virtual css::uno::Any getItemByStringIndex(const OUString &sIndex)
css::uno::Reference< ov::excel::XWorksheet > createSheetCopy(css::uno::Reference< ov::excel::XWorksheet > const &xSheet, bool bAfter)
exports com.sun.star. sheet
exports com.sun.star. container
uno::Reference< XHelperInterface > getUnoSheetModuleObj(const uno::Reference< table::XCellRange > &xRange)
std::vector< uno::Reference< sheet::XSpreadsheet > > Sheets
SfxFrame & GetFrame() const
virtual css::uno::Any SAL_CALL Item(const css::uno::Any &Index1, const css::uno::Any &Index2) override
SC_DLLPUBLIC void SetSelectedTabs(const ScMarkData &rMark)
virtual css::uno::Sequence< OUString > getServiceNames() override
ScViewData & GetViewData()
void WaitUntilPreviewIsClosed(SfxViewFrame *pViewFrame)
virtual css::uno::Type SAL_CALL getElementType() override
virtual void SAL_CALL setVisible(const css::uno::Any &_visible) override
virtual OUString getServiceImplName() override
void SelectTable(SCTAB nTab, bool bNew)
virtual void SAL_CALL PrintOut(const css::uno::Any &From, const css::uno::Any &To, const css::uno::Any &Copies, const css::uno::Any &Preview, const css::uno::Any &ActivePrinter, const css::uno::Any &PrintToFile, const css::uno::Any &Collate, const css::uno::Any &PrToFileName) override
SC_DLLPUBLIC void SetPageNo(tools::Long nPage)
ScSheetLimits & GetSheetLimits() const
const css::uno::Reference< css::sheet::XSpreadsheet > & getSheet() const
css::uno::Reference< css::container::XNameAccess > m_xNameAccess
void SelectOneTable(SCTAB nTab)
sal_Int16 getSheetID() const
bool isSelectedSheets() const
virtual void SAL_CALL Delete() override
const ScMarkData::MarkedTabsType & GetSelectedTabs() const
css::uno::Type const & get()
static bool nameExists(const css::uno::Reference< css::sheet::XSpreadsheetDocument > &xSpreadDoc, std::u16string_view name, SCTAB &nTab)
const css::uno::Reference< css::frame::XFrame > & GetFrameInterface() const
css::uno::Reference< ov::excel::XWorksheet > createSheetCopyInNewDoc(const OUString &)
ScTabViewShell * getBestViewShell(const css::uno::Reference< css::frame::XModel > &xModel)
virtual void SAL_CALL Select(const css::uno::Any &Replace) override
SC_DLLPUBLIC tools::Long GetFirstPage(SCTAB nTab)
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
void dispatchExecute(SfxViewShell const *pViewShell, sal_uInt16 nSlot)
css::uno::Reference< css::uno::XComponentContext > mxContext
virtual css::uno::Reference< ov::XHelperInterface > SAL_CALL getParent() override
static SfxViewShell * Get(const css::uno::Reference< css::frame::XController > &i_rController)
virtual css::uno::Any SAL_CALL getVisible() override
virtual void SAL_CALL Copy(const css::uno::Any &Before, const css::uno::Any &After) override
css::uno::Reference< css::sheet::XSpreadsheets > m_xSheets
css::uno::Reference< css::frame::XModel > mxModel
Reference< XModel > xModel
uno::Reference< script::XTypeConverter > const & getTypeConverter(const uno::Reference< uno::XComponentContext > &xContext)
virtual css::uno::Any SAL_CALL Item(const css::uno::Any &Index1, const css::uno::Any &) override
Reference< XComponentContext > m_xContext
std::set< SCTAB > MarkedTabsType
ScVbaWorksheets(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext, const css::uno::Reference< css::container::XIndexAccess > &xSheets, const css::uno::Reference< css::frame::XModel > &xModel)
virtual ::sal_Int32 SAL_CALL getCount() override
void SetSelectedTabs(const MarkedTabsType &rTabs)