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
|
/* -*- 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 "xilink.hxx"
#include "document.hxx"
#include "formulacell.hxx"
#include "scextopt.hxx"
#include "tablink.hxx"
#include "xistream.hxx"
#include "xihelper.hxx"
#include "xiname.hxx"
#include "excform.hxx"
#include "tokenarray.hxx"
#include "externalrefmgr.hxx"
#include "scmatrix.hxx"
#include <svl/sharedstringpool.hxx>
#include <o3tl/make_unique.hxx>
#include <vector>
#include <memory>
// *** Helper classes ***
// Cached external cells ======================================================
/**
* Contains the address and value of an external referenced cell.
* Note that this is non-copyable, so cannot be used in most stl/boost containers.
*/
class XclImpCrn : public XclImpCachedValue
{
public:
/** Reads a cached value and stores it with its cell address. */
explicit XclImpCrn( XclImpStream& rStrm, const XclAddress& rXclPos );
const XclAddress& GetAddress() const { return maXclPos;}
private:
XclAddress maXclPos; /// Excel position of the cached cell.
};
// Sheet in an external document ==============================================
/** Contains the name and sheet index of one sheet in an external document. */
class XclImpSupbookTab
{
public:
/** Stores the sheet name and marks the sheet index as invalid.
The sheet index is set while creating the Calc sheet with CreateTable(). */
explicit XclImpSupbookTab( const OUString& rTabName );
~XclImpSupbookTab();
inline const OUString& GetTabName() const { return maTabName; }
/** Reads a CRN record (external referenced cell) at the specified address. */
void ReadCrn( XclImpStream& rStrm, const XclAddress& rXclPos );
void LoadCachedValues(ScExternalRefCache::TableTypeRef pCacheTable);
private:
typedef std::shared_ptr< XclImpCrn > XclImpCrnRef;
typedef std::vector< XclImpCrnRef > XclImpCrnList;
XclImpCrnList maCrnList; /// List of CRN records (cached cell values).
OUString maTabName; /// Name of the external sheet.
};
// External document (SUPBOOK) ================================================
/** This class represents an external linked document (record SUPBOOK).
@descr Contains a list of all referenced sheets in the document. */
class XclImpSupbook : protected XclImpRoot
{
public:
/** Reads the SUPBOOK record from stream. */
explicit XclImpSupbook( XclImpStream& rStrm );
/** Reads an XCT record (count of following CRNs and current sheet). */
void ReadXct( XclImpStream& rStrm );
/** Reads a CRN record (external referenced cell). */
void ReadCrn( XclImpStream& rStrm );
/** Reads an EXTERNNAME record. */
void ReadExternname( XclImpStream& rStrm, ExcelToSc* pFormulaConv = nullptr );
/** Returns the SUPBOOK record type. */
inline XclSupbookType GetType() const { return meType; }
/** Returns the URL of the external document. */
inline const OUString& GetXclUrl() const { return maXclUrl; }
/** Returns the external name specified by an index from the Excel document (one-based). */
const XclImpExtName* GetExternName( sal_uInt16 nXclIndex ) const;
/** Tries to decode the URL to OLE or DDE link components.
@descr For DDE links: Decodes to application name and topic.
For OLE object links: Decodes to class name and document URL.
@return true = decoding was successful, returned strings are valid (not empty). */
bool GetLinkData( OUString& rApplic, OUString& rDoc ) const;
/** Returns the specified macro name (1-based) or an empty string on error. */
const OUString& GetMacroName( sal_uInt16 nXclNameIdx ) const;
const OUString& GetTabName( sal_uInt16 nXtiTab ) const;
sal_uInt16 GetTabCount() const;
void LoadCachedValues();
svl::SharedStringPool& GetSharedStringPool();
private:
typedef std::vector< std::unique_ptr<XclImpSupbookTab> > XclImpSupbookTabList;
typedef std::vector< std::unique_ptr<XclImpExtName> > XclImpExtNameList;
XclImpSupbookTabList maSupbTabList; /// All sheet names of the document.
XclImpExtNameList maExtNameList; /// All external names of the document.
OUString maXclUrl; /// URL of the external document (Excel mode).
XclSupbookType meType; /// Type of the supbook record.
sal_uInt16 mnSBTab; /// Current Excel sheet index from SUPBOOK for XCT/CRN records.
};
// Import link manager ========================================================
/** Contains the SUPBOOK index and sheet indexes of an external link.
@descr It is possible to enter a formula like =SUM(Sheet1:Sheet3!A1),
therefore here occurs a sheet range. */
struct XclImpXti
{
sal_uInt16 mnSupbook; /// Index to SUPBOOK record.
sal_uInt16 mnSBTabFirst; /// Index to the first sheet of the range in the SUPBOOK.
sal_uInt16 mnSBTabLast; /// Index to the last sheet of the range in the SUPBOOK.
inline explicit XclImpXti() : mnSupbook( SAL_MAX_UINT16 ), mnSBTabFirst( SAL_MAX_UINT16 ), mnSBTabLast( SAL_MAX_UINT16 ) {}
};
inline XclImpStream& operator>>( XclImpStream& rStrm, XclImpXti& rXti )
{
rXti.mnSupbook = rStrm.ReaduInt16();
rXti.mnSBTabFirst = rStrm.ReaduInt16();
rXti.mnSBTabLast = rStrm.ReaduInt16();
return rStrm;
}
/** Implementation of the link manager. */
class XclImpLinkManagerImpl : protected XclImpRoot
{
public:
explicit XclImpLinkManagerImpl( const XclImpRoot& rRoot );
/** Reads the EXTERNSHEET record. */
void ReadExternsheet( XclImpStream& rStrm );
/** Reads a SUPBOOK record. */
void ReadSupbook( XclImpStream& rStrm );
/** Reads an XCT record and appends it to the current SUPBOOK. */
void ReadXct( XclImpStream& rStrm );
/** Reads a CRN record and appends it to the current SUPBOOK. */
void ReadCrn( XclImpStream& rStrm );
/** Reads an EXTERNNAME record and appends it to the current SUPBOOK. */
void ReadExternname( XclImpStream& rStrm, ExcelToSc* pFormulaConv = nullptr );
/** Returns true, if the specified XTI entry contains an internal reference. */
bool IsSelfRef( sal_uInt16 nXtiIndex ) const;
/** Returns the Calc sheet index range of the specified XTI entry.
@return true = XTI data found, returned sheet index range is valid. */
bool GetScTabRange(
SCTAB& rnFirstScTab, SCTAB& rnLastScTab,
sal_uInt16 nXtiIndex ) const;
/** Returns the specified external name or 0 on error. */
const XclImpExtName* GetExternName( sal_uInt16 nXtiIndex, sal_uInt16 nExtName ) const;
/** Returns the absolute file URL of a supporting workbook specified by
the index. */
const OUString* GetSupbookUrl( sal_uInt16 nXtiIndex ) const;
const OUString& GetSupbookTabName( sal_uInt16 nXti, sal_uInt16 nXtiTab ) const;
/** Tries to decode the URL of the specified XTI entry to OLE or DDE link components.
@descr For DDE links: Decodes to application name and topic.
For OLE object links: Decodes to class name and document URL.
@return true = decoding was successful, returned strings are valid (not empty). */
bool GetLinkData( OUString& rApplic, OUString& rTopic, sal_uInt16 nXtiIndex ) const;
/** Returns the specified macro name or an empty string on error. */
const OUString& GetMacroName( sal_uInt16 nExtSheet, sal_uInt16 nExtName ) const;
private:
/** Returns the specified XTI (link entry from BIFF8 EXTERNSHEET record). */
const XclImpXti* GetXti( sal_uInt16 nXtiIndex ) const;
/** Returns the specified SUPBOOK (external document). */
const XclImpSupbook* GetSupbook( sal_uInt16 nXtiIndex ) const;
void LoadCachedValues();
private:
typedef std::vector< XclImpXti > XclImpXtiVector;
typedef std::vector< std::unique_ptr<XclImpSupbook> > XclImpSupbookList;
XclImpXtiVector maXtiList; /// List of all XTI structures.
XclImpSupbookList maSupbookList; /// List of external documents.
};
// *** Implementation ***
// Excel sheet indexes ========================================================
// original Excel sheet names -------------------------------------------------
void XclImpTabInfo::AppendXclTabName( const OUString& rXclTabName, SCTAB nScTab )
{
maTabNames[ rXclTabName ] = nScTab;
}
void XclImpTabInfo::InsertScTab( SCTAB nScTab )
{
for( XclTabNameMap::iterator aIt = maTabNames.begin(), aEnd = maTabNames.end(); aIt != aEnd; ++aIt )
if( aIt->second >= nScTab )
++aIt->second;
}
SCTAB XclImpTabInfo::GetScTabFromXclName( const OUString& rXclTabName ) const
{
XclTabNameMap::const_iterator aIt = maTabNames.find( rXclTabName );
return (aIt != maTabNames.end()) ? aIt->second : SCTAB_INVALID;
}
// record creation order - TABID record ---------------------------------------
void XclImpTabInfo::ReadTabid( XclImpStream& rStrm )
{
OSL_ENSURE_BIFF( rStrm.GetRoot().GetBiff() == EXC_BIFF8 );
if( rStrm.GetRoot().GetBiff() == EXC_BIFF8 )
{
rStrm.EnableDecryption();
sal_Size nReadCount = rStrm.GetRecLeft() / 2;
OSL_ENSURE( nReadCount <= 0xFFFF, "XclImpTabInfo::ReadTabid - record too long" );
maTabIdVec.clear();
maTabIdVec.reserve( nReadCount );
for( sal_Size nIndex = 0; rStrm.IsValid() && (nIndex < nReadCount); ++nIndex )
// zero index is not allowed in BIFF8, but it seems that it occurs in real life
maTabIdVec.push_back( rStrm.ReaduInt16() );
}
}
sal_uInt16 XclImpTabInfo::GetCurrentIndex( sal_uInt16 nCreatedId, sal_uInt16 nMaxTabId ) const
{
sal_uInt16 nReturn = 0;
for( ScfUInt16Vec::const_iterator aIt = maTabIdVec.begin(), aEnd = maTabIdVec.end(); aIt != aEnd; ++aIt )
{
sal_uInt16 nValue = *aIt;
if( nValue == nCreatedId )
return nReturn;
if( nValue <= nMaxTabId )
++nReturn;
}
return 0;
}
// External names =============================================================
XclImpExtName::MOper::MOper(svl::SharedStringPool& rPool, XclImpStream& rStrm) :
mxCached(new ScFullMatrix(0,0))
{
SCSIZE nLastCol = rStrm.ReaduInt8();
SCSIZE nLastRow = rStrm.ReaduInt16();
//assuming worse case scenario of nOp + one byte unistring len
const size_t nMinRecordSize = 2;
const size_t nMaxRows = rStrm.GetRecLeft() / (nMinRecordSize * (nLastCol+1));
if (nLastRow >= nMaxRows)
{
SAL_WARN("sc", "Parsing error: " << nMaxRows <<
" max possible rows, but " << nLastRow << " index claimed, truncating");
nLastRow = nMaxRows-1;
}
mxCached->Resize(nLastCol+1, nLastRow+1);
for (SCSIZE nRow = 0; nRow <= nLastRow; ++nRow)
{
for (SCSIZE nCol = 0; nCol <= nLastCol; ++nCol)
{
sal_uInt8 nOp;
nOp = rStrm.ReaduInt8();
switch (nOp)
{
case 0x01:
{
double fVal = rStrm.ReadDouble();
mxCached->PutDouble(fVal, nCol, nRow);
}
break;
case 0x02:
{
OUString aStr = rStrm.ReadUniString();
mxCached->PutString(rPool.intern(aStr), nCol, nRow);
}
break;
case 0x04:
{
bool bVal = rStrm.ReaduInt8();
mxCached->PutBoolean(bVal, nCol, nRow);
rStrm.Ignore(7);
}
break;
case 0x10:
{
sal_uInt8 nErr = rStrm.ReaduInt8();
// TODO: Map the error code from xls to calc.
mxCached->PutError(nErr, nCol, nRow);
rStrm.Ignore(7);
}
break;
default:
rStrm.Ignore(8);
}
}
}
}
const ScMatrix& XclImpExtName::MOper::GetCache() const
{
return *mxCached;
}
XclImpExtName::XclImpExtName( XclImpSupbook& rSupbook, XclImpStream& rStrm, XclSupbookType eSubType, ExcelToSc* pFormulaConv )
: mpMOper(nullptr)
, mnStorageId(0)
{
sal_uInt16 nFlags(0);
sal_uInt8 nLen(0);
nFlags = rStrm.ReaduInt16();
mnStorageId = rStrm.ReaduInt32();
nLen = rStrm.ReaduInt8();
maName = rStrm.ReadUniString( nLen );
if( ::get_flag( nFlags, EXC_EXTN_BUILTIN ) || !::get_flag( nFlags, EXC_EXTN_OLE_OR_DDE ) )
{
if( eSubType == EXC_SBTYPE_ADDIN )
{
meType = xlExtAddIn;
maName = XclImpRoot::GetScAddInName( maName );
}
else if ( (eSubType == EXC_SBTYPE_EUROTOOL) &&
maName.equalsIgnoreAsciiCase( "EUROCONVERT" ) )
meType = xlExtEuroConvert;
else
{
meType = xlExtName;
maName = ScfTools::ConvertToScDefinedName( maName );
}
}
else
{
meType = ::get_flagvalue( nFlags, EXC_EXTN_OLE, xlExtOLE, xlExtDDE );
}
switch (meType)
{
case xlExtDDE:
if (rStrm.GetRecLeft() > 1)
mxDdeMatrix.reset(new XclImpCachedMatrix(rStrm));
break;
case xlExtName:
// TODO: For now, only global external names are supported. In future
// we should extend this to supporting per-sheet external names.
if (mnStorageId == 0)
{
if (pFormulaConv)
{
const ScTokenArray* pArray = nullptr;
sal_uInt16 nFmlaLen;
nFmlaLen = rStrm.ReaduInt16();
std::vector<OUString> aTabNames;
sal_uInt16 nCount = rSupbook.GetTabCount();
aTabNames.reserve(nCount);
for (sal_uInt16 i = 0; i < nCount; ++i)
aTabNames.push_back(rSupbook.GetTabName(i));
pFormulaConv->ConvertExternName(pArray, rStrm, nFmlaLen, rSupbook.GetXclUrl(), aTabNames);
if (pArray)
mxArray.reset(pArray->Clone());
}
}
break;
case xlExtOLE:
mpMOper = new MOper(rSupbook.GetSharedStringPool(), rStrm);
break;
default:
;
}
}
XclImpExtName::~XclImpExtName()
{
delete mpMOper;
}
void XclImpExtName::CreateDdeData( ScDocument& rDoc, const OUString& rApplic, const OUString& rTopic ) const
{
ScMatrixRef xResults;
if( mxDdeMatrix.get() )
xResults = mxDdeMatrix->CreateScMatrix(rDoc.GetSharedStringPool());
rDoc.CreateDdeLink( rApplic, rTopic, maName, SC_DDE_DEFAULT, xResults );
}
void XclImpExtName::CreateExtNameData( ScDocument& rDoc, sal_uInt16 nFileId ) const
{
if (!mxArray.get())
return;
ScExternalRefManager* pRefMgr = rDoc.GetExternalRefManager();
pRefMgr->storeRangeNameTokens(nFileId, maName, *mxArray);
}
namespace {
/**
* Decompose the name into sheet name and range name. An OLE link name is
* always formatted like this [ !Sheet1!R1C1:R5C2 ] and it always uses R1C1
* notation.
*/
bool extractSheetAndRange(const OUString& rName, OUString& rSheet, OUString& rRange)
{
sal_Int32 n = rName.getLength();
const sal_Unicode* p = rName.getStr();
OUStringBuffer aBuf;
bool bInSheet = true;
for (sal_Int32 i = 0; i < n; ++i, ++p)
{
if (i == 0)
{
// first character must be '!'.
if (*p != '!')
return false;
continue;
}
if (*p == '!')
{
// sheet name to range separator.
if (!bInSheet)
return false;
rSheet = aBuf.makeStringAndClear();
bInSheet = false;
continue;
}
aBuf.append(*p);
}
rRange = aBuf.makeStringAndClear();
return true;
}
}
bool XclImpExtName::CreateOleData(ScDocument& rDoc, const OUString& rUrl,
sal_uInt16& rFileId, OUString& rTabName, ScRange& rRange) const
{
if (!mpMOper)
return false;
OUString aSheet, aRangeStr;
if (!extractSheetAndRange(maName, aSheet, aRangeStr))
return false;
ScRange aRange;
sal_uInt16 nRes = aRange.ParseAny(aRangeStr, &rDoc, formula::FormulaGrammar::CONV_XL_R1C1);
if ((nRes & SCA_VALID) != SCA_VALID)
return false;
if (aRange.aStart.Tab() != aRange.aEnd.Tab())
// We don't support multi-sheet range for this.
return false;
const ScMatrix& rCache = mpMOper->GetCache();
SCSIZE nC, nR;
rCache.GetDimensions(nC, nR);
if (!nC || !nR)
// cache matrix is empty.
return false;
ScExternalRefManager* pRefMgr = rDoc.GetExternalRefManager();
sal_uInt16 nFileId = pRefMgr->getExternalFileId(rUrl);
ScExternalRefCache::TableTypeRef xTab = pRefMgr->getCacheTable(nFileId, aSheet, true);
if (!xTab)
// cache table creation failed.
return false;
xTab->setWholeTableCached();
for (SCSIZE i = 0; i < nR; ++i)
{
for (SCSIZE j = 0; j < nC; ++j)
{
SCCOL nCol = aRange.aStart.Col() + j;
SCROW nRow = aRange.aStart.Row() + i;
ScMatrixValue aVal = rCache.Get(j, i);
switch (aVal.nType)
{
case SC_MATVAL_BOOLEAN:
{
bool b = aVal.GetBoolean();
ScExternalRefCache::TokenRef pToken(new formula::FormulaDoubleToken(b ? 1.0 : 0.0));
xTab->setCell(nCol, nRow, pToken, 0, false);
}
break;
case SC_MATVAL_VALUE:
{
ScExternalRefCache::TokenRef pToken(new formula::FormulaDoubleToken(aVal.fVal));
xTab->setCell(nCol, nRow, pToken, 0, false);
}
break;
case SC_MATVAL_STRING:
{
const svl::SharedString aStr( aVal.GetString());
ScExternalRefCache::TokenRef pToken(new formula::FormulaStringToken(aStr));
xTab->setCell(nCol, nRow, pToken, 0, false);
}
break;
default:
;
}
}
}
rFileId = nFileId;
rTabName = aSheet;
rRange = aRange;
return true;
}
bool XclImpExtName::HasFormulaTokens() const
{
return (mxArray.get() != nullptr);
}
// Cached external cells ======================================================
XclImpCrn::XclImpCrn( XclImpStream& rStrm, const XclAddress& rXclPos ) :
XclImpCachedValue( rStrm ),
maXclPos( rXclPos )
{
}
// Sheet in an external document ==============================================
XclImpSupbookTab::XclImpSupbookTab( const OUString& rTabName ) :
maTabName( rTabName )
{
}
XclImpSupbookTab::~XclImpSupbookTab()
{
}
void XclImpSupbookTab::ReadCrn( XclImpStream& rStrm, const XclAddress& rXclPos )
{
XclImpCrnRef crnRef( new XclImpCrn(rStrm, rXclPos) );
maCrnList.push_back( crnRef );
}
void XclImpSupbookTab::LoadCachedValues(ScExternalRefCache::TableTypeRef pCacheTable)
{
if (maCrnList.empty())
return;
for (XclImpCrnList::iterator itCrnRef = maCrnList.begin(); itCrnRef != maCrnList.end(); ++itCrnRef)
{
const XclImpCrn* const pCrn = itCrnRef->get();
const XclAddress& rAddr = pCrn->GetAddress();
switch (pCrn->GetType())
{
case EXC_CACHEDVAL_BOOL:
{
bool b = pCrn->GetBool();
ScExternalRefCache::TokenRef pToken(new formula::FormulaDoubleToken(b ? 1.0 : 0.0));
pCacheTable->setCell(rAddr.mnCol, rAddr.mnRow, pToken, 0, false);
}
break;
case EXC_CACHEDVAL_DOUBLE:
{
double f = pCrn->GetValue();
ScExternalRefCache::TokenRef pToken(new formula::FormulaDoubleToken(f));
pCacheTable->setCell(rAddr.mnCol, rAddr.mnRow, pToken, 0, false);
}
break;
case EXC_CACHEDVAL_ERROR:
{
double fError = XclTools::ErrorToDouble( pCrn->GetXclError() );
ScExternalRefCache::TokenRef pToken(new formula::FormulaDoubleToken(fError));
pCacheTable->setCell(rAddr.mnCol, rAddr.mnRow, pToken, 0, false);
}
break;
case EXC_CACHEDVAL_STRING:
{
const OUString& rStr = pCrn->GetString();
ScExternalRefCache::TokenRef pToken(new formula::FormulaStringToken(rStr));
pCacheTable->setCell(rAddr.mnCol, rAddr.mnRow, pToken, 0, false);
}
break;
default:
;
}
}
}
// External document (SUPBOOK) ================================================
XclImpSupbook::XclImpSupbook( XclImpStream& rStrm ) :
XclImpRoot( rStrm.GetRoot() ),
meType( EXC_SBTYPE_UNKNOWN ),
mnSBTab( EXC_TAB_DELETED )
{
sal_uInt16 nSBTabCnt;
nSBTabCnt = rStrm.ReaduInt16();
if( rStrm.GetRecLeft() == 2 )
{
switch( rStrm.ReaduInt16() )
{
case EXC_SUPB_SELF: meType = EXC_SBTYPE_SELF; break;
case EXC_SUPB_ADDIN: meType = EXC_SBTYPE_ADDIN; break;
default: OSL_FAIL( "XclImpSupbook::XclImpSupbook - unknown special SUPBOOK type" );
}
return;
}
OUString aEncUrl( rStrm.ReadUniString() );
bool bSelf = false;
XclImpUrlHelper::DecodeUrl( maXclUrl, bSelf, GetRoot(), aEncUrl );
if( maXclUrl.equalsIgnoreAsciiCase( "\010EUROTOOL.XLA" ) )
{
meType = EXC_SBTYPE_EUROTOOL;
maSupbTabList.push_back( o3tl::make_unique<XclImpSupbookTab>( maXclUrl ) );
}
else if( nSBTabCnt )
{
meType = EXC_SBTYPE_EXTERN;
//assuming all empty strings with just len header of 0
const size_t nMinRecordSize = sizeof(sal_Int16);
const size_t nMaxRecords = rStrm.GetRecLeft() / nMinRecordSize;
if (nSBTabCnt > nMaxRecords)
{
SAL_WARN("sc", "Parsing error: " << nMaxRecords <<
" max possible entries, but " << nSBTabCnt << " claimed, truncating");
nSBTabCnt = nMaxRecords;
}
for( sal_uInt16 nSBTab = 0; nSBTab < nSBTabCnt; ++nSBTab )
{
OUString aTabName( rStrm.ReadUniString() );
maSupbTabList.push_back( o3tl::make_unique<XclImpSupbookTab>( aTabName ) );
}
}
else
{
meType = EXC_SBTYPE_SPECIAL;
// create dummy list entry
maSupbTabList.push_back( o3tl::make_unique<XclImpSupbookTab>( maXclUrl ) );
}
}
void XclImpSupbook::ReadXct( XclImpStream& rStrm )
{
rStrm.Ignore( 2 );
mnSBTab = rStrm.ReaduInt16();
}
void XclImpSupbook::ReadCrn( XclImpStream& rStrm )
{
if (mnSBTab >= maSupbTabList.size())
return;
XclImpSupbookTab& rSbTab = *maSupbTabList[mnSBTab].get();
sal_uInt8 nXclColLast, nXclColFirst;
sal_uInt16 nXclRow;
nXclColLast = rStrm.ReaduInt8();
nXclColFirst = rStrm.ReaduInt8();
nXclRow = rStrm.ReaduInt16();
for( sal_uInt8 nXclCol = nXclColFirst; (nXclCol <= nXclColLast) && (rStrm.GetRecLeft() > 1); ++nXclCol )
rSbTab.ReadCrn( rStrm, XclAddress( nXclCol, nXclRow ) );
}
void XclImpSupbook::ReadExternname( XclImpStream& rStrm, ExcelToSc* pFormulaConv )
{
maExtNameList.push_back( o3tl::make_unique<XclImpExtName>( *this, rStrm, meType, pFormulaConv ) );
}
const XclImpExtName* XclImpSupbook::GetExternName( sal_uInt16 nXclIndex ) const
{
if (nXclIndex == 0)
{
SAL_WARN("sc", "XclImpSupbook::GetExternName - index must be >0");
return nullptr;
}
if (meType == EXC_SBTYPE_SELF || nXclIndex > maExtNameList.size())
return nullptr;
return maExtNameList[nXclIndex-1].get();
}
bool XclImpSupbook::GetLinkData( OUString& rApplic, OUString& rTopic ) const
{
return (meType == EXC_SBTYPE_SPECIAL) && XclImpUrlHelper::DecodeLink( rApplic, rTopic, maXclUrl );
}
const OUString& XclImpSupbook::GetMacroName( sal_uInt16 nXclNameIdx ) const
{
OSL_ENSURE( nXclNameIdx > 0, "XclImpSupbook::GetMacroName - index must be >0" );
const XclImpName* pName = (meType == EXC_SBTYPE_SELF) ? GetNameManager().GetName( nXclNameIdx ) : nullptr;
return (pName && pName->IsVBName()) ? pName->GetScName() : EMPTY_OUSTRING;
}
const OUString& XclImpSupbook::GetTabName( sal_uInt16 nXtiTab ) const
{
if (nXtiTab >= maSupbTabList.size())
return EMPTY_OUSTRING;
return maSupbTabList[nXtiTab]->GetTabName();
}
sal_uInt16 XclImpSupbook::GetTabCount() const
{
return ulimit_cast<sal_uInt16>(maSupbTabList.size());
}
void XclImpSupbook::LoadCachedValues()
{
if (meType != EXC_SBTYPE_EXTERN || GetExtDocOptions().GetDocSettings().mnLinkCnt > 0 || !GetDocShell())
return;
OUString aAbsUrl( ScGlobal::GetAbsDocName(maXclUrl, GetDocShell()) );
ScExternalRefManager* pRefMgr = GetRoot().GetDoc().GetExternalRefManager();
sal_uInt16 nFileId = pRefMgr->getExternalFileId(aAbsUrl);
for (XclImpSupbookTabList::iterator itTab = maSupbTabList.begin(); itTab != maSupbTabList.end(); ++itTab)
{
const OUString& rTabName = (*itTab)->GetTabName();
ScExternalRefCache::TableTypeRef pCacheTable = pRefMgr->getCacheTable(nFileId, rTabName, true);
(*itTab)->LoadCachedValues(pCacheTable);
pCacheTable->setWholeTableCached();
}
}
svl::SharedStringPool& XclImpSupbook::GetSharedStringPool()
{
return GetDoc().GetSharedStringPool();
}
// Import link manager ========================================================
XclImpLinkManagerImpl::XclImpLinkManagerImpl( const XclImpRoot& rRoot ) :
XclImpRoot( rRoot )
{
}
void XclImpLinkManagerImpl::ReadExternsheet( XclImpStream& rStrm )
{
sal_uInt16 nXtiCount;
nXtiCount = rStrm.ReaduInt16();
OSL_ENSURE( static_cast< sal_Size >( nXtiCount * 6 ) == rStrm.GetRecLeft(), "XclImpLinkManagerImpl::ReadExternsheet - invalid count" );
nXtiCount = static_cast< sal_uInt16 >( ::std::min< sal_Size >( nXtiCount, rStrm.GetRecLeft() / 6 ) );
/* #i104057# A weird external XLS generator writes multiple EXTERNSHEET
records instead of only one as expected. Surprisingly, Excel seems to
insert the entries of the second record before the entries of the first
record. */
XclImpXtiVector aNewEntries( nXtiCount );
for( XclImpXtiVector::iterator aIt = aNewEntries.begin(), aEnd = aNewEntries.end(); rStrm.IsValid() && (aIt != aEnd); ++aIt )
rStrm >> *aIt;
maXtiList.insert( maXtiList.begin(), aNewEntries.begin(), aNewEntries.end() );
LoadCachedValues();
}
void XclImpLinkManagerImpl::ReadSupbook( XclImpStream& rStrm )
{
maSupbookList.push_back( o3tl::make_unique<XclImpSupbook>( rStrm ) );
}
void XclImpLinkManagerImpl::ReadXct( XclImpStream& rStrm )
{
if( !maSupbookList.empty() )
maSupbookList.back()->ReadXct( rStrm );
}
void XclImpLinkManagerImpl::ReadCrn( XclImpStream& rStrm )
{
if( !maSupbookList.empty() )
maSupbookList.back()->ReadCrn( rStrm );
}
void XclImpLinkManagerImpl::ReadExternname( XclImpStream& rStrm, ExcelToSc* pFormulaConv )
{
if( !maSupbookList.empty() )
maSupbookList.back()->ReadExternname( rStrm, pFormulaConv );
}
bool XclImpLinkManagerImpl::IsSelfRef( sal_uInt16 nXtiIndex ) const
{
const XclImpSupbook* pSupbook = GetSupbook( nXtiIndex );
return pSupbook && (pSupbook->GetType() == EXC_SBTYPE_SELF);
}
bool XclImpLinkManagerImpl::GetScTabRange(
SCTAB& rnFirstScTab, SCTAB& rnLastScTab, sal_uInt16 nXtiIndex ) const
{
if( const XclImpXti* pXti = GetXti( nXtiIndex ) )
{
if (!maSupbookList.empty() && (pXti->mnSupbook < maSupbookList.size()) )
{
rnFirstScTab = pXti->mnSBTabFirst;
rnLastScTab = pXti->mnSBTabLast;
return true;
}
}
return false;
}
const XclImpExtName* XclImpLinkManagerImpl::GetExternName( sal_uInt16 nXtiIndex, sal_uInt16 nExtName ) const
{
const XclImpSupbook* pSupbook = GetSupbook( nXtiIndex );
return pSupbook ? pSupbook->GetExternName( nExtName ) : nullptr;
}
const OUString* XclImpLinkManagerImpl::GetSupbookUrl( sal_uInt16 nXtiIndex ) const
{
const XclImpSupbook* p = GetSupbook( nXtiIndex );
if (!p)
return nullptr;
return &p->GetXclUrl();
}
const OUString& XclImpLinkManagerImpl::GetSupbookTabName( sal_uInt16 nXti, sal_uInt16 nXtiTab ) const
{
const XclImpSupbook* p = GetSupbook(nXti);
return p ? p->GetTabName(nXtiTab) : EMPTY_OUSTRING;
}
bool XclImpLinkManagerImpl::GetLinkData( OUString& rApplic, OUString& rTopic, sal_uInt16 nXtiIndex ) const
{
const XclImpSupbook* pSupbook = GetSupbook( nXtiIndex );
return pSupbook && pSupbook->GetLinkData( rApplic, rTopic );
}
const OUString& XclImpLinkManagerImpl::GetMacroName( sal_uInt16 nExtSheet, sal_uInt16 nExtName ) const
{
const XclImpSupbook* pSupbook = GetSupbook( nExtSheet );
return pSupbook ? pSupbook->GetMacroName( nExtName ) : EMPTY_OUSTRING;
}
const XclImpXti* XclImpLinkManagerImpl::GetXti( sal_uInt16 nXtiIndex ) const
{
return (nXtiIndex < maXtiList.size()) ? &maXtiList[ nXtiIndex ] : nullptr;
}
const XclImpSupbook* XclImpLinkManagerImpl::GetSupbook( sal_uInt16 nXtiIndex ) const
{
if ( maSupbookList.empty() )
return nullptr;
const XclImpXti* pXti = GetXti( nXtiIndex );
if (!pXti || pXti->mnSupbook >= maSupbookList.size())
return nullptr;
return maSupbookList.at( pXti->mnSupbook ).get();
}
void XclImpLinkManagerImpl::LoadCachedValues()
{
// Read all CRN records which can be accessed via XclImpSupbook, and store
// the cached values to the external reference manager.
for (XclImpSupbookList::iterator itSupbook = maSupbookList.begin(); itSupbook != maSupbookList.end(); ++itSupbook)
(*itSupbook)->LoadCachedValues();
}
XclImpLinkManager::XclImpLinkManager( const XclImpRoot& rRoot ) :
XclImpRoot( rRoot ),
mxImpl( new XclImpLinkManagerImpl( rRoot ) )
{
}
XclImpLinkManager::~XclImpLinkManager()
{
}
void XclImpLinkManager::ReadExternsheet( XclImpStream& rStrm )
{
mxImpl->ReadExternsheet( rStrm );
}
void XclImpLinkManager::ReadSupbook( XclImpStream& rStrm )
{
mxImpl->ReadSupbook( rStrm );
}
void XclImpLinkManager::ReadXct( XclImpStream& rStrm )
{
mxImpl->ReadXct( rStrm );
}
void XclImpLinkManager::ReadCrn( XclImpStream& rStrm )
{
mxImpl->ReadCrn( rStrm );
}
void XclImpLinkManager::ReadExternname( XclImpStream& rStrm, ExcelToSc* pFormulaConv )
{
mxImpl->ReadExternname( rStrm, pFormulaConv );
}
bool XclImpLinkManager::IsSelfRef( sal_uInt16 nXtiIndex ) const
{
return mxImpl->IsSelfRef( nXtiIndex );
}
bool XclImpLinkManager::GetScTabRange(
SCTAB& rnFirstScTab, SCTAB& rnLastScTab, sal_uInt16 nXtiIndex ) const
{
return mxImpl->GetScTabRange( rnFirstScTab, rnLastScTab, nXtiIndex );
}
const XclImpExtName* XclImpLinkManager::GetExternName( sal_uInt16 nXtiIndex, sal_uInt16 nExtName ) const
{
return mxImpl->GetExternName( nXtiIndex, nExtName );
}
const OUString* XclImpLinkManager::GetSupbookUrl( sal_uInt16 nXtiIndex ) const
{
return mxImpl->GetSupbookUrl(nXtiIndex);
}
const OUString& XclImpLinkManager::GetSupbookTabName( sal_uInt16 nXti, sal_uInt16 nXtiTab ) const
{
return mxImpl->GetSupbookTabName(nXti, nXtiTab);
}
bool XclImpLinkManager::GetLinkData( OUString& rApplic, OUString& rTopic, sal_uInt16 nXtiIndex ) const
{
return mxImpl->GetLinkData( rApplic, rTopic, nXtiIndex );
}
const OUString& XclImpLinkManager::GetMacroName( sal_uInt16 nExtSheet, sal_uInt16 nExtName ) const
{
return mxImpl->GetMacroName( nExtSheet, nExtName );
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|