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 .
22 #include <editeng/editdata.hxx>
24 #include "xladdress.hxx"
26 #include "xltools.hxx"
34 class SharedStringPool
;
38 // Excel->Calc cell address/range conversion ==================================
40 /** Provides functions to convert Excel cell addresses to Calc cell addresses. */
41 class XclImpAddressConverter
: public XclAddressConverterBase
44 explicit XclImpAddressConverter( const XclImpRoot
& rRoot
);
46 // cell address -----------------------------------------------------------
48 /** Checks if the passed Excel cell address is valid.
49 @param rXclPos The Excel cell address to check.
50 @param bWarn true = Sets the internal flag that produces a warning box
51 after loading/saving the file, if the cell address is not valid.
52 @return true = Cell address in rXclPos is valid. */
53 bool CheckAddress( const XclAddress
& rXclPos
, bool bWarn
);
55 /** Converts the passed Excel cell address to a Calc cell address.
56 @param rScPos (Out) The converted Calc cell address, if valid.
57 @param rXclPos The Excel cell address to convert.
58 @param bWarn true = Sets the internal flag that produces a warning box
59 after loading/saving the file, if the cell address is invalid.
60 @return true = Cell address returned in rScPos is valid. */
61 bool ConvertAddress( ScAddress
& rScPos
,
62 const XclAddress
& rXclPos
, SCTAB nScTab
, bool bWarn
);
64 /** Returns a valid cell address by moving it into allowed dimensions.
65 @param rXclPos The Excel cell address to convert.
66 @param bWarn true = Sets the internal flag that produces a warning box
67 after loading/saving the file, if the cell address is invalid.
68 @return The converted Calc cell address. */
69 ScAddress
CreateValidAddress( const XclAddress
& rXclPos
,
70 SCTAB nScTab
, bool bWarn
);
72 // cell range -------------------------------------------------------------
74 /** Converts the passed Excel cell range to a Calc cell range.
75 @param rScRange (Out) The converted Calc cell range, if valid.
76 @param rXclRange The Excel cell range to convert.
77 @param bWarn true = Sets the internal flag that produces a warning box
78 after loading/saving the file, if the cell range contains invalid cells.
79 @return true = Cell range returned in rScRange is valid (original or cropped). */
80 bool ConvertRange( ScRange
& rScRange
, const XclRange
& rXclRange
,
81 SCTAB nScTab1
, SCTAB nScTab2
, bool bWarn
);
83 // cell range list --------------------------------------------------------
85 /** Converts the passed Excel cell range list to a Calc cell range list.
86 @descr The start position of the ranges will not be modified. Cell
87 ranges that fit partly into valid dimensions are cropped
88 accordingly. Cell ranges that do not fit at all, are not inserted
89 into the Calc cell range list.
90 @param rScRanges (Out) The converted Calc cell range list.
91 @param rXclRanges The Excel cell range list to convert.
92 @param bWarn true = Sets the internal flag that produces a warning box
93 after loading/saving the file, if at least one of the cell ranges
94 contains invalid cells. */
95 void ConvertRangeList( ScRangeList
& rScRanges
,
96 const XclRangeList
& rXclRanges
, SCTAB nScTab
, bool bWarn
);
99 // String->EditEngine conversion ==============================================
101 class EditTextObject
;
103 /** This class provides methods to convert an XclImpString.
104 @The string can be converted to an edit engine text object or directly
105 to a Calc edit cell. */
106 class XclImpStringHelper
109 /** delete copy constructor */
110 XclImpStringHelper(const XclImpStringHelper
&) = delete;
111 /** delete copy-assignment operator */
112 const XclImpStringHelper
& operator=(const XclImpStringHelper
&) = delete;
113 /** We don't want anybody to instantiate this class, since it is just a
114 collection of static methods. */
115 XclImpStringHelper() = delete;
116 /** Returns a new edit engine text object.
117 @param nXFIndex Index to XF for first text portion (for escapement). */
118 static std::unique_ptr
<EditTextObject
> CreateTextObject(
119 const XclImpRoot
& rRoot
,
120 const XclImpString
& rString
);
122 static void SetToDocument(
123 ScDocumentImport
& rDoc
, const ScAddress
& rPos
, const XclImpRoot
& rRoot
,
124 const XclImpString
& rString
, sal_uInt16 nXFIndex
);
127 // Header/footer conversion ===================================================
134 /** Converts an Excel header/footer string into three edit engine text objects.
135 @descr Header/footer content is divided into three parts: Left, center and
136 right portion. All formatting information is encoded in the Excel string
137 using special character sequences. A control sequence starts with the ampersand
140 Supported control sequences:
141 &L start of left portion
142 &C start of center portion
143 &R start of right portion
144 &P current page number
149 &F file name without path (see also &Z&F)
150 &Z file path without file name (converted to full file name, see also &Z&F)
151 &Z&F file path and name
152 &U underlining on/off
153 &E double underlining on/off
154 &S strikeout characters on/off
155 &X superscript on/off
157 &"fontname,fontstyle" use font with name 'fontname' and style 'fontstyle'
158 &fontheight set font height in points ('fontheight' is a decimal value)
160 Known but unsupported control sequences:
163 class XclImpHFConverter
: protected XclImpRoot
166 /** delete copy constructor */
167 XclImpHFConverter(const XclImpHFConverter
&) = delete;
168 /** delete copy-assignment operator */
169 const XclImpHFConverter
& operator=(const XclImpHFConverter
&) = delete;
171 explicit XclImpHFConverter( const XclImpRoot
& rRoot
);
172 virtual ~XclImpHFConverter() override
;
174 /** Parses the passed string and creates three new edit engine text objects. */
175 void ParseString( const OUString
& rHFString
);
177 /** Creates a ScPageHFItem and inserts it into the passed item set. */
178 void FillToItemSet( SfxItemSet
& rItemSet
, sal_uInt16 nWhichId
) const;
179 /** Returns the total height of the converted header or footer in twips. */
180 sal_Int32
GetTotalHeight() const;
183 typedef ::std::unique_ptr
< XclFontData
> XclFontDataPtr
;
185 /** Enumerates the supported header/footer portions. */
186 enum XclImpHFPortion
{ EXC_HF_LEFT
, EXC_HF_CENTER
, EXC_HF_RIGHT
, EXC_HF_PORTION_COUNT
};
188 /** Contains all information about a header/footer portion. */
189 struct XclImpHFPortionInfo
191 typedef std::shared_ptr
< EditTextObject
> EditTextObjectRef
;
192 EditTextObjectRef mxObj
; /// Edit engine text object.
193 ESelection maSel
; /// Edit engine selection.
194 sal_Int32 mnHeight
; /// Height of previous lines in twips.
195 sal_uInt16 mnMaxLineHt
; /// Maximum font height for the current text line.
196 explicit XclImpHFPortionInfo();
200 /** Returns the current edit engine text object. */
201 XclImpHFPortionInfo
& GetCurrInfo() { return maInfos
[ meCurrObj
]; }
202 /** Returns the current edit engine text object. */
203 XclImpHFPortionInfo::EditTextObjectRef
& GetCurrObj() { return GetCurrInfo().mxObj
; }
204 /** Returns the current selection. */
205 ESelection
& GetCurrSel() { return GetCurrInfo().maSel
; }
207 /** Returns the maximum line height of the specified portion. */
208 sal_uInt16
GetMaxLineHeight( XclImpHFPortion ePortion
) const;
210 /** Updates the maximum line height of the specified portion, using the current font size. */
211 void UpdateMaxLineHeight( XclImpHFPortion ePortion
);
212 /** Updates the current maximum line height, using the current font size. */
213 void UpdateCurrMaxLineHeight();
215 /** Sets the font attributes at the current selection.
216 @descr After that, the start position of the current selection object is
217 adjusted to the end of the selection. */
219 /** Resets font data to application default font. */
220 void ResetFontData();
222 /** Inserts maCurrText into edit engine and adjusts the current selection object.
223 @descr The text shall not contain a newline character.
224 The text will be cleared after insertion. */
226 /** Inserts the passed text field and adjusts the current selection object. */
227 void InsertField( const SvxFieldItem
& rFieldItem
);
228 /** Inserts a line break and adjusts the current selection object. */
229 void InsertLineBreak();
231 /** Creates the edit engine text object of current portion from edit engine. */
232 void CreateCurrObject();
233 /** Changes current header/footer portion to eNew.
234 @descr Creates text object of current portion and reinitializes edit engine. */
235 void SetNewPortion( XclImpHFPortion eNew
);
238 EditEngine
& mrEE
; /// The header/footer edit engine.
239 std::vector
< XclImpHFPortionInfo
>
240 maInfos
; /// Edit engine text objects for all portions.
241 OUStringBuffer maCurrText
; /// Current text to insert into edit engine.
242 XclFontDataPtr mxFontData
; /// Font data of current text.
243 XclImpHFPortion meCurrObj
; /// The current portion.
246 // URL conversion =============================================================
248 /** This class contains static methods to decode a URL stored in an Excel file.
249 @descr Excel URLs can contain a sheet name, for instance: path\[test.xls]Sheet1
250 This sheet name will be extracted automatically. */
251 class XclImpUrlHelper
254 /** delete copy constructor */
255 XclImpUrlHelper(const XclImpUrlHelper
&) = delete;
256 /** delete copy-assignment operator */
257 const XclImpUrlHelper
& operator=(const XclImpUrlHelper
&) = delete;
258 /** We don't want anybody to instantiate this class, since it is just a
259 collection of static methods. */
260 XclImpUrlHelper() = delete;
262 /** Decodes an encoded external document URL with optional sheet name.
263 @param rUrl Returns the decoded file name incl. path.
264 @param rTabName Returns the decoded sheet name.
265 @param rbSameWb Returns true, if the URL is a reference to the own workbook.
266 @param rEncodedUrl An encoded URL from Excel. */
267 static void DecodeUrl(
271 const XclImpRoot
& rRoot
,
272 const OUString
& rEncodedUrl
);
274 /** Decodes an encoded external document URL without sheet name.
275 @param rUrl Returns the decoded file name incl. path.
276 @param rbSameWb Returns true, if the URL is a reference to the own workbook.
277 @param rEncodedUrl An encoded URL from Excel. */
279 static void DecodeUrl(
282 const XclImpRoot
& rRoot
,
283 const OUString
& rEncodedUrl
);
285 /** Decodes the passed URL to OLE or DDE link components.
286 @descr For DDE links: Decodes to application name and topic.
287 For OLE object links: Decodes to class name and document URL.
288 @return true = decoding was successful, returned strings are valid (not empty). */
289 static bool DecodeLink( OUString
& rApplic
, OUString
& rTopic
, std::u16string_view aEncUrl
);
292 // Cached values ==============================================================
296 /** This class stores one cached value of a cached value list (used for instance in
297 CRN, EXTERNNAME, tArray). */
298 class XclImpCachedValue
301 /** delete copy constructor */
302 XclImpCachedValue(const XclImpCachedValue
&) = delete;
303 /** delete copy-assignment operator */
304 const XclImpCachedValue
& operator=(const XclImpCachedValue
&) = delete;
305 /** Creates a cached value and reads contents from stream and stores it with its array address. */
306 explicit XclImpCachedValue( XclImpStream
& rStrm
);
307 virtual ~XclImpCachedValue();
309 /** Returns the type of the cached value (EXC_CACHEDVAL_*). */
310 sal_uInt8
GetType() const { return mnType
; }
311 /** Returns the cached string value, if this value is a string, else an empty string. */
312 const OUString
& GetString() const { return maStr
;}
313 /** Returns the cached number, if this value has number type, else 0.0. */
314 double GetValue() const { return mfValue
; }
315 /** Returns the cached Boolean value, if this value has Boolean type, else false. */
316 bool GetBool() const { return (mnType
== EXC_CACHEDVAL_BOOL
) && (mnBoolErr
!= 0); }
317 /** Returns the cached Calc error code, if this value has Error type, else 0. */
318 sal_uInt8
GetXclError() const { return (mnType
== EXC_CACHEDVAL_ERROR
) ? mnBoolErr
: EXC_ERR_NA
; }
319 /** Returns the cached Calc error code, if this value has Error type, else 0. */
320 FormulaError
GetScError() const;
323 typedef ::std::unique_ptr
< const ScTokenArray
> ScTokenArrayPtr
;
325 OUString maStr
; /// Cached value is a string.
326 double mfValue
; /// Cached value is a double.
327 ScTokenArrayPtr mxTokArr
; /// Cached value is a formula or error code or Boolean.
328 sal_uInt8 mnBoolErr
; /// Boolean value or Excel error code.
329 sal_uInt8 mnType
; /// The type of the cached value (EXC_CACHEDVAL_*).
332 /** Contains cached values in a 2-dimensional array. */
333 class XclImpCachedMatrix
336 explicit XclImpCachedMatrix( XclImpStream
& rStrm
);
337 ~XclImpCachedMatrix();
339 /** Creates a new ScMatrix object and fills it with the contained values. */
340 ScMatrixRef
CreateScMatrix( svl::SharedStringPool
& rPool
) const;
343 typedef std::vector
< std::unique_ptr
<XclImpCachedValue
> > XclImpValueList
;
345 XclImpValueList maValueList
; /// List of cached cell values.
346 SCSIZE mnScCols
; /// Number of cached columns.
347 SCSIZE mnScRows
; /// Number of cached rows.
350 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */