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 
49 using namespace ::ooo::vba;
50 using namespace ::com::sun::star;
51 
52 uno::Sequence< sal_Int32 > ScVbaWorkbook::ColorData;
53 
54 void 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
67 ScVbaWorkbook::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 
81 bool 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 
182 void
184 {
185  if ( !ColorData.hasElements() )
186  ResetColors();
187  uno::Reference< frame::XModel > xModel = getModel();
188  if ( xModel.is() )
190 }
191 
192 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 )
193 {
194  init();
195 }
196 
197 ScVbaWorkbook::ScVbaWorkbook( uno::Sequence< uno::Any> const & args,
198  uno::Reference< uno::XComponentContext> const & xContext ) : ScVbaWorkbook_BASE( args, xContext )
199 {
200  init();
201 }
202 
203 uno::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 
216 uno::Any SAL_CALL
218 {
219  return Worksheets( aIndex );
220 }
221 
222 uno::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 }
236 uno::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 
246 void SAL_CALL
248 {
250 }
251 
252 void
253 ScVbaWorkbook::Protect( const uno::Any &aPassword )
254 {
255  VbaDocumentBase::Protect( aPassword );
256 }
257 
258 sal_Bool
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 );
268  ScDocument& rDoc = excel::getDocShell( xModel )->GetDocument();
269  return rDoc.GetDocOptions().IsCalcAsShown();
270 }
271 
272 void SAL_CALL ScVbaWorkbook::setPrecisionAsDisplayed( sal_Bool _precisionAsDisplayed )
273 {
274  uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_SET_THROW );
275  ScDocument& rDoc = excel::getDocShell( xModel )->GetDocument();
276  ScDocOptions aOpt = rDoc.GetDocOptions();
277  aOpt.SetCalcAsShown( _precisionAsDisplayed );
278  rDoc.SetDocOptions( aOpt );
279 }
280 
281 OUString 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 
290 void 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 
299 void
300 ScVbaWorkbook::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 
310 void SAL_CALL
311 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*/ )
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 );
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  }
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 
355 css::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 
367 uno::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 
379 OUString
381 {
382  return "ScVbaWorkbook";
383 }
384 
385 uno::Sequence< OUString >
387 {
388  static uno::Sequence< OUString > const aServiceNames
389  {
390  "ooo.vba.excel.Workbook"
391  };
392  return aServiceNames;
393 }
394 
395 OUString SAL_CALL
397 {
398  uno::Reference< beans::XPropertySet > xModelProp( getModel(), uno::UNO_QUERY_THROW );
399  return xModelProp->getPropertyValue("CodeName").get< OUString >();
400 }
401 
402 sal_Int64
403 ScVbaWorkbook::getSomething(const uno::Sequence<sal_Int8 >& rId )
404 {
405  if (comphelper::isUnoTunnelId<ScVbaWorksheet>(rId)) // ???
406  {
407  return comphelper::getSomething_cast(this);
408  }
409  return 0;
410 }
411 
412 extern "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: */
URL aURL
bool hasValue()
sal_Int32 nIndex
virtual css::uno::Any SAL_CALL Windows(const css::uno::Any &aIndex) override
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * Calc_ScVbaWorkbook_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &args)
void RegisterAutomationWorkbookObject(css::uno::Reference< ooo::vba::excel::XWorkbook > const &xWorkbook)
Definition: docsh.cxx:3405
virtual OUString SAL_CALL getAuthor() 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)
uno::Reference< uno::XComponentContext > mxContext
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
virtual css::uno::Sequence< OUString > getServiceNames() override
Sequence< OUString > aServiceNames
virtual void SAL_CALL ResetColors() override
Definition: vbaworkbook.cxx:55
OUString Name
sal_Int32 XLRGBToOORGB(sal_Int32 nCol)
virtual css::uno::Any SAL_CALL Sheets(const css::uno::Any &aIndex) override
virtual css::uno::Any SAL_CALL Worksheets(const css::uno::Any &aIndex) override
constexpr OUStringLiteral SC_TEXT_CSV_FILTER_NAME
Definition: global.hxx:62
uno::Reference< XHelperInterface > getUnoSheetModuleObj(const uno::Reference< table::XCellRange > &xRange)
enumrange< T >::Iterator begin(enumrange< T >)
virtual void SAL_CALL setPrecisionAsDisplayed(sal_Bool _precisionAsDisplayed) override
virtual void SAL_CALL Activate() 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
virtual ::sal_Int32 SAL_CALL getFileFormat() override
sal_Int64 getSomething_cast(void *p)
void SetCalcAsShown(bool bVal)
Definition: docoptio.hxx:82
SC_DLLPUBLIC const ScDocOptions & GetDocOptions() const
Definition: documen3.cxx:1946
virtual ::sal_Int64 SAL_CALL getSomething(const css::uno::Sequence< sal_Int8 > &rId) override
ScDocShell * getDocShell(const css::uno::Reference< css::frame::XModel > &xModel)
cppu::ImplInheritanceHelper< VbaDocumentBase, ov::excel::XWorkbook > ScVbaWorkbook_BASE
Definition: vbaworkbook.hxx:26
virtual void SAL_CALL Protect(const css::uno::Any &aPassword) override
static css::uno::Sequence< sal_Int32 > ColorData
Definition: vbaworkbook.hxx:30
unsigned char sal_Bool
virtual css::uno::Any SAL_CALL Names(const css::uno::Any &aIndex) override
virtual void SAL_CALL Protect(const css::uno::Any &aPassword)
tuple index
enumrange< T >::Iterator end(enumrange< T >)
virtual css::uno::Any SAL_CALL Styles(const css::uno::Any &Item) override
virtual void SAL_CALL Activate() override
bool SetURL(std::u16string_view rTheAbsURIRef, EncodeMechanism eMechanism=EncodeMechanism::WasEncoded, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8)
virtual css::uno::Any SAL_CALL Colors(const css::uno::Any &Index) override
Definition: vbaworkbook.cxx:67
OUString GetMainURL(DecodeMechanism eMechanism, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
SC_DLLPUBLIC void SetDocOptions(const ScDocOptions &rOpt)
Definition: documen3.cxx:1952
static bool setFilterPropsFromFormat(sal_Int32 nFormat, css::uno::Sequence< css::beans::PropertyValue > &rProps)
Definition: vbaworkbook.cxx:81
bool IsCalcAsShown() const
Definition: docoptio.hxx:81
virtual void SAL_CALL SaveCopyAs(const OUString &Filename) override
bool Append(std::u16string_view rTheSegment, EncodeMechanism eMechanism=EncodeMechanism::WasEncoded, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8)
const ScDocument & GetDocument() const
Definition: docsh.hxx:220
uno::Reference< frame::XModel > getCurrentExcelDoc(const uno::Reference< uno::XComponentContext > &xContext)
virtual void SAL_CALL setAuthor(const OUString &_author) override
virtual OUString getServiceImplName() override
Reference< XModel > xModel
virtual css::uno::Reference< ov::excel::XWorksheet > SAL_CALL getActiveSheet() override
virtual sal_Bool SAL_CALL getProtectStructure() override
virtual sal_Bool SAL_CALL getPrecisionAsDisplayed() override
struct _ADOIndex Index
static css::uno::Reference< css::container::XIndexAccess > getDefaultPalette()
Definition: vbapalette.cxx:94
virtual OUString SAL_CALL getCodeName() override