Line data Source code
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/sheet/XDatabaseRange.hpp>
23 : #include <com/sun/star/sheet/XDatabaseRanges.hpp>
24 : #include <osl/diagnose.h>
25 : #include <oox/helper/attributelist.hxx>
26 : #include <oox/helper/binaryinputstream.hxx>
27 : #include <oox/helper/propertyset.hxx>
28 : #include <oox/token/properties.hxx>
29 : #include "addressconverter.hxx"
30 :
31 : namespace oox {
32 : namespace xls {
33 :
34 : using namespace ::com::sun::star::container;
35 : using namespace ::com::sun::star::sheet;
36 : using namespace ::com::sun::star::uno;
37 :
38 0 : TableModel::TableModel() :
39 : mnId( -1 ),
40 : mnType( XML_worksheet ),
41 : mnHeaderRows( 1 ),
42 0 : mnTotalsRows( 0 )
43 : {
44 0 : }
45 :
46 0 : Table::Table( const WorkbookHelper& rHelper ) :
47 : WorkbookHelper( rHelper ),
48 : maAutoFilters( rHelper ),
49 0 : mnTokenIndex( -1 )
50 : {
51 0 : }
52 :
53 0 : void Table::importTable( const AttributeList& rAttribs, sal_Int16 nSheet )
54 : {
55 0 : AddressConverter::convertToCellRangeUnchecked( maModel.maRange, rAttribs.getString( XML_ref, OUString() ), nSheet );
56 0 : maModel.maProgName = rAttribs.getXString( XML_name, OUString() );
57 0 : maModel.maDisplayName = rAttribs.getXString( XML_displayName, OUString() );
58 0 : maModel.mnId = rAttribs.getInteger( XML_id, -1 );
59 0 : maModel.mnType = rAttribs.getToken( XML_tableType, XML_worksheet );
60 0 : maModel.mnHeaderRows = rAttribs.getInteger( XML_headerRowCount, 1 );
61 0 : maModel.mnTotalsRows = rAttribs.getInteger( XML_totalsRowCount, 0 );
62 0 : }
63 :
64 0 : void Table::importTable( SequenceInputStream& rStrm, sal_Int16 nSheet )
65 : {
66 0 : BinRange aBinRange;
67 : sal_Int32 nType;
68 0 : rStrm >> aBinRange;
69 0 : nType = rStrm.readInt32();
70 0 : maModel.mnId = rStrm.readInt32();
71 0 : maModel.mnHeaderRows = rStrm.readInt32();
72 0 : maModel.mnTotalsRows = rStrm.readInt32();
73 0 : rStrm.skip( 32 );
74 0 : rStrm >> maModel.maProgName >> maModel.maDisplayName;
75 :
76 0 : AddressConverter::convertToCellRangeUnchecked( maModel.maRange, aBinRange, nSheet );
77 : static const sal_Int32 spnTypes[] = { XML_worksheet, XML_TOKEN_INVALID, XML_TOKEN_INVALID, XML_queryTable };
78 0 : maModel.mnType = STATIC_ARRAY_SELECT( spnTypes, nType, XML_TOKEN_INVALID );
79 0 : }
80 :
81 0 : void Table::finalizeImport()
82 : {
83 : // Create database range. Note that Excel 2007 and later names database
84 : // ranges (or tables in their terminology) as Table1, Table2 etc. We need
85 : // to import them as named db ranges because they may be referenced by
86 : // name in formula expressions.
87 0 : if( (maModel.mnId > 0) && !maModel.maDisplayName.isEmpty() ) try
88 : {
89 0 : maDBRangeName = maModel.maDisplayName;
90 : Reference< XDatabaseRange > xDatabaseRange(
91 0 : createDatabaseRangeObject( maDBRangeName, maModel.maRange ), UNO_SET_THROW);
92 0 : maDestRange = xDatabaseRange->getDataArea();
93 :
94 0 : PropertySet aPropSet( xDatabaseRange );
95 :
96 0 : if (maModel.mnTotalsRows > 0)
97 : {
98 : SAL_WARN_IF( maModel.mnTotalsRows > 1, "sc.filter",
99 : "Table TotalsRows > 1 not supported: " << maModel.mnTotalsRows);
100 0 : aPropSet.setProperty( PROP_TotalsRow, true);
101 : }
102 :
103 : // get formula token index of the database range
104 0 : if( !aPropSet.getProperty( mnTokenIndex, PROP_TokenIndex ) )
105 0 : mnTokenIndex = -1;
106 : }
107 0 : catch( Exception& )
108 : {
109 : OSL_FAIL( "Table::finalizeImport - cannot create database range" );
110 : }
111 0 : }
112 :
113 0 : void Table::applyAutoFilters()
114 : {
115 0 : if( !maDBRangeName.isEmpty() )
116 : {
117 : try
118 : {
119 : // get the range ( maybe we should cache the xDatabaseRange from finalizeImport )
120 0 : PropertySet aDocProps( getDocument() );
121 0 : Reference< XDatabaseRanges > xDatabaseRanges( aDocProps.getAnyProperty( PROP_DatabaseRanges ), UNO_QUERY_THROW );
122 0 : Reference< XDatabaseRange > xDatabaseRange( xDatabaseRanges->getByName( maDBRangeName ), UNO_QUERY );
123 0 : maAutoFilters.finalizeImport( xDatabaseRange );
124 : }
125 0 : catch( Exception& )
126 : {
127 : OSL_FAIL( "Table::applyAutofilters - cannot create filter" );
128 : }
129 : }
130 0 : }
131 :
132 145 : TableBuffer::TableBuffer( const WorkbookHelper& rHelper ) :
133 145 : WorkbookHelper( rHelper )
134 : {
135 145 : }
136 :
137 0 : Table& TableBuffer::createTable()
138 : {
139 0 : TableVector::value_type xTable( new Table( *this ) );
140 0 : maTables.push_back( xTable );
141 0 : return *xTable;
142 : }
143 :
144 145 : void TableBuffer::finalizeImport()
145 : {
146 : // map all tables by identifier and display name
147 145 : for( TableVector::iterator aIt = maTables.begin(), aEnd = maTables.end(); aIt != aEnd; ++aIt )
148 0 : insertTableToMaps( *aIt );
149 : // finalize all valid tables
150 145 : maIdTables.forEachMem( &Table::finalizeImport );
151 145 : }
152 :
153 269 : void TableBuffer::applyAutoFilters()
154 : {
155 269 : maIdTables.forEachMem( &Table::applyAutoFilters );
156 269 : }
157 :
158 0 : TableRef TableBuffer::getTable( sal_Int32 nTableId ) const
159 : {
160 0 : return maIdTables.get( nTableId );
161 : }
162 :
163 0 : TableRef TableBuffer::getTable( const OUString& rDispName ) const
164 : {
165 0 : return maNameTables.get( rDispName );
166 : }
167 :
168 : // private --------------------------------------------------------------------
169 :
170 0 : void TableBuffer::insertTableToMaps( const TableRef& rxTable )
171 : {
172 0 : sal_Int32 nTableId = rxTable->getTableId();
173 0 : const OUString& rDispName = rxTable->getDisplayName();
174 0 : if( (nTableId > 0) && !rDispName.isEmpty() )
175 : {
176 : OSL_ENSURE( !maIdTables.has( nTableId ), "TableBuffer::insertTableToMaps - multiple table identifier" );
177 0 : maIdTables[ nTableId ] = rxTable;
178 : OSL_ENSURE( !maNameTables.has( rDispName ), "TableBuffer::insertTableToMaps - multiple table name" );
179 0 : maNameTables[ rDispName ] = rxTable;
180 : }
181 0 : }
182 :
183 : } // namespace xls
184 30 : } // namespace oox
185 :
186 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|