LibreOffice Module sc (master) 1
tablebuffer.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 <tablebuffer.hxx>
21
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>
31#include <oox/token/properties.hxx>
32#include <oox/token/tokens.hxx>
33#include <addressconverter.hxx>
34#include <biffhelper.hxx>
35
36namespace oox::xls {
37
38using namespace ::com::sun::star::sheet;
39using namespace ::com::sun::star::uno;
40
42 mnId( -1 ),
43 mnType( XML_worksheet ),
44 mnHeaderRows( 1 ),
45 mnTotalsRows( 0 )
46{
47}
48
49Table::Table( const WorkbookHelper& rHelper ) :
50 WorkbookHelper( rHelper ),
51 maAutoFilters( rHelper ),
52 maTableColumns( rHelper ),
53 mnTokenIndex( -1 )
54{
55}
56
57void Table::importTable( const AttributeList& rAttribs, sal_Int16 nSheet )
58{
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 );
66}
67
68void Table::importTable( SequenceInputStream& rStrm, sal_Int16 nSheet )
69{
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 );
79
81 static const sal_Int32 spnTypes[] = { XML_worksheet, XML_TOKEN_INVALID, XML_TOKEN_INVALID, XML_queryTable };
83}
84
86{
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() )
92 return;
93
94 try
95 {
97
98 Reference< XDatabaseRange > xDatabaseRange(
100 ::css::table::CellRangeAddress aAddressRange = xDatabaseRange->getDataArea();
101 maDestRange = ScRange( aAddressRange.StartColumn, aAddressRange.StartRow, aAddressRange.Sheet,
102 aAddressRange.EndColumn, aAddressRange.EndRow, aAddressRange.Sheet );
103
104 PropertySet aPropSet( xDatabaseRange );
105
106 // Default HasHeader is true at ScDBData.
107 if (maModel.mnHeaderRows != 1)
108 {
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);
113 }
114
115 if (maModel.mnTotalsRows > 0)
116 {
117 SAL_WARN_IF( maModel.mnTotalsRows > 1, "sc.filter",
118 "Table TotalsRows > 1 not supported: " << maModel.mnTotalsRows);
119 aPropSet.setProperty( PROP_TotalsRow, true);
120 }
121
122 // get formula token index of the database range
123 if( !aPropSet.getProperty( mnTokenIndex, PROP_TokenIndex ) )
124 mnTokenIndex = -1;
125 }
126 catch( Exception& )
127 {
128 OSL_FAIL( "Table::finalizeImport - cannot create database range" );
129 }
130}
131
133{
134 if( maDBRangeName.isEmpty() )
135 return;
136
137 try
138 {
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 );
144 }
145 catch( Exception& )
146 {
147 OSL_FAIL( "Table::applyAutofilters - cannot create filter" );
148 }
149}
150
152{
154}
155
157 WorkbookHelper( rHelper )
158{
159}
160
162{
163 TableVector::value_type xTable = std::make_shared<Table>( *this );
164 maTables.push_back( xTable );
165 return *xTable;
166}
167
169{
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 );
175}
176
178{
179 maIdTables.forEachMem( &Table::applyAutoFilters );
180}
181
183{
185}
186
187TableRef TableBuffer::getTable( sal_Int32 nTableId ) const
188{
189 return maIdTables.get( nTableId );
190}
191
192TableRef TableBuffer::getTable( const OUString& rDispName ) const
193{
194 return maNameTables.get( rDispName );
195}
196
197// private --------------------------------------------------------------------
198
200{
201 sal_Int32 nTableId = rxTable->getTableId();
202 const OUString& rDispName = rxTable->getDisplayName();
203 if( (nTableId > 0) && !rDispName.isEmpty() )
204 {
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;
209 }
210}
211
212} // namespace oox::xls
213
214/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SCTAB Tab() const
Definition: address.hxx:283
ScAddress aStart
Definition: address.hxx:497
std::optional< OUString > getXString(sal_Int32 nAttrToken) const
std::optional< sal_Int32 > getInteger(sal_Int32 nAttrToken) const
std::optional< OUString > getString(sal_Int32 nAttrToken) const
std::optional< sal_Int32 > getToken(sal_Int32 nAttrToken) const
css::uno::Any getAnyProperty(sal_Int32 nPropId) const
bool getProperty(Type &orValue, sal_Int32 nPropId) const
bool setProperty(sal_Int32 nPropId, const Type &rValue)
container_type::value_type value_type
static bool convertToCellRangeUnchecked(ScRange &orRange, std::u16string_view aString, sal_Int16 nSheet)
Converts the passed string to a cell range address, without checking any sheet limits.
void finalizeImport(sal_Int16 nSheet)
Applies filter settings to a new database range object (used for sheet autofilter or advanced filter ...
Table & createTable()
Creates a new empty table.
TableRef getTable(sal_Int32 nTableId) const
Returns a table by its identifier.
void applyTableColumns()
Applies columns names from created database range ( requires finalizeImport to have run before being ...
void applyAutoFilters()
Applies autofilters from created database range ( requires finalizeImport to have run before being ca...
TableBuffer(const WorkbookHelper &rHelper)
RefMap< OUString, Table > maNameTables
void insertTableToMaps(const TableRef &rxTable)
Inserts the passed table into the maps according to its identifier and name.
RefMap< sal_Int32, Table > maIdTables
void finalizeImport()
Creates database ranges from all imported tables.
void finalizeImport(ScDBData *pDBData)
Applies the table columns to the passed database range.
void applyAutoFilters()
TableModel maModel
Definition: tablebuffer.hxx:81
TableColumnsBuffer maTableColumns
Filter settings for this table.
Definition: tablebuffer.hxx:83
sal_Int32 mnTokenIndex
Validated range of the table in the worksheet.
Definition: tablebuffer.hxx:86
void applyTableColumns()
void finalizeImport()
Creates a database range from this tables.
Definition: tablebuffer.cxx:85
OUString maDBRangeName
Column names of this table.
Definition: tablebuffer.hxx:84
void importTable(const AttributeList &rAttribs, sal_Int16 nSheet)
Imports a table definition from the passed attributes.
Definition: tablebuffer.cxx:57
ScRange maDestRange
Name of the database range in the Calc document.
Definition: tablebuffer.hxx:85
AutoFilterBuffer maAutoFilters
Definition: tablebuffer.hxx:82
Table(const WorkbookHelper &rHelper)
Definition: tablebuffer.cxx:49
Helper class to provide access to global workbook data.
css::uno::Reference< css::sheet::XDatabaseRange > createDatabaseRangeObject(OUString &orName, const ScRange &rRangeAddr) const
Creates and returns a database range on-the-fly in the Calc document.
ScDBData * findDatabaseRangeByIndex(sal_uInt16 nIndex) const
Finds the (already existing) database range of the given formula token index.
const css::uno::Reference< css::sheet::XSpreadsheetDocument > & getDocument() const
Returns a reference to the source/target spreadsheet document model.
sal_uInt16 mnId
#define STATIC_ARRAY_SELECT(array, index, def)
#define SAL_WARN_IF(condition, area, stream)
@ Exception
void SvStream & rStrm
std::shared_ptr< Table > TableRef
Definition: tablebuffer.hxx:89
XML_TOKEN_INVALID
QPRO_FUNC_TYPE nType
Definition: qproform.cxx:398
sal_Int32 mnType
A 2D cell range address struct for binary filters.
sal_Int32 mnType
Unique table identifier.
Definition: tablebuffer.hxx:34
OUString maDisplayName
Programmatical name.
Definition: tablebuffer.hxx:32
OUString maProgName
Original (unchecked) range of the table.
Definition: tablebuffer.hxx:31
sal_Int32 mnId
Display name.
Definition: tablebuffer.hxx:33
TableModel()
Number of totals rows.
Definition: tablebuffer.cxx:41
sal_Int32 mnTotalsRows
Number of header rows.
Definition: tablebuffer.hxx:36
sal_Int32 mnHeaderRows
Table type (worksheet, query, etc.).
Definition: tablebuffer.hxx:35