LibreOffice Module sc (master) 1
xedbdata.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
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>
16
17using namespace oox;
18
19namespace {
20
22class XclExpTablesImpl5 : public XclExpTables
23{
24public:
25 explicit XclExpTablesImpl5( const XclExpRoot& rRoot );
26
27 virtual void Save( XclExpStream& rStrm ) override;
28 virtual void SaveXml( XclExpXmlStream& rStrm ) override;
29};
30
32class XclExpTablesImpl8 : public XclExpTables
33{
34public:
35 explicit XclExpTablesImpl8( const XclExpRoot& rRoot );
36
37 virtual void Save( XclExpStream& rStrm ) override;
38 virtual void SaveXml( XclExpXmlStream& rStrm ) override;
39};
40
41}
42
43XclExpTablesImpl5::XclExpTablesImpl5( const XclExpRoot& rRoot ) :
44 XclExpTables( rRoot )
45{
46}
47
48void XclExpTablesImpl5::Save( XclExpStream& /*rStrm*/ )
49{
50 // not implemented
51}
52
53void XclExpTablesImpl5::SaveXml( XclExpXmlStream& /*rStrm*/ )
54{
55 // not applicable
56}
57
58
59XclExpTablesImpl8::XclExpTablesImpl8( const XclExpRoot& rRoot ) :
60 XclExpTables( rRoot )
61{
62}
63
64void XclExpTablesImpl8::Save( XclExpStream& /*rStrm*/ )
65{
66 // not implemented
67}
68
69void XclExpTablesImpl8::SaveXml( XclExpXmlStream& rStrm )
70{
71
72 sax_fastparser::FSHelperPtr& pWorksheetStrm = rStrm.GetCurrentStream();
73 pWorksheetStrm->startElement(XML_tableParts);
74 for (auto const& it : maTables)
75 {
76 OUString aRelId;
77 sax_fastparser::FSHelperPtr pTableStrm = rStrm.CreateOutputStream(
78 XclXmlUtils::GetStreamName("xl/tables/", "table", it.mnTableId),
79 XclXmlUtils::GetStreamName("../tables/", "table", it.mnTableId),
80 pWorksheetStrm->getOutputStream(),
83 &aRelId);
84
85 pWorksheetStrm->singleElement(XML_tablePart, FSNS(XML_r, XML_id), aRelId.toUtf8());
86
87 rStrm.PushStream( pTableStrm);
88 SaveTableXml( rStrm, it);
89 rStrm.PopStream();
90 }
91 pWorksheetStrm->endElement( XML_tableParts);
92}
93
94
96 XclExpRoot( rRoot )
97{
98}
99
101{
102}
103
105{
106 // All non-const to be able to call RefreshTableColumnNames().
107 ScDocument& rDoc = GetDoc();
108 ScDBCollection* pDBColl = rDoc.GetDBCollection();
109 if (!pDBColl)
110 return;
111
112 ScDBCollection::NamedDBs& rDBs = pDBColl->getNamedDBs();
113 if (rDBs.empty())
114 return;
115
116 sal_Int32 nTableId = 0;
117 for (const auto& rxDB : rDBs)
118 {
119 ScDBData* pDBData = rxDB.get();
120 pDBData->RefreshTableColumnNames( &rDoc); // currently not in sync, so refresh
122 pDBData->GetArea( aRange);
123 SCTAB nTab = aRange.aStart.Tab();
124 TablesMapType::iterator it = maTablesMap.find( nTab);
125 if (it == maTablesMap.end())
126 {
128 switch( GetBiff() )
129 {
130 case EXC_BIFF5:
131 pNew = new XclExpTablesImpl5( GetRoot());
132 break;
133 case EXC_BIFF8:
134 pNew = new XclExpTablesImpl8( GetRoot());
135 break;
136 default:
137 assert(!"Unknown BIFF type!");
138 continue; // for
139 }
140 ::std::pair< TablesMapType::iterator, bool > ins( maTablesMap.insert( ::std::make_pair( nTab, pNew)));
141 if (!ins.second)
142 {
143 assert(!"XclExpTablesManager::Initialize - XclExpTables insert failed");
144 continue; // for
145 }
146 it = ins.first;
147 }
148 it->second->AppendTable( pDBData, ++nTableId);
149 }
150}
151
153{
154 TablesMapType::iterator it = maTablesMap.find(nTab);
155 return it == maTablesMap.end() ? nullptr : it->second;
156}
157
158XclExpTables::Entry::Entry( const ScDBData* pData, sal_Int32 nTableId ) :
159 mpData(pData), mnTableId(nTableId)
160{
161}
162
164 XclExpRoot(rRoot)
165{
166}
167
169{
170}
171
172void XclExpTables::AppendTable( const ScDBData* pData, sal_Int32 nTableId )
173{
174 maTables.emplace_back( pData, nTableId);
175}
176
178{
179 const ScDBData& rData = *rEntry.mpData;
181 rData.GetArea( aRange);
182 sax_fastparser::FSHelperPtr& pTableStrm = rStrm.GetCurrentStream();
183 pTableStrm->startElement( XML_table,
184 XML_xmlns, rStrm.getNamespaceURL(OOX_NS(xls)).toUtf8(),
185 XML_id, OString::number( rEntry.mnTableId),
186 XML_name, rData.GetName().toUtf8(),
187 XML_displayName, rData.GetName().toUtf8(),
188 XML_ref, XclXmlUtils::ToOString(rStrm.GetRoot().GetDoc(), aRange),
189 XML_headerRowCount, ToPsz10(rData.HasHeader()),
190 XML_totalsRowCount, ToPsz10(rData.HasTotals()),
191 XML_totalsRowShown, ToPsz10(rData.HasTotals()) // we don't support that but if there are totals they are shown
192 // OOXTODO: XML_comment, ...,
193 // OOXTODO: XML_connectionId, ...,
194 // OOXTODO: XML_dataCellStyle, ...,
195 // OOXTODO: XML_dataDxfId, ...,
196 // OOXTODO: XML_headerRowBorderDxfId, ...,
197 // OOXTODO: XML_headerRowCellStyle, ...,
198 // OOXTODO: XML_headerRowDxfId, ...,
199 // OOXTODO: XML_insertRow, ...,
200 // OOXTODO: XML_insertRowShift, ...,
201 // OOXTODO: XML_published, ...,
202 // OOXTODO: XML_tableBorderDxfId, ...,
203 // OOXTODO: XML_tableType, ...,
204 // OOXTODO: XML_totalsRowBorderDxfId, ...,
205 // OOXTODO: XML_totalsRowCellStyle, ...,
206 // OOXTODO: XML_totalsRowDxfId, ...
207 );
208
209 if (rData.HasAutoFilter())
210 {
211 /* TODO: does this need to exclude totals row? */
212
213 /* TODO: in OOXML 12.3.21 Table Definition Part has information
214 * that an applied autoFilter has child elements
215 * <af:filterColumn><af:filters><af:filter>.
216 * When not applied but buttons hidden, Excel writes, for example,
217 * <filterColumn colId="0" hiddenButton="1"/> */
218
219 ExcAutoFilterRecs aAutoFilter( rStrm.GetRoot(), aRange.aStart.Tab(), &rData);
220 aAutoFilter.SaveXml( rStrm);
221 }
222
223 const std::vector< OUString >& rColNames = rData.GetTableColumnNames();
224 const std::vector< TableColumnAttributes >& rColAttributes = rData.GetTableColumnAttributes();
225 if (!rColNames.empty())
226 {
227 pTableStrm->startElement(XML_tableColumns,
228 XML_count, OString::number(aRange.aEnd.Col() - aRange.aStart.Col() + 1));
229
230 for (size_t i=0, n=rColNames.size(); i < n; ++i)
231 {
232 // OOXTODO: write <calculatedColumnFormula> once we support it, in
233 // which case we'd need start/endElement XML_tableColumn for such
234 // column.
235
236 // OOXTODO: write <totalsRowFormula> once we support it.
237
238 pTableStrm->singleElement( XML_tableColumn,
239 XML_id, OString::number(i+1),
240 XML_name, rColNames[i].toUtf8(),
241 XML_totalsRowFunction, (i < rColAttributes.size() ? rColAttributes[i].maTotalsFunction : std::nullopt)
242 // OOXTODO: XML_dataCellStyle, ...,
243 // OOXTODO: XML_dataDxfId, ...,
244 // OOXTODO: XML_headerRowCellStyle, ...,
245 // OOXTODO: XML_headerRowDxfId, ...,
246 // OOXTODO: XML_queryTableFieldId, ...,
247 // OOXTODO: XML_totalsRowCellStyle, ...,
248 // OOXTODO: XML_totalsRowDxfId, ...,
249 // OOXTODO: XML_totalsRowLabel, ...,
250 // OOXTODO: XML_uniqueName, ...
251 );
252 }
253
254 pTableStrm->endElement( XML_tableColumns);
255 }
256
257 // OOXTODO: write <tableStyleInfo> once we have table styles.
258
259 pTableStrm->endElement( XML_table);
260}
261
262/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void SaveXml(XclExpXmlStream &rStrm) override
Definition: excrecds.cxx:1146
@ UNINITIALIZED
Definition: address.hxx:220
SCTAB Tab() const
Definition: address.hxx:283
SCCOL Col() const
Definition: address.hxx:279
Stores global named database ranges.
Definition: dbdata.hxx:243
NamedDBs & getNamedDBs()
Definition: dbdata.hxx:324
bool HasAutoFilter() const
Definition: dbdata.hxx:212
bool HasHeader() const
Definition: dbdata.hxx:136
const OUString & GetName() const
Definition: dbdata.hxx:127
bool HasTotals() const
Definition: dbdata.hxx:138
SC_DLLPUBLIC void RefreshTableColumnNames(ScDocument *pDoc)
Refresh/update the column names with the header row's cell contents.
Definition: dbdata.cxx:813
void GetArea(SCTAB &rTab, SCCOL &rCol1, SCROW &rRow1, SCCOL &rCol2, SCROW &rRow2) const
Definition: dbdata.cxx:298
SC_DLLPUBLIC const ::std::vector< TableColumnAttributes > & GetTableColumnAttributes() const
Definition: dbdata.hxx:157
SC_DLLPUBLIC const ::std::vector< OUString > & GetTableColumnNames() const
Definition: dbdata.hxx:155
SC_DLLPUBLIC ScDBCollection * GetDBCollection() const
Definition: document.hxx:827
ScAddress aEnd
Definition: address.hxx:498
ScAddress aStart
Definition: address.hxx:497
virtual void SaveXml(XclExpXmlStream &rStrm)
Definition: xerecord.cxx:40
virtual void Save(XclExpStream &rStrm)
Overwrite this method to do any operation while saving the record.
Definition: xerecord.cxx:36
Access to global data from other classes.
Definition: xeroot.hxx:113
const XclExpRoot & GetRoot() const
Returns this root instance - for code readability in derived classes.
Definition: xeroot.hxx:118
This class is used to export Excel record streams.
Definition: xestream.hxx:73
rtl::Reference< XclExpTables > GetTablesBySheet(SCTAB nTab)
Definition: xedbdata.cxx:152
TablesMapType maTablesMap
Definition: xedbdata.hxx:63
virtual ~XclExpTablesManager() override
Definition: xedbdata.cxx:100
XclExpTablesManager(const XclExpRoot &rRoot)
Definition: xedbdata.cxx:95
virtual ~XclExpTables() override
Definition: xedbdata.cxx:168
static void SaveTableXml(XclExpXmlStream &rStrm, const Entry &rEntry)
Definition: xedbdata.cxx:177
void AppendTable(const ScDBData *pData, sal_Int32 nTableId)
Definition: xedbdata.cxx:172
XclExpTables(const XclExpRoot &rRoot)
Definition: xedbdata.cxx:163
TablesType maTables
Definition: xedbdata.hxx:45
XclBiff GetBiff() const
Returns the current BIFF version of the importer/exporter.
Definition: xlroot.hxx:141
ScDocument & GetDoc() const
Returns reference to the destination document (import) or source document (export).
Definition: xlroot.cxx:285
static OString ToOString(const Color &rColor)
Definition: xestream.cxx:712
static OUString GetStreamName(const char *sStreamDir, const char *sStream, sal_Int32 nId)
Definition: xestream.cxx:697
constexpr sal_Int32 FSNS(sal_Int32 namespc, sal_Int32 element)
sal_Int64 n
std::unique_ptr< sal_Int32[]> pData
int i
void SvStream & rStrm
std::shared_ptr< FastSerializerHelper > FSHelperPtr
#define CREATE_OFFICEDOC_RELATION_TYPE(ascii)
#define CREATE_XL_CONTENT_TYPE(ascii)
Entry(const ScDBData *pData, sal_Int32 nTableId)
used as [n] in table[n].xml part name.
Definition: xedbdata.cxx:158
sal_Int32 mnTableId
Definition: xedbdata.hxx:39
const ScDBData * mpData
Definition: xedbdata.hxx:38
sal_Int16 SCTAB
Definition: types.hxx:22
@ EXC_BIFF5
MS Excel 4.0.
Definition: xlconst.hxx:34
@ EXC_BIFF8
MS Excel 5.0, MS Excel 7.0 (95)
Definition: xlconst.hxx:35