Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / sc / source / filter / oox / excelchartconverter.cxx
blobbc5b74690f3a777147465e41c80240f89f0fe08e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <excelchartconverter.hxx>
22 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
23 #include <com/sun/star/chart2/data/XDataProvider.hpp>
24 #include <com/sun/star/chart2/data/XDataReceiver.hpp>
25 #include <com/sun/star/chart2/data/XSheetDataProvider.hpp>
27 #include <osl/diagnose.h>
28 #include <oox/core/filterbase.hxx>
29 #include <oox/drawingml/chart/datasourcemodel.hxx>
30 #include <oox/helper/containerhelper.hxx>
31 #include <formulaparser.hxx>
33 namespace oox {
34 namespace xls {
36 using namespace ::com::sun::star::chart2;
37 using namespace ::com::sun::star::chart2::data;
38 using namespace ::com::sun::star::uno;
40 using ::oox::drawingml::chart::DataSequenceModel;
42 ExcelChartConverter::ExcelChartConverter( const WorkbookHelper& rHelper ) :
43 WorkbookHelper( rHelper )
47 ExcelChartConverter::~ExcelChartConverter()
51 void ExcelChartConverter::createDataProvider( const Reference< XChartDocument >& rxChartDoc )
53 try
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 );
60 catch( Exception& )
65 Reference< XDataSequence > ExcelChartConverter::createDataSequence(
66 const Reference< XDataProvider >& rxDataProvider, const DataSequenceModel& rDataSeq,
67 const OUString& /*rRole*/ )
69 Reference< XDataSequence > xDataSeq;
70 if (!rxDataProvider.is())
71 return xDataSeq;
73 Reference<XSheetDataProvider> xSheetProvider(rxDataProvider, UNO_QUERY);
74 if (!xSheetProvider.is())
75 return xDataSeq;
77 if (!rDataSeq.maFormula.isEmpty())
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 );
84 try
86 // create the data sequence
87 xDataSeq = xSheetProvider->createDataSequenceByFormulaTokens(aTokens);
89 catch (Exception&)
91 OSL_FAIL( "ExcelChartConverter::createDataSequence - cannot create data sequence" );
94 else if (!rDataSeq.maData.empty())
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 )
102 *aMIt = rEntry.second;
103 ++aMIt;
105 OUString aRangeRep = FormulaProcessorBase::generateApiArray( aMatrix );
107 if (!aRangeRep.isEmpty())
111 // create the data sequence
112 xDataSeq = rxDataProvider->createDataSequenceByRangeRepresentation( aRangeRep );
114 catch (Exception&)
116 OSL_FAIL( "ExcelChartConverter::createDataSequence - cannot create data sequence" );
120 return xDataSeq;
123 } // namespace xls
124 } // namespace oox
126 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */