1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <tools/urlobj.hxx>
22 #include <com/sun/star/util/XProtectable.hpp>
23 #include <com/sun/star/sheet/XNamedRanges.hpp>
24 #include <com/sun/star/sheet/XSpreadsheetView.hpp>
25 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
26 #include <com/sun/star/frame/XStorable.hpp>
27 #include <com/sun/star/beans/XPropertySet.hpp>
28 #include <ooo/vba/excel/XlFileFormat.hpp>
29 #include <ooo/vba/excel/XApplication.hpp>
31 #include "vbaworksheet.hxx"
32 #include "vbaworksheets.hxx"
33 #include "vbaworkbook.hxx"
34 #include "vbawindows.hxx"
35 #include "vbastyles.hxx"
36 #include "excelvbahelper.hxx"
37 #include "vbapalette.hxx"
38 #include <osl/file.hxx>
39 #include "vbanames.hxx"
40 #include <docoptio.hxx>
43 // Much of the impl. for the equivalent UNO module is
44 // sc/source/ui/unoobj/docuno.cxx, viewuno.cxx
46 using namespace ::ooo::vba
;
47 using namespace ::com::sun::star
;
49 uno::Sequence
< sal_Int32
> ScVbaWorkbook::ColorData
;
51 void ScVbaWorkbook::initColorData( const uno::Sequence
< sal_Int32
>& sColors
)
53 std::copy(sColors
.begin(), sColors
.end(), ColorData
.begin());
57 ScVbaWorkbook::ResetColors( )
59 uno::Reference
< container::XIndexAccess
> xIndexAccess( ScVbaPalette::getDefaultPalette(), uno::UNO_SET_THROW
);
60 sal_Int32 nLen
= xIndexAccess
->getCount();
61 ColorData
.realloc( nLen
);
63 uno::Sequence
< sal_Int32
> dDefaultColors( nLen
);
64 sal_Int32
* pDest
= dDefaultColors
.getArray();
65 for ( sal_Int32 index
=0; index
< nLen
; ++pDest
, ++index
)
66 xIndexAccess
->getByIndex( index
) >>= *pDest
;
67 initColorData( dDefaultColors
);
71 ScVbaWorkbook::Colors( const ::uno::Any
& Index
)
74 if ( Index
.hasValue() )
78 aRet
<<= XLRGBToOORGB( ColorData
[ --nIndex
] );
85 bool ScVbaWorkbook::setFilterPropsFromFormat( sal_Int32 nFormat
, uno::Sequence
< beans::PropertyValue
>& rProps
)
87 auto pProp
= std::find_if(rProps
.begin(), rProps
.end(),
88 [](const beans::PropertyValue
& rProp
) { return rProp
.Name
== "FilterName"; });
89 bool bRes
= pProp
!= rProps
.end();
94 case excel::XlFileFormat::xlCSV
:
95 pProp
->Value
<<= OUString(SC_TEXT_CSV_FILTER_NAME
);
97 case excel::XlFileFormat::xlDBF4
:
98 pProp
->Value
<<= OUString("DBF");
100 case excel::XlFileFormat::xlDIF
:
101 pProp
->Value
<<= OUString("DIF");
103 case excel::XlFileFormat::xlWK3
:
104 pProp
->Value
<<= OUString("Lotus");
106 case excel::XlFileFormat::xlExcel4Workbook
:
107 pProp
->Value
<<= OUString("MS Excel 4.0");
109 case excel::XlFileFormat::xlExcel5
:
110 pProp
->Value
<<= OUString("MS Excel 5.0/95");
112 case excel::XlFileFormat::xlHtml
:
113 pProp
->Value
<<= OUString("HTML (StarCalc)");
115 case excel::XlFileFormat::xlExcel9795
:
117 pProp
->Value
<<= OUString("MS Excel 97");
125 ScVbaWorkbook::getFileFormat( )
127 sal_Int32 aFileFormat
= 0;
128 OUString aFilterName
;
129 uno::Sequence
< beans::PropertyValue
> aArgs
= getModel()->getArgs();
131 // #FIXME - seems suspect should we not walk through the properties
132 // to find the FilterName
133 if ( aArgs
[0].Name
== "FilterName" ) {
134 aArgs
[0].Value
>>= aFilterName
;
136 aArgs
[1].Value
>>= aFilterName
;
139 if (aFilterName
== SC_TEXT_CSV_FILTER_NAME
) {
140 aFileFormat
= excel::XlFileFormat::xlCSV
; //xlFileFormat.
143 if ( aFilterName
== "DBF" ) {
144 aFileFormat
= excel::XlFileFormat::xlDBF4
;
147 if ( aFilterName
== "DIF" ) {
148 aFileFormat
= excel::XlFileFormat::xlDIF
;
151 if ( aFilterName
== "Lotus" ) {
152 aFileFormat
= excel::XlFileFormat::xlWK3
;
155 if ( aFilterName
== "MS Excel 4.0" ) {
156 aFileFormat
= excel::XlFileFormat::xlExcel4Workbook
;
159 if ( aFilterName
== "MS Excel 5.0/95" ) {
160 aFileFormat
= excel::XlFileFormat::xlExcel5
;
163 if ( aFilterName
== "MS Excel 97" ) {
164 aFileFormat
= excel::XlFileFormat::xlExcel9795
;
167 if (aFilterName
== "HTML (StarCalc)") {
168 aFileFormat
= excel::XlFileFormat::xlHtml
;
171 if ( aFilterName
== "calc_StarOffice_XML_Calc_Template" ) {
172 aFileFormat
= excel::XlFileFormat::xlTemplate
;
175 if (aFilterName
== "StarOffice XML (Calc)") {
176 aFileFormat
= excel::XlFileFormat::xlWorkbookNormal
;
178 if ( aFilterName
== "calc8" ) {
179 aFileFormat
= excel::XlFileFormat::xlWorkbookNormal
;
186 ScVbaWorkbook::init()
188 if ( !ColorData
.hasElements() )
190 uno::Reference
< frame::XModel
> xModel
= getModel();
192 excel::getDocShell( xModel
)->RegisterAutomationWorkbookObject( this );
195 ScVbaWorkbook::ScVbaWorkbook( const css::uno::Reference
< ov::XHelperInterface
>& xParent
, const css::uno::Reference
< css::uno::XComponentContext
>& xContext
, css::uno::Reference
< css::frame::XModel
> const & xModel
) : ScVbaWorkbook_BASE( xParent
, xContext
, xModel
)
200 ScVbaWorkbook::ScVbaWorkbook( uno::Sequence
< uno::Any
> const & args
,
201 uno::Reference
< uno::XComponentContext
> const & xContext
) : ScVbaWorkbook_BASE( args
, xContext
)
206 uno::Reference
< excel::XWorksheet
>
207 ScVbaWorkbook::getActiveSheet()
209 uno::Reference
< frame::XModel
> xModel( getCurrentExcelDoc( mxContext
), uno::UNO_SET_THROW
);
210 uno::Reference
< sheet::XSpreadsheetView
> xView( xModel
->getCurrentController(), uno::UNO_QUERY_THROW
);
211 uno::Reference
< sheet::XSpreadsheet
> xSheet( xView
->getActiveSheet(), uno::UNO_SET_THROW
);
212 // #162503# return the original sheet module wrapper object, instead of a new instance
213 uno::Reference
< excel::XWorksheet
> xWorksheet( excel::getUnoSheetModuleObj( xSheet
), uno::UNO_QUERY
);
214 if( xWorksheet
.is() ) return xWorksheet
;
215 // #i116936# excel::getUnoSheetModuleObj() may return null in documents without global VBA mode enabled
216 return new ScVbaWorksheet( this, mxContext
, xSheet
, xModel
);
220 ScVbaWorkbook::Sheets( const uno::Any
& aIndex
)
222 return Worksheets( aIndex
);
226 ScVbaWorkbook::Worksheets( const uno::Any
& aIndex
)
228 uno::Reference
< frame::XModel
> xModel( getModel() );
229 uno::Reference
<sheet::XSpreadsheetDocument
> xSpreadDoc( xModel
, uno::UNO_QUERY_THROW
);
230 uno::Reference
<container::XIndexAccess
> xSheets( xSpreadDoc
->getSheets(), uno::UNO_QUERY_THROW
);
231 uno::Reference
< XCollection
> xWorkSheets( new ScVbaWorksheets( this, mxContext
, xSheets
, xModel
) );
232 if ( aIndex
.getValueTypeClass() == uno::TypeClass_VOID
)
234 return uno::Any( xWorkSheets
);
236 // pass on to collection
237 return xWorkSheets
->Item( aIndex
, uno::Any() );
240 ScVbaWorkbook::Windows( const uno::Any
& aIndex
)
243 uno::Reference
< excel::XWindows
> xWindows( new ScVbaWindows( getParent(), mxContext
) );
244 if ( aIndex
.getValueTypeClass() == uno::TypeClass_VOID
)
245 return uno::Any( xWindows
);
246 return xWindows
->Item( aIndex
, uno::Any() );
250 ScVbaWorkbook::Activate()
252 VbaDocumentBase::Activate();
256 ScVbaWorkbook::Protect( const uno::Any
&aPassword
)
258 VbaDocumentBase::Protect( aPassword
);
262 ScVbaWorkbook::getProtectStructure()
264 uno::Reference
< util::XProtectable
> xProt( getModel(), uno::UNO_QUERY_THROW
);
265 return xProt
->isProtected();
268 sal_Bool SAL_CALL
ScVbaWorkbook::getPrecisionAsDisplayed()
270 uno::Reference
< frame::XModel
> xModel( getModel(), uno::UNO_SET_THROW
);
271 ScDocument
& rDoc
= excel::getDocShell( xModel
)->GetDocument();
272 return rDoc
.GetDocOptions().IsCalcAsShown();
275 void SAL_CALL
ScVbaWorkbook::setPrecisionAsDisplayed( sal_Bool _precisionAsDisplayed
)
277 uno::Reference
< frame::XModel
> xModel( getModel(), uno::UNO_SET_THROW
);
278 ScDocument
& rDoc
= excel::getDocShell( xModel
)->GetDocument();
279 ScDocOptions aOpt
= rDoc
.GetDocOptions();
280 aOpt
.SetCalcAsShown( _precisionAsDisplayed
);
281 rDoc
.SetDocOptions( aOpt
);
284 OUString SAL_CALL
ScVbaWorkbook::getAuthor()
286 uno::Reference
<document::XDocumentPropertiesSupplier
> xDPS( getModel(), uno::UNO_QUERY
);
289 uno::Reference
<document::XDocumentProperties
> xDocProps
= xDPS
->getDocumentProperties();
290 return xDocProps
->getAuthor();
293 void SAL_CALL
ScVbaWorkbook::setAuthor( const OUString
& _author
)
295 uno::Reference
<document::XDocumentPropertiesSupplier
> xDPS( getModel(), uno::UNO_QUERY
);
298 uno::Reference
<document::XDocumentProperties
> xDocProps
= xDPS
->getDocumentProperties();
299 xDocProps
->setAuthor( _author
);
303 ScVbaWorkbook::SaveCopyAs( const OUString
& sFileName
)
306 osl::FileBase::getFileURLFromSystemPath( sFileName
, aURL
);
307 uno::Reference
< frame::XStorable
> xStor( getModel(), uno::UNO_QUERY_THROW
);
308 uno::Sequence
< beans::PropertyValue
> storeProps(1);
309 storeProps
[0].Name
= "FilterName";
310 storeProps
[0].Value
<<= OUString( "MS Excel 97" );
311 xStor
->storeToURL( aURL
, storeProps
);
315 ScVbaWorkbook::SaveAs( const uno::Any
& FileName
, const uno::Any
& FileFormat
, const uno::Any
& /*Password*/, const uno::Any
& /*WriteResPassword*/, const uno::Any
& /*ReadOnlyRecommended*/, const uno::Any
& /*CreateBackup*/, const uno::Any
& /*AccessMode*/, const uno::Any
& /*ConflictResolution*/, const uno::Any
& /*AddToMru*/, const uno::Any
& /*TextCodepage*/, const uno::Any
& /*TextVisualLayout*/, const uno::Any
& /*Local*/ )
318 FileName
>>= sFileName
;
320 osl::FileBase::getFileURLFromSystemPath( sFileName
, sURL
);
321 // detect if there is no path then we need
322 // to use the current folder
323 INetURLObject
aURL( sURL
);
324 sURL
= aURL
.GetMainURL( INetURLObject::DecodeMechanism::ToIUri
);
327 // need to add cur dir ( of this workbook ) or else the 'Work' dir
328 sURL
= getModel()->getURL();
330 if ( sURL
.isEmpty() )
332 // not path available from 'this' document
333 // need to add the 'document'/work directory then
334 uno::Reference
< excel::XApplication
> xApplication ( Application(),uno::UNO_QUERY_THROW
);
335 OUString sWorkPath
= xApplication
->getDefaultFilePath();
337 osl::FileBase::getFileURLFromSystemPath( sWorkPath
, sWorkURL
);
338 aURL
.SetURL( sWorkURL
);
343 aURL
.Append( sFileName
);
345 sURL
= aURL
.GetMainURL( INetURLObject::DecodeMechanism::ToIUri
);
349 sal_Int32 nFileFormat
= excel::XlFileFormat::xlExcel9795
;
350 FileFormat
>>= nFileFormat
;
352 uno::Sequence
< beans::PropertyValue
> storeProps(1);
353 storeProps
[0].Name
= "FilterName" ;
355 setFilterPropsFromFormat( nFileFormat
, storeProps
);
357 uno::Reference
< frame::XStorable
> xStor( getModel(), uno::UNO_QUERY_THROW
);
358 xStor
->storeAsURL( sURL
, storeProps
);
361 css::uno::Any SAL_CALL
362 ScVbaWorkbook::Styles( const uno::Any
& Item
)
364 // quick look and Styles object doesn't seem to have a valid parent
365 // or a least the object browser just shows an object that has no
366 // variables ( therefore... leave as NULL for now )
367 uno::Reference
< XCollection
> dStyles
= new ScVbaStyles( uno::Reference
< XHelperInterface
>(), mxContext
, getModel() );
368 if ( Item
.hasValue() )
369 return dStyles
->Item( Item
, uno::Any() );
370 return uno::makeAny( dStyles
);
374 ScVbaWorkbook::Names( const uno::Any
& aIndex
)
376 uno::Reference
< frame::XModel
> xModel( getModel(), uno::UNO_SET_THROW
);
377 uno::Reference
< beans::XPropertySet
> xProps( xModel
, uno::UNO_QUERY_THROW
);
378 uno::Reference
< sheet::XNamedRanges
> xNamedRanges( xProps
->getPropertyValue("NamedRanges"), uno::UNO_QUERY_THROW
);
379 uno::Reference
< XCollection
> xNames( new ScVbaNames( this, mxContext
, xNamedRanges
, xModel
) );
380 if ( aIndex
.hasValue() )
381 return xNames
->Item( aIndex
, uno::Any() );
382 return uno::Any( xNames
);
386 ScVbaWorkbook::getServiceImplName()
388 return "ScVbaWorkbook";
391 uno::Sequence
< OUString
>
392 ScVbaWorkbook::getServiceNames()
394 static uno::Sequence
< OUString
> const aServiceNames
396 "ooo.vba.excel.Workbook"
398 return aServiceNames
;
402 ScVbaWorkbook::getCodeName()
404 uno::Reference
< beans::XPropertySet
> xModelProp( getModel(), uno::UNO_QUERY_THROW
);
405 return xModelProp
->getPropertyValue("CodeName").get
< OUString
>();
409 ScVbaWorkbook::getSomething(const uno::Sequence
<sal_Int8
>& rId
)
411 if (isUnoTunnelId
<ScVbaWorksheet
>(rId
))
413 return sal::static_int_cast
<sal_Int64
>(reinterpret_cast<sal_IntPtr
>(this));
418 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
419 Calc_ScVbaWorkbook_get_implementation(
420 css::uno::XComponentContext
* context
, css::uno::Sequence
<css::uno::Any
> const& args
)
422 return cppu::acquire(new ScVbaWorkbook(args
, context
));
426 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */