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 .
31 class SharedStringPool
;
35 /* ============================================================================
36 Classes for import of different kinds of internal/external references.
37 - 3D cell and cell range links
38 - External cell and cell range links
39 - External defined names
43 ============================================================================ */
45 // Excel sheet indexes ========================================================
47 /** A buffer containing information about names and creation order of sheets.
49 The first purpose of this buffer is to translate original Excel
50 sheet names into Calc sheet indexes. This is not trivial because the filter
51 may rename the Calc sheets during creation. This buffer stores the original
52 Excel sheet names with the corresponding Calc sheet indexes.
54 The second purpose is to store the creation order of all sheets inside the
55 Excel workbook. The creation order list is contained in the TABID record
56 and needed to import the change log. Example: if the list contains 3;1;2
57 this means that the second sheet in the file was created first, then the
58 third sheet in the file was created and finally the first sheet.
63 // original Excel sheet names ---------------------------------------------
65 /** Appends an original Excel sheet name with corresponding Calc sheet index. */
66 void AppendXclTabName( const OUString
& rXclTabName
, SCTAB nScTab
);
67 /** Inserts a Calc sheet index (increases all following sheet indexes). */
68 void InsertScTab( SCTAB nScTab
);
70 /** Returns the Calc sheet index from the passed original Excel sheet name. */
71 SCTAB
GetScTabFromXclName( const OUString
& rXclTabName
) const;
73 // record creation order - TABID record -----------------------------------
75 /** Reads the TABID record. */
76 void ReadTabid( XclImpStream
& rStrm
);
78 /** Returns the current sheet index calculated from creation index.
79 @param nCreatedId The creation index of the sheet (1-based).
80 @param nMaxTabId All values greater than this parameter are not used to find the index.
81 @return The 0-based index of the sheet nCreatedId if it is contained in the list.
82 Example: The buffer is 3;5;2;4;1, nCreatedId is 1 and nMaxTabId is 3. The function will
83 return 2 which is the 0-based index of sheet 1 in the list 3;2;1. */
84 sal_uInt16
GetCurrentIndex( sal_uInt16 nCreatedId
, sal_uInt16 nMaxTabId
) const;
87 typedef ::std::map
< OUString
, SCTAB
> XclTabNameMap
;
89 XclTabNameMap maTabNames
; /// All Excel sheet names with Calc sheet index.
90 ScfUInt16Vec maTabIdVec
; /// The vector with sheet indexes.
93 // External names =============================================================
95 /** Type of an external name. */
96 enum XclImpExtNameType
98 xlExtName
, /// An external defined name.
99 xlExtAddIn
, /// An add-in function name.
100 xlExtDDE
, /// A DDE link range.
101 xlExtOLE
, /// An OLE object link.
102 xlExtEuroConvert
/// An external in Excel, but internal in OO function name.
105 class XclImpCachedMatrix
;
109 /** Stores contents of an external name.
110 @descr Supported: External defined names, AddIn names, DDE links and OLE objects. */
114 * MOper, multiple operands, stores cached values of external range
115 * specified in the record.
120 MOper(svl::SharedStringPool
& rPool
, XclImpStream
& rStrm
);
121 const ScMatrix
& GetCache() const;
123 ScMatrixRef mxCached
;
127 /** Reads the external name from the stream. */
128 explicit XclImpExtName( XclImpSupbook
& rSupbook
, XclImpStream
& rStrm
,
129 XclSupbookType eSubType
, ExcelToSc
* pFormulaConv
);
132 /** Create and apply the cached list of this DDE Link to the document. */
133 void CreateDdeData( ScDocument
& rDoc
,
134 const OUString
& rApplc
, const OUString
& rExtDoc
) const;
136 void CreateExtNameData( const ScDocument
& rDoc
, sal_uInt16 nFileId
) const;
139 * Create OLE link data. OLE link data is converted to external
140 * reference, since OLE link doesn't work cross-platform, and is not very
141 * reliable even on Windows.
143 bool CreateOleData(const ScDocument
& rDoc
, const OUString
& rUrl
,
144 sal_uInt16
& rFileId
, OUString
& rTabName
, ScRange
& rRange
) const;
146 bool HasFormulaTokens() const;
148 XclImpExtNameType
GetType() const { return meType
; }
149 const OUString
& GetName() const { return maName
; }
150 sal_uInt32
GetStorageId() const { return mnStorageId
; }
153 typedef ::std::unique_ptr
< XclImpCachedMatrix
> XclImpCachedMatrixPtr
;
154 typedef ::std::unique_ptr
< ScTokenArray
> TokenArrayPtr
;
156 XclImpCachedMatrixPtr mxDdeMatrix
; /// Cached results of the DDE link.
157 std::unique_ptr
<MOper
> mpMOper
; /// Cached values for OLE link
158 TokenArrayPtr mxArray
; /// Formula tokens for external name.
159 OUString maName
; /// The name of the external name.
160 sal_uInt32 mnStorageId
; /// Storage ID for OLE object storages.
161 XclImpExtNameType meType
; /// Type of the external name.
164 // Import link manager ========================================================
166 class XclImpLinkManagerImpl
;
168 /** This is the central class for the import of all internal/external links.
169 @descr This manager stores all data about external documents with their sheets
170 and cached cell contents. Additionally it handles external names, such as add-in
171 function names, DDE links, and OLE object links.
172 File contents in BIFF8:
173 - Record SUPBOOK: Contains the name of an external document and the names of its sheets.
174 This record is optionally followed by NAME, EXTERNNAME, XCT and CRN records.
175 - Record XCT: Contains the number and sheet index of the following CRN records.
176 - Record CRN: Contains addresses (row and column) and values of external referenced cells.
177 - Record NAME: Contains defined names of the own workbook.
178 - Record EXTERNNAME: Contains external defined names, DDE links, or OLE object links.
179 - Record EXTERNSHEET: Contains indexes to URLs of external documents (SUPBOOKs)
180 and sheet indexes for each external reference used anywhere in the workbook.
181 This record follows a list of SUPBOOK records (with their attached records).
183 class XclImpLinkManager
: protected XclImpRoot
186 explicit XclImpLinkManager( const XclImpRoot
& rRoot
);
187 virtual ~XclImpLinkManager() override
;
189 /** Reads the EXTERNSHEET record. */
190 void ReadExternsheet( XclImpStream
& rStrm
);
191 /** Reads a SUPBOOK record. */
192 void ReadSupbook( XclImpStream
& rStrm
);
193 /** Reads an XCT record and appends it to the current SUPBOOK. */
194 void ReadXct( XclImpStream
& rStrm
);
195 /** Reads a CRN record and appends it to the current SUPBOOK. */
196 void ReadCrn( XclImpStream
& rStrm
);
197 /** Reads an EXTERNNAME record and appends it to the current SUPBOOK. */
198 void ReadExternname( XclImpStream
& rStrm
, ExcelToSc
* pFormulaConv
);
200 /** Returns true, if the specified XTI entry contains an internal reference. */
201 bool IsSelfRef( sal_uInt16 nXtiIndex
) const;
202 /** Returns the Calc sheet index range of the specified XTI entry.
203 @return true = XTI data found, returned sheet index range is valid. */
205 SCTAB
& rnFirstScTab
, SCTAB
& rnLastScTab
,
206 sal_uInt16 nXtiIndex
) const;
207 /** Returns the specified external name or 0 on error. */
208 const XclImpExtName
* GetExternName( sal_uInt16 nXtiIndex
, sal_uInt16 nExtName
) const;
210 const OUString
* GetSupbookUrl( sal_uInt16 nXtiIndex
) const;
212 OUString
GetSupbookTabName( sal_uInt16 nXti
, sal_uInt16 nXtiTab
) const;
214 /** Tries to decode the URL of the specified XTI entry to OLE or DDE link components.
215 @descr For DDE links: Decodes to application name and topic.
216 For OLE object links: Decodes to class name and document URL.
217 @return true = decoding was successful, returned strings are valid (not empty). */
218 bool GetLinkData( OUString
& rApplic
, OUString
& rTopic
, sal_uInt16 nXtiIndex
) const;
219 /** Returns the specified macro name or an empty string on error. */
220 OUString
GetMacroName( sal_uInt16 nExtSheet
, sal_uInt16 nExtName
) const;
223 typedef ::std::unique_ptr
< XclImpLinkManagerImpl
> XclImpLinkMgrImplPtr
;
224 XclImpLinkMgrImplPtr mxImpl
;
227 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */