1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <memory>
#include "worksheethelper.hxx"
#include <algorithm>
#include <list>
#include <utility>
#include <com/sun/star/awt/Point.hpp>
#include <com/sun/star/awt/Size.hpp>
#include <com/sun/star/drawing/XDrawPageSupplier.hpp>
#include <com/sun/star/sheet/TableValidationVisibility.hpp>
#include <com/sun/star/sheet/ValidationType.hpp>
#include <com/sun/star/sheet/ValidationAlertStyle.hpp>
#include <com/sun/star/sheet/XCellAddressable.hpp>
#include <com/sun/star/sheet/XMultiFormulaTokens.hpp>
#include <com/sun/star/sheet/XSheetCellRangeContainer.hpp>
#include <com/sun/star/sheet/XSheetCondition2.hpp>
#include <com/sun/star/sheet/XSheetOutline.hpp>
#include <com/sun/star/sheet/XSpreadsheet.hpp>
#include <com/sun/star/table/XColumnRowRange.hpp>
#include <com/sun/star/text/WritingMode2.hpp>
#include <osl/diagnose.h>
#include <rtl/ustrbuf.hxx>
#include <oox/core/filterbase.hxx>
#include <oox/helper/propertyset.hxx>
#include <oox/token/properties.hxx>
#include <oox/token/tokens.hxx>
#include "addressconverter.hxx"
#include "autofilterbuffer.hxx"
#include "commentsbuffer.hxx"
#include "condformatbuffer.hxx"
#include "convuno.hxx"
#include "document.hxx"
#include "drawingfragment.hxx"
#include "formulaparser.hxx"
#include "pagesettings.hxx"
#include "querytablebuffer.hxx"
#include "sharedstringsbuffer.hxx"
#include "sheetdatabuffer.hxx"
#include "stylesbuffer.hxx"
#include "tokenuno.hxx"
#include "unitconverter.hxx"
#include "viewsettings.hxx"
#include "workbooksettings.hxx"
#include "worksheetbuffer.hxx"
#include "worksheetsettings.hxx"
#include "formulabuffer.hxx"
#include "scitems.hxx"
#include "editutil.hxx"
#include "tokenarray.hxx"
#include "tablebuffer.hxx"
#include "documentimport.hxx"
#include "stlsheet.hxx"
#include "stlpool.hxx"
#include "cellvalue.hxx"
#include <svl/stritem.hxx>
#include <editeng/editobj.hxx>
#include <editeng/flditem.hxx>
#include <vcl/virdev.hxx>
namespace oox {
namespace xls {
using namespace ::com::sun::star;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::drawing;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::sheet;
using namespace ::com::sun::star::table;
using namespace ::com::sun::star::text;
using namespace ::com::sun::star::uno;
namespace {
void lclUpdateProgressBar( const ISegmentProgressBarRef& rxProgressBar, double fPosition )
{
if( rxProgressBar.get() )
rxProgressBar->setPosition( fPosition );
}
} // namespace
ColumnModel::ColumnModel() :
maRange( -1 ),
mfWidth( 0.0 ),
mnXfId( -1 ),
mnLevel( 0 ),
mbShowPhonetic( false ),
mbHidden( false ),
mbCollapsed( false )
{
}
bool ColumnModel::isMergeable( const ColumnModel& rModel ) const
{
return
(maRange.mnFirst <= rModel.maRange.mnFirst) &&
(rModel.maRange.mnFirst <= maRange.mnLast + 1) &&
(mfWidth == rModel.mfWidth) &&
// ignore mnXfId, cell formatting is always set directly
(mnLevel == rModel.mnLevel) &&
(mbHidden == rModel.mbHidden) &&
(mbCollapsed == rModel.mbCollapsed);
}
RowModel::RowModel() :
mnRow( -1 ),
mfHeight( 0.0 ),
mnXfId( -1 ),
mnLevel( 0 ),
mbCustomHeight( false ),
mbCustomFormat( false ),
mbShowPhonetic( false ),
mbHidden( false ),
mbCollapsed( false ),
mbThickTop( false ),
mbThickBottom( false )
{
}
void RowModel::insertColSpan( const ValueRange& rColSpan )
{
if( (0 <= rColSpan.mnFirst) && (rColSpan.mnFirst <= rColSpan.mnLast) )
maColSpans.insert( rColSpan );
}
bool RowModel::isMergeable( const RowModel& rModel ) const
{
return
// ignore maColSpans - is handled separately in SheetDataBuffer class
(mfHeight == rModel.mfHeight) &&
// ignore mnXfId, mbCustomFormat, mbShowPhonetic - cell formatting is always set directly
(mnLevel == rModel.mnLevel) &&
(mbCustomHeight == rModel.mbCustomHeight) &&
(mbHidden == rModel.mbHidden) &&
(mbCollapsed == rModel.mbCollapsed);
}
PageBreakModel::PageBreakModel()
: mnColRow(0)
, mnMin(0)
, mnMax(0)
, mbManual(false)
{
}
HyperlinkModel::HyperlinkModel()
{
}
ValidationModel::ValidationModel() :
mnType( XML_none ),
mnOperator( XML_between ),
mnErrorStyle( XML_stop ),
mbShowInputMsg( false ),
mbShowErrorMsg( false ),
mbNoDropDown( false ),
mbAllowBlank( false )
{
}
void ValidationModel::setBiffType( sal_uInt8 nType )
{
static const sal_Int32 spnTypeIds[] = {
XML_none, XML_whole, XML_decimal, XML_list, XML_date, XML_time, XML_textLength, XML_custom };
mnType = STATIC_ARRAY_SELECT( spnTypeIds, nType, XML_none );
}
void ValidationModel::setBiffOperator( sal_uInt8 nOperator )
{
static const sal_Int32 spnOperators[] = {
XML_between, XML_notBetween, XML_equal, XML_notEqual,
XML_greaterThan, XML_lessThan, XML_greaterThanOrEqual, XML_lessThanOrEqual };
mnOperator = STATIC_ARRAY_SELECT( spnOperators, nOperator, XML_TOKEN_INVALID );
}
void ValidationModel::setBiffErrorStyle( sal_uInt8 nErrorStyle )
{
static const sal_Int32 spnErrorStyles[] = { XML_stop, XML_warning, XML_information };
mnErrorStyle = STATIC_ARRAY_SELECT( spnErrorStyles, nErrorStyle, XML_stop );
}
class WorksheetGlobals : public WorkbookHelper, public IWorksheetProgress
{
public:
explicit WorksheetGlobals(
const WorkbookHelper& rHelper,
const ISegmentProgressBarRef& rxProgressBar,
WorksheetType eSheetType,
SCTAB nSheet );
/** Returns true, if this helper refers to an existing Calc sheet. */
bool isValidSheet() const { return mxSheet.is(); }
/** Returns the type of this sheet. */
WorksheetType getSheetType() const { return meSheetType; }
/** Returns the index of the current sheet. */
SCTAB getSheetIndex() const { return maUsedArea.aStart.Tab(); }
/** Returns the XSpreadsheet interface of the current sheet. */
const Reference< XSpreadsheet >& getSheet() const { return mxSheet; }
/** Returns the XCell interface for the passed cell address. */
Reference< XCell > getCell( const ScAddress& rAddress ) const;
/** Returns the XCellRange interface for the passed cell range address. */
Reference< XCellRange > getCellRange( const ScRange& rRange ) const;
/** Returns the XSheetCellRanges interface for the passed cell range addresses. */
Reference< XSheetCellRanges > getCellRangeList( const ScRangeList& rRanges ) const;
/** Returns the XCellRange interface for a column. */
Reference< XCellRange > getColumn( sal_Int32 nCol ) const;
/** Returns the XCellRange interface for a row. */
Reference< XCellRange > getRow( sal_Int32 nRow ) const;
/** Returns the XDrawPage interface of the draw page of the current sheet. */
Reference< XDrawPage > getDrawPage() const;
/** Returns the size of the entire drawing page in 1/100 mm. */
const awt::Size& getDrawPageSize() const;
/** Returns the absolute position of the top-left corner of the cell in 1/100 mm. */
awt::Point getCellPosition( sal_Int32 nCol, sal_Int32 nRow ) const;
/** Returns the address of the cell that contains the passed point in 1/100 mm. */
ScAddress getCellAddressFromPosition( const awt::Point& rPosition ) const;
/** Returns the cell range address that contains the passed rectangle in 1/100 mm. */
ScRange getCellRangeFromRectangle( const awt::Rectangle& rRect ) const;
/** Returns the buffer for cell contents and cell formatting. */
SheetDataBuffer& getSheetData() { return maSheetData; }
/** Returns the conditional formatting in this sheet. */
CondFormatBuffer& getCondFormats() { return maCondFormats; }
/** Returns the buffer for all cell comments in this sheet. */
CommentsBuffer& getComments() { return maComments; }
/** Returns the auto filters for the sheet. */
AutoFilterBuffer& getAutoFilters() { return maAutoFilters; }
/** Returns the buffer for all web query tables in this sheet. */
QueryTableBuffer& getQueryTables() { return maQueryTables; }
/** Returns the worksheet settings object. */
WorksheetSettings& getWorksheetSettings() { return maSheetSett; }
/** Returns the page/print settings for this sheet. */
PageSettings& getPageSettings() { return maPageSett; }
/** Returns the view settings for this sheet. */
SheetViewSettings& getSheetViewSettings() { return maSheetViewSett; }
/** Returns the VML drawing page for this sheet (OOXML/BIFF12 only). */
VmlDrawing& getVmlDrawing() { return *mxVmlDrawing; }
/** returns the ExtLst entries that need to be filled */
ExtLst& getExtLst() { return maExtLst; }
/** Sets a column or row page break described in the passed struct. */
void setPageBreak( const PageBreakModel& rModel, bool bRowBreak );
/** Inserts the hyperlink URL into the spreadsheet. */
void setHyperlink( const HyperlinkModel& rModel );
/** Inserts the data validation settings into the spreadsheet. */
void setValidation( const ValidationModel& rModel );
/** Sets the path to the DrawingML fragment of this sheet. */
void setDrawingPath( const OUString& rDrawingPath );
/** Sets the path to the legacy VML drawing fragment of this sheet. */
void setVmlDrawingPath( const OUString& rVmlDrawingPath );
/** Extends the used area of this sheet by the passed cell position. */
void extendUsedArea( const ScAddress& rAddress );
/** Extends the used area of this sheet by the passed cell range. */
void extendUsedArea( const ScRange& rRange );
/** Extends the shape bounding box by the position and size of the passed rectangle. */
void extendShapeBoundingBox( const awt::Rectangle& rShapeRect );
/** Sets base width for all columns (without padding pixels). This value
is only used, if base width has not been set with setDefaultColumnWidth(). */
void setBaseColumnWidth( sal_Int32 nWidth );
/** Sets default width for all columns. This function overrides the base
width set with the setBaseColumnWidth() function. */
void setDefaultColumnWidth( double fWidth );
/** Sets column settings for a specific column range.
@descr Column default formatting is converted directly, other settings
are cached and converted in the finalizeImport() call. */
void setColumnModel( const ColumnModel& rModel );
/** Converts column default cell formatting. */
void convertColumnFormat( sal_Int32 nFirstCol, sal_Int32 nLastCol, sal_Int32 nXfId );
/** Sets default height and hidden state for all unused rows in the sheet. */
void setDefaultRowSettings( double fHeight, bool bCustomHeight, bool bHidden, bool bThickTop, bool bThickBottom );
/** Sets row settings for a specific row.
@descr Row default formatting is converted directly, other settings
are cached and converted in the finalizeImport() call. */
void setRowModel( const RowModel& rModel );
/** Initial conversion before importing the worksheet. */
void initializeWorksheetImport();
/** Final conversion after importing the worksheet. */
void finalizeWorksheetImport();
void finalizeDrawingImport();
/// Allow the threaded importer to override our progress bar impl.
virtual ISegmentProgressBarRef getRowProgress() override
{
return mxRowProgress;
}
virtual void setCustomRowProgress( const ISegmentProgressBarRef &rxRowProgress ) override
{
mxRowProgress = rxRowProgress;
mbFastRowProgress = true;
}
private:
typedef ::std::vector< sal_Int32 > OutlineLevelVec;
typedef ::std::pair< ColumnModel, sal_Int32 > ColumnModelRange;
typedef ::std::map< sal_Int32, ColumnModelRange > ColumnModelRangeMap;
typedef ::std::pair< RowModel, sal_Int32 > RowModelRange;
typedef ::std::map< sal_Int32, RowModelRange > RowModelRangeMap;
typedef ::std::list< HyperlinkModel > HyperlinkModelList;
typedef ::std::list< ValidationModel > ValidationModelList;
/** Inserts all imported hyperlinks into their cell ranges. */
void finalizeHyperlinkRanges();
/** Generates the final URL for the passed hyperlink. */
OUString getHyperlinkUrl( const HyperlinkModel& rHyperlink ) const;
/** Inserts a hyperlinks into the specified cell. */
void insertHyperlink( const ScAddress& rAddress, const OUString& rUrl );
/** Inserts all imported data validations into their cell ranges. */
void finalizeValidationRanges() const;
/** Converts column properties for all columns in the sheet. */
void convertColumns();
/** Converts column properties. */
void convertColumns( OutlineLevelVec& orColLevels, const ValueRange& rColRange, const ColumnModel& rModel );
/** Converts row properties for all rows in the sheet. */
void convertRows();
/** Converts row properties. */
void convertRows( OutlineLevelVec& orRowLevels, const ValueRange& rRowRange, const RowModel& rModel, double fDefHeight = -1.0 );
/** Converts outline grouping for the passed column or row. */
void convertOutlines( OutlineLevelVec& orLevels, sal_Int32 nColRow, sal_Int32 nLevel, bool bCollapsed, bool bRows );
/** Groups columns or rows for the given range. */
void groupColumnsOrRows( sal_Int32 nFirstColRow, sal_Int32 nLastColRow, bool bCollapsed, bool bRows );
/** Imports the drawings of the sheet (DML, VML, DFF) and updates the used area. */
void finalizeDrawings();
/** Update the row import progress bar */
void UpdateRowProgress( const ScRange& rUsedArea, SCROW nRow );
private:
typedef ::std::unique_ptr< VmlDrawing > VmlDrawingPtr;
const OUString maSheetCellRanges; /// Service name for a SheetCellRanges object.
const ScAddress& mrMaxApiPos; /// Reference to maximum Calc cell address from address converter.
ScRange maUsedArea; /// Used area of the sheet, and sheet index of the sheet.
ColumnModel maDefColModel; /// Default column formatting.
ColumnModelRangeMap maColModels; /// Ranges of columns sorted by first column index.
RowModel maDefRowModel; /// Default row formatting.
RowModelRangeMap maRowModels; /// Ranges of rows sorted by first row index.
HyperlinkModelList maHyperlinks; /// Cell ranges containing hyperlinks.
ValidationModelList maValidations; /// Cell ranges containing data validation settings.
SheetDataBuffer maSheetData; /// Buffer for cell contents and cell formatting.
CondFormatBuffer maCondFormats; /// Buffer for conditional formatting.
CommentsBuffer maComments; /// Buffer for all cell comments in this sheet.
AutoFilterBuffer maAutoFilters; /// Sheet auto filters (not associated to a table).
QueryTableBuffer maQueryTables; /// Buffer for all web query tables in this sheet.
WorksheetSettings maSheetSett; /// Global settings for this sheet.
PageSettings maPageSett; /// Page/print settings for this sheet.
SheetViewSettings maSheetViewSett; /// View settings for this sheet.
VmlDrawingPtr mxVmlDrawing; /// Collection of all VML shapes.
ExtLst maExtLst; /// List of extended elements
OUString maDrawingPath; /// Path to DrawingML fragment.
OUString maVmlDrawingPath; /// Path to legacy VML drawing fragment.
awt::Size maDrawPageSize; /// Current size of the drawing page in 1/100 mm.
awt::Rectangle maShapeBoundingBox; /// Bounding box for all shapes from all drawings.
ISegmentProgressBarRef mxProgressBar; /// Sheet progress bar.
bool mbFastRowProgress; /// Do we have a progress bar thread ?
ISegmentProgressBarRef mxRowProgress; /// Progress bar for row/cell processing.
ISegmentProgressBarRef mxFinalProgress; /// Progress bar for finalization.
WorksheetType meSheetType; /// Type of this sheet.
Reference< XSpreadsheet > mxSheet; /// Reference to the current sheet.
bool mbHasDefWidth; /// True = default column width is set from defaultColWidth attribute.
};
WorksheetGlobals::WorksheetGlobals( const WorkbookHelper& rHelper, const ISegmentProgressBarRef& rxProgressBar, WorksheetType eSheetType, SCTAB nSheet ) :
WorkbookHelper( rHelper ),
maSheetCellRanges( "com.sun.star.sheet.SheetCellRanges" ),
mrMaxApiPos( rHelper.getAddressConverter().getMaxApiAddress() ),
maUsedArea( SCCOL_MAX, SCROW_MAX, nSheet, -1, -1, nSheet ), // Set start address to largest possible value, and end address to smallest
maSheetData( *this ),
maCondFormats( *this ),
maComments( *this ),
maAutoFilters( *this ),
maQueryTables( *this ),
maSheetSett( *this ),
maPageSett( *this ),
maSheetViewSett( *this ),
mxProgressBar( rxProgressBar ),
mbFastRowProgress( false ),
meSheetType( eSheetType ),
mbHasDefWidth( false )
{
mxSheet = getSheetFromDoc( nSheet );
if( !mxSheet.is() )
maUsedArea.aStart.SetTab( -1 );
// default column settings (width and hidden state may be updated later)
maDefColModel.mfWidth = 8.5;
maDefColModel.mnXfId = -1;
maDefColModel.mnLevel = 0;
maDefColModel.mbHidden = false;
maDefColModel.mbCollapsed = false;
// default row settings (height and hidden state may be updated later)
maDefRowModel.mfHeight = 0.0;
maDefRowModel.mnXfId = -1;
maDefRowModel.mnLevel = 0;
maDefRowModel.mbCustomHeight = false;
maDefRowModel.mbCustomFormat = false;
maDefRowModel.mbShowPhonetic = false;
maDefRowModel.mbHidden = false;
maDefRowModel.mbCollapsed = false;
// buffers
mxVmlDrawing.reset( new VmlDrawing( *this ) );
// prepare progress bars
if( mxProgressBar.get() )
{
mxRowProgress = mxProgressBar->createSegment( 0.5 );
mxFinalProgress = mxProgressBar->createSegment( 0.5 );
}
}
Reference< XCell > WorksheetGlobals::getCell( const ScAddress& rAddress ) const
{
Reference< XCell > xCell;
if( mxSheet.is() ) try
{
xCell = mxSheet->getCellByPosition( rAddress.Col(), rAddress.Row() );
}
catch( Exception& )
{
}
return xCell;
}
Reference< XCellRange > WorksheetGlobals::getCellRange( const ScRange& rRange ) const
{
Reference< XCellRange > xRange;
if( mxSheet.is() ) try
{
xRange = mxSheet->getCellRangeByPosition( rRange.aStart.Col(), rRange.aStart.Row(), rRange.aEnd.Col(), rRange.aEnd.Row() );
}
catch( Exception& )
{
}
return xRange;
}
Reference< XSheetCellRanges > WorksheetGlobals::getCellRangeList( const ScRangeList& rRanges ) const
{
Reference< XSheetCellRanges > xRanges;
if( mxSheet.is() && !rRanges.empty() ) try
{
xRanges.set( getBaseFilter().getModelFactory()->createInstance( maSheetCellRanges ), UNO_QUERY_THROW );
Reference< XSheetCellRangeContainer > xRangeCont( xRanges, UNO_QUERY_THROW );
xRangeCont->addRangeAddresses( AddressConverter::toApiSequence(rRanges), false );
}
catch( Exception& )
{
}
return xRanges;
}
Reference< XCellRange > WorksheetGlobals::getColumn( sal_Int32 nCol ) const
{
Reference< XCellRange > xColumn;
try
{
Reference< XColumnRowRange > xColRowRange( mxSheet, UNO_QUERY_THROW );
Reference< XTableColumns > xColumns( xColRowRange->getColumns(), UNO_SET_THROW );
xColumn.set( xColumns->getByIndex( nCol ), UNO_QUERY );
}
catch( Exception& )
{
}
return xColumn;
}
Reference< XCellRange > WorksheetGlobals::getRow( sal_Int32 nRow ) const
{
Reference< XCellRange > xRow;
try
{
Reference< XColumnRowRange > xColRowRange( mxSheet, UNO_QUERY_THROW );
Reference< XTableRows > xRows( xColRowRange->getRows(), UNO_SET_THROW );
xRow.set( xRows->getByIndex( nRow ), UNO_QUERY );
}
catch( Exception& )
{
}
return xRow;
}
Reference< XDrawPage > WorksheetGlobals::getDrawPage() const
{
Reference< XDrawPage > xDrawPage;
try
{
xDrawPage = Reference< XDrawPageSupplier >( mxSheet, UNO_QUERY_THROW )->getDrawPage();
}
catch( Exception& )
{
}
return xDrawPage;
}
const awt::Size& WorksheetGlobals::getDrawPageSize() const
{
OSL_ENSURE( (maDrawPageSize.Width > 0) && (maDrawPageSize.Height > 0), "WorksheetGlobals::getDrawPageSize - called too early, size invalid" );
return maDrawPageSize;
}
awt::Point WorksheetGlobals::getCellPosition( sal_Int32 nCol, sal_Int32 nRow ) const
{
awt::Point aPoint;
PropertySet aCellProp( getCell( ScAddress( nCol, nRow, getSheetIndex() ) ) );
aCellProp.getProperty( aPoint, PROP_Position );
return aPoint;
}
namespace {
inline sal_Int32 lclGetMidAddr( sal_Int32 nBegAddr, sal_Int32 nEndAddr, sal_Int32 nBegPos, sal_Int32 nEndPos, sal_Int32 nSearchPos )
{
// use sal_Int64 to prevent integer overflow
return nBegAddr + 1 + static_cast< sal_Int32 >( static_cast< sal_Int64 >( nEndAddr - nBegAddr - 2 ) * (nSearchPos - nBegPos) / (nEndPos - nBegPos) );
}
bool lclPrepareInterval( sal_Int32 nBegAddr, sal_Int32& rnMidAddr, sal_Int32 nEndAddr,
sal_Int32 nBegPos, sal_Int32 nEndPos, sal_Int32 nSearchPos )
{
// searched position before nBegPos -> use nBegAddr
if( nSearchPos <= nBegPos )
{
rnMidAddr = nBegAddr;
return false;
}
// searched position after nEndPos, or begin next to end -> use nEndAddr
if( (nSearchPos >= nEndPos) || (nBegAddr + 1 >= nEndAddr) )
{
rnMidAddr = nEndAddr;
return false;
}
/* Otherwise find mid address according to position. lclGetMidAddr() will
return an address between nBegAddr and nEndAddr. */
rnMidAddr = lclGetMidAddr( nBegAddr, nEndAddr, nBegPos, nEndPos, nSearchPos );
return true;
}
bool lclUpdateInterval( sal_Int32& rnBegAddr, sal_Int32& rnMidAddr, sal_Int32& rnEndAddr,
sal_Int32& rnBegPos, sal_Int32 nMidPos, sal_Int32& rnEndPos, sal_Int32 nSearchPos )
{
// nSearchPos < nMidPos: use the interval [begin,mid] in the next iteration
if( nSearchPos < nMidPos )
{
// if rnBegAddr is next to rnMidAddr, the latter is the column/row in question
if( rnBegAddr + 1 >= rnMidAddr )
return false;
// otherwise, set interval end to mid
rnEndPos = nMidPos;
rnEndAddr = rnMidAddr;
rnMidAddr = lclGetMidAddr( rnBegAddr, rnEndAddr, rnBegPos, rnEndPos, nSearchPos );
return true;
}
// nSearchPos > nMidPos: use the interval [mid,end] in the next iteration
if( nSearchPos > nMidPos )
{
// if rnMidAddr is next to rnEndAddr, the latter is the column/row in question
if( rnMidAddr + 1 >= rnEndAddr )
{
rnMidAddr = rnEndAddr;
return false;
}
// otherwise, set interval start to mid
rnBegPos = nMidPos;
rnBegAddr = rnMidAddr;
rnMidAddr = lclGetMidAddr( rnBegAddr, rnEndAddr, rnBegPos, rnEndPos, nSearchPos );
return true;
}
// nSearchPos == nMidPos: rnMidAddr is the column/row in question, do not loop anymore
return false;
}
} // namespace
ScAddress WorksheetGlobals::getCellAddressFromPosition( const awt::Point& rPosition ) const
{
// starting cell address and its position in drawing layer (top-left edge)
sal_Int32 nBegCol = 0;
sal_Int32 nBegRow = 0;
awt::Point aBegPos( 0, 0 );
// end cell address and its position in drawing layer (bottom-right edge)
sal_Int32 nEndCol = mrMaxApiPos.Col() + 1;
sal_Int32 nEndRow = mrMaxApiPos.Row() + 1;
awt::Point aEndPos( maDrawPageSize.Width, maDrawPageSize.Height );
// starting point for interval search
sal_Int32 nMidCol, nMidRow;
bool bLoopCols = lclPrepareInterval( nBegCol, nMidCol, nEndCol, aBegPos.X, aEndPos.X, rPosition.X );
bool bLoopRows = lclPrepareInterval( nBegRow, nMidRow, nEndRow, aBegPos.Y, aEndPos.Y, rPosition.Y );
awt::Point aMidPos = getCellPosition( nMidCol, nMidRow );
/* The loop will find the column/row index of the cell right of/below
the cell containing the passed point, unless the point is located at
the top or left border of the containing cell. */
while( bLoopCols || bLoopRows )
{
bLoopCols = bLoopCols && lclUpdateInterval( nBegCol, nMidCol, nEndCol, aBegPos.X, aMidPos.X, aEndPos.X, rPosition.X );
bLoopRows = bLoopRows && lclUpdateInterval( nBegRow, nMidRow, nEndRow, aBegPos.Y, aMidPos.Y, aEndPos.Y, rPosition.Y );
aMidPos = getCellPosition( nMidCol, nMidRow );
}
/* The cell left of/above the current search position contains the passed
point, unless the point is located on the top/left border of the cell,
or the last column/row of the sheet has been reached. */
if( aMidPos.X > rPosition.X ) --nMidCol;
if( aMidPos.Y > rPosition.Y ) --nMidRow;
return ScAddress( nMidCol, nMidRow, getSheetIndex() );
}
ScRange WorksheetGlobals::getCellRangeFromRectangle( const awt::Rectangle& rRect ) const
{
ScAddress aStartAddr = getCellAddressFromPosition( awt::Point( rRect.X, rRect.Y ) );
awt::Point aBotRight( rRect.X + rRect.Width, rRect.Y + rRect.Height );
ScAddress aEndAddr = getCellAddressFromPosition( aBotRight );
bool bMultiCols = aStartAddr.Col() < aEndAddr.Col();
bool bMultiRows = aStartAddr.Row() < aEndAddr.Row();
if( bMultiCols || bMultiRows )
{
/* Reduce end position of the cell range to previous column or row, if
the rectangle ends exactly between two columns or rows. */
awt::Point aEndPos = getCellPosition( aEndAddr.Col(), aEndAddr.Row() );
if( bMultiCols && (aBotRight.X <= aEndPos.X) )
aEndAddr.IncCol(-1);
if( bMultiRows && (aBotRight.Y <= aEndPos.Y) )
aEndAddr.IncRow(-1);
}
return ScRange( aStartAddr.Col(), aStartAddr.Row(), getSheetIndex(),
aEndAddr.Col(), aEndAddr.Row(), getSheetIndex() );
}
void WorksheetGlobals::setPageBreak( const PageBreakModel& rModel, bool bRowBreak )
{
if( rModel.mbManual && (rModel.mnColRow > 0) )
{
PropertySet aPropSet( bRowBreak ? getRow( rModel.mnColRow ) : getColumn( rModel.mnColRow ) );
aPropSet.setProperty( PROP_IsStartOfNewPage, true );
}
}
void WorksheetGlobals::setHyperlink( const HyperlinkModel& rModel )
{
maHyperlinks.push_back( rModel );
}
void WorksheetGlobals::setValidation( const ValidationModel& rModel )
{
maValidations.push_back( rModel );
}
void WorksheetGlobals::setDrawingPath( const OUString& rDrawingPath )
{
maDrawingPath = rDrawingPath;
}
void WorksheetGlobals::setVmlDrawingPath( const OUString& rVmlDrawingPath )
{
maVmlDrawingPath = rVmlDrawingPath;
}
void WorksheetGlobals::extendUsedArea( const ScAddress& rAddress )
{
maUsedArea.aStart.SetCol( ::std::min( maUsedArea.aStart.Col(), rAddress.Col() ) );
maUsedArea.aStart.SetRow( ::std::min( maUsedArea.aStart.Row(), rAddress.Row() ) );
maUsedArea.aEnd.SetCol( ::std::max( maUsedArea.aEnd.Col(), rAddress.Col() ) );
maUsedArea.aEnd.SetRow( ::std::max( maUsedArea.aEnd.Row(), rAddress.Row() ) );
}
void WorksheetGlobals::extendUsedArea( const ScRange& rRange )
{
extendUsedArea( rRange.aStart );
extendUsedArea( rRange.aEnd );
}
void WorksheetHelper::extendUsedArea( const ScRange& rRange )
{
extendUsedArea( rRange.aStart );
extendUsedArea( rRange.aEnd );
}
void WorksheetGlobals::extendShapeBoundingBox( const awt::Rectangle& rShapeRect )
{
if( (maShapeBoundingBox.Width == 0) && (maShapeBoundingBox.Height == 0) )
{
// width and height of maShapeBoundingBox are assumed to be zero on first cell
maShapeBoundingBox = rShapeRect;
}
else
{
sal_Int32 nEndX = ::std::max( maShapeBoundingBox.X + maShapeBoundingBox.Width, rShapeRect.X + rShapeRect.Width );
sal_Int32 nEndY = ::std::max( maShapeBoundingBox.Y + maShapeBoundingBox.Height, rShapeRect.Y + rShapeRect.Height );
maShapeBoundingBox.X = ::std::min( maShapeBoundingBox.X, rShapeRect.X );
maShapeBoundingBox.Y = ::std::min( maShapeBoundingBox.Y, rShapeRect.Y );
maShapeBoundingBox.Width = nEndX - maShapeBoundingBox.X;
maShapeBoundingBox.Height = nEndY - maShapeBoundingBox.Y;
}
}
void WorksheetGlobals::setBaseColumnWidth( sal_Int32 nWidth )
{
// do not modify width, if setDefaultColumnWidth() has been used
if( !mbHasDefWidth && (nWidth > 0) )
{
// #i3006# add 5 pixels padding to the width
const UnitConverter& rUnitConv = getUnitConverter();
maDefColModel.mfWidth = rUnitConv.scaleFromMm100(
rUnitConv.scaleToMm100( nWidth, UNIT_DIGIT ) + rUnitConv.scaleToMm100( 5, UNIT_SCREENX ), UNIT_DIGIT );
}
}
void WorksheetGlobals::setDefaultColumnWidth( double fWidth )
{
// overrides a width set with setBaseColumnWidth()
if( fWidth > 0.0 )
{
maDefColModel.mfWidth = fWidth;
mbHasDefWidth = true;
}
}
void WorksheetGlobals::setColumnModel( const ColumnModel& rModel )
{
// convert 1-based OOXML column indexes to 0-based API column indexes
sal_Int32 nFirstCol = rModel.maRange.mnFirst - 1;
sal_Int32 nLastCol = rModel.maRange.mnLast - 1;
if( getAddressConverter().checkCol( nFirstCol, true ) && (nFirstCol <= nLastCol) )
{
// validate last column index
if( !getAddressConverter().checkCol( nLastCol, true ) )
nLastCol = mrMaxApiPos.Col();
// try to find entry in column model map that is able to merge with the passed model
bool bInsertModel = true;
if( !maColModels.empty() )
{
// find first column model range following nFirstCol (nFirstCol < aIt->first), or end of map
ColumnModelRangeMap::iterator aIt = maColModels.upper_bound( nFirstCol );
OSL_ENSURE( aIt == maColModels.end(), "WorksheetGlobals::setColModel - columns are unsorted" );
// if inserting before another column model, get last free column
OSL_ENSURE( (aIt == maColModels.end()) || (nLastCol < aIt->first), "WorksheetGlobals::setColModel - multiple models of the same column" );
if( aIt != maColModels.end() )
nLastCol = ::std::min( nLastCol, aIt->first - 1 );
if( aIt != maColModels.begin() )
{
// go to previous map element (which may be able to merge with the passed model)
--aIt;
// the usage of upper_bound() above ensures that aIt->first is less than or equal to nFirstCol now
sal_Int32& rnLastMapCol = aIt->second.second;
OSL_ENSURE( rnLastMapCol < nFirstCol, "WorksheetGlobals::setColModel - multiple models of the same column" );
nFirstCol = ::std::max( rnLastMapCol + 1, nFirstCol );
if( (rnLastMapCol + 1 == nFirstCol) && (nFirstCol <= nLastCol) && aIt->second.first.isMergeable( rModel ) )
{
// can merge with existing model, update last column index
rnLastMapCol = nLastCol;
bInsertModel = false;
}
}
}
if( nFirstCol <= nLastCol )
{
// insert the column model, if it has not been merged with another
if( bInsertModel )
maColModels[ nFirstCol ] = ColumnModelRange( rModel, nLastCol );
// set column formatting directly
convertColumnFormat( nFirstCol, nLastCol, rModel.mnXfId );
}
}
}
void WorksheetGlobals::convertColumnFormat( sal_Int32 nFirstCol, sal_Int32 nLastCol, sal_Int32 nXfId )
{
ScRange aRange( nFirstCol, 0, getSheetIndex(), nLastCol, mrMaxApiPos.Row(), getSheetIndex() );
if( getAddressConverter().validateCellRange( aRange, true, false ) )
{
const StylesBuffer& rStyles = getStyles();
// Set cell styles via direct API - the preferred approach.
ScDocumentImport& rDoc = getDocImport();
rStyles.writeCellXfToDoc(rDoc, aRange, nXfId);
}
}
void WorksheetGlobals::setDefaultRowSettings( double fHeight, bool bCustomHeight, bool bHidden, bool bThickTop, bool bThickBottom )
{
maDefRowModel.mfHeight = fHeight;
maDefRowModel.mbCustomHeight = bCustomHeight;
maDefRowModel.mbHidden = bHidden;
maDefRowModel.mbThickTop = bThickTop;
maDefRowModel.mbThickBottom = bThickBottom;
}
void WorksheetGlobals::setRowModel( const RowModel& rModel )
{
// convert 1-based OOXML row index to 0-based API row index
sal_Int32 nRow = rModel.mnRow - 1;
if( getAddressConverter().checkRow( nRow, true ) )
{
// try to find entry in row model map that is able to merge with the passed model
bool bInsertModel = true;
bool bUnusedRow = true;
if( !maRowModels.empty() )
{
// find first row model range following nRow (nRow < aIt->first), or end of map
RowModelRangeMap::iterator aIt = maRowModels.upper_bound( nRow );
OSL_ENSURE( aIt == maRowModels.end(), "WorksheetGlobals::setRowModel - rows are unsorted" );
if( aIt != maRowModels.begin() )
{
// go to previous map element (which may be able to merge with the passed model)
--aIt;
// the usage of upper_bound() above ensures that aIt->first is less than or equal to nRow now
sal_Int32& rnLastMapRow = aIt->second.second;
bUnusedRow = rnLastMapRow < nRow;
OSL_ENSURE( bUnusedRow, "WorksheetGlobals::setRowModel - multiple models of the same row" );
if( (rnLastMapRow + 1 == nRow) && aIt->second.first.isMergeable( rModel ) )
{
// can merge with existing model, update last row index
++rnLastMapRow;
bInsertModel = false;
}
}
}
if( bUnusedRow )
{
// insert the row model, if it has not been merged with another
if( bInsertModel )
maRowModels[ nRow ] = RowModelRange( rModel, nRow );
// set row formatting
maSheetData.setRowFormat( nRow, rModel.mnXfId, rModel.mbCustomFormat );
// set column spans
maSheetData.setColSpans( nRow, rModel.maColSpans );
}
}
UpdateRowProgress( maUsedArea, nRow );
}
// This is called at a higher frequency inside the (threaded) inner loop.
void WorksheetGlobals::UpdateRowProgress( const ScRange& rUsedArea, SCROW nRow )
{
if (!mxRowProgress || nRow < rUsedArea.aStart.Row() || rUsedArea.aEnd.Row() < nRow)
return;
double fNewPos = static_cast<double>(nRow - rUsedArea.aStart.Row() + 1.0) / (rUsedArea.aEnd.Row() - rUsedArea.aStart.Row() + 1.0);
if (mbFastRowProgress)
mxRowProgress->setPosition(fNewPos);
else
{
double fCurPos = mxRowProgress->getPosition();
if (fCurPos < fNewPos && (fNewPos - fCurPos) > 0.3)
// Try not to re-draw progress bar too frequently.
mxRowProgress->setPosition(fNewPos);
}
}
void WorksheetGlobals::initializeWorksheetImport()
{
// set default cell style for unused cells
ScDocumentImport& rDoc = getDocImport();
ScStyleSheet* pStyleSheet =
static_cast<ScStyleSheet*>(rDoc.getDoc().GetStyleSheetPool()->Find(
getStyles().getDefaultStyleName(), SfxStyleFamily::Para));
if (pStyleSheet)
rDoc.setCellStyleToSheet(getSheetIndex(), *pStyleSheet);
/* Remember the current sheet index in global data, needed by global
objects, e.g. the chart converter. */
setCurrentSheetIndex( getSheetIndex() );
}
void WorksheetGlobals::finalizeWorksheetImport()
{
lclUpdateProgressBar( mxRowProgress, 1.0 );
maSheetData.finalizeImport();
// assumes getTables().finalizeImport ( which creates the DatabaseRanges )
// has been called already
getTables().applyAutoFilters();
getCondFormats().finalizeImport();
lclUpdateProgressBar( mxFinalProgress, 0.25 );
finalizeHyperlinkRanges();
finalizeValidationRanges();
maAutoFilters.finalizeImport( getSheetIndex() );
maQueryTables.finalizeImport();
maSheetSett.finalizeImport();
maPageSett.finalizeImport();
maSheetViewSett.finalizeImport();
lclUpdateProgressBar( mxFinalProgress, 0.5 );
convertColumns();
convertRows();
lclUpdateProgressBar( mxFinalProgress, 1.0 );
}
void WorksheetGlobals::finalizeDrawingImport()
{
finalizeDrawings();
// forget current sheet index in global data
setCurrentSheetIndex( -1 );
}
// private --------------------------------------------------------------------
void WorksheetGlobals::finalizeHyperlinkRanges()
{
for( HyperlinkModelList::const_iterator aIt = maHyperlinks.begin(), aEnd = maHyperlinks.end(); aIt != aEnd; ++aIt )
{
OUString aUrl = getHyperlinkUrl( *aIt );
// try to insert URL into each cell of the range
if( !aUrl.isEmpty() )
for( ScAddress aAddress( aIt->maRange.aStart.Col(), aIt->maRange.aStart.Row(), getSheetIndex() ); aAddress.Row() <= aIt->maRange.aEnd.Row(); aAddress.IncRow() )
for( aAddress.SetCol(aIt->maRange.aStart.Col()); aAddress.Col() <= aIt->maRange.aEnd.Col(); aAddress.IncCol() )
insertHyperlink( aAddress, aUrl );
}
}
OUString WorksheetGlobals::getHyperlinkUrl( const HyperlinkModel& rHyperlink ) const
{
OUStringBuffer aUrlBuffer;
if( !rHyperlink.maTarget.isEmpty() )
aUrlBuffer.append( getBaseFilter().getAbsoluteUrl( rHyperlink.maTarget ) );
if( !rHyperlink.maLocation.isEmpty() )
aUrlBuffer.append( '#' ).append( rHyperlink.maLocation );
OUString aUrl = aUrlBuffer.makeStringAndClear();
if( aUrl.startsWith("#") )
{
sal_Int32 nSepPos = aUrl.lastIndexOf( '!' );
if( nSepPos > 0 )
{
// Do not attempt to blindly convert '#SheetName!A1' to
// '#SheetName.A1', it can be #SheetName!R1C1 as well. Hyperlink
// handler has to handle all, but prefer '#SheetName.A1' if
// possible.
if (nSepPos < aUrl.getLength() - 1)
{
ScRange aRange;
if ((aRange.ParseAny( aUrl.copy( nSepPos + 1 ), nullptr,
formula::FormulaGrammar::CONV_XL_R1C1)
& ScRefFlags::VALID) == ScRefFlags::ZERO)
aUrl = aUrl.replaceAt( nSepPos, 1, OUString( '.' ) );
}
// #i66592# convert sheet names that have been renamed on import
OUString aSheetName = aUrl.copy( 1, nSepPos - 1 );
OUString aCalcName = getWorksheets().getCalcSheetName( aSheetName );
if( !aCalcName.isEmpty() )
aUrl = aUrl.replaceAt( 1, nSepPos - 1, aCalcName );
}
}
return aUrl;
}
void WorksheetGlobals::insertHyperlink( const ScAddress& rAddress, const OUString& rUrl )
{
ScDocumentImport& rDoc = getDocImport();
ScRefCellValue aCell(rDoc.getDoc(), rAddress);
if (aCell.meType == CELLTYPE_STRING || aCell.meType == CELLTYPE_EDIT)
{
OUString aStr = aCell.getString(&rDoc.getDoc());
ScFieldEditEngine& rEE = rDoc.getDoc().GetEditEngine();
rEE.Clear();
SvxURLField aURLField(rUrl, aStr, SvxURLFormat::Repr);
SvxFieldItem aURLItem(aURLField, EE_FEATURE_FIELD);
rEE.QuickInsertField(aURLItem, ESelection());
rDoc.setEditCell(rAddress, rEE.CreateTextObject());
}
else
{
// Handle other cell types e.g. formulas ( and ? ) that have associated
// hyperlinks.
// Ideally all hyperlinks should be treated as below. For the moment,
// given the current absence of ods support lets just handle what we
// previously didn't handle the new way.
// Unfortunately we won't be able to preserve such hyperlinks when
// saving to ods. Note: when we are able to save such hyperlinks to ods
// we should handle *all* imported hyperlinks as below ( e.g. as cell
// attribute ) for better interoperability.
SfxStringItem aItem(ATTR_HYPERLINK, rUrl);
rDoc.getDoc().ApplyAttr(rAddress.Col(), rAddress.Row(), rAddress.Tab(), aItem);
}
}
void WorksheetGlobals::finalizeValidationRanges() const
{
for( ValidationModelList::const_iterator aIt = maValidations.begin(), aEnd = maValidations.end(); aIt != aEnd; ++aIt )
{
PropertySet aPropSet( getCellRangeList( aIt->maRanges ) );
Reference< XPropertySet > xValidation( aPropSet.getAnyProperty( PROP_Validation ), UNO_QUERY );
if( xValidation.is() )
{
PropertySet aValProps( xValidation );
try
{
sal_Int32 nIndex = 0;
OUString aToken = aIt->msRef.getToken( 0, ' ', nIndex );
Reference<XSpreadsheet> xSheet = getSheetFromDoc( getCurrentSheetIndex() );
Reference<XCellRange> xDBCellRange;
Reference<XCell> xCell;
xDBCellRange = xSheet->getCellRangeByName( aToken );
xCell = xDBCellRange->getCellByPosition( 0, 0 );
Reference<XCellAddressable> xCellAddressable( xCell, UNO_QUERY_THROW );
CellAddress aFirstCell = xCellAddressable->getCellAddress();
Reference<XSheetCondition> xCondition( xValidation, UNO_QUERY_THROW );
xCondition->setSourcePosition( aFirstCell );
}
catch(const Exception&)
{
}
// convert validation type to API enum
ValidationType eType = ValidationType_ANY;
switch( aIt->mnType )
{
case XML_custom: eType = ValidationType_CUSTOM; break;
case XML_date: eType = ValidationType_DATE; break;
case XML_decimal: eType = ValidationType_DECIMAL; break;
case XML_list: eType = ValidationType_LIST; break;
case XML_none: eType = ValidationType_ANY; break;
case XML_textLength: eType = ValidationType_TEXT_LEN; break;
case XML_time: eType = ValidationType_TIME; break;
case XML_whole: eType = ValidationType_WHOLE; break;
default: OSL_FAIL( "WorksheetData::finalizeValidationRanges - unknown validation type" );
}
aValProps.setProperty( PROP_Type, eType );
// convert error alert style to API enum
ValidationAlertStyle eAlertStyle = ValidationAlertStyle_STOP;
switch( aIt->mnErrorStyle )
{
case XML_information: eAlertStyle = ValidationAlertStyle_INFO; break;
case XML_stop: eAlertStyle = ValidationAlertStyle_STOP; break;
case XML_warning: eAlertStyle = ValidationAlertStyle_WARNING; break;
default: OSL_FAIL( "WorksheetData::finalizeValidationRanges - unknown error style" );
}
aValProps.setProperty( PROP_ErrorAlertStyle, eAlertStyle );
// convert dropdown style to API visibility constants
sal_Int16 nVisibility = aIt->mbNoDropDown ? TableValidationVisibility::INVISIBLE : TableValidationVisibility::UNSORTED;
aValProps.setProperty( PROP_ShowList, nVisibility );
// messages
aValProps.setProperty( PROP_ShowInputMessage, aIt->mbShowInputMsg );
aValProps.setProperty( PROP_InputTitle, aIt->maInputTitle );
aValProps.setProperty( PROP_InputMessage, aIt->maInputMessage );
aValProps.setProperty( PROP_ShowErrorMessage, aIt->mbShowErrorMsg );
aValProps.setProperty( PROP_ErrorTitle, aIt->maErrorTitle );
aValProps.setProperty( PROP_ErrorMessage, aIt->maErrorMessage );
// allow blank cells
aValProps.setProperty( PROP_IgnoreBlankCells, aIt->mbAllowBlank );
try
{
// condition operator
Reference< XSheetCondition2 > xSheetCond( xValidation, UNO_QUERY_THROW );
xSheetCond->setConditionOperator( CondFormatBuffer::convertToApiOperator( aIt->mnOperator ) );
// condition formulas
Reference< XMultiFormulaTokens > xTokens( xValidation, UNO_QUERY_THROW );
xTokens->setTokens( 0, aIt->maTokens1 );
xTokens->setTokens( 1, aIt->maTokens2 );
}
catch( Exception& )
{
}
// write back validation settings to cell range(s)
aPropSet.setProperty( PROP_Validation, xValidation );
}
}
}
void WorksheetGlobals::convertColumns()
{
sal_Int32 nNextCol = 0;
sal_Int32 nMaxCol = mrMaxApiPos.Col();
// stores first grouped column index for each level
OutlineLevelVec aColLevels;
for( ColumnModelRangeMap::iterator aIt = maColModels.begin(), aEnd = maColModels.end(); aIt != aEnd; ++aIt )
{
// column indexes are stored 0-based in maColModels
ValueRange aColRange( ::std::max( aIt->first, nNextCol ), ::std::min( aIt->second.second, nMaxCol ) );
// process gap between two column models, use default column model
if( nNextCol < aColRange.mnFirst )
convertColumns( aColLevels, ValueRange( nNextCol, aColRange.mnFirst - 1 ), maDefColModel );
// process the column model
convertColumns( aColLevels, aColRange, aIt->second.first );
// cache next column to be processed
nNextCol = aColRange.mnLast + 1;
}
// remaining default columns to end of sheet
convertColumns( aColLevels, ValueRange( nNextCol, nMaxCol ), maDefColModel );
// close remaining column outlines spanning to end of sheet
convertOutlines( aColLevels, nMaxCol + 1, 0, false, false );
}
void WorksheetGlobals::convertColumns( OutlineLevelVec& orColLevels,
const ValueRange& rColRange, const ColumnModel& rModel )
{
// column width: convert 'number of characters' to column width in 1/100 mm
sal_Int32 nWidth = getUnitConverter().scaleToMm100( rModel.mfWidth, UNIT_DIGIT );
// macro sheets have double width
if( meSheetType == WorksheetType::Macro )
nWidth *= 2;
SCTAB nTab = getSheetIndex();
ScDocument& rDoc = getScDocument();
SCCOL nStartCol = rColRange.mnFirst;
SCCOL nEndCol = rColRange.mnLast;
if( nWidth > 0 )
{
for( SCCOL nCol = nStartCol; nCol <= nEndCol; ++nCol )
{
rDoc.SetColWidthOnly( nCol, nTab, (sal_uInt16)sc::HMMToTwips( nWidth ) );
}
}
// hidden columns: TODO: #108683# hide columns later?
if( rModel.mbHidden )
{
rDoc.SetColHidden( nStartCol, nEndCol, nTab, true );
}
// outline settings for this column range
convertOutlines( orColLevels, rColRange.mnFirst, rModel.mnLevel, rModel.mbCollapsed, false );
}
void WorksheetGlobals::convertRows()
{
sal_Int32 nNextRow = 0;
sal_Int32 nMaxRow = mrMaxApiPos.Row();
// stores first grouped row index for each level
OutlineLevelVec aRowLevels;
for( RowModelRangeMap::iterator aIt = maRowModels.begin(), aEnd = maRowModels.end(); aIt != aEnd; ++aIt )
{
// row indexes are stored 0-based in maRowModels
ValueRange aRowRange( ::std::max( aIt->first, nNextRow ), ::std::min( aIt->second.second, nMaxRow ) );
// process gap between two row models, use default row model
if( nNextRow < aRowRange.mnFirst )
convertRows( aRowLevels, ValueRange( nNextRow, aRowRange.mnFirst - 1 ), maDefRowModel );
// process the row model
convertRows( aRowLevels, aRowRange, aIt->second.first, maDefRowModel.mfHeight );
// cache next row to be processed
nNextRow = aRowRange.mnLast + 1;
}
// remaining default rows to end of sheet
convertRows( aRowLevels, ValueRange( nNextRow, nMaxRow ), maDefRowModel );
// close remaining row outlines spanning to end of sheet
convertOutlines( aRowLevels, nMaxRow + 1, 0, false, true );
}
void WorksheetGlobals::convertRows( OutlineLevelVec& orRowLevels,
const ValueRange& rRowRange, const RowModel& rModel, double fDefHeight )
{
// row height: convert points to row height in 1/100 mm
double fHeight = (rModel.mfHeight >= 0.0) ? rModel.mfHeight : fDefHeight;
sal_Int32 nHeight = getUnitConverter().scaleToMm100( fHeight, UNIT_POINT );
SCROW nStartRow = rRowRange.mnFirst;
SCROW nEndRow = rRowRange.mnLast;
SCTAB nTab = getSheetIndex();
if( nHeight > 0 )
{
/* always import the row height, ensures better layout */
ScDocument& rDoc = getScDocument();
rDoc.SetRowHeightOnly( nStartRow, nEndRow, nTab, (sal_uInt16)sc::HMMToTwips(nHeight) );
if(rModel.mbCustomHeight)
rDoc.SetManualHeight( nStartRow, nEndRow, nTab, true );
}
// hidden rows: TODO: #108683# hide rows later?
if( rModel.mbHidden )
{
ScDocument& rDoc = getScDocument();
rDoc.SetRowHidden( nStartRow, nEndRow, nTab, true );
}
// outline settings for this row range
convertOutlines( orRowLevels, rRowRange.mnFirst, rModel.mnLevel, rModel.mbCollapsed, true );
}
void WorksheetGlobals::convertOutlines( OutlineLevelVec& orLevels,
sal_Int32 nColRow, sal_Int32 nLevel, bool bCollapsed, bool bRows )
{
/* It is ensured from caller functions, that this function is called
without any gaps between the processed column or row ranges. */
OSL_ENSURE( nLevel >= 0, "WorksheetGlobals::convertOutlines - negative outline level" );
nLevel = ::std::max< sal_Int32 >( nLevel, 0 );
sal_Int32 nSize = orLevels.size();
if( nSize < nLevel )
{
// Outline level increased. Push the begin column position.
orLevels.insert(orLevels.end(), nLevel - nSize, nColRow);
}
else if( nLevel < nSize )
{
// Outline level decreased. Pop them all out.
for( sal_Int32 nIndex = nLevel; nIndex < nSize; ++nIndex )
{
sal_Int32 nFirstInLevel = orLevels.back();
orLevels.pop_back();
groupColumnsOrRows( nFirstInLevel, nColRow - 1, bCollapsed, bRows );
bCollapsed = false; // collapse only once
}
}
}
void WorksheetGlobals::groupColumnsOrRows( sal_Int32 nFirstColRow, sal_Int32 nLastColRow, bool bCollapse, bool bRows )
{
try
{
Reference< XSheetOutline > xOutline( mxSheet, UNO_QUERY_THROW );
if( bRows )
{
CellRangeAddress aRange( getSheetIndex(), 0, nFirstColRow, 0, nLastColRow );
xOutline->group( aRange, TableOrientation_ROWS );
if( bCollapse )
xOutline->hideDetail( aRange );
}
else
{
CellRangeAddress aRange( getSheetIndex(), nFirstColRow, 0, nLastColRow, 0 );
xOutline->group( aRange, TableOrientation_COLUMNS );
if( bCollapse )
xOutline->hideDetail( aRange );
}
}
catch( Exception& )
{
}
}
void WorksheetGlobals::finalizeDrawings()
{
// calculate the current drawing page size (after rows/columns are imported)
PropertySet aRangeProp( getCellRange( ScRange( 0, 0, getSheetIndex(), mrMaxApiPos.Col(), mrMaxApiPos.Row(), getSheetIndex() ) ) );
aRangeProp.getProperty( maDrawPageSize, PROP_Size );
// import DML and VML
if( !maDrawingPath.isEmpty() )
importOoxFragment( new DrawingFragment( *this, maDrawingPath ) );
if( !maVmlDrawingPath.isEmpty() )
importOoxFragment( new VmlDrawingFragment( *this, maVmlDrawingPath ) );
// comments (after callout shapes have been imported from VML/DFF)
maComments.finalizeImport();
/* Extend used area of the sheet by cells covered with drawing objects.
Needed if the imported document is inserted as "OLE object from file"
and thus does not provide an OLE size property by itself. */
if( (maShapeBoundingBox.Width > 0) || (maShapeBoundingBox.Height > 0) )
extendUsedArea( getCellRangeFromRectangle( maShapeBoundingBox ) );
// if no used area is set, default to A1
if( maUsedArea.aStart.Col() > maUsedArea.aEnd.Col() )
{
maUsedArea.aStart.SetCol( 0 );
maUsedArea.aEnd.SetCol( 0 );
}
if( maUsedArea.aStart.Row() > maUsedArea.aEnd.Row() )
{
maUsedArea.aStart.SetRow( 0 );
maUsedArea.aEnd.SetRow( 0 );
}
/* Register the used area of this sheet in global view settings. The
global view settings will set the visible area if this document is an
embedded OLE object. */
getViewSettings().setSheetUsedArea( maUsedArea );
/* #i103686# Set right-to-left sheet layout. Must be done after all
drawing shapes to simplify calculation of shape coordinates. */
if( maSheetViewSett.isSheetRightToLeft() )
{
PropertySet aPropSet( mxSheet );
aPropSet.setProperty( PROP_TableLayout, WritingMode2::RL_TB );
}
}
WorksheetHelper::WorksheetHelper( WorksheetGlobals& rSheetGlob ) :
WorkbookHelper( rSheetGlob ),
mrSheetGlob( rSheetGlob )
{
}
/*static*/ WorksheetGlobalsRef WorksheetHelper::constructGlobals( const WorkbookHelper& rHelper,
const ISegmentProgressBarRef& rxProgressBar, WorksheetType eSheetType, SCTAB nSheet )
{
WorksheetGlobalsRef xSheetGlob( new WorksheetGlobals( rHelper, rxProgressBar, eSheetType, nSheet ) );
if( !xSheetGlob->isValidSheet() )
xSheetGlob.reset();
return xSheetGlob;
}
/* static */ IWorksheetProgress *WorksheetHelper::getWorksheetInterface( const WorksheetGlobalsRef &xRef )
{
return static_cast< IWorksheetProgress *>( xRef.get() );
}
WorksheetType WorksheetHelper::getSheetType() const
{
return mrSheetGlob.getSheetType();
}
SCTAB WorksheetHelper::getSheetIndex() const
{
return mrSheetGlob.getSheetIndex();
}
const Reference< XSpreadsheet >& WorksheetHelper::getSheet() const
{
return mrSheetGlob.getSheet();
}
Reference< XCell > WorksheetHelper::getCell( const ScAddress& rAddress ) const
{
return mrSheetGlob.getCell( rAddress );
}
Reference< XCellRange > WorksheetHelper::getCellRange( const ScRange& rRange ) const
{
return mrSheetGlob.getCellRange( rRange );
}
Reference< XDrawPage > WorksheetHelper::getDrawPage() const
{
return mrSheetGlob.getDrawPage();
}
awt::Point WorksheetHelper::getCellPosition( sal_Int32 nCol, sal_Int32 nRow ) const
{
return mrSheetGlob.getCellPosition( nCol, nRow );
}
awt::Size WorksheetHelper::getDrawPageSize() const
{
return mrSheetGlob.getDrawPageSize();
}
SheetDataBuffer& WorksheetHelper::getSheetData() const
{
return mrSheetGlob.getSheetData();
}
CondFormatBuffer& WorksheetHelper::getCondFormats() const
{
return mrSheetGlob.getCondFormats();
}
CommentsBuffer& WorksheetHelper::getComments() const
{
return mrSheetGlob.getComments();
}
AutoFilterBuffer& WorksheetHelper::getAutoFilters() const
{
return mrSheetGlob.getAutoFilters();
}
QueryTableBuffer& WorksheetHelper::getQueryTables() const
{
return mrSheetGlob.getQueryTables();
}
WorksheetSettings& WorksheetHelper::getWorksheetSettings() const
{
return mrSheetGlob.getWorksheetSettings();
}
PageSettings& WorksheetHelper::getPageSettings() const
{
return mrSheetGlob.getPageSettings();
}
SheetViewSettings& WorksheetHelper::getSheetViewSettings() const
{
return mrSheetGlob.getSheetViewSettings();
}
VmlDrawing& WorksheetHelper::getVmlDrawing() const
{
return mrSheetGlob.getVmlDrawing();
}
ExtLst& WorksheetHelper::getExtLst() const
{
return mrSheetGlob.getExtLst();
}
void WorksheetHelper::setPageBreak( const PageBreakModel& rModel, bool bRowBreak )
{
mrSheetGlob.setPageBreak( rModel, bRowBreak );
}
void WorksheetHelper::setHyperlink( const HyperlinkModel& rModel )
{
mrSheetGlob.setHyperlink( rModel );
}
void WorksheetHelper::setValidation( const ValidationModel& rModel )
{
mrSheetGlob.setValidation( rModel );
}
void WorksheetHelper::setDrawingPath( const OUString& rDrawingPath )
{
mrSheetGlob.setDrawingPath( rDrawingPath );
}
void WorksheetHelper::setVmlDrawingPath( const OUString& rVmlDrawingPath )
{
mrSheetGlob.setVmlDrawingPath( rVmlDrawingPath );
}
void WorksheetHelper::extendUsedArea( const ScAddress& rAddress )
{
mrSheetGlob.extendUsedArea( rAddress );
}
void WorksheetHelper::extendShapeBoundingBox( const awt::Rectangle& rShapeRect )
{
mrSheetGlob.extendShapeBoundingBox( rShapeRect );
}
void WorksheetHelper::setBaseColumnWidth( sal_Int32 nWidth )
{
mrSheetGlob.setBaseColumnWidth( nWidth );
}
void WorksheetHelper::setDefaultColumnWidth( double fWidth )
{
mrSheetGlob.setDefaultColumnWidth( fWidth );
}
void WorksheetHelper::setColumnModel( const ColumnModel& rModel )
{
mrSheetGlob.setColumnModel( rModel );
}
void WorksheetHelper::setDefaultRowSettings( double fHeight, bool bCustomHeight, bool bHidden, bool bThickTop, bool bThickBottom )
{
mrSheetGlob.setDefaultRowSettings( fHeight, bCustomHeight, bHidden, bThickTop, bThickBottom );
}
void WorksheetHelper::setRowModel( const RowModel& rModel )
{
mrSheetGlob.setRowModel( rModel );
}
void WorksheetHelper::setCellFormulaValue(
const ScAddress& rAddress, const OUString& rValueStr, sal_Int32 nCellType )
{
getFormulaBuffer().setCellFormulaValue(rAddress, rValueStr, nCellType);
}
void WorksheetHelper::putRichString( const ScAddress& rAddress, const RichString& rString, const oox::xls::Font* pFirstPortionFont )
{
ScEditEngineDefaulter& rEE = getEditEngine();
// The cell will own the text object instance returned from convert().
getDocImport().setEditCell(rAddress, rString.convert(rEE, pFirstPortionFont));
}
void WorksheetHelper::putFormulaTokens( const ScAddress& rAddress, const ApiTokenSequence& rTokens )
{
ScDocumentImport& rDoc = getDocImport();
ScTokenArray aTokenArray;
ScTokenConversion::ConvertToTokenArray(rDoc.getDoc(), aTokenArray, rTokens);
rDoc.setFormulaCell(rAddress, new ScTokenArray(aTokenArray));
}
void WorksheetHelper::initializeWorksheetImport()
{
mrSheetGlob.initializeWorksheetImport();
}
void WorksheetHelper::finalizeWorksheetImport()
{
mrSheetGlob.finalizeWorksheetImport();
}
void WorksheetHelper::finalizeDrawingImport()
{
mrSheetGlob.finalizeDrawingImport();
}
void WorksheetHelper::setCellFormula( const ScAddress& rTokenAddress, const OUString& rTokenStr )
{
getFormulaBuffer().setCellFormula( rTokenAddress, rTokenStr );
}
void WorksheetHelper::setCellFormula(
const ScAddress& rAddr, sal_Int32 nSharedId,
const OUString& rCellValue, sal_Int32 nValueType )
{
getFormulaBuffer().setCellFormula(rAddr, nSharedId, rCellValue, nValueType);
}
void WorksheetHelper::setCellArrayFormula( const ScRange& rRangeAddress, const ScAddress& rTokenAddress, const OUString& rTokenStr )
{
getFormulaBuffer().setCellArrayFormula( rRangeAddress, rTokenAddress, rTokenStr );
}
void WorksheetHelper::createSharedFormulaMapEntry(
const ScAddress& rAddress, sal_Int32 nSharedId, const OUString& rTokens )
{
getFormulaBuffer().createSharedFormulaMapEntry(rAddress, nSharedId, rTokens);
}
} // namespace xls
} // namespace oox
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|