1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <com/sun/star/sheet/XSpreadsheetDocument.hpp>
26 #include <osl/diagnose.h>
27 #include <sal/log.hxx>
28 #include <oox/helper/attributelist.hxx>
29 #include <oox/helper/binaryinputstream.hxx>
30 #include <oox/helper/propertyset.hxx>
31 #include <oox/token/properties.hxx>
32 #include <oox/token/tokens.hxx>
33 #include <addressconverter.hxx>
34 #include <biffhelper.hxx>
38 using namespace ::com::sun::star::sheet
;
39 using namespace ::com::sun::star::uno
;
41 TableModel::TableModel() :
43 mnType( XML_worksheet
),
49 Table::Table( const WorkbookHelper
& rHelper
) :
50 WorkbookHelper( rHelper
),
51 maAutoFilters( rHelper
),
52 maTableColumns( rHelper
),
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
)
73 nType
= rStrm
.readInt32();
74 maModel
.mnId
= rStrm
.readInt32();
75 maModel
.mnHeaderRows
= rStrm
.readInt32();
76 maModel
.mnTotalsRows
= rStrm
.readInt32();
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() )
96 maDBRangeName
= maModel
.maDisplayName
;
98 Reference
< XDatabaseRange
> xDatabaseRange(
99 createDatabaseRangeObject( maDBRangeName
, maModel
.maRange
), UNO_SET_THROW
);
100 ::css::table::CellRangeAddress aAddressRange
= xDatabaseRange
->getDataArea();
101 maDestRange
= ScRange( aAddressRange
.StartColumn
, aAddressRange
.StartRow
, aAddressRange
.Sheet
,
102 aAddressRange
.EndColumn
, aAddressRange
.EndRow
, aAddressRange
.Sheet
);
104 PropertySet
aPropSet( xDatabaseRange
);
106 // Default HasHeader is true at ScDBData.
107 if (maModel
.mnHeaderRows
!= 1)
109 SAL_WARN_IF( maModel
.mnHeaderRows
> 1, "sc.filter",
110 "Table HeaderRows > 1 not supported: " << maModel
.mnHeaderRows
);
111 if (maModel
.mnHeaderRows
== 0)
112 aPropSet
.setProperty( PROP_ContainsHeader
, false);
115 if (maModel
.mnTotalsRows
> 0)
117 SAL_WARN_IF( maModel
.mnTotalsRows
> 1, "sc.filter",
118 "Table TotalsRows > 1 not supported: " << maModel
.mnTotalsRows
);
119 aPropSet
.setProperty( PROP_TotalsRow
, true);
122 // get formula token index of the database range
123 if( !aPropSet
.getProperty( mnTokenIndex
, PROP_TokenIndex
) )
128 OSL_FAIL( "Table::finalizeImport - cannot create database range" );
132 void Table::applyAutoFilters()
134 if( maDBRangeName
.isEmpty() )
139 // get the range ( maybe we should cache the xDatabaseRange from finalizeImport )
140 PropertySet
aDocProps( getDocument() );
141 Reference
< XDatabaseRanges
> xDatabaseRanges( aDocProps
.getAnyProperty( PROP_DatabaseRanges
), UNO_QUERY_THROW
);
142 Reference
< XDatabaseRange
> xDatabaseRange( xDatabaseRanges
->getByName( maDBRangeName
), UNO_QUERY
);
143 maAutoFilters
.finalizeImport( xDatabaseRange
, maModel
.maRange
.aStart
.Tab() );
147 OSL_FAIL( "Table::applyAutofilters - cannot create filter" );
151 void Table::applyTableColumns()
153 maTableColumns
.finalizeImport( findDatabaseRangeByIndex( mnTokenIndex
));
156 TableBuffer::TableBuffer( const WorkbookHelper
& rHelper
) :
157 WorkbookHelper( rHelper
)
161 Table
& TableBuffer::createTable()
163 TableVector::value_type xTable
= std::make_shared
<Table
>( *this );
164 maTables
.push_back( xTable
);
168 void TableBuffer::finalizeImport()
170 // map all tables by identifier and display name
171 for( const auto& rxTable
: maTables
)
172 insertTableToMaps( rxTable
);
173 // finalize all valid tables
174 maIdTables
.forEachMem( &Table::finalizeImport
);
177 void TableBuffer::applyAutoFilters()
179 maIdTables
.forEachMem( &Table::applyAutoFilters
);
182 void TableBuffer::applyTableColumns()
184 maIdTables
.forEachMem( &Table::applyTableColumns
);
187 TableRef
TableBuffer::getTable( sal_Int32 nTableId
) const
189 return maIdTables
.get( nTableId
);
192 TableRef
TableBuffer::getTable( const OUString
& rDispName
) const
194 return maNameTables
.get( rDispName
);
197 // private --------------------------------------------------------------------
199 void TableBuffer::insertTableToMaps( const TableRef
& rxTable
)
201 sal_Int32 nTableId
= rxTable
->getTableId();
202 const OUString
& rDispName
= rxTable
->getDisplayName();
203 if( (nTableId
> 0) && !rDispName
.isEmpty() )
205 OSL_ENSURE( !maIdTables
.has( nTableId
), "TableBuffer::insertTableToMaps - multiple table identifier" );
206 maIdTables
[ nTableId
] = rxTable
;
207 OSL_ENSURE( !maNameTables
.has( rDispName
), "TableBuffer::insertTableToMaps - multiple table name" );
208 maNameTables
[ rDispName
] = rxTable
;
212 } // namespace oox::xls
214 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */