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>
39 using namespace ::com::sun::star::sheet
;
40 using namespace ::com::sun::star::uno
;
42 TableModel::TableModel() :
44 mnType( XML_worksheet
),
50 Table::Table( const WorkbookHelper
& rHelper
) :
51 WorkbookHelper( rHelper
),
52 maAutoFilters( rHelper
),
53 maTableColumns( rHelper
),
58 void Table::importTable( const AttributeList
& rAttribs
, sal_Int16 nSheet
)
60 AddressConverter::convertToCellRangeUnchecked(maModel
.maRange
,
61 rAttribs
.getString(XML_ref
, OUString()), nSheet
,
63 maModel
.maProgName
= rAttribs
.getXString( XML_name
, OUString() );
64 maModel
.maDisplayName
= rAttribs
.getXString( XML_displayName
, OUString() );
65 maModel
.mnId
= rAttribs
.getInteger( XML_id
, -1 );
66 maModel
.mnType
= rAttribs
.getToken( XML_tableType
, XML_worksheet
);
67 maModel
.mnHeaderRows
= rAttribs
.getInteger( XML_headerRowCount
, 1 );
68 maModel
.mnTotalsRows
= rAttribs
.getInteger( XML_totalsRowCount
, 0 );
71 void Table::importTable( SequenceInputStream
& rStrm
, sal_Int16 nSheet
)
76 nType
= rStrm
.readInt32();
77 maModel
.mnId
= rStrm
.readInt32();
78 maModel
.mnHeaderRows
= rStrm
.readInt32();
79 maModel
.mnTotalsRows
= rStrm
.readInt32();
81 rStrm
>> maModel
.maProgName
>> maModel
.maDisplayName
;
83 AddressConverter::convertToCellRangeUnchecked( maModel
.maRange
, aBinRange
, nSheet
);
84 static const sal_Int32 spnTypes
[] = { XML_worksheet
, XML_TOKEN_INVALID
, XML_TOKEN_INVALID
, XML_queryTable
};
85 maModel
.mnType
= STATIC_ARRAY_SELECT( spnTypes
, nType
, XML_TOKEN_INVALID
);
88 void Table::finalizeImport()
90 // Create database range. Note that Excel 2007 and later names database
91 // ranges (or tables in their terminology) as Table1, Table2 etc. We need
92 // to import them as named db ranges because they may be referenced by
93 // name in formula expressions.
94 if( (maModel
.mnId
<= 0) || maModel
.maDisplayName
.isEmpty() )
99 maDBRangeName
= maModel
.maDisplayName
;
101 Reference
< XDatabaseRange
> xDatabaseRange(
102 createDatabaseRangeObject( maDBRangeName
, maModel
.maRange
), UNO_SET_THROW
);
103 ::css::table::CellRangeAddress aAddressRange
= xDatabaseRange
->getDataArea();
104 maDestRange
= ScRange( aAddressRange
.StartColumn
, aAddressRange
.StartRow
, aAddressRange
.Sheet
,
105 aAddressRange
.EndColumn
, aAddressRange
.EndRow
, aAddressRange
.Sheet
);
107 PropertySet
aPropSet( xDatabaseRange
);
109 // Default HasHeader is true at ScDBData.
110 if (maModel
.mnHeaderRows
!= 1)
112 SAL_WARN_IF( maModel
.mnHeaderRows
> 1, "sc.filter",
113 "Table HeaderRows > 1 not supported: " << maModel
.mnHeaderRows
);
114 if (maModel
.mnHeaderRows
== 0)
115 aPropSet
.setProperty( PROP_ContainsHeader
, false);
118 if (maModel
.mnTotalsRows
> 0)
120 SAL_WARN_IF( maModel
.mnTotalsRows
> 1, "sc.filter",
121 "Table TotalsRows > 1 not supported: " << maModel
.mnTotalsRows
);
122 aPropSet
.setProperty( PROP_TotalsRow
, true);
125 // get formula token index of the database range
126 if( !aPropSet
.getProperty( mnTokenIndex
, PROP_TokenIndex
) )
131 OSL_FAIL( "Table::finalizeImport - cannot create database range" );
135 void Table::applyAutoFilters()
137 if( maDBRangeName
.isEmpty() )
142 // get the range ( maybe we should cache the xDatabaseRange from finalizeImport )
143 PropertySet
aDocProps(( Reference
< css::beans::XPropertySet
>(getDocument()) ));
144 Reference
< XDatabaseRanges
> xDatabaseRanges( aDocProps
.getAnyProperty( PROP_DatabaseRanges
), UNO_QUERY_THROW
);
145 Reference
< XDatabaseRange
> xDatabaseRange( xDatabaseRanges
->getByName( maDBRangeName
), UNO_QUERY
);
146 maAutoFilters
.finalizeImport( xDatabaseRange
, maModel
.maRange
.aStart
.Tab() );
150 OSL_FAIL( "Table::applyAutofilters - cannot create filter" );
154 void Table::applyTableColumns()
156 maTableColumns
.finalizeImport( findDatabaseRangeByIndex( mnTokenIndex
));
159 TableBuffer::TableBuffer( const WorkbookHelper
& rHelper
) :
160 WorkbookHelper( rHelper
)
164 Table
& TableBuffer::createTable()
166 TableVector::value_type xTable
= std::make_shared
<Table
>( *this );
167 maTables
.push_back( xTable
);
171 void TableBuffer::finalizeImport()
173 // map all tables by identifier and display name
174 for( const auto& rxTable
: maTables
)
175 insertTableToMaps( rxTable
);
176 // finalize all valid tables
177 maIdTables
.forEachMem( &Table::finalizeImport
);
180 void TableBuffer::applyAutoFilters()
182 maIdTables
.forEachMem( &Table::applyAutoFilters
);
185 void TableBuffer::applyTableColumns()
187 maIdTables
.forEachMem( &Table::applyTableColumns
);
190 TableRef
TableBuffer::getTable( sal_Int32 nTableId
) const
192 return maIdTables
.get( nTableId
);
195 TableRef
TableBuffer::getTable( const OUString
& rDispName
) const
197 return maNameTables
.get( rDispName
);
200 // private --------------------------------------------------------------------
202 void TableBuffer::insertTableToMaps( const TableRef
& rxTable
)
204 sal_Int32 nTableId
= rxTable
->getTableId();
205 const OUString
& rDispName
= rxTable
->getDisplayName();
206 if( (nTableId
> 0) && !rDispName
.isEmpty() )
208 OSL_ENSURE( !maIdTables
.has( nTableId
), "TableBuffer::insertTableToMaps - multiple table identifier" );
209 maIdTables
[ nTableId
] = rxTable
;
210 OSL_ENSURE( !maNameTables
.has( rDispName
), "TableBuffer::insertTableToMaps - multiple table name" );
211 maNameTables
[ rDispName
] = rxTable
;
215 } // namespace oox::xls
217 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */