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 
36 namespace oox::xls {
37 
38 using namespace ::com::sun::star::sheet;
39 using namespace ::com::sun::star::uno;
40 
42  mnId( -1 ),
43  mnType( XML_worksheet ),
44  mnHeaderRows( 1 ),
45  mnTotalsRows( 0 )
46 {
47 }
48 
49 Table::Table( const WorkbookHelper& rHelper ) :
50  WorkbookHelper( rHelper ),
51  maAutoFilters( rHelper ),
52  maTableColumns( rHelper ),
53  mnTokenIndex( -1 )
54 {
55 }
56 
57 void 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 
68 void 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 );
143  maAutoFilters.finalizeImport( xDatabaseRange, maModel.maRange.aStart.Tab() );
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 {
184  maIdTables.forEachMem( &Table::applyTableColumns );
185 }
186 
187 TableRef TableBuffer::getTable( sal_Int32 nTableId ) const
188 {
189  return maIdTables.get( nTableId );
190 }
191 
192 TableRef 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: */
void insertTableToMaps(const TableRef &rxTable)
Inserts the passed table into the maps according to its identifier and name.
Helper class to provide access to global workbook data.
sal_Int32 mnId
Display name.
Definition: tablebuffer.hxx:34
ScRange maDestRange
Name of the database range in the Calc document.
Definition: tablebuffer.hxx:86
Table & createTable()
Creates a new empty table.
ScAddress aStart
Definition: address.hxx:500
virtual void skip(sal_Int32 nBytes, size_t nAtomSize=1) override
OUString maDisplayName
Programmatical name.
Definition: tablebuffer.hxx:33
void importTable(const AttributeList &rAttribs, sal_Int16 nSheet)
Imports a table definition from the passed attributes.
Definition: tablebuffer.cxx:57
bool getProperty(Type &orValue, sal_Int32 nPropId) const
OptValue< OUString > getXString(sal_Int32 nAttrToken) const
void applyAutoFilters()
OptValue< sal_Int32 > getInteger(sal_Int32 nAttrToken) const
sal_Int32 mnHeaderRows
Table type (worksheet, query, etc.).
Definition: tablebuffer.hxx:36
sal_Int32 mnTotalsRows
Number of header rows.
Definition: tablebuffer.hxx:37
A 2D cell range address struct for binary filters.
css::uno::Any getAnyProperty(sal_Int32 nPropId) const
#define STATIC_ARRAY_SELECT(array, index, def)
OptValue< OUString > getString(sal_Int32 nAttrToken) const
sal_uInt16 mnId
static bool convertToCellRangeUnchecked(ScRange &orRange, const OUString &rString, sal_Int16 nSheet)
Converts the passed string to a cell range address, without checking any sheet limits.
void applyTableColumns()
sal_Int32 mnTokenIndex
Validated range of the table in the worksheet.
Definition: tablebuffer.hxx:87
TableModel()
Number of totals rows.
Definition: tablebuffer.cxx:41
TableModel maModel
Definition: tablebuffer.hxx:82
RefMap< sal_Int32, Table > maIdTables
std::shared_ptr< Table > TableRef
Definition: tablebuffer.hxx:90
OUString maProgName
Original (unchecked) range of the table.
Definition: tablebuffer.hxx:32
ScDBData * findDatabaseRangeByIndex(sal_uInt16 nIndex) const
Finds the (already existing) database range of the given formula token index.
SCTAB Tab() const
Definition: address.hxx:271
void applyAutoFilters()
Applies autofilters from created database range ( requires finalizeImport to have run before being ca...
AutoFilterBuffer maAutoFilters
Definition: tablebuffer.hxx:83
void finalizeImport()
Creates database ranges from all imported tables.
container_type::value_type value_type
sal_Int32 mnType
const css::uno::Reference< css::sheet::XSpreadsheetDocument > & getDocument() const
Returns a reference to the source/target spreadsheet document model.
XML_TOKEN_INVALID
void finalizeImport(sal_Int16 nSheet)
Applies filter settings to a new database range object (used for sheet autofilter or advanced filter ...
void applyTableColumns()
Applies columns names from created database range ( requires finalizeImport to have run before being ...
void finalizeImport()
Creates a database range from this tables.
Definition: tablebuffer.cxx:85
sal_Int32 mnType
Unique table identifier.
Definition: tablebuffer.hxx:35
#define SAL_WARN_IF(condition, area, stream)
TableColumnsBuffer maTableColumns
Filter settings for this table.
Definition: tablebuffer.hxx:84
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.
TableRef getTable(sal_Int32 nTableId) const
Returns a table by its identifier.
QPRO_FUNC_TYPE nType
Definition: qproform.cxx:401
void finalizeImport(ScDBData *pDBData)
Applies the table columns to the passed database range.
RefMap< OUString, Table > maNameTables
Table(const WorkbookHelper &rHelper)
Definition: tablebuffer.cxx:49
TableBuffer(const WorkbookHelper &rHelper)
bool setProperty(sal_Int32 nPropId, const Type &rValue)
OUString maDBRangeName
Column names of this table.
Definition: tablebuffer.hxx:85
OptValue< sal_Int32 > getToken(sal_Int32 nAttrToken) const