LibreOffice Module sc (master) 1
vbaworkbook.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21
23#include <tools/urlobj.hxx>
24
25#include <com/sun/star/util/XProtectable.hpp>
26#include <com/sun/star/sheet/XNamedRanges.hpp>
27#include <com/sun/star/sheet/XSpreadsheetView.hpp>
28#include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
29#include <com/sun/star/frame/XStorable.hpp>
30#include <com/sun/star/beans/XPropertySet.hpp>
31#include <ooo/vba/excel/XlFileFormat.hpp>
32#include <ooo/vba/excel/XApplication.hpp>
33
34#include "vbaworksheet.hxx"
35#include "vbaworksheets.hxx"
36#include "vbaworkbook.hxx"
37#include "vbawindows.hxx"
38#include "vbastyles.hxx"
39#include "excelvbahelper.hxx"
40#include "vbapalette.hxx"
41#include <osl/file.hxx>
42#include "vbanames.hxx"
43#include <docoptio.hxx>
44#include <docsh.hxx>
45
46// Much of the impl. for the equivalent UNO module is
47// sc/source/ui/unoobj/docuno.cxx, viewuno.cxx
48
49using namespace ::ooo::vba;
50using namespace ::com::sun::star;
51
52uno::Sequence< sal_Int32 > ScVbaWorkbook::ColorData;
53
54void SAL_CALL
56{
57 uno::Reference< container::XIndexAccess > xIndexAccess( ScVbaPalette::getDefaultPalette(), uno::UNO_SET_THROW );
58 sal_Int32 nLen = xIndexAccess->getCount();
59 ColorData.realloc( nLen );
60
61 sal_Int32* pDest = ColorData.getArray();
62 for ( sal_Int32 index=0; index < nLen; ++pDest, ++index )
63 xIndexAccess->getByIndex( index ) >>= *pDest;
64}
65
66::uno::Any SAL_CALL
67ScVbaWorkbook::Colors( const ::uno::Any& Index )
68{
69 uno::Any aRet;
70 if ( Index.hasValue() )
71 {
72 sal_Int32 nIndex = 0;
73 Index >>= nIndex;
74 aRet <<= XLRGBToOORGB( ColorData[ --nIndex ] );
75 }
76 else
77 aRet <<= ColorData;
78 return aRet;
79}
80
81bool ScVbaWorkbook::setFilterPropsFromFormat( sal_Int32 nFormat, uno::Sequence< beans::PropertyValue >& rProps )
82{
83 auto [begin, end] = asNonConstRange(rProps);
84 auto pProp = std::find_if(begin, end,
85 [](const beans::PropertyValue& rProp) { return rProp.Name == "FilterName"; });
86 bool bRes = pProp != end;
87 if (bRes)
88 {
89 switch( nFormat )
90 {
91 case excel::XlFileFormat::xlCSV:
92 pProp->Value <<= OUString(SC_TEXT_CSV_FILTER_NAME);
93 break;
94 case excel::XlFileFormat::xlDBF4:
95 pProp->Value <<= OUString("DBF");
96 break;
97 case excel::XlFileFormat::xlDIF:
98 pProp->Value <<= OUString("DIF");
99 break;
100 case excel::XlFileFormat::xlWK3:
101 pProp->Value <<= OUString("Lotus");
102 break;
103 case excel::XlFileFormat::xlExcel4Workbook:
104 pProp->Value <<= OUString("MS Excel 4.0");
105 break;
106 case excel::XlFileFormat::xlExcel5:
107 pProp->Value <<= OUString("MS Excel 5.0/95");
108 break;
109 case excel::XlFileFormat::xlHtml:
110 pProp->Value <<= OUString("HTML (StarCalc)");
111 break;
112 case excel::XlFileFormat::xlExcel9795:
113 default:
114 pProp->Value <<= OUString("MS Excel 97");
115 break;
116 }
117 }
118 return bRes;
119}
120
121::sal_Int32 SAL_CALL
123{
124 sal_Int32 aFileFormat = 0;
125 OUString aFilterName;
126 uno::Sequence< beans::PropertyValue > aArgs = getModel()->getArgs();
127
128 // #FIXME - seems suspect should we not walk through the properties
129 // to find the FilterName
130 if ( aArgs[0].Name == "FilterName" ) {
131 aArgs[0].Value >>= aFilterName;
132 } else {
133 aArgs[1].Value >>= aFilterName;
134 }
135
136 if (aFilterName == SC_TEXT_CSV_FILTER_NAME) {
137 aFileFormat = excel::XlFileFormat::xlCSV; //xlFileFormat.
138 }
139
140 if ( aFilterName == "DBF" ) {
141 aFileFormat = excel::XlFileFormat::xlDBF4;
142 }
143
144 if ( aFilterName == "DIF" ) {
145 aFileFormat = excel::XlFileFormat::xlDIF;
146 }
147
148 if ( aFilterName == "Lotus" ) {
149 aFileFormat = excel::XlFileFormat::xlWK3;
150 }
151
152 if ( aFilterName == "MS Excel 4.0" ) {
153 aFileFormat = excel::XlFileFormat::xlExcel4Workbook;
154 }
155
156 if ( aFilterName == "MS Excel 5.0/95" ) {
157 aFileFormat = excel::XlFileFormat::xlExcel5;
158 }
159
160 if ( aFilterName == "MS Excel 97" ) {
161 aFileFormat = excel::XlFileFormat::xlExcel9795;
162 }
163
164 if (aFilterName == "HTML (StarCalc)") {
165 aFileFormat = excel::XlFileFormat::xlHtml;
166 }
167
168 if ( aFilterName == "calc_StarOffice_XML_Calc_Template" ) {
169 aFileFormat = excel::XlFileFormat::xlTemplate;
170 }
171
172 if (aFilterName == "StarOffice XML (Calc)") {
173 aFileFormat = excel::XlFileFormat::xlWorkbookNormal;
174 }
175 if ( aFilterName == "calc8" ) {
176 aFileFormat = excel::XlFileFormat::xlWorkbookNormal;
177 }
178
179 return aFileFormat;
180}
181
182void
184{
185 if ( !ColorData.hasElements() )
186 ResetColors();
187 if ( ScDocShell* pShell = excel::getDocShell( getModel() ))
188 pShell->RegisterAutomationWorkbookObject( this );
189}
190
191ScVbaWorkbook::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 )
192{
193 init();
194}
195
196ScVbaWorkbook::ScVbaWorkbook( uno::Sequence< uno::Any> const & args,
197 uno::Reference< uno::XComponentContext> const & xContext ) : ScVbaWorkbook_BASE( args, xContext )
198{
199 init();
200}
201
202uno::Reference< excel::XWorksheet >
204{
205 uno::Reference< frame::XModel > xModel( getCurrentExcelDoc( mxContext ), uno::UNO_SET_THROW );
206 uno::Reference< sheet::XSpreadsheetView > xView( xModel->getCurrentController(), uno::UNO_QUERY_THROW );
207 uno::Reference< sheet::XSpreadsheet > xSheet( xView->getActiveSheet(), uno::UNO_SET_THROW );
208 // #162503# return the original sheet module wrapper object, instead of a new instance
209 uno::Reference< excel::XWorksheet > xWorksheet( excel::getUnoSheetModuleObj( xSheet ), uno::UNO_QUERY );
210 if( xWorksheet.is() ) return xWorksheet;
211 // #i116936# excel::getUnoSheetModuleObj() may return null in documents without global VBA mode enabled
212 return new ScVbaWorksheet( this, mxContext, xSheet, xModel );
213}
214
215uno::Any SAL_CALL
217{
218 return Worksheets( aIndex );
219}
220
221uno::Any SAL_CALL
223{
224 uno::Reference< frame::XModel > xModel( getModel() );
225 uno::Reference <sheet::XSpreadsheetDocument> xSpreadDoc( xModel, uno::UNO_QUERY_THROW );
226 uno::Reference<container::XIndexAccess > xSheets( xSpreadDoc->getSheets(), uno::UNO_QUERY_THROW );
227 uno::Reference< XCollection > xWorkSheets( new ScVbaWorksheets( this, mxContext, xSheets, xModel ) );
228 if ( aIndex.getValueTypeClass() == uno::TypeClass_VOID )
229 {
230 return uno::Any( xWorkSheets );
231 }
232 // pass on to collection
233 return xWorkSheets->Item( aIndex, uno::Any() );
234}
235uno::Any SAL_CALL
237{
238
239 uno::Reference< excel::XWindows > xWindows( new ScVbaWindows( getParent(), mxContext ) );
240 if ( aIndex.getValueTypeClass() == uno::TypeClass_VOID )
241 return uno::Any( xWindows );
242 return xWindows->Item( aIndex, uno::Any() );
243}
244
245void SAL_CALL
247{
249}
250
251void
253{
254 VbaDocumentBase::Protect( aPassword );
255}
256
259{
260 uno::Reference< util::XProtectable > xProt( getModel(), uno::UNO_QUERY_THROW );
261 return xProt->isProtected();
262}
263
265{
266 if ( ScDocShell* pShell = excel::getDocShell( getModel() ))
267 {
268 ScDocument& rDoc = pShell->GetDocument();
269 return rDoc.GetDocOptions().IsCalcAsShown();
270 }
271 return false;
272}
273
274void SAL_CALL ScVbaWorkbook::setPrecisionAsDisplayed( sal_Bool _precisionAsDisplayed )
275{
276 if ( ScDocShell* pShell = excel::getDocShell( getModel() ))
277 {
278 ScDocument& rDoc = pShell->GetDocument();
279 ScDocOptions aOpt = rDoc.GetDocOptions();
280 aOpt.SetCalcAsShown( _precisionAsDisplayed );
281 rDoc.SetDocOptions( aOpt );
282 }
283}
284
285OUString SAL_CALL ScVbaWorkbook::getAuthor()
286{
287 uno::Reference<document::XDocumentPropertiesSupplier> xDPS( getModel(), uno::UNO_QUERY );
288 if (!xDPS.is())
289 return "?";
290 uno::Reference<document::XDocumentProperties> xDocProps = xDPS->getDocumentProperties();
291 return xDocProps->getAuthor();
292}
293
294void SAL_CALL ScVbaWorkbook::setAuthor( const OUString& _author )
295{
296 uno::Reference<document::XDocumentPropertiesSupplier> xDPS( getModel(), uno::UNO_QUERY );
297 if (!xDPS.is())
298 return;
299 uno::Reference<document::XDocumentProperties> xDocProps = xDPS->getDocumentProperties();
300 xDocProps->setAuthor( _author );
301}
302
303void
304ScVbaWorkbook::SaveCopyAs( const OUString& sFileName )
305{
306 OUString aURL;
307 osl::FileBase::getFileURLFromSystemPath( sFileName, aURL );
308 uno::Reference< frame::XStorable > xStor( getModel(), uno::UNO_QUERY_THROW );
309 uno::Sequence< beans::PropertyValue > storeProps{ comphelper::makePropertyValue(
310 "FilterName", OUString( "MS Excel 97" )) };
311 xStor->storeToURL( aURL, storeProps );
312}
313
314void SAL_CALL
315ScVbaWorkbook::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*/ )
316{
317 OUString sFileName;
318 FileName >>= sFileName;
319 OUString sURL;
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 );
325 if( sURL.isEmpty() )
326 {
327 // need to add cur dir ( of this workbook ) or else the 'Work' dir
328 sURL = getModel()->getURL();
329
330 if ( sURL.isEmpty() )
331 {
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();
336 OUString sWorkURL;
337 osl::FileBase::getFileURLFromSystemPath( sWorkPath, sWorkURL );
338 aURL.SetURL( sWorkURL );
339 }
340 else
341 {
342 aURL.SetURL( sURL );
343 aURL.Append( sFileName );
344 }
345 sURL = aURL.GetMainURL( INetURLObject::DecodeMechanism::ToIUri );
346
347 }
348
349 sal_Int32 nFileFormat = excel::XlFileFormat::xlExcel9795;
350 FileFormat >>= nFileFormat;
351
352 uno::Sequence storeProps{ comphelper::makePropertyValue("FilterName", uno::Any()) };
353 setFilterPropsFromFormat( nFileFormat, storeProps );
354
355 uno::Reference< frame::XStorable > xStor( getModel(), uno::UNO_QUERY_THROW );
356 xStor->storeAsURL( sURL, storeProps );
357}
358
359void SAL_CALL
360ScVbaWorkbook::ExportAsFixedFormat(const css::uno::Any& Type, const css::uno::Any& FileName, const css::uno::Any& Quality,
361 const css::uno::Any& IncludeDocProperties, const css::uno::Any& /*IgnorePrintAreas*/, const css::uno::Any& From,
362 const css::uno::Any& To, const css::uno::Any& OpenAfterPublish, const css::uno::Any& /*FixedFormatExtClassPtr*/)
363{
364 uno::Reference< frame::XModel > xModel(getModel(), uno::UNO_SET_THROW);
365 uno::Reference< excel::XApplication > xApplication(Application(), uno::UNO_QUERY_THROW);
366
367 excel::ExportAsFixedFormatHelper(xModel, xApplication, Type, FileName, Quality,
368 IncludeDocProperties, From, To, OpenAfterPublish);
369}
370
371css::uno::Any SAL_CALL
373{
374 // quick look and Styles object doesn't seem to have a valid parent
375 // or a least the object browser just shows an object that has no
376 // variables ( therefore... leave as NULL for now )
377 uno::Reference< XCollection > dStyles = new ScVbaStyles( uno::Reference< XHelperInterface >(), mxContext, getModel() );
378 if ( Item.hasValue() )
379 return dStyles->Item( Item, uno::Any() );
380 return uno::Any( dStyles );
381}
382
383uno::Any SAL_CALL
385{
386 uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_SET_THROW );
387 uno::Reference< beans::XPropertySet > xProps( xModel, uno::UNO_QUERY_THROW );
388 uno::Reference< sheet::XNamedRanges > xNamedRanges( xProps->getPropertyValue("NamedRanges"), uno::UNO_QUERY_THROW );
389 uno::Reference< XCollection > xNames( new ScVbaNames( this, mxContext, xNamedRanges, xModel ) );
390 if ( aIndex.hasValue() )
391 return xNames->Item( aIndex, uno::Any() );
392 return uno::Any( xNames );
393}
394
395OUString
397{
398 return "ScVbaWorkbook";
399}
400
401uno::Sequence< OUString >
403{
404 static uno::Sequence< OUString > const aServiceNames
405 {
406 "ooo.vba.excel.Workbook"
407 };
408 return aServiceNames;
409}
410
411OUString SAL_CALL
413{
414 uno::Reference< beans::XPropertySet > xModelProp( getModel(), uno::UNO_QUERY_THROW );
415 return xModelProp->getPropertyValue("CodeName").get< OUString >();
416}
417
418sal_Int64
419ScVbaWorkbook::getSomething(const uno::Sequence<sal_Int8 >& rId )
420{
421 if (comphelper::isUnoTunnelId<ScVbaWorksheet>(rId)) // ???
422 {
424 }
425 return 0;
426}
427
428extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
430 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& args)
431{
432 return cppu::acquire(new ScVbaWorkbook(args, context));
433}
434
435
436/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
struct _ADOIndex Index
void SetCalcAsShown(bool bVal)
Definition: docoptio.hxx:82
bool IsCalcAsShown() const
Definition: docoptio.hxx:81
SC_DLLPUBLIC void SetDocOptions(const ScDocOptions &rOpt)
Definition: documen3.cxx:1942
SC_DLLPUBLIC const ScDocOptions & GetDocOptions() const
Definition: documen3.cxx:1936
static css::uno::Reference< css::container::XIndexAccess > getDefaultPalette()
Definition: vbapalette.cxx:94
virtual css::uno::Any SAL_CALL Windows(const css::uno::Any &aIndex) override
virtual OUString SAL_CALL getCodeName() override
virtual ::sal_Int64 SAL_CALL getSomething(const css::uno::Sequence< sal_Int8 > &rId) override
virtual css::uno::Any SAL_CALL Sheets(const css::uno::Any &aIndex) override
virtual OUString SAL_CALL getAuthor() override
virtual void SAL_CALL setAuthor(const OUString &_author) override
virtual css::uno::Sequence< OUString > getServiceNames() override
virtual css::uno::Any SAL_CALL Worksheets(const css::uno::Any &aIndex) override
virtual void SAL_CALL ExportAsFixedFormat(const css::uno::Any &Type, const css::uno::Any &FileName, const css::uno::Any &Quality, const css::uno::Any &IncludeDocProperties, const css::uno::Any &IgnorePrintAreas, const css::uno::Any &From, const css::uno::Any &To, const css::uno::Any &OpenAfterPublish, const css::uno::Any &FixedFormatExtClassPtr) override
virtual void SAL_CALL ResetColors() override
Definition: vbaworkbook.cxx:55
virtual void SAL_CALL setPrecisionAsDisplayed(sal_Bool _precisionAsDisplayed) override
virtual void SAL_CALL Protect(const css::uno::Any &aPassword) override
virtual sal_Bool SAL_CALL getPrecisionAsDisplayed() override
virtual css::uno::Any SAL_CALL Styles(const css::uno::Any &Item) override
virtual OUString getServiceImplName() override
virtual css::uno::Any SAL_CALL Names(const css::uno::Any &aIndex) override
virtual css::uno::Reference< ov::excel::XWorksheet > SAL_CALL getActiveSheet() override
virtual void SAL_CALL Activate() override
virtual sal_Bool SAL_CALL getProtectStructure() override
static css::uno::Sequence< sal_Int32 > ColorData
Definition: vbaworkbook.hxx:30
static bool setFilterPropsFromFormat(sal_Int32 nFormat, css::uno::Sequence< css::beans::PropertyValue > &rProps)
Definition: vbaworkbook.cxx:81
virtual ::sal_Int32 SAL_CALL getFileFormat() override
virtual void SAL_CALL SaveAs(const css::uno::Any &FileName, const css::uno::Any &FileFormat, const css::uno::Any &Password, const css::uno::Any &WriteResPassword, const css::uno::Any &ReadOnlyRecommended, const css::uno::Any &CreateBackup, const css::uno::Any &AccessMode, const css::uno::Any &ConflictResolution, const css::uno::Any &AddToMru, const css::uno::Any &TextCodepage, const css::uno::Any &TextVisualLayout, const css::uno::Any &Local) override
ScVbaWorkbook(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext, css::uno::Reference< css::frame::XModel > const &xModel)
virtual void SAL_CALL SaveCopyAs(const OUString &Filename) override
virtual css::uno::Any SAL_CALL Colors(const css::uno::Any &Index) override
Definition: vbaworkbook.cxx:67
virtual void SAL_CALL Activate() override
virtual void SAL_CALL Protect(const css::uno::Any &aPassword)
URL aURL
uno::Reference< uno::XComponentContext > mxContext
std::deque< AttacherIndex_Impl > aIndex
Sequence< OUString > aServiceNames
constexpr OUStringLiteral SC_TEXT_CSV_FILTER_NAME
Definition: global.hxx:63
sal_Int32 nIndex
sal_Int64 getSomething_cast(void *p)
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
Type
index
enumrange< T >::Iterator begin(enumrange< T >)
ScDocShell * getDocShell(const css::uno::Reference< css::frame::XModel > &xModel)
void ExportAsFixedFormatHelper(const uno::Reference< frame::XModel > &xModel, const css::uno::Reference< XApplication > &xApplication, const css::uno::Any &Type, const css::uno::Any &FileName, const css::uno::Any &Quality, const css::uno::Any &IncludeDocProperties, const css::uno::Any &From, const css::uno::Any &To, const css::uno::Any &OpenAfterPublish)
uno::Reference< XHelperInterface > getUnoSheetModuleObj(const uno::Reference< table::XCellRange > &xRange)
sal_Int32 XLRGBToOORGB(sal_Int32 nCol)
VBAHELPER_DLLPUBLIC css::uno::Reference< css::frame::XModel > getCurrentExcelDoc(const css::uno::Reference< css::uno::XComponentContext > &xContext)
end
args
bool hasValue()
Reference< XModel > xModel
OUString Name
unsigned char sal_Bool
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * Calc_ScVbaWorkbook_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &args)
cppu::ImplInheritanceHelper< VbaDocumentBase, ov::excel::XWorkbook > ScVbaWorkbook_BASE
Definition: vbaworkbook.hxx:26