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 /** Fills the passed Calc cell range with the passed Excel cell range.
75 @param XclRange The Excel cell range to be transferred.
76 @param ScRange (Out) The filled Calc cell range. */
77 static void FillRange(const XclRange
& rXclRange
, ScRange
& rScRange
);
79 /** Converts the passed Excel cell range to a Calc cell range.
80 @param rScRange (Out) The converted Calc cell range, if valid.
81 @param rXclRange The Excel cell range to convert.
82 @param bWarn true = Sets the internal flag that produces a warning box
83 after loading/saving the file, if the cell range contains invalid cells.
84 @return true = Cell range returned in rScRange is valid (original or cropped). */
85 bool ConvertRange( ScRange
& rScRange
, const XclRange
& rXclRange
,
86 SCTAB nScTab1
, SCTAB nScTab2
, bool bWarn
);
88 // cell range list --------------------------------------------------------
90 /** Converts the passed Excel cell range list to a Calc cell range list.
91 @descr The start position of the ranges will not be modified. Cell
92 ranges that fit partly into valid dimensions are cropped
93 accordingly. Cell ranges that do not fit at all, are not inserted
94 into the Calc cell range list.
95 @param rScRanges (Out) The converted Calc cell range list.
96 @param rXclRanges The Excel cell range list to convert.
97 @param bWarn true = Sets the internal flag that produces a warning box
98 after loading/saving the file, if at least one of the cell ranges
99 contains invalid cells. */
100 void ConvertRangeList( ScRangeList
& rScRanges
,
101 const XclRangeList
& rXclRanges
, SCTAB nScTab
, bool bWarn
);
104 // String->EditEngine conversion ==============================================
106 class EditTextObject
;
108 /** This class provides methods to convert an XclImpString.
109 @The string can be converted to an edit engine text object or directly
110 to a Calc edit cell. */
111 class XclImpStringHelper
114 /** delete copy constructor */
115 XclImpStringHelper(const XclImpStringHelper
&) = delete;
116 /** delete copy-assignment operator */
117 const XclImpStringHelper
& operator=(const XclImpStringHelper
&) = delete;
118 /** We don't want anybody to instantiate this class, since it is just a
119 collection of static methods. */
120 XclImpStringHelper() = delete;
121 /** Returns a new edit engine text object.
122 @param nXFIndex Index to XF for first text portion (for escapement). */
123 static std::unique_ptr
<EditTextObject
> CreateTextObject(
124 const XclImpRoot
& rRoot
,
125 const XclImpString
& rString
);
127 static void SetToDocument(
128 ScDocumentImport
& rDoc
, const ScAddress
& rPos
, const XclImpRoot
& rRoot
,
129 const XclImpString
& rString
, sal_uInt16 nXFIndex
);
132 // Header/footer conversion ===================================================
139 /** Converts an Excel header/footer string into three edit engine text objects.
140 @descr Header/footer content is divided into three parts: Left, center and
141 right portion. All formatting information is encoded in the Excel string
142 using special character sequences. A control sequence starts with the ampersand
145 Supported control sequences:
146 &L start of left portion
147 &C start of center portion
148 &R start of right portion
149 &P current page number
154 &F file name without path (see also &Z&F)
155 &Z file path without file name (converted to full file name, see also &Z&F)
156 &Z&F file path and name
157 &U underlining on/off
158 &E double underlining on/off
159 &S strikeout characters on/off
160 &X superscript on/off
162 &"fontname,fontstyle" use font with name 'fontname' and style 'fontstyle'
163 &fontheight set font height in points ('fontheight' is a decimal value)
165 Known but unsupported control sequences:
168 class XclImpHFConverter
: protected XclImpRoot
171 /** delete copy constructor */
172 XclImpHFConverter(const XclImpHFConverter
&) = delete;
173 /** delete copy-assignment operator */
174 const XclImpHFConverter
& operator=(const XclImpHFConverter
&) = delete;
176 explicit XclImpHFConverter( const XclImpRoot
& rRoot
);
177 virtual ~XclImpHFConverter() override
;
179 /** Parses the passed string and creates three new edit engine text objects. */
180 void ParseString( const OUString
& rHFString
);
182 /** Creates a ScPageHFItem and inserts it into the passed item set. */
183 void FillToItemSet( SfxItemSet
& rItemSet
, sal_uInt16 nWhichId
) const;
184 /** Returns the total height of the converted header or footer in twips. */
185 sal_Int32
GetTotalHeight() const;
188 typedef ::std::unique_ptr
< XclFontData
> XclFontDataPtr
;
190 /** Enumerates the supported header/footer portions. */
191 enum XclImpHFPortion
{ EXC_HF_LEFT
, EXC_HF_CENTER
, EXC_HF_RIGHT
, EXC_HF_PORTION_COUNT
};
193 /** Contains all information about a header/footer portion. */
194 struct XclImpHFPortionInfo
196 typedef std::shared_ptr
< EditTextObject
> EditTextObjectRef
;
197 EditTextObjectRef mxObj
; /// Edit engine text object.
198 ESelection maSel
; /// Edit engine selection.
199 sal_Int32 mnHeight
; /// Height of previous lines in twips.
200 sal_uInt16 mnMaxLineHt
; /// Maximum font height for the current text line.
201 explicit XclImpHFPortionInfo();
205 /** Returns the current edit engine text object. */
206 XclImpHFPortionInfo
& GetCurrInfo() { return maInfos
[ meCurrObj
]; }
207 /** Returns the current edit engine text object. */
208 XclImpHFPortionInfo::EditTextObjectRef
& GetCurrObj() { return GetCurrInfo().mxObj
; }
209 /** Returns the current selection. */
210 ESelection
& GetCurrSel() { return GetCurrInfo().maSel
; }
212 /** Returns the maximum line height of the specified portion. */
213 sal_uInt16
GetMaxLineHeight( XclImpHFPortion ePortion
) const;
215 /** Updates the maximum line height of the specified portion, using the current font size. */
216 void UpdateMaxLineHeight( XclImpHFPortion ePortion
);
217 /** Updates the current maximum line height, using the current font size. */
218 void UpdateCurrMaxLineHeight();
220 /** Sets the font attributes at the current selection.
221 @descr After that, the start position of the current selection object is
222 adjusted to the end of the selection. */
224 /** Resets font data to application default font. */
225 void ResetFontData();
227 /** Inserts maCurrText into edit engine and adjusts the current selection object.
228 @descr The text shall not contain a newline character.
229 The text will be cleared after insertion. */
231 /** Inserts the passed text field and adjusts the current selection object. */
232 void InsertField( const SvxFieldItem
& rFieldItem
);
233 /** Inserts a line break and adjusts the current selection object. */
234 void InsertLineBreak();
236 /** Creates the edit engine text object of current portion from edit engine. */
237 void CreateCurrObject();
238 /** Changes current header/footer portion to eNew.
239 @descr Creates text object of current portion and reinitializes edit engine. */
240 void SetNewPortion( XclImpHFPortion eNew
);
243 EditEngine
& mrEE
; /// The header/footer edit engine.
244 std::vector
< XclImpHFPortionInfo
>
245 maInfos
; /// Edit engine text objects for all portions.
246 OUStringBuffer maCurrText
; /// Current text to insert into edit engine.
247 XclFontDataPtr mxFontData
; /// Font data of current text.
248 XclImpHFPortion meCurrObj
; /// The current portion.
251 // URL conversion =============================================================
253 /** This class contains static methods to decode a URL stored in an Excel file.
254 @descr Excel URLs can contain a sheet name, for instance: path\[test.xls]Sheet1
255 This sheet name will be extracted automatically. */
256 class XclImpUrlHelper
259 /** delete copy constructor */
260 XclImpUrlHelper(const XclImpUrlHelper
&) = delete;
261 /** delete copy-assignment operator */
262 const XclImpUrlHelper
& operator=(const XclImpUrlHelper
&) = delete;
263 /** We don't want anybody to instantiate this class, since it is just a
264 collection of static methods. */
265 XclImpUrlHelper() = delete;
267 /** Decodes an encoded external document URL with optional sheet name.
268 @param rUrl Returns the decoded file name incl. path.
269 @param rTabName Returns the decoded sheet name.
270 @param rbSameWb Returns true, if the URL is a reference to the own workbook.
271 @param rEncodedUrl An encoded URL from Excel. */
272 static void DecodeUrl(
276 const XclImpRoot
& rRoot
,
277 const OUString
& rEncodedUrl
);
279 /** Decodes an encoded external document URL without sheet name.
280 @param rUrl Returns the decoded file name incl. path.
281 @param rbSameWb Returns true, if the URL is a reference to the own workbook.
282 @param rEncodedUrl An encoded URL from Excel. */
284 static void DecodeUrl(
287 const XclImpRoot
& rRoot
,
288 const OUString
& rEncodedUrl
);
290 /** Decodes the passed URL to OLE or DDE link components.
291 @descr For DDE links: Decodes to application name and topic.
292 For OLE object links: Decodes to class name and document URL.
293 @return true = decoding was successful, returned strings are valid (not empty). */
294 static bool DecodeLink( OUString
& rApplic
, OUString
& rTopic
, std::u16string_view aEncUrl
);
297 // Cached values ==============================================================
301 /** This class stores one cached value of a cached value list (used for instance in
302 CRN, EXTERNNAME, tArray). */
303 class XclImpCachedValue
306 /** delete copy constructor */
307 XclImpCachedValue(const XclImpCachedValue
&) = delete;
308 /** delete copy-assignment operator */
309 const XclImpCachedValue
& operator=(const XclImpCachedValue
&) = delete;
310 /** Creates a cached value and reads contents from stream and stores it with its array address. */
311 explicit XclImpCachedValue( XclImpStream
& rStrm
);
312 virtual ~XclImpCachedValue();
314 /** Returns the type of the cached value (EXC_CACHEDVAL_*). */
315 sal_uInt8
GetType() const { return mnType
; }
316 /** Returns the cached string value, if this value is a string, else an empty string. */
317 const OUString
& GetString() const { return maStr
;}
318 /** Returns the cached number, if this value has number type, else 0.0. */
319 double GetValue() const { return mfValue
; }
320 /** Returns the cached Boolean value, if this value has Boolean type, else false. */
321 bool GetBool() const { return (mnType
== EXC_CACHEDVAL_BOOL
) && (mnBoolErr
!= 0); }
322 /** Returns the cached Calc error code, if this value has Error type, else 0. */
323 sal_uInt8
GetXclError() const { return (mnType
== EXC_CACHEDVAL_ERROR
) ? mnBoolErr
: EXC_ERR_NA
; }
324 /** Returns the cached Calc error code, if this value has Error type, else 0. */
325 FormulaError
GetScError() const;
328 typedef ::std::unique_ptr
< const ScTokenArray
> ScTokenArrayPtr
;
330 OUString maStr
; /// Cached value is a string.
331 double mfValue
; /// Cached value is a double.
332 ScTokenArrayPtr mxTokArr
; /// Cached value is a formula or error code or Boolean.
333 sal_uInt8 mnBoolErr
; /// Boolean value or Excel error code.
334 sal_uInt8 mnType
; /// The type of the cached value (EXC_CACHEDVAL_*).
337 /** Contains cached values in a 2-dimensional array. */
338 class XclImpCachedMatrix
341 explicit XclImpCachedMatrix( XclImpStream
& rStrm
);
342 ~XclImpCachedMatrix();
344 /** Creates a new ScMatrix object and fills it with the contained values. */
345 ScMatrixRef
CreateScMatrix( svl::SharedStringPool
& rPool
) const;
348 typedef std::vector
< std::unique_ptr
<XclImpCachedValue
> > XclImpValueList
;
350 XclImpValueList maValueList
; /// List of cached cell values.
351 SCSIZE mnScCols
; /// Number of cached columns.
352 SCSIZE mnScRows
; /// Number of cached rows.
355 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */