Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / sc / source / filter / inc / addressconverter.hxx
blob0d5b3dac1b70651971757551652aaff3f14ce95a
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/.
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 .
20 #ifndef INCLUDED_SC_SOURCE_FILTER_INC_ADDRESSCONVERTER_HXX
21 #define INCLUDED_SC_SOURCE_FILTER_INC_ADDRESSCONVERTER_HXX
23 #include <vector>
24 #include <rangelst.hxx>
25 #include "workbookhelper.hxx"
26 #include <com/sun/star/uno/Sequence.h>
28 namespace com { namespace sun { namespace star { namespace table { struct CellRangeAddress; } } } }
29 namespace oox { class SequenceInputStream; }
31 namespace oox {
32 namespace xls {
34 /** A 2D cell address struct for binary filters. */
35 struct BinAddress
37 sal_Int32 mnCol;
38 sal_Int32 mnRow;
40 explicit BinAddress() : mnCol( 0 ), mnRow( 0 ) {}
41 explicit BinAddress( sal_Int32 nCol, sal_Int32 nRow ) : mnCol( nCol ), mnRow( nRow ) {}
42 explicit BinAddress( const ScAddress& rAddr ) : mnCol( rAddr.Col() ), mnRow( rAddr.Row() ) {}
44 void read( SequenceInputStream& rStrm );
47 inline bool operator<( const BinAddress& rL, const BinAddress& rR )
49 return (rL.mnCol < rR.mnCol) || ((rL.mnCol == rR.mnCol) && (rL.mnRow < rR.mnRow));
52 inline SequenceInputStream& operator>>( SequenceInputStream& rStrm, BinAddress& orPos )
54 orPos.read( rStrm );
55 return rStrm;
58 /** A 2D cell range address struct for binary filters. */
59 struct BinRange
61 BinAddress maFirst;
62 BinAddress maLast;
64 void read( SequenceInputStream& rStrm );
67 inline SequenceInputStream& operator>>( SequenceInputStream& rStrm, BinRange& orRange )
69 orRange.read( rStrm );
70 return rStrm;
73 /** A 2D cell range address list for binary filters. */
74 class BinRangeList
76 public:
77 explicit BinRangeList() : mvRanges() {}
79 ::std::vector< BinRange >::const_iterator begin() const { return mvRanges.begin(); }
80 ::std::vector< BinRange >::const_iterator end() const { return mvRanges.end(); }
82 void read( SequenceInputStream& rStrm );
84 private:
85 ::std::vector< BinRange > mvRanges;
88 inline SequenceInputStream& operator>>( SequenceInputStream& rStrm, BinRangeList& orRanges )
90 orRanges.read( rStrm );
91 return rStrm;
94 /** Converter for cell addresses and cell ranges for OOXML and BIFF filters.
96 class AddressConverter : public WorkbookHelper
98 public:
99 explicit AddressConverter( const WorkbookHelper& rHelper );
101 /** Tries to parse the passed string for a 2d cell address in A1 notation.
103 This function accepts all strings that match the regular expression
104 "[a-zA-Z]{1,6}0*[1-9][0-9]{0,8}" (without quotes), i.e. 1 to 6 letters
105 for the column index (translated to 0-based column indexes from 0 to
106 321,272,405), and 1 to 9 digits for the 1-based row index (translated
107 to 0-based row indexes from 0 to 999,999,998). The row number part may
108 contain leading zeros, they will be ignored. It is up to the caller to
109 handle cell addresses outside of a specific valid range (e.g. the
110 entire spreadsheet).
112 @param ornColumn (out-parameter) Returns the converted column index.
113 @param ornRow (out-parameter) returns the converted row index.
114 @param rString The string containing the cell address.
115 @param nStart Start index of string part in rString to be parsed.
116 @param nLength Length of string part in rString to be parsed.
118 @return true = Parsed string was valid, returned values can be used.
120 static bool parseOoxAddress2d(
121 sal_Int32& ornColumn, sal_Int32& ornRow,
122 const OUString& rString,
123 sal_Int32 nStart = 0,
124 sal_Int32 nLength = SAL_MAX_INT32 );
126 static bool parseOoxAddress2d(
127 sal_Int32& ornColumn, sal_Int32& ornRow, const char* pStr );
129 /** Tries to parse the passed string for a 2d cell range in A1 notation.
131 This function accepts all strings that match the regular expression
132 "ADDR(:ADDR)?" (without quotes), where ADDR is a cell address accepted
133 by the parseOoxAddress2d() function of this class. It is up to the
134 caller to handle cell ranges outside of a specific valid range (e.g.
135 the entire spreadsheet).
137 @param ornStartColumn (out-parameter) Returns the converted start column index.
138 @param ornStartRow (out-parameter) returns the converted start row index.
139 @param ornEndColumn (out-parameter) Returns the converted end column index.
140 @param ornEndRow (out-parameter) returns the converted end row index.
141 @param rString The string containing the cell address.
142 @param nStart Start index of string part in rString to be parsed.
144 @return true = Parsed string was valid, returned values can be used.
146 static bool parseOoxRange2d(
147 sal_Int32& ornStartColumn, sal_Int32& ornStartRow,
148 sal_Int32& ornEndColumn, sal_Int32& ornEndRow,
149 const OUString& rString,
150 sal_Int32 nStart = 0 );
152 /** Returns the biggest valid cell address in the own Calc document. */
153 const ScAddress&
154 getMaxApiAddress() const { return maMaxApiPos; }
156 /** Returns the biggest valid cell address in the imported/exported
157 Excel document. */
158 const ScAddress&
159 getMaxXlsAddress() const { return maMaxXlsPos; }
161 /** Returns the biggest valid cell address in both Calc and the
162 imported/exported Excel document. */
163 const ScAddress&
164 getMaxAddress() const { return maMaxPos; }
166 /** Checks if the passed column index is valid.
168 @param nCol The column index to check.
169 @param bTrackOverflow true = Update the internal overflow flag, if the
170 column index is outside of the supported limits.
171 @return true = Passed column index is valid (no index overflow).
173 bool checkCol( sal_Int32 nCol, bool bTrackOverflow );
175 /** Checks if the passed row index is valid.
177 @param nRow The row index to check.
178 @param bTrackOverflow true = Update the internal overflow flag, if the
179 row index is outside of the supported limits.
180 @return true = Passed row index is valid (no index overflow).
182 bool checkRow( sal_Int32 nRow, bool bTrackOverflow );
184 /** Checks if the passed sheet index is valid.
186 @param nSheet The sheet index to check.
187 @param bTrackOverflow true = Update the internal overflow flag, if the
188 sheet index is outside of the supported limits.
189 @return true = Passed sheet index is valid (no index overflow).
191 bool checkTab( sal_Int16 nSheet, bool bTrackOverflow );
193 /** Checks the passed cell address if it fits into the spreadsheet limits.
195 @param rAddress The cell address to be checked.
196 @param bTrackOverflow true = Update the internal overflow flags, if
197 the address is outside of the supported sheet limits.
198 @return true = Passed address is valid (no index overflow).
200 bool checkCellAddress(
201 const ScAddress& rAddress,
202 bool bTrackOverflow );
204 /** Converts the passed string to a single cell address, without checking
205 any sheet limits.
207 @param orAddress (out-parameter) Returns the converted cell address.
208 @param rString Cell address string in A1 notation.
209 @param nSheet Sheet index to be inserted into orAddress.
210 @return true = Cell address could be parsed from the passed string.
212 static bool convertToCellAddressUnchecked(
213 ScAddress& orAddress,
214 const OUString& rString,
215 sal_Int16 nSheet );
217 static bool convertToCellAddressUnchecked(
218 ScAddress& orAddress, const char* pStr, sal_Int16 nSheet );
220 /** Tries to convert the passed string to a single cell address.
222 @param orAddress (out-parameter) Returns the converted cell address.
223 @param rString Cell address string in A1 notation.
224 @param nSheet Sheet index to be inserted into orAddress (will be checked).
225 @param bTrackOverflow true = Update the internal overflow flags, if
226 the address is outside of the supported sheet limits.
227 @return true = Converted address is valid (no index overflow).
229 bool convertToCellAddress(
230 ScAddress& orAddress,
231 const OUString& rString,
232 sal_Int16 nSheet,
233 bool bTrackOverflow );
235 bool convertToCellAddress(
236 ScAddress& rAddress,
237 const char* pStr, sal_Int16 nSheet, bool bTrackOverflow );
239 /** Returns a valid cell address by moving it into allowed dimensions.
241 @param rString Cell address string in A1 notation.
242 @param nSheet Sheet index for the returned address (will be checked).
243 @param bTrackOverflow true = Update the internal overflow flags, if
244 the address is outside of the supported sheet limits.
245 @return A valid cell address struct. */
246 ScAddress createValidCellAddress(
247 const OUString& rString,
248 sal_Int16 nSheet,
249 bool bTrackOverflow );
251 /** Converts the passed address to a single cell address, without checking
252 any sheet limits.
254 @param orAddress (out-parameter) Returns the converted cell address.
255 @param rBinAddress Binary cell address struct.
256 @param nSheet Sheet index to be inserted into orAddress.
258 static void convertToCellAddressUnchecked(
259 ScAddress& orAddress,
260 const BinAddress& rBinAddress,
261 sal_Int16 nSheet );
263 /** Tries to convert the passed address to a single cell address.
265 @param orAddress (out-parameter) Returns the converted cell address.
266 @param rBinAddress Binary cell address struct.
267 @param nSheet Sheet index to be inserted into orAddress (will be checked).
268 @param bTrackOverflow true = Update the internal overflow flags, if
269 the address is outside of the supported sheet limits.
270 @return true = Converted address is valid (no index overflow).
272 bool convertToCellAddress(
273 ScAddress& orAddress,
274 const BinAddress& rBinAddress,
275 sal_Int16 nSheet,
276 bool bTrackOverflow );
278 /** Returns a valid cell address by moving it into allowed dimensions.
280 @param rBinAddress Binary cell address struct.
281 @param nSheet Sheet index for the returned address (will be checked).
282 @param bTrackOverflow true = Update the internal overflow flags, if
283 the address is outside of the supported sheet limits.
284 @return A valid cell address struct. */
285 ScAddress createValidCellAddress(
286 const BinAddress& rBinAddress,
287 sal_Int16 nSheet,
288 bool bTrackOverflow );
290 /** Checks the passed cell range if it fits into the spreadsheet limits.
292 @param rRange The cell range address to be checked.
293 @param bAllowOverflow true = Allow ranges that start inside the
294 supported sheet limits but may end outside of these limits.
295 false = Do not allow ranges that overflow the supported limits.
296 @param bTrackOverflow true = Update the internal overflow flags, if
297 the passed range contains cells outside of the supported sheet
298 limits.
299 @return true = Cell range is valid. This function returns also true,
300 if only parts of the range are outside the current sheet limits and
301 such an overflow is allowed via parameter bAllowOverflow. Returns
302 false, if the entire range is outside the sheet limits, or if
303 overflow is not allowed via parameter bAllowOverflow.
305 bool checkCellRange(
306 const ScRange& rRange,
307 bool bAllowOverflow, bool bTrackOverflow );
309 /** Checks the passed cell range, may try to fit it to current sheet limits.
311 First, this function reorders the column and row indexes so that the
312 starting indexes are less than or equal to the end indexes. Then,
313 depending on the parameter bAllowOverflow, the range is just checked or
314 cropped to the current sheet limits.
316 @param orRange (in-out-parameter) Converts the passed cell range
317 into a valid cell range address. If the passed range contains cells
318 outside the currently supported spreadsheet limits, it will be
319 cropped to these limits.
320 @param bAllowOverflow true = Allow ranges that start inside the
321 supported sheet limits but may end outside of these limits. The
322 cell range returned in orRange will be cropped to these limits.
323 false = Do not allow ranges that overflow the supported limits. The
324 function will return false when the range overflows the sheet limits.
325 @param bTrackOverflow true = Update the internal overflow flags, if
326 the original range contains cells outside of the supported sheet
327 limits.
328 @return true = Converted range address is valid. This function
329 returns also true, if overflowing ranges are allowed via parameter
330 bAllowOverflow and the range has been cropped, but still contains
331 cells inside the current sheet limits. Returns false, if the entire
332 range is outside the sheet limits or overflowing ranges are not
333 allowed via parameter bAllowOverflow.
335 bool validateCellRange(
336 ScRange& orRange,
337 bool bAllowOverflow, bool bTrackOverflow );
339 /** Converts the passed string to a cell range address, without checking
340 any sheet limits.
342 @param orRange (out-parameter) Returns the converted range address.
343 @param rString Cell range string in A1 notation.
344 @param nSheet Sheet index to be inserted into orRange.
345 @return true = Range address could be parsed from the passed string.
347 static bool convertToCellRangeUnchecked(
348 ScRange& orRange,
349 const OUString& rString,
350 sal_Int16 nSheet );
352 /** Tries to convert the passed string to a cell range address.
354 @param orRange (out-parameter) Returns the converted cell range
355 address. If the original range in the passed string contains cells
356 outside the currently supported spreadsheet limits, and parameter
357 bAllowOverflow is set to true, the range will be cropped to these
358 limits. Example: the range string "A1:ZZ100000" may be converted to
359 the range A1:IV65536.
360 @param rString Cell range string in A1 notation.
361 @param nSheet Sheet index to be inserted into orRange (will be checked).
362 @param bAllowOverflow true = Allow ranges that start inside the
363 supported sheet limits but may end outside of these limits. The
364 cell range returned in orRange will be cropped to these limits.
365 false = Do not allow ranges that overflow the supported limits.
366 @param bTrackOverflow true = Update the internal overflow flags, if
367 the original range contains cells outside of the supported sheet
368 limits.
369 @return true = Converted and returned range is valid. This function
370 returns also true, if overflowing ranges are allowed via parameter
371 bAllowOverflow and the range has been cropped, but still contains
372 cells inside the current sheet limits. Returns false, if the entire
373 range is outside the sheet limits or overflowing ranges are not
374 allowed via parameter bAllowOverflow.
376 bool convertToCellRange(
377 ScRange& orRange,
378 const OUString& rString,
379 sal_Int16 nSheet,
380 bool bAllowOverflow, bool bTrackOverflow );
382 /** Converts the passed range to a cell range address, without checking any
383 sheet limits.
385 @param orRange (out-parameter) Returns the converted range address.
386 @param rBinRange Binary cell range struct.
387 @param nSheet Sheet index to be inserted into orRange.
389 static void convertToCellRangeUnchecked(
390 ScRange& orRange,
391 const BinRange& rBinRange,
392 sal_Int16 nSheet );
394 /** Tries to convert the passed range to a cell range address.
396 @param orRange (out-parameter) Returns the converted cell range
397 address. If the passed original range contains cells outside the
398 currently supported spreadsheet limits, and parameter bAllowOverflow
399 is set to true, the range will be cropped to these limits.
400 @param rBinRange Binary cell range struct.
401 @param nSheet Sheet index to be inserted into orRange (will be checked).
402 @param bAllowOverflow true = Allow ranges that start inside the
403 supported sheet limits but may end outside of these limits. The
404 cell range returned in orRange will be cropped to these limits.
405 false = Do not allow ranges that overflow the supported limits.
406 @param bTrackOverflow true = Update the internal overflow flags, if
407 the original range contains cells outside of the supported sheet
408 limits.
409 @return true = Converted and returned range is valid. This function
410 returns also true, if overflowing ranges are allowed via parameter
411 bAllowOverflow and the range has been cropped, but still contains
412 cells inside the current sheet limits. Returns false, if the entire
413 range is outside the sheet limits or if overflowing ranges are not
414 allowed via parameter bAllowOverflow.
416 bool convertToCellRange(
417 ScRange& orRange,
418 const BinRange& rBinRange,
419 sal_Int16 nSheet,
420 bool bAllowOverflow, bool bTrackOverflow );
423 /** Tries to restrict the passed cell range list to current sheet limits.
425 @param orRanges (in-out-parameter) Restricts the cell range addresses
426 in the passed list to the current sheet limits and removes invalid
427 ranges from the list.
428 @param bTrackOverflow true = Update the internal overflow flags, if
429 the original ranges contain cells outside of the supported sheet
430 limits.
432 void validateCellRangeList(
433 ScRangeList& orRanges,
434 bool bTrackOverflow );
436 /** Tries to convert the passed string to a cell range list.
438 @param orRanges (out-parameter) Returns the converted cell range
439 addresses. If a range in the passed string contains cells outside
440 the currently supported spreadsheet limits, it will be cropped to
441 these limits. Example: the range string "A1:ZZ100000" may be
442 converted to the range A1:IV65536. If a range is completely outside
443 the limits, it will be omitted.
444 @param rString Cell range list string in A1 notation, space separated.
445 @param nSheet Sheet index to be inserted into orRanges (will be checked).
446 @param bTrackOverflow true = Update the internal overflow flags, if
447 the original ranges contain cells outside of the supported sheet
448 limits.
450 void convertToCellRangeList(
451 ScRangeList& orRanges,
452 const OUString& rString,
453 sal_Int16 nSheet,
454 bool bTrackOverflow );
456 /** Tries to convert the passed range list to a cell range list.
458 @param orRanges (out-parameter) Returns the converted cell range
459 addresses. If a range in the passed string contains cells outside
460 the currently supported spreadsheet limits, it will be cropped to
461 these limits. Example: the range string "A1:ZZ100000" may be
462 converted to the range A1:IV65536. If a range is completely outside
463 the limits, it will be omitted.
464 @param rBinRanges List of binary cell range objects.
465 @param nSheet Sheet index to be inserted into orRanges (will be checked).
466 @param bTrackOverflow true = Update the internal overflow flags, if
467 the original ranges contain cells outside of the supported sheet
468 limits.
470 void convertToCellRangeList(
471 ScRangeList& orRanges,
472 const BinRangeList& rBinRanges,
473 sal_Int16 nSheet,
474 bool bTrackOverflow );
476 /** Converts the passed range list to a sequence of cell range addresses.
478 @param orRanges List of range objects.
479 @return A uno sequence of cell range addresses as used in API calls.
480 Does not check ranges for supported sheet limits.
482 static css::uno::Sequence<css::table::CellRangeAddress>
483 toApiSequence(const ScRangeList& orRanges);
485 bool isColOverflow() const { return mbColOverflow; }
486 bool isRowOverflow() const { return mbRowOverflow; }
487 bool isTabOverflow() const { return mbTabOverflow; }
489 private:
490 void initializeMaxPos(
491 sal_Int16 nMaxXlsTab, sal_Int32 nMaxXlsCol, sal_Int32 nMaxXlsRow );
493 private:
494 ScAddress maMaxApiPos; /// Maximum valid cell address in Calc.
495 ScAddress maMaxXlsPos; /// Maximum valid cell address in Excel.
496 ScAddress maMaxPos; /// Maximum valid cell address in Calc/Excel.
497 bool mbColOverflow; /// Flag for "columns overflow".
498 bool mbRowOverflow; /// Flag for "rows overflow".
499 bool mbTabOverflow; /// Flag for "tables overflow".
502 } // namespace xls
503 } // namespace oox
505 #endif
507 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */