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 uno::Reference< frame::XModel > xModel = getModel();
188 if ( xModel.is() )
190}
191
192ScVbaWorkbook::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 )
193{
194 init();
195}
196
197ScVbaWorkbook::ScVbaWorkbook( uno::Sequence< uno::Any> const & args,
198 uno::Reference< uno::XComponentContext> const & xContext ) : ScVbaWorkbook_BASE( args, xContext )
199{
200 init();
201}
202
203uno::Reference< excel::XWorksheet >
205{
206 uno::Reference< frame::XModel > xModel( getCurrentExcelDoc( mxContext ), uno::UNO_SET_THROW );
207 uno::Reference< sheet::XSpreadsheetView > xView( xModel->getCurrentController(), uno::UNO_QUERY_THROW );
208 uno::Reference< sheet::XSpreadsheet > xSheet( xView->getActiveSheet(), uno::UNO_SET_THROW );
209 // #162503# return the original sheet module wrapper object, instead of a new instance
210 uno::Reference< excel::XWorksheet > xWorksheet( excel::getUnoSheetModuleObj( xSheet ), uno::UNO_QUERY );
211 if( xWorksheet.is() ) return xWorksheet;
212 // #i116936# excel::getUnoSheetModuleObj() may return null in documents without global VBA mode enabled
213 return new ScVbaWorksheet( this, mxContext, xSheet, xModel );
214}
215
216uno::Any SAL_CALL
218{
219 return Worksheets( aIndex );
220}
221
222uno::Any SAL_CALL
224{
225 uno::Reference< frame::XModel > xModel( getModel() );
226 uno::Reference <sheet::XSpreadsheetDocument> xSpreadDoc( xModel, uno::UNO_QUERY_THROW );
227 uno::Reference<container::XIndexAccess > xSheets( xSpreadDoc->getSheets(), uno::UNO_QUERY_THROW );
228 uno::Reference< XCollection > xWorkSheets( new ScVbaWorksheets( this, mxContext, xSheets, xModel ) );
229 if ( aIndex.getValueTypeClass() == uno::TypeClass_VOID )
230 {
231 return uno::Any( xWorkSheets );
232 }
233 // pass on to collection
234 return xWorkSheets->Item( aIndex, uno::Any() );
235}
236uno::Any SAL_CALL
238{
239
240 uno::Reference< excel::XWindows > xWindows( new ScVbaWindows( getParent(), mxContext ) );
241 if ( aIndex.getValueTypeClass() == uno::TypeClass_VOID )
242 return uno::Any( xWindows );
243 return xWindows->Item( aIndex, uno::Any() );
244}
245
246void SAL_CALL
248{
250}
251
252void
254{
255 VbaDocumentBase::Protect( aPassword );
256}
257
260{
261 uno::Reference< util::XProtectable > xProt( getModel(), uno::UNO_QUERY_THROW );
262 return xProt->isProtected();
263}
264
266{
267 uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_SET_THROW );
269 return rDoc.GetDocOptions().IsCalcAsShown();
270}
271
272void SAL_CALL ScVbaWorkbook::setPrecisionAsDisplayed( sal_Bool _precisionAsDisplayed )
273{
274 uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_SET_THROW );
276 ScDocOptions aOpt = rDoc.GetDocOptions();
277 aOpt.SetCalcAsShown( _precisionAsDisplayed );
278 rDoc.SetDocOptions( aOpt );
279}
280
281OUString SAL_CALL ScVbaWorkbook::getAuthor()
282{
283 uno::Reference<document::XDocumentPropertiesSupplier> xDPS( getModel(), uno::UNO_QUERY );
284 if (!xDPS.is())
285 return "?";
286 uno::Reference<document::XDocumentProperties> xDocProps = xDPS->getDocumentProperties();
287 return xDocProps->getAuthor();
288}
289
290void SAL_CALL ScVbaWorkbook::setAuthor( const OUString& _author )
291{
292 uno::Reference<document::XDocumentPropertiesSupplier> xDPS( getModel(), uno::UNO_QUERY );
293 if (!xDPS.is())
294 return;
295 uno::Reference<document::XDocumentProperties> xDocProps = xDPS->getDocumentProperties();
296 xDocProps->setAuthor( _author );
297}
298
299void
300ScVbaWorkbook::SaveCopyAs( const OUString& sFileName )
301{
302 OUString aURL;
303 osl::FileBase::getFileURLFromSystemPath( sFileName, aURL );
304 uno::Reference< frame::XStorable > xStor( getModel(), uno::UNO_QUERY_THROW );
305 uno::Sequence< beans::PropertyValue > storeProps{ comphelper::makePropertyValue(
306 "FilterName", OUString( "MS Excel 97" )) };
307 xStor->storeToURL( aURL, storeProps );
308}
309
310void SAL_CALL
311ScVbaWorkbook::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*/ )
312{
313 OUString sFileName;
314 FileName >>= sFileName;
315 OUString sURL;
316 osl::FileBase::getFileURLFromSystemPath( sFileName, sURL );
317 // detect if there is no path then we need
318 // to use the current folder
319 INetURLObject aURL( sURL );
320 sURL = aURL.GetMainURL( INetURLObject::DecodeMechanism::ToIUri );
321 if( sURL.isEmpty() )
322 {
323 // need to add cur dir ( of this workbook ) or else the 'Work' dir
324 sURL = getModel()->getURL();
325
326 if ( sURL.isEmpty() )
327 {
328 // not path available from 'this' document
329 // need to add the 'document'/work directory then
330 uno::Reference< excel::XApplication > xApplication ( Application(),uno::UNO_QUERY_THROW );
331 OUString sWorkPath = xApplication->getDefaultFilePath();
332 OUString sWorkURL;
333 osl::FileBase::getFileURLFromSystemPath( sWorkPath, sWorkURL );
334 aURL.SetURL( sWorkURL );
335 }
336 else
337 {
338 aURL.SetURL( sURL );
339 aURL.Append( sFileName );
340 }
341 sURL = aURL.GetMainURL( INetURLObject::DecodeMechanism::ToIUri );
342
343 }
344
345 sal_Int32 nFileFormat = excel::XlFileFormat::xlExcel9795;
346 FileFormat >>= nFileFormat;
347
348 uno::Sequence storeProps{ comphelper::makePropertyValue("FilterName", uno::Any()) };
349 setFilterPropsFromFormat( nFileFormat, storeProps );
350
351 uno::Reference< frame::XStorable > xStor( getModel(), uno::UNO_QUERY_THROW );
352 xStor->storeAsURL( sURL, storeProps );
353}
354
355css::uno::Any SAL_CALL
357{
358 // quick look and Styles object doesn't seem to have a valid parent
359 // or a least the object browser just shows an object that has no
360 // variables ( therefore... leave as NULL for now )
361 uno::Reference< XCollection > dStyles = new ScVbaStyles( uno::Reference< XHelperInterface >(), mxContext, getModel() );
362 if ( Item.hasValue() )
363 return dStyles->Item( Item, uno::Any() );
364 return uno::Any( dStyles );
365}
366
367uno::Any SAL_CALL
369{
370 uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_SET_THROW );
371 uno::Reference< beans::XPropertySet > xProps( xModel, uno::UNO_QUERY_THROW );
372 uno::Reference< sheet::XNamedRanges > xNamedRanges( xProps->getPropertyValue("NamedRanges"), uno::UNO_QUERY_THROW );
373 uno::Reference< XCollection > xNames( new ScVbaNames( this, mxContext, xNamedRanges, xModel ) );
374 if ( aIndex.hasValue() )
375 return xNames->Item( aIndex, uno::Any() );
376 return uno::Any( xNames );
377}
378
379OUString
381{
382 return "ScVbaWorkbook";
383}
384
385uno::Sequence< OUString >
387{
388 static uno::Sequence< OUString > const aServiceNames
389 {
390 "ooo.vba.excel.Workbook"
391 };
392 return aServiceNames;
393}
394
395OUString SAL_CALL
397{
398 uno::Reference< beans::XPropertySet > xModelProp( getModel(), uno::UNO_QUERY_THROW );
399 return xModelProp->getPropertyValue("CodeName").get< OUString >();
400}
401
402sal_Int64
403ScVbaWorkbook::getSomething(const uno::Sequence<sal_Int8 >& rId )
404{
405 if (comphelper::isUnoTunnelId<ScVbaWorksheet>(rId)) // ???
406 {
408 }
409 return 0;
410}
411
412extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
414 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& args)
415{
416 return cppu::acquire(new ScVbaWorkbook(args, context));
417}
418
419
420/* 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
void RegisterAutomationWorkbookObject(css::uno::Reference< ooo::vba::excel::XWorkbook > const &xWorkbook)
Definition: docsh.cxx:3407
const ScDocument & GetDocument() const
Definition: docsh.hxx:220
SC_DLLPUBLIC void SetDocOptions(const ScDocOptions &rOpt)
Definition: documen3.cxx:1958
SC_DLLPUBLIC const ScDocOptions & GetDocOptions() const
Definition: documen3.cxx:1952
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 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)
index
enumrange< T >::Iterator begin(enumrange< T >)
ScDocShell * getDocShell(const css::uno::Reference< css::frame::XModel > &xModel)
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