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/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"
34 using namespace ::com::sun::star::container
;
35 using namespace ::com::sun::star::sheet
;
36 using namespace ::com::sun::star::uno
;
38 TableModel::TableModel() :
40 mnType( XML_worksheet
),
46 Table::Table( const WorkbookHelper
& rHelper
) :
47 WorkbookHelper( rHelper
),
48 maAutoFilters( rHelper
),
49 maTableColumns( rHelper
),
54 void Table::importTable( const AttributeList
& rAttribs
, sal_Int16 nSheet
)
56 AddressConverter::convertToCellRangeUnchecked( maModel
.maRange
, rAttribs
.getString( XML_ref
, OUString() ), nSheet
);
57 maModel
.maProgName
= rAttribs
.getXString( XML_name
, OUString() );
58 maModel
.maDisplayName
= rAttribs
.getXString( XML_displayName
, OUString() );
59 maModel
.mnId
= rAttribs
.getInteger( XML_id
, -1 );
60 maModel
.mnType
= rAttribs
.getToken( XML_tableType
, XML_worksheet
);
61 maModel
.mnHeaderRows
= rAttribs
.getInteger( XML_headerRowCount
, 1 );
62 maModel
.mnTotalsRows
= rAttribs
.getInteger( XML_totalsRowCount
, 0 );
65 void Table::importTable( SequenceInputStream
& rStrm
, sal_Int16 nSheet
)
70 nType
= rStrm
.readInt32();
71 maModel
.mnId
= rStrm
.readInt32();
72 maModel
.mnHeaderRows
= rStrm
.readInt32();
73 maModel
.mnTotalsRows
= rStrm
.readInt32();
75 rStrm
>> maModel
.maProgName
>> maModel
.maDisplayName
;
77 AddressConverter::convertToCellRangeUnchecked( maModel
.maRange
, aBinRange
, nSheet
);
78 static const sal_Int32 spnTypes
[] = { XML_worksheet
, XML_TOKEN_INVALID
, XML_TOKEN_INVALID
, XML_queryTable
};
79 maModel
.mnType
= STATIC_ARRAY_SELECT( spnTypes
, nType
, XML_TOKEN_INVALID
);
82 void Table::finalizeImport()
84 // Create database range. Note that Excel 2007 and later names database
85 // ranges (or tables in their terminology) as Table1, Table2 etc. We need
86 // to import them as named db ranges because they may be referenced by
87 // name in formula expressions.
88 if( (maModel
.mnId
> 0) && !maModel
.maDisplayName
.isEmpty() ) try
90 maDBRangeName
= maModel
.maDisplayName
;
91 Reference
< XDatabaseRange
> xDatabaseRange(
92 createDatabaseRangeObject( maDBRangeName
, maModel
.maRange
), UNO_SET_THROW
);
93 maDestRange
= xDatabaseRange
->getDataArea();
95 PropertySet
aPropSet( xDatabaseRange
);
97 // Default HasHeader is true at ScDBData.
98 if (maModel
.mnHeaderRows
!= 1)
100 SAL_WARN_IF( maModel
.mnHeaderRows
> 1, "sc.filter",
101 "Table HeaderRows > 1 not supported: " << maModel
.mnHeaderRows
);
102 if (maModel
.mnHeaderRows
== 0)
103 aPropSet
.setProperty( PROP_ContainsHeader
, false);
106 if (maModel
.mnTotalsRows
> 0)
108 SAL_WARN_IF( maModel
.mnTotalsRows
> 1, "sc.filter",
109 "Table TotalsRows > 1 not supported: " << maModel
.mnTotalsRows
);
110 aPropSet
.setProperty( PROP_TotalsRow
, true);
113 // get formula token index of the database range
114 if( !aPropSet
.getProperty( mnTokenIndex
, PROP_TokenIndex
) )
119 OSL_FAIL( "Table::finalizeImport - cannot create database range" );
123 void Table::applyAutoFilters()
125 if( !maDBRangeName
.isEmpty() )
129 // get the range ( maybe we should cache the xDatabaseRange from finalizeImport )
130 PropertySet
aDocProps( getDocument() );
131 Reference
< XDatabaseRanges
> xDatabaseRanges( aDocProps
.getAnyProperty( PROP_DatabaseRanges
), UNO_QUERY_THROW
);
132 Reference
< XDatabaseRange
> xDatabaseRange( xDatabaseRanges
->getByName( maDBRangeName
), UNO_QUERY
);
133 maAutoFilters
.finalizeImport( xDatabaseRange
);
137 OSL_FAIL( "Table::applyAutofilters - cannot create filter" );
142 void Table::applyTableColumns()
144 maTableColumns
.finalizeImport( findDatabaseRangeByIndex( mnTokenIndex
));
147 TableBuffer::TableBuffer( const WorkbookHelper
& rHelper
) :
148 WorkbookHelper( rHelper
)
152 Table
& TableBuffer::createTable()
154 TableVector::value_type
xTable( new Table( *this ) );
155 maTables
.push_back( xTable
);
159 void TableBuffer::finalizeImport()
161 // map all tables by identifier and display name
162 for( TableVector::iterator aIt
= maTables
.begin(), aEnd
= maTables
.end(); aIt
!= aEnd
; ++aIt
)
163 insertTableToMaps( *aIt
);
164 // finalize all valid tables
165 maIdTables
.forEachMem( &Table::finalizeImport
);
168 void TableBuffer::applyAutoFilters()
170 maIdTables
.forEachMem( &Table::applyAutoFilters
);
173 void TableBuffer::applyTableColumns()
175 maIdTables
.forEachMem( &Table::applyTableColumns
);
178 TableRef
TableBuffer::getTable( sal_Int32 nTableId
) const
180 return maIdTables
.get( nTableId
);
183 TableRef
TableBuffer::getTable( const OUString
& rDispName
) const
185 return maNameTables
.get( rDispName
);
188 // private --------------------------------------------------------------------
190 void TableBuffer::insertTableToMaps( const TableRef
& rxTable
)
192 sal_Int32 nTableId
= rxTable
->getTableId();
193 const OUString
& rDispName
= rxTable
->getDisplayName();
194 if( (nTableId
> 0) && !rDispName
.isEmpty() )
196 OSL_ENSURE( !maIdTables
.has( nTableId
), "TableBuffer::insertTableToMaps - multiple table identifier" );
197 maIdTables
[ nTableId
] = rxTable
;
198 OSL_ENSURE( !maNameTables
.has( rDispName
), "TableBuffer::insertTableToMaps - multiple table name" );
199 maNameTables
[ rDispName
] = rxTable
;
206 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */