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 .
23 #include <rangelst.hxx>
24 #include "workbookhelper.hxx"
25 #include <com/sun/star/uno/Sequence.h>
27 namespace com::sun::star::table
{ struct CellRangeAddress
; }
28 namespace oox
{ class SequenceInputStream
; }
33 /** A 2D cell address struct for binary filters. */
39 explicit BinAddress() : mnCol( 0 ), mnRow( 0 ) {}
40 explicit BinAddress( sal_Int32 nCol
, sal_Int32 nRow
) : mnCol( nCol
), mnRow( nRow
) {}
41 explicit BinAddress( const ScAddress
& rAddr
) : mnCol( rAddr
.Col() ), mnRow( rAddr
.Row() ) {}
43 void read( SequenceInputStream
& rStrm
);
46 inline bool operator<( const BinAddress
& rL
, const BinAddress
& rR
)
48 return (rL
.mnCol
< rR
.mnCol
) || ((rL
.mnCol
== rR
.mnCol
) && (rL
.mnRow
< rR
.mnRow
));
51 inline SequenceInputStream
& operator>>( SequenceInputStream
& rStrm
, BinAddress
& orPos
)
57 /** A 2D cell range address struct for binary filters. */
63 void read( SequenceInputStream
& rStrm
);
66 inline SequenceInputStream
& operator>>( SequenceInputStream
& rStrm
, BinRange
& orRange
)
68 orRange
.read( rStrm
);
72 /** A 2D cell range address list for binary filters. */
76 explicit BinRangeList() : mvRanges() {}
78 ::std::vector
< BinRange
>::const_iterator
begin() const { return mvRanges
.begin(); }
79 ::std::vector
< BinRange
>::const_iterator
end() const { return mvRanges
.end(); }
81 void read( SequenceInputStream
& rStrm
);
84 ::std::vector
< BinRange
> mvRanges
;
87 inline SequenceInputStream
& operator>>( SequenceInputStream
& rStrm
, BinRangeList
& orRanges
)
89 orRanges
.read( rStrm
);
93 /** Converter for cell addresses and cell ranges for OOXML and BIFF filters.
95 class AddressConverter final
: public WorkbookHelper
98 explicit AddressConverter( const WorkbookHelper
& rHelper
);
100 /** Tries to parse the passed string for a 2d cell address in A1 notation.
102 This function accepts all strings that match the regular expression
103 "[a-zA-Z]{1,6}0*[1-9][0-9]{0,8}" (without quotes), i.e. 1 to 6 letters
104 for the column index (translated to 0-based column indexes from 0 to
105 321,272,405), and 1 to 9 digits for the 1-based row index (translated
106 to 0-based row indexes from 0 to 999,999,998). The row number part may
107 contain leading zeros, they will be ignored. It is up to the caller to
108 handle cell addresses outside of a specific valid range (e.g. the
111 @param ornColumn (out-parameter) Returns the converted column index.
112 @param ornRow (out-parameter) returns the converted row index.
113 @param rString The string containing the cell address.
114 @param nStart Start index of string part in rString to be parsed.
115 @param nLength Length of string part in rString to be parsed.
117 @return true = Parsed string was valid, returned values can be used.
119 static bool parseOoxAddress2d(
120 sal_Int32
& ornColumn
, sal_Int32
& ornRow
,
121 const OUString
& rString
,
122 sal_Int32 nStart
= 0,
123 sal_Int32 nLength
= SAL_MAX_INT32
);
125 static bool parseOoxAddress2d(
126 sal_Int32
& ornColumn
, sal_Int32
& ornRow
, const char* pStr
);
128 /** Tries to parse the passed string for a 2d cell range in A1 notation.
130 This function accepts all strings that match the regular expression
131 "ADDR(:ADDR)?" (without quotes), where ADDR is a cell address accepted
132 by the parseOoxAddress2d() function of this class. It is up to the
133 caller to handle cell ranges outside of a specific valid range (e.g.
134 the entire spreadsheet).
136 @param ornStartColumn (out-parameter) Returns the converted start column index.
137 @param ornStartRow (out-parameter) returns the converted start row index.
138 @param ornEndColumn (out-parameter) Returns the converted end column index.
139 @param ornEndRow (out-parameter) returns the converted end row index.
140 @param rString The string containing the cell address.
141 @param nStart Start index of string part in rString to be parsed.
143 @return true = Parsed string was valid, returned values can be used.
145 static bool parseOoxRange2d(
146 sal_Int32
& ornStartColumn
, sal_Int32
& ornStartRow
,
147 sal_Int32
& ornEndColumn
, sal_Int32
& ornEndRow
,
148 const OUString
& rString
,
149 sal_Int32 nStart
= 0 );
151 /** Returns the biggest valid cell address in the own Calc document. */
153 getMaxApiAddress() const { return maMaxApiPos
; }
155 /** Returns the biggest valid cell address in the imported/exported
158 getMaxXlsAddress() const { return maMaxXlsPos
; }
160 /** Returns the biggest valid cell address in both Calc and the
161 imported/exported Excel document. */
163 getMaxAddress() const { return maMaxPos
; }
165 /** Checks if the passed column index is valid.
167 @param nCol The column index to check.
168 @param bTrackOverflow true = Update the internal overflow flag, if the
169 column index is outside of the supported limits.
170 @return true = Passed column index is valid (no index overflow).
172 bool checkCol( sal_Int32 nCol
, bool bTrackOverflow
);
174 /** Checks if the passed row index is valid.
176 @param nRow The row index to check.
177 @param bTrackOverflow true = Update the internal overflow flag, if the
178 row index is outside of the supported limits.
179 @return true = Passed row index is valid (no index overflow).
181 bool checkRow( sal_Int32 nRow
, bool bTrackOverflow
);
183 /** Checks if the passed sheet index is valid.
185 @param nSheet The sheet index to check.
186 @param bTrackOverflow true = Update the internal overflow flag, if the
187 sheet index is outside of the supported limits.
188 @return true = Passed sheet index is valid (no index overflow).
190 bool checkTab( sal_Int16 nSheet
, bool bTrackOverflow
);
192 /** Checks the passed cell address if it fits into the spreadsheet limits.
194 @param rAddress The cell address to be checked.
195 @param bTrackOverflow true = Update the internal overflow flags, if
196 the address is outside of the supported sheet limits.
197 @return true = Passed address is valid (no index overflow).
199 bool checkCellAddress(
200 const ScAddress
& rAddress
,
201 bool bTrackOverflow
);
203 /** Converts the passed string to a single cell address, without checking
206 @param orAddress (out-parameter) Returns the converted cell address.
207 @param rString Cell address string in A1 notation.
208 @param nSheet Sheet index to be inserted into orAddress.
209 @return true = Cell address could be parsed from the passed string.
211 static bool convertToCellAddressUnchecked(
212 ScAddress
& orAddress
,
213 const OUString
& rString
,
216 static bool convertToCellAddressUnchecked(
217 ScAddress
& orAddress
, const char* pStr
, sal_Int16 nSheet
);
219 /** Tries to convert the passed string to a single cell address.
221 @param orAddress (out-parameter) Returns the converted cell address.
222 @param rString Cell address string in A1 notation.
223 @param nSheet Sheet index to be inserted into orAddress (will be checked).
224 @param bTrackOverflow true = Update the internal overflow flags, if
225 the address is outside of the supported sheet limits.
226 @return true = Converted address is valid (no index overflow).
228 bool convertToCellAddress(
229 ScAddress
& orAddress
,
230 const OUString
& rString
,
232 bool bTrackOverflow
);
234 bool convertToCellAddress(
236 const char* pStr
, sal_Int16 nSheet
, bool bTrackOverflow
);
238 /** Returns a valid cell address by moving it into allowed dimensions.
240 @param rString Cell address string in A1 notation.
241 @param nSheet Sheet index for the returned address (will be checked).
242 @param bTrackOverflow true = Update the internal overflow flags, if
243 the address is outside of the supported sheet limits.
244 @return A valid cell address struct. */
245 ScAddress
createValidCellAddress(
246 const OUString
& rString
,
248 bool bTrackOverflow
);
250 /** Converts the passed address to a single cell address, without checking
253 @param orAddress (out-parameter) Returns the converted cell address.
254 @param rBinAddress Binary cell address struct.
255 @param nSheet Sheet index to be inserted into orAddress.
257 static void convertToCellAddressUnchecked(
258 ScAddress
& orAddress
,
259 const BinAddress
& rBinAddress
,
262 /** Tries to convert the passed address to a single cell address.
264 @param orAddress (out-parameter) Returns the converted cell address.
265 @param rBinAddress Binary cell address struct.
266 @param nSheet Sheet index to be inserted into orAddress (will be checked).
267 @param bTrackOverflow true = Update the internal overflow flags, if
268 the address is outside of the supported sheet limits.
269 @return true = Converted address is valid (no index overflow).
271 bool convertToCellAddress(
272 ScAddress
& orAddress
,
273 const BinAddress
& rBinAddress
,
275 bool bTrackOverflow
);
277 /** Returns a valid cell address by moving it into allowed dimensions.
279 @param rBinAddress Binary cell address struct.
280 @param nSheet Sheet index for the returned address (will be checked).
281 @param bTrackOverflow true = Update the internal overflow flags, if
282 the address is outside of the supported sheet limits.
283 @return A valid cell address struct. */
284 ScAddress
createValidCellAddress(
285 const BinAddress
& rBinAddress
,
287 bool bTrackOverflow
);
289 /** Checks the passed cell range if it fits into the spreadsheet limits.
291 @param rRange The cell range address to be checked.
292 @param bAllowOverflow true = Allow ranges that start inside the
293 supported sheet limits but may end outside of these limits.
294 false = Do not allow ranges that overflow the supported limits.
295 @param bTrackOverflow true = Update the internal overflow flags, if
296 the passed range contains cells outside of the supported sheet
298 @return true = Cell range is valid. This function returns also true,
299 if only parts of the range are outside the current sheet limits and
300 such an overflow is allowed via parameter bAllowOverflow. Returns
301 false, if the entire range is outside the sheet limits, or if
302 overflow is not allowed via parameter bAllowOverflow.
305 const ScRange
& rRange
,
306 bool bAllowOverflow
, bool bTrackOverflow
);
308 /** Checks the passed cell range, may try to fit it to current sheet limits.
310 First, this function reorders the column and row indexes so that the
311 starting indexes are less than or equal to the end indexes. Then,
312 depending on the parameter bAllowOverflow, the range is just checked or
313 cropped to the current sheet limits.
315 @param orRange (in-out-parameter) Converts the passed cell range
316 into a valid cell range address. If the passed range contains cells
317 outside the currently supported spreadsheet limits, it will be
318 cropped to these limits.
319 @param bAllowOverflow true = Allow ranges that start inside the
320 supported sheet limits but may end outside of these limits. The
321 cell range returned in orRange will be cropped to these limits.
322 false = Do not allow ranges that overflow the supported limits. The
323 function will return false when the range overflows the sheet limits.
324 @param bTrackOverflow true = Update the internal overflow flags, if
325 the original range contains cells outside of the supported sheet
327 @return true = Converted range address is valid. This function
328 returns also true, if overflowing ranges are allowed via parameter
329 bAllowOverflow and the range has been cropped, but still contains
330 cells inside the current sheet limits. Returns false, if the entire
331 range is outside the sheet limits or overflowing ranges are not
332 allowed via parameter bAllowOverflow.
334 bool validateCellRange(
336 bool bAllowOverflow
, bool bTrackOverflow
);
338 /** Converts the passed string to a cell range address, without checking
341 @param orRange (out-parameter) Returns the converted range address.
342 @param rString Cell range string in A1 notation.
343 @param nSheet Sheet index to be inserted into orRange.
344 @return true = Range address could be parsed from the passed string.
346 static bool convertToCellRangeUnchecked(
348 const OUString
& rString
,
351 /** Tries to convert the passed string to a cell range address.
353 @param orRange (out-parameter) Returns the converted cell range
354 address. If the original range in the passed string contains cells
355 outside the currently supported spreadsheet limits, and parameter
356 bAllowOverflow is set to true, the range will be cropped to these
357 limits. Example: the range string "A1:ZZ100000" may be converted to
358 the range A1:IV65536.
359 @param rString Cell range string in A1 notation.
360 @param nSheet Sheet index to be inserted into orRange (will be checked).
361 @param bAllowOverflow true = Allow ranges that start inside the
362 supported sheet limits but may end outside of these limits. The
363 cell range returned in orRange will be cropped to these limits.
364 false = Do not allow ranges that overflow the supported limits.
365 @param bTrackOverflow true = Update the internal overflow flags, if
366 the original range contains cells outside of the supported sheet
368 @return true = Converted and returned range is valid. This function
369 returns also true, if overflowing ranges are allowed via parameter
370 bAllowOverflow and the range has been cropped, but still contains
371 cells inside the current sheet limits. Returns false, if the entire
372 range is outside the sheet limits or overflowing ranges are not
373 allowed via parameter bAllowOverflow.
375 bool convertToCellRange(
377 const OUString
& rString
,
379 bool bAllowOverflow
, bool bTrackOverflow
);
381 /** Converts the passed range to a cell range address, without checking any
384 @param orRange (out-parameter) Returns the converted range address.
385 @param rBinRange Binary cell range struct.
386 @param nSheet Sheet index to be inserted into orRange.
388 static void convertToCellRangeUnchecked(
390 const BinRange
& rBinRange
,
393 /** Tries to convert the passed range to a cell range address.
395 @param orRange (out-parameter) Returns the converted cell range
396 address. If the passed original range contains cells outside the
397 currently supported spreadsheet limits, and parameter bAllowOverflow
398 is set to true, the range will be cropped to these limits.
399 @param rBinRange Binary cell range struct.
400 @param nSheet Sheet index to be inserted into orRange (will be checked).
401 @param bAllowOverflow true = Allow ranges that start inside the
402 supported sheet limits but may end outside of these limits. The
403 cell range returned in orRange will be cropped to these limits.
404 false = Do not allow ranges that overflow the supported limits.
405 @param bTrackOverflow true = Update the internal overflow flags, if
406 the original range contains cells outside of the supported sheet
408 @return true = Converted and returned range is valid. This function
409 returns also true, if overflowing ranges are allowed via parameter
410 bAllowOverflow and the range has been cropped, but still contains
411 cells inside the current sheet limits. Returns false, if the entire
412 range is outside the sheet limits or if overflowing ranges are not
413 allowed via parameter bAllowOverflow.
415 bool convertToCellRange(
417 const BinRange
& rBinRange
,
419 bool bAllowOverflow
, bool bTrackOverflow
);
422 /** Tries to restrict the passed cell range list to current sheet limits.
424 @param orRanges (in-out-parameter) Restricts the cell range addresses
425 in the passed list to the current sheet limits and removes invalid
426 ranges from the list.
427 @param bTrackOverflow true = Update the internal overflow flags, if
428 the original ranges contain cells outside of the supported sheet
431 void validateCellRangeList(
432 ScRangeList
& orRanges
,
433 bool bTrackOverflow
);
435 /** Tries to convert the passed string to a cell range list.
437 @param orRanges (out-parameter) Returns the converted cell range
438 addresses. If a range in the passed string contains cells outside
439 the currently supported spreadsheet limits, it will be cropped to
440 these limits. Example: the range string "A1:ZZ100000" may be
441 converted to the range A1:IV65536. If a range is completely outside
442 the limits, it will be omitted.
443 @param rString Cell range list string in A1 notation, space separated.
444 @param nSheet Sheet index to be inserted into orRanges (will be checked).
445 @param bTrackOverflow true = Update the internal overflow flags, if
446 the original ranges contain cells outside of the supported sheet
449 void convertToCellRangeList(
450 ScRangeList
& orRanges
,
451 const OUString
& rString
,
453 bool bTrackOverflow
);
455 /** Tries to convert the passed range list to a cell range list.
457 @param orRanges (out-parameter) Returns the converted cell range
458 addresses. If a range in the passed string contains cells outside
459 the currently supported spreadsheet limits, it will be cropped to
460 these limits. Example: the range string "A1:ZZ100000" may be
461 converted to the range A1:IV65536. If a range is completely outside
462 the limits, it will be omitted.
463 @param rBinRanges List of binary cell range objects.
464 @param nSheet Sheet index to be inserted into orRanges (will be checked).
465 @param bTrackOverflow true = Update the internal overflow flags, if
466 the original ranges contain cells outside of the supported sheet
469 void convertToCellRangeList(
470 ScRangeList
& orRanges
,
471 const BinRangeList
& rBinRanges
,
473 bool bTrackOverflow
);
475 /** Converts the passed range list to a sequence of cell range addresses.
477 @param orRanges List of range objects.
478 @return A uno sequence of cell range addresses as used in API calls.
479 Does not check ranges for supported sheet limits.
481 static css::uno::Sequence
<css::table::CellRangeAddress
>
482 toApiSequence(const ScRangeList
& orRanges
);
484 bool isColOverflow() const { return mbColOverflow
; }
485 bool isRowOverflow() const { return mbRowOverflow
; }
486 bool isTabOverflow() const { return mbTabOverflow
; }
489 void initializeMaxPos(
490 sal_Int16 nMaxXlsTab
, sal_Int32 nMaxXlsCol
, sal_Int32 nMaxXlsRow
);
493 ScAddress maMaxApiPos
; /// Maximum valid cell address in Calc.
494 ScAddress maMaxXlsPos
; /// Maximum valid cell address in Excel.
495 ScAddress maMaxPos
; /// Maximum valid cell address in Calc/Excel.
496 bool mbColOverflow
; /// Flag for "columns overflow".
497 bool mbRowOverflow
; /// Flag for "rows overflow".
498 bool mbTabOverflow
; /// Flag for "tables overflow".
505 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */