LibreOffice Module sc (master)  1
excelchartconverter.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 <excelchartconverter.hxx>
21 
22 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
23 #include <com/sun/star/chart2/XChartDocument.hpp>
24 #include <com/sun/star/chart2/data/XDataProvider.hpp>
25 #include <com/sun/star/chart2/data/XDataReceiver.hpp>
26 #include <com/sun/star/chart2/data/XSheetDataProvider.hpp>
27 
28 #include <osl/diagnose.h>
29 #include <oox/core/filterbase.hxx>
32 #include <formulaparser.hxx>
33 
34 namespace oox::xls {
35 
36 using namespace ::com::sun::star::chart2;
37 using namespace ::com::sun::star::chart2::data;
38 using namespace ::com::sun::star::uno;
39 
40 using ::oox::drawingml::chart::DataSequenceModel;
41 
43  WorkbookHelper( rHelper )
44 {
45 }
46 
48 {
49 }
50 
51 void ExcelChartConverter::createDataProvider( const Reference< XChartDocument >& rxChartDoc )
52 {
53  try
54  {
55  Reference< XDataReceiver > xDataRec( rxChartDoc, UNO_QUERY_THROW );
56  Reference< XDataProvider > xDataProv( getBaseFilter().getModelFactory()->createInstance(
57  "com.sun.star.chart2.data.DataProvider" ), UNO_QUERY_THROW );
58  xDataRec->attachDataProvider( xDataProv );
59  }
60  catch( Exception& )
61  {
62  }
63 }
64 
65 Reference< XDataSequence > ExcelChartConverter::createDataSequence(
66  const Reference< XDataProvider >& rxDataProvider, const DataSequenceModel& rDataSeq,
67  const OUString& /*rRole*/ )
68 {
69  Reference< XDataSequence > xDataSeq;
70  if (!rxDataProvider.is())
71  return xDataSeq;
72 
73  Reference<XSheetDataProvider> xSheetProvider(rxDataProvider, UNO_QUERY);
74  if (!xSheetProvider.is())
75  return xDataSeq;
76 
77  if (!rDataSeq.maFormula.isEmpty())
78  {
79  // parse the formula string, create a token sequence
80  FormulaParser& rParser = getFormulaParser();
81  ScAddress aBaseAddr( SCCOL( 0 ), SCROW( 0 ), SCTAB( getCurrentSheetIndex() ) );
82  ApiTokenSequence aTokens = rParser.importFormula( aBaseAddr, rDataSeq.maFormula );
83 
84  try
85  {
86  // create the data sequence
87  xDataSeq = xSheetProvider->createDataSequenceByFormulaTokens(aTokens);
88  }
89  catch (Exception&)
90  {
91  OSL_FAIL( "ExcelChartConverter::createDataSequence - cannot create data sequence" );
92  }
93  }
94  else if (!rDataSeq.maData.empty())
95  {
96  // create a single-row array from constant source data
97  Matrix< Any > aMatrix( rDataSeq.maData.size(), 1 );
98  Matrix< Any >::iterator aMIt = aMatrix.begin();
99  // TODO: how to handle missing values in the map?
100  for( const auto& rEntry : rDataSeq.maData )
101  {
102  *aMIt = rEntry.second;
103  ++aMIt;
104  }
105  OUString aRangeRep = FormulaProcessorBase::generateApiArray( aMatrix );
106 
107  if (!aRangeRep.isEmpty())
108  {
109  try
110  {
111  // create the data sequence
112  xDataSeq = rxDataProvider->createDataSequenceByRangeRepresentation( aRangeRep );
113  }
114  catch (Exception&)
115  {
116  OSL_FAIL( "ExcelChartConverter::createDataSequence - cannot create data sequence" );
117  }
118  }
119  }
120  return xDataSeq;
121 }
122 
123 } // namespace oox::xls
124 
125 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Helper class to provide access to global workbook data.
ExcelChartConverter(const WorkbookHelper &rHelper)
virtual void createDataProvider(const css::uno::Reference< css::chart2::XChartDocument > &rxChartDoc) override
Creates an external data provider that is able to use spreadsheet data.
container_type::iterator iterator
::oox::core::FilterBase & getBaseFilter() const
Returns the base filter object (base class of all filters).
sal_Int16 getCurrentSheetIndex() const
Returns the index of the current Calc sheet, if filter currently processes a sheet.
ApiTokenSequence importFormula(const ScAddress &rBaseAddr, const OUString &rFormulaString) const
Converts an OOXML formula string.
virtual css::uno::Reference< css::chart2::data::XDataSequence > createDataSequence(const css::uno::Reference< css::chart2::data::XDataProvider > &rxDataProvider, const oox::drawingml::chart::DataSequenceModel &rDataSeq, const OUString &rRole) override
Creates a data sequence from the passed formula.
sal_Int16 SCCOL
Definition: types.hxx:22
HRESULT createInstance(REFIID iid, Ifc **ppIfc)
css::uno::Sequence< ApiToken > ApiTokenSequence
sal_Int32 SCROW
Definition: types.hxx:18
iterator begin()
Import formula parser for OOXML and BIFF filters.
FormulaParser & getFormulaParser() const
Returns a shared import formula parser (import filter only!).
static OUString generateApiArray(const Matrix< css::uno::Any > &rMatrix)
Generates an array string in Calc formula notation from the passed matrix with Any's containing doubl...
sal_Int16 SCTAB
Definition: types.hxx:23