Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / sc / source / filter / excel / xedbdata.cxx
blob7dfb44eec05811ed5a5efce93da1bb4374327268
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
10 #include <xedbdata.hxx>
11 #include <excrecds.hxx>
12 #include <dbdata.hxx>
13 #include <document.hxx>
14 #include <oox/export/utils.hxx>
15 #include <oox/token/namespaces.hxx>
17 using namespace oox;
19 /** (So far) dummy implementation of table export for BIFF5/BIFF7. */
20 class XclExpTablesImpl5 : public XclExpTables
22 public:
23 explicit XclExpTablesImpl5( const XclExpRoot& rRoot );
25 virtual void Save( XclExpStream& rStrm ) override;
26 virtual void SaveXml( XclExpXmlStream& rStrm ) override;
29 /** Implementation of table export for OOXML, so far dummy for BIFF8. */
30 class XclExpTablesImpl8 : public XclExpTables
32 public:
33 explicit XclExpTablesImpl8( const XclExpRoot& rRoot );
35 virtual void Save( XclExpStream& rStrm ) override;
36 virtual void SaveXml( XclExpXmlStream& rStrm ) override;
40 XclExpTablesImpl5::XclExpTablesImpl5( const XclExpRoot& rRoot ) :
41 XclExpTables( rRoot )
45 void XclExpTablesImpl5::Save( XclExpStream& /*rStrm*/ )
47 // not implemented
50 void XclExpTablesImpl5::SaveXml( XclExpXmlStream& /*rStrm*/ )
52 // not applicable
56 XclExpTablesImpl8::XclExpTablesImpl8( const XclExpRoot& rRoot ) :
57 XclExpTables( rRoot )
61 void XclExpTablesImpl8::Save( XclExpStream& /*rStrm*/ )
63 // not implemented
66 void XclExpTablesImpl8::SaveXml( XclExpXmlStream& rStrm )
69 sax_fastparser::FSHelperPtr& pWorksheetStrm = rStrm.GetCurrentStream();
70 pWorksheetStrm->startElement(XML_tableParts);
71 for (auto const& it : maTables)
73 OUString aRelId;
74 sax_fastparser::FSHelperPtr pTableStrm = rStrm.CreateOutputStream(
75 XclXmlUtils::GetStreamName("xl/tables/", "table", it.mnTableId),
76 XclXmlUtils::GetStreamName("../tables/", "table", it.mnTableId),
77 pWorksheetStrm->getOutputStream(),
78 CREATE_XL_CONTENT_TYPE("table"),
79 CREATE_OFFICEDOC_RELATION_TYPE("table"),
80 &aRelId);
82 pWorksheetStrm->singleElement(XML_tablePart, FSNS(XML_r, XML_id), aRelId.toUtf8());
84 rStrm.PushStream( pTableStrm);
85 SaveTableXml( rStrm, it);
86 rStrm.PopStream();
88 pWorksheetStrm->endElement( XML_tableParts);
92 XclExpTablesManager::XclExpTablesManager( const XclExpRoot& rRoot ) :
93 XclExpRoot( rRoot )
97 XclExpTablesManager::~XclExpTablesManager()
101 void XclExpTablesManager::Initialize()
103 // All non-const to be able to call RefreshTableColumnNames().
104 ScDocument& rDoc = GetDoc();
105 ScDBCollection* pDBColl = rDoc.GetDBCollection();
106 if (!pDBColl)
107 return;
109 ScDBCollection::NamedDBs& rDBs = pDBColl->getNamedDBs();
110 if (rDBs.empty())
111 return;
113 sal_Int32 nTableId = 0;
114 for (const auto& rxDB : rDBs)
116 ScDBData* pDBData = rxDB.get();
117 pDBData->RefreshTableColumnNames( &rDoc); // currently not in sync, so refresh
118 ScRange aRange( ScAddress::UNINITIALIZED);
119 pDBData->GetArea( aRange);
120 SCTAB nTab = aRange.aStart.Tab();
121 TablesMapType::iterator it = maTablesMap.find( nTab);
122 if (it == maTablesMap.end())
124 ::std::shared_ptr< XclExpTables > pNew;
125 switch( GetBiff() )
127 case EXC_BIFF5:
128 pNew.reset( new XclExpTablesImpl5( GetRoot()));
129 break;
130 case EXC_BIFF8:
131 pNew.reset( new XclExpTablesImpl8( GetRoot()));
132 break;
133 default:
134 assert(!"Unknown BIFF type!");
135 continue; // for
137 ::std::pair< TablesMapType::iterator, bool > ins( maTablesMap.insert( ::std::make_pair( nTab, pNew)));
138 if (!ins.second)
140 assert(!"XclExpTablesManager::Initialize - XclExpTables insert failed");
141 continue; // for
143 it = ins.first;
145 it->second->AppendTable( pDBData, ++nTableId);
149 ::std::shared_ptr< XclExpTables > XclExpTablesManager::GetTablesBySheet( SCTAB nTab )
151 TablesMapType::iterator it = maTablesMap.find(nTab);
152 return it == maTablesMap.end() ? nullptr : it->second;
155 XclExpTables::Entry::Entry( const ScDBData* pData, sal_Int32 nTableId ) :
156 mpData(pData), mnTableId(nTableId)
160 XclExpTables::XclExpTables( const XclExpRoot& rRoot ) :
161 XclExpRoot(rRoot)
165 XclExpTables::~XclExpTables()
169 void XclExpTables::AppendTable( const ScDBData* pData, sal_Int32 nTableId )
171 maTables.emplace_back( pData, nTableId);
174 void XclExpTables::SaveTableXml( XclExpXmlStream& rStrm, const Entry& rEntry )
176 const ScDBData& rData = *rEntry.mpData;
177 ScRange aRange( ScAddress::UNINITIALIZED);
178 rData.GetArea( aRange);
179 sax_fastparser::FSHelperPtr& pTableStrm = rStrm.GetCurrentStream();
180 pTableStrm->startElement( XML_table,
181 XML_xmlns, rStrm.getNamespaceURL(OOX_NS(xls)).toUtf8(),
182 XML_id, OString::number( rEntry.mnTableId),
183 XML_name, rData.GetName().toUtf8(),
184 XML_displayName, rData.GetName().toUtf8(),
185 XML_ref, XclXmlUtils::ToOString(&rStrm.GetRoot().GetDoc(), aRange),
186 XML_headerRowCount, ToPsz10(rData.HasHeader()),
187 XML_totalsRowCount, ToPsz10(rData.HasTotals()),
188 XML_totalsRowShown, ToPsz10(rData.HasTotals()) // we don't support that but if there are totals they are shown
189 // OOXTODO: XML_comment, ...,
190 // OOXTODO: XML_connectionId, ...,
191 // OOXTODO: XML_dataCellStyle, ...,
192 // OOXTODO: XML_dataDxfId, ...,
193 // OOXTODO: XML_headerRowBorderDxfId, ...,
194 // OOXTODO: XML_headerRowCellStyle, ...,
195 // OOXTODO: XML_headerRowDxfId, ...,
196 // OOXTODO: XML_insertRow, ...,
197 // OOXTODO: XML_insertRowShift, ...,
198 // OOXTODO: XML_published, ...,
199 // OOXTODO: XML_tableBorderDxfId, ...,
200 // OOXTODO: XML_tableType, ...,
201 // OOXTODO: XML_totalsRowBorderDxfId, ...,
202 // OOXTODO: XML_totalsRowCellStyle, ...,
203 // OOXTODO: XML_totalsRowDxfId, ...
206 if (rData.HasAutoFilter())
208 /* TODO: does this need to exclude totals row? */
210 /* TODO: in OOXML 12.3.21 Table Definition Part has information
211 * that an applied autoFilter has child elements
212 * <af:filterColumn><af:filters><af:filter>.
213 * When not applied but buttons hidden, Excel writes, for example,
214 * <filterColumn colId="0" hiddenButton="1"/> */
216 ExcAutoFilterRecs aAutoFilter( rStrm.GetRoot(), aRange.aStart.Tab(), &rData);
217 aAutoFilter.SaveXml( rStrm);
220 const std::vector< OUString >& rColNames = rData.GetTableColumnNames();
221 if (!rColNames.empty())
223 pTableStrm->startElement(XML_tableColumns,
224 XML_count, OString::number(aRange.aEnd.Col() - aRange.aStart.Col() + 1));
226 for (size_t i=0, n=rColNames.size(); i < n; ++i)
228 // OOXTODO: write <calculatedColumnFormula> once we support it, in
229 // which case we'd need start/endElement XML_tableColumn for such
230 // column.
232 // OOXTODO: write <totalsRowFormula> once we support it.
234 pTableStrm->singleElement( XML_tableColumn,
235 XML_id, OString::number(i+1),
236 XML_name, rColNames[i].toUtf8()
237 // OOXTODO: XML_dataCellStyle, ...,
238 // OOXTODO: XML_dataDxfId, ...,
239 // OOXTODO: XML_headerRowCellStyle, ...,
240 // OOXTODO: XML_headerRowDxfId, ...,
241 // OOXTODO: XML_queryTableFieldId, ...,
242 // OOXTODO: XML_totalsRowCellStyle, ...,
243 // OOXTODO: XML_totalsRowDxfId, ...,
244 // OOXTODO: XML_totalsRowFunction, ...,
245 // OOXTODO: XML_totalsRowLabel, ...,
246 // OOXTODO: XML_uniqueName, ...
250 pTableStrm->endElement( XML_tableColumns);
253 // OOXTODO: write <tableStyleInfo> once we have table styles.
255 pTableStrm->endElement( XML_table);
258 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */