Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / sc / source / filter / oox / tablebuffer.cxx
blob8c8526ce9a57d8ada9f1d183144390ca7739c971
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 <tablebuffer.hxx>
22 #include <com/sun/star/beans/XPropertySet.hpp>
23 #include <com/sun/star/sheet/XDatabaseRange.hpp>
24 #include <com/sun/star/sheet/XDatabaseRanges.hpp>
25 #include <osl/diagnose.h>
26 #include <sal/log.hxx>
27 #include <oox/helper/attributelist.hxx>
28 #include <oox/helper/binaryinputstream.hxx>
29 #include <oox/helper/propertyset.hxx>
30 #include <oox/token/properties.hxx>
31 #include <oox/token/tokens.hxx>
32 #include <addressconverter.hxx>
33 #include <biffhelper.hxx>
35 namespace oox {
36 namespace xls {
38 using namespace ::com::sun::star::sheet;
39 using namespace ::com::sun::star::uno;
41 TableModel::TableModel() :
42 mnId( -1 ),
43 mnType( XML_worksheet ),
44 mnHeaderRows( 1 ),
45 mnTotalsRows( 0 )
49 Table::Table( const WorkbookHelper& rHelper ) :
50 WorkbookHelper( rHelper ),
51 maAutoFilters( rHelper ),
52 maTableColumns( rHelper ),
53 mnTokenIndex( -1 )
57 void Table::importTable( const AttributeList& rAttribs, sal_Int16 nSheet )
59 AddressConverter::convertToCellRangeUnchecked( maModel.maRange, rAttribs.getString( XML_ref, OUString() ), nSheet );
60 maModel.maProgName = rAttribs.getXString( XML_name, OUString() );
61 maModel.maDisplayName = rAttribs.getXString( XML_displayName, OUString() );
62 maModel.mnId = rAttribs.getInteger( XML_id, -1 );
63 maModel.mnType = rAttribs.getToken( XML_tableType, XML_worksheet );
64 maModel.mnHeaderRows = rAttribs.getInteger( XML_headerRowCount, 1 );
65 maModel.mnTotalsRows = rAttribs.getInteger( XML_totalsRowCount, 0 );
68 void Table::importTable( SequenceInputStream& rStrm, sal_Int16 nSheet )
70 BinRange aBinRange;
71 sal_Int32 nType;
72 rStrm >> aBinRange;
73 nType = rStrm.readInt32();
74 maModel.mnId = rStrm.readInt32();
75 maModel.mnHeaderRows = rStrm.readInt32();
76 maModel.mnTotalsRows = rStrm.readInt32();
77 rStrm.skip( 32 );
78 rStrm >> maModel.maProgName >> maModel.maDisplayName;
80 AddressConverter::convertToCellRangeUnchecked( maModel.maRange, aBinRange, nSheet );
81 static const sal_Int32 spnTypes[] = { XML_worksheet, XML_TOKEN_INVALID, XML_TOKEN_INVALID, XML_queryTable };
82 maModel.mnType = STATIC_ARRAY_SELECT( spnTypes, nType, XML_TOKEN_INVALID );
85 void Table::finalizeImport()
87 // Create database range. Note that Excel 2007 and later names database
88 // ranges (or tables in their terminology) as Table1, Table2 etc. We need
89 // to import them as named db ranges because they may be referenced by
90 // name in formula expressions.
91 if( (maModel.mnId > 0) && !maModel.maDisplayName.isEmpty() ) try
93 maDBRangeName = maModel.maDisplayName;
95 Reference< XDatabaseRange > xDatabaseRange(
96 createDatabaseRangeObject( maDBRangeName, maModel.maRange ), UNO_SET_THROW);
97 ::css::table::CellRangeAddress aAddressRange = xDatabaseRange->getDataArea();
98 maDestRange = ScRange( aAddressRange.StartColumn, aAddressRange.StartRow, aAddressRange.Sheet,
99 aAddressRange.EndColumn, aAddressRange.EndRow, aAddressRange.Sheet );
101 PropertySet aPropSet( xDatabaseRange );
103 // Default HasHeader is true at ScDBData.
104 if (maModel.mnHeaderRows != 1)
106 SAL_WARN_IF( maModel.mnHeaderRows > 1, "sc.filter",
107 "Table HeaderRows > 1 not supported: " << maModel.mnHeaderRows);
108 if (maModel.mnHeaderRows == 0)
109 aPropSet.setProperty( PROP_ContainsHeader, false);
112 if (maModel.mnTotalsRows > 0)
114 SAL_WARN_IF( maModel.mnTotalsRows > 1, "sc.filter",
115 "Table TotalsRows > 1 not supported: " << maModel.mnTotalsRows);
116 aPropSet.setProperty( PROP_TotalsRow, true);
119 // get formula token index of the database range
120 if( !aPropSet.getProperty( mnTokenIndex, PROP_TokenIndex ) )
121 mnTokenIndex = -1;
123 catch( Exception& )
125 OSL_FAIL( "Table::finalizeImport - cannot create database range" );
129 void Table::applyAutoFilters()
131 if( !maDBRangeName.isEmpty() )
135 // get the range ( maybe we should cache the xDatabaseRange from finalizeImport )
136 PropertySet aDocProps( getDocument() );
137 Reference< XDatabaseRanges > xDatabaseRanges( aDocProps.getAnyProperty( PROP_DatabaseRanges ), UNO_QUERY_THROW );
138 Reference< XDatabaseRange > xDatabaseRange( xDatabaseRanges->getByName( maDBRangeName ), UNO_QUERY );
139 maAutoFilters.finalizeImport( xDatabaseRange );
141 catch( Exception& )
143 OSL_FAIL( "Table::applyAutofilters - cannot create filter" );
148 void Table::applyTableColumns()
150 maTableColumns.finalizeImport( findDatabaseRangeByIndex( mnTokenIndex ));
153 TableBuffer::TableBuffer( const WorkbookHelper& rHelper ) :
154 WorkbookHelper( rHelper )
158 Table& TableBuffer::createTable()
160 TableVector::value_type xTable( new Table( *this ) );
161 maTables.push_back( xTable );
162 return *xTable;
165 void TableBuffer::finalizeImport()
167 // map all tables by identifier and display name
168 for( const auto& rxTable : maTables )
169 insertTableToMaps( rxTable );
170 // finalize all valid tables
171 maIdTables.forEachMem( &Table::finalizeImport );
174 void TableBuffer::applyAutoFilters()
176 maIdTables.forEachMem( &Table::applyAutoFilters );
179 void TableBuffer::applyTableColumns()
181 maIdTables.forEachMem( &Table::applyTableColumns );
184 TableRef TableBuffer::getTable( sal_Int32 nTableId ) const
186 return maIdTables.get( nTableId );
189 TableRef TableBuffer::getTable( const OUString& rDispName ) const
191 return maNameTables.get( rDispName );
194 // private --------------------------------------------------------------------
196 void TableBuffer::insertTableToMaps( const TableRef& rxTable )
198 sal_Int32 nTableId = rxTable->getTableId();
199 const OUString& rDispName = rxTable->getDisplayName();
200 if( (nTableId > 0) && !rDispName.isEmpty() )
202 OSL_ENSURE( !maIdTables.has( nTableId ), "TableBuffer::insertTableToMaps - multiple table identifier" );
203 maIdTables[ nTableId ] = rxTable;
204 OSL_ENSURE( !maNameTables.has( rDispName ), "TableBuffer::insertTableToMaps - multiple table name" );
205 maNameTables[ rDispName ] = rxTable;
209 } // namespace xls
210 } // namespace oox
212 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */