LibreOffice Module sc (master)  1
excelfilter.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 <excelfilter.hxx>
21 
22 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
23 #include <osl/diagnose.h>
24 #include <sal/log.hxx>
25 
26 #include <excelvbaproject.hxx>
27 #include <stylesbuffer.hxx>
28 #include <themebuffer.hxx>
29 #include <workbookfragment.hxx>
30 #include <xestream.hxx>
31 
32 #include <addressconverter.hxx>
33 #include <document.hxx>
34 #include <docsh.hxx>
35 #include <scerrors.hxx>
36 #include <tools/diagnose_ex.h>
37 
38 namespace oox::xls {
39 
40 using namespace ::com::sun::star::lang;
41 using namespace ::com::sun::star::sheet;
42 using namespace ::com::sun::star::uno;
43 using namespace ::oox::core;
44 
45 using ::oox::drawingml::table::TableStyleListPtr;
46 
48  XmlFilterBase( rxContext ),
49  mpBookGlob( nullptr )
50 {
51 }
52 
54 {
55  OSL_ENSURE( !mpBookGlob, "ExcelFilter::~ExcelFilter - workbook data not cleared" );
56 }
57 
59 {
60  mpBookGlob = &rBookGlob;
61 }
62 
64 {
65  OSL_ENSURE( mpBookGlob, "ExcelFilter::getWorkbookGlobals - missing workbook data" );
66  return *mpBookGlob;
67 }
68 
70 {
71  mpBookGlob = nullptr;
72 }
73 
75 {
76  /* To activate the XLSX/XLSB dumper, insert the full path to the file
77  file:///<path-to-oox-module>/source/dump/xlsbdumper.ini
78  into the environment variable OOO_XLSBDUMPER and start the office with
79  this variable (nonpro only). */
80  //OOX_DUMP_FILE( ::oox::dump::xlsb::Dumper );
81 
82  OUString aWorkbookPath = getFragmentPathFromFirstTypeFromOfficeDoc( u"officeDocument" );
83  if( aWorkbookPath.isEmpty() )
84  return false;
85 
86  try
87  {
88  try
89  {
91  }
92  catch( const Exception& )
93  {
94  TOOLS_WARN_EXCEPTION("sc", "exception when importing document properties");
95  }
96  catch( ... )
97  {
98  SAL_WARN("sc", "exception when importing document properties");
99  }
100  /* Construct the WorkbookGlobals object referred to by every instance of
101  the class WorkbookHelper, and execute the import filter by constructing
102  an instance of WorkbookFragment and loading the file. */
104  if (xBookGlob)
105  {
106  rtl::Reference<WorkbookFragment> xWorkbookFragment( new WorkbookFragment(*xBookGlob, aWorkbookPath));
107 
108  ScDocument& rDoc = xWorkbookFragment->getScDocument();
109  ScDocShell* pDocSh = static_cast<ScDocShell*>(rDoc.GetDocumentShell());
110  assert( pDocSh );
111  pDocSh->SetInitialLinkUpdate( pDocSh->GetMedium());
112 
113  bool bRet = importFragment( xWorkbookFragment);
114  if (bRet && !pDocSh->GetErrorCode())
115  {
116  const AddressConverter& rAC = xWorkbookFragment->getAddressConverter();
117  if (rAC.isTabOverflow())
119  else if (rAC.isColOverflow())
121  else if (rAC.isRowOverflow())
123  }
124  return bRet;
125  }
126  }
127  catch (...)
128  {
129  }
130 
131  return false;
132 }
133 
135 {
136  return false;
137 }
138 
139 const ::oox::drawingml::Theme* ExcelFilter::getCurrentTheme() const
140 {
142 }
143 
145 {
146  return nullptr;
147 }
148 
150 {
151  return TableStyleListPtr();
152 }
153 
155 {
157 }
158 
160 {
162 }
163 
165 {
166  return new ExcelGraphicHelper( getWorkbookGlobals() );
167 }
168 
170 {
171  return new ExcelVbaProject( getComponentContext(), Reference< XSpreadsheetDocument >( getModel(), UNO_QUERY ) );
172 }
173 
174 sal_Bool SAL_CALL ExcelFilter::filter( const css::uno::Sequence< css::beans::PropertyValue >& rDescriptor )
175 {
176  if ( XmlFilterBase::filter( rDescriptor ) )
177  return true;
178 
179  if ( isExportFilter() )
180  {
181  bool bExportVBA = exportVBA();
182  Reference< XExporter > xExporter(
183  new XclExpXmlStream( getComponentContext(), bExportVBA, isExportTemplate() ) );
184 
185  Reference< XComponent > xDocument = getModel();
186  Reference< XFilter > xFilter( xExporter, UNO_QUERY );
187 
188  if ( xFilter.is() )
189  {
190  xExporter->setSourceDocument( xDocument );
191  if ( xFilter->filter( rDescriptor ) )
192  return true;
193  }
194  }
195 
196  return false;
197 }
198 
200 {
201  return "com.sun.star.comp.oox.xls.ExcelFilter";
202 }
203 
204 } // namespace oox::xls
205 
206 
207 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
208 com_sun_star_comp_oox_xls_ExcelFilter_get_implementation(css::uno::XComponentContext* context,
209  css::uno::Sequence<css::uno::Any> const &)
210 {
211  return cppu::acquire(new oox::xls::ExcelFilter(context));
212 }
213 
214 
215 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Helper class to provide access to global workbook data.
void registerWorkbookGlobals(WorkbookGlobals &rBookGlob)
Definition: excelfilter.cxx:58
bool exportVBA() const
void SetInitialLinkUpdate(const SfxMedium *pMedium)
Definition: docsh4.cxx:116
ErrCode GetErrorCode() const
oox::drawingml::chart::ChartConverter * getChartConverter() const
Returns the chart object converter.
virtual bool exportDocument() noexcept override
std::shared_ptr< TableStyleList > TableStyleListPtr
virtual ::oox::ole::VbaProject * implCreateVbaProject() const override
bool importFragment(const rtl::Reference< FragmentHandler > &rxHandler)
bool isExportTemplate() const
virtual ::oox::drawingml::table::TableStyleListPtr getTableStyles() override
virtual GraphicHelper * implCreateGraphicHelper() const override
bool isExportFilter() const
void useInternalChartDataTable(bool bInternal)
#define TOOLS_WARN_EXCEPTION(area, stream)
virtual bool importDocument() override
Definition: excelfilter.cxx:74
Special implementation of the VBA project for the Excel filters.
virtual ::oox::drawingml::chart::ChartConverter * getChartConverter() override
Relative character height if escaped.
WorkbookGlobals & getWorkbookGlobals() const
Definition: excelfilter.cxx:63
float u
unsigned char sal_Bool
const css::uno::Reference< css::uno::XComponentContext > & getComponentContext() const
std::shared_ptr< WorkbookGlobals > WorkbookGlobalsRef
virtual ::oox::vml::Drawing * getVmlDrawing() override
virtual OUString SAL_CALL getImplementationName() override
const css::uno::Reference< css::frame::XModel > & getModel() const
static WorkbookGlobalsRef constructGlobals(ExcelFilter &rFilter)
void unregisterWorkbookGlobals()
Definition: excelfilter.cxx:69
virtual ~ExcelFilter() override
Definition: excelfilter.cxx:53
Converter for cell addresses and cell ranges for OOXML and BIFF filters.
virtual void useInternalChartDataTable(bool bInternal) override
#define SCWARN_IMPORT_SHEET_OVERFLOW
Definition: scerrors.hxx:64
#define SCWARN_IMPORT_ROW_OVERFLOW
Definition: scerrors.hxx:62
#define SCWARN_IMPORT_COLUMN_OVERFLOW
Definition: scerrors.hxx:63
OUString getFragmentPathFromFirstTypeFromOfficeDoc(std::u16string_view rPart)
WorkbookGlobals * mpBookGlob
Definition: excelfilter.hxx:56
ExcelFilter(const css::uno::Reference< css::uno::XComponentContext > &rxContext)
Definition: excelfilter.cxx:47
ThemeBuffer & getTheme() const
Returns the office theme object read from the theme substorage.
SfxObjectShell * GetDocumentShell() const
Definition: document.hxx:1080
#define SAL_WARN(area, stream)
virtual const ::oox::drawingml::Theme * getCurrentTheme() const override
virtual sal_Bool SAL_CALL filter(const css::uno::Sequence< css::beans::PropertyValue > &rDescriptor) override
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_oox_xls_ExcelFilter_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
void SetError(ErrCode rErr)
SfxMedium * GetMedium() const