Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / sc / source / filter / excel / excimp8.cxx
blob71551f452e992491774e8113252a61c2e485e271
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <config_features.h>
22 #include "excimp8.hxx"
24 #include <scitems.hxx>
25 #include <comphelper/processfactory.hxx>
26 #include <comphelper/sequence.hxx>
27 #include <unotools/fltrcfg.hxx>
29 #include <vcl/wmf.hxx>
31 #include <editeng/eeitem.hxx>
33 #include <sfx2/docfile.hxx>
34 #include <sfx2/objsh.hxx>
35 #include <sfx2/request.hxx>
36 #include <sfx2/app.hxx>
37 #include <sfx2/docinf.hxx>
38 #include <sfx2/frame.hxx>
40 #include <editeng/brushitem.hxx>
41 #include <editeng/editdata.hxx>
42 #include <editeng/editeng.hxx>
43 #include <editeng/editobj.hxx>
44 #include <editeng/editstat.hxx>
45 #include <editeng/colritem.hxx>
46 #include <editeng/udlnitem.hxx>
47 #include <editeng/wghtitem.hxx>
48 #include <editeng/postitem.hxx>
49 #include <editeng/crossedoutitem.hxx>
50 #include <editeng/flditem.hxx>
51 #include <svx/xflclit.hxx>
53 #include <vcl/graph.hxx>
54 #include <vcl/bitmapaccess.hxx>
55 #include <sot/exchange.hxx>
57 #include <svl/stritem.hxx>
58 #include <svl/sharedstringpool.hxx>
60 #include <rtl/math.hxx>
61 #include <rtl/ustring.hxx>
62 #include <unotools/localedatawrapper.hxx>
63 #include <unotools/charclass.hxx>
64 #include <drwlayer.hxx>
66 #include "formulacell.hxx"
67 #include "document.hxx"
68 #include "patattr.hxx"
69 #include "docpool.hxx"
70 #include "attrib.hxx"
71 #include "conditio.hxx"
72 #include "dbdata.hxx"
73 #include "globalnames.hxx"
74 #include "editutil.hxx"
75 #include "markdata.hxx"
76 #include "rangenam.hxx"
77 #include "docoptio.hxx"
78 #include "globstr.hrc"
79 #include "fprogressbar.hxx"
80 #include "xltracer.hxx"
81 #include "xihelper.hxx"
82 #include "xipage.hxx"
83 #include "xicontent.hxx"
84 #include "xilink.hxx"
85 #include "xiescher.hxx"
86 #include "xipivot.hxx"
88 #include "excform.hxx"
89 #include "scextopt.hxx"
90 #include "stlpool.hxx"
91 #include "stlsheet.hxx"
92 #include "detfunc.hxx"
93 #include "macromgr.hxx"
94 #include "queryentry.hxx"
96 #include <com/sun/star/document/XDocumentProperties.hpp>
97 #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
98 #include <com/sun/star/script/ModuleInfo.hpp>
99 #include <com/sun/star/container/XIndexContainer.hpp>
100 #include <cppuhelper/component_context.hxx>
101 #include <cppuhelper/implbase.hxx>
102 #include "xltoolbar.hxx"
103 #include <oox/ole/vbaproject.hxx>
104 #include <oox/ole/olestorage.hxx>
105 #include <unotools/streamwrap.hxx>
106 #include <o3tl/make_unique.hxx>
108 using namespace com::sun::star;
109 using namespace ::comphelper;
111 //OleNameOverrideContainer
113 class OleNameOverrideContainer : public ::cppu::WeakImplHelper< container::XNameContainer >
115 private:
116 typedef std::unordered_map< OUString, uno::Reference< container::XIndexContainer >, OUStringHash > NamedIndexToOleName;
117 NamedIndexToOleName IdToOleNameHash;
118 ::osl::Mutex m_aMutex;
119 public:
120 // XElementAccess
121 virtual uno::Type SAL_CALL getElementType( ) throw (uno::RuntimeException, std::exception) override { return cppu::UnoType<container::XIndexContainer>::get(); }
122 virtual sal_Bool SAL_CALL hasElements( ) throw (uno::RuntimeException, std::exception) override
124 ::osl::MutexGuard aGuard( m_aMutex );
125 return ( !IdToOleNameHash.empty() );
127 // XNameAcess
128 virtual uno::Any SAL_CALL getByName( const OUString& aName ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception) override
130 ::osl::MutexGuard aGuard( m_aMutex );
131 if ( !hasByName(aName) )
132 throw container::NoSuchElementException();
133 return uno::makeAny( IdToOleNameHash[ aName ] );
135 virtual uno::Sequence< OUString > SAL_CALL getElementNames( ) throw (uno::RuntimeException, std::exception) override
137 ::osl::MutexGuard aGuard( m_aMutex );
138 return comphelper::mapKeysToSequence( IdToOleNameHash);
140 virtual sal_Bool SAL_CALL hasByName( const OUString& aName ) throw (uno::RuntimeException, std::exception) override
142 ::osl::MutexGuard aGuard( m_aMutex );
143 return ( IdToOleNameHash.find( aName ) != IdToOleNameHash.end() );
146 // XNameContainer
147 virtual void SAL_CALL insertByName( const OUString& aName, const uno::Any& aElement ) throw(lang::IllegalArgumentException, container::ElementExistException, lang::WrappedTargetException, uno::RuntimeException, std::exception) override
149 ::osl::MutexGuard aGuard( m_aMutex );
150 if ( hasByName( aName ) )
151 throw container::ElementExistException();
152 uno::Reference< container::XIndexContainer > xElement;
153 if ( ! ( aElement >>= xElement ) )
154 throw lang::IllegalArgumentException();
155 IdToOleNameHash[ aName ] = xElement;
157 virtual void SAL_CALL removeByName( const OUString& aName ) throw(container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception) override
159 ::osl::MutexGuard aGuard( m_aMutex );
160 if ( !hasByName( aName ) )
161 throw container::NoSuchElementException();
162 IdToOleNameHash.erase( IdToOleNameHash.find( aName ) );
164 virtual void SAL_CALL replaceByName( const OUString& aName, const uno::Any& aElement ) throw(lang::IllegalArgumentException, container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception) override
166 ::osl::MutexGuard aGuard( m_aMutex );
167 if ( !hasByName( aName ) )
168 throw container::NoSuchElementException();
169 uno::Reference< container::XIndexContainer > xElement;
170 if ( ! ( aElement >>= xElement ) )
171 throw lang::IllegalArgumentException();
172 IdToOleNameHash[ aName ] = xElement;
176 namespace {
178 /** Future Record Type header.
179 @return whether read rt matches nRecordID
181 bool readFrtHeader( XclImpStream& rStrm, sal_uInt16 nRecordID )
183 sal_uInt16 nRt = rStrm.ReaduInt16();
184 rStrm.Ignore(10); // grbitFrt (2 bytes) and reserved (8 bytes)
185 return nRt == nRecordID;
190 ImportExcel8::ImportExcel8( XclImpRootData& rImpData, SvStream& rStrm ) :
191 ImportExcel( rImpData, rStrm )
193 // replace BIFF2-BIFF5 formula importer with BIFF8 formula importer
194 delete pFormConv;
195 pFormConv = pExcRoot->pFmlaConverter = new ExcelToSc8( GetRoot() );
198 ImportExcel8::~ImportExcel8()
202 void ImportExcel8::Calccount()
204 ScDocOptions aOpt = pD->GetDocOptions();
205 aOpt.SetIterCount( aIn.ReaduInt16() );
206 pD->SetDocOptions( aOpt );
209 void ImportExcel8::Precision()
211 ScDocOptions aOpt = pD->GetDocOptions();
212 aOpt.SetCalcAsShown( aIn.ReaduInt16() == 0 );
213 pD->SetDocOptions( aOpt );
216 void ImportExcel8::Delta()
218 ScDocOptions aOpt = pD->GetDocOptions();
219 aOpt.SetIterEps( aIn.ReadDouble() );
220 pD->SetDocOptions( aOpt );
223 void ImportExcel8::Iteration()
225 ScDocOptions aOpt = pD->GetDocOptions();
226 aOpt.SetIter( aIn.ReaduInt16() == 1 );
227 pD->SetDocOptions( aOpt );
230 void ImportExcel8::Boundsheet()
232 sal_uInt8 nLen;
233 sal_uInt16 nGrbit;
235 aIn.DisableDecryption();
236 maSheetOffsets.push_back( aIn.ReaduInt32() );
237 aIn.EnableDecryption();
238 nGrbit = aIn.ReaduInt16();
239 nLen = aIn.ReaduInt8();
241 OUString aName( aIn.ReadUniString( nLen ) );
242 GetTabInfo().AppendXclTabName( aName, nBdshtTab );
244 SCTAB nScTab = static_cast< SCTAB >( nBdshtTab );
245 if( nScTab > 0 )
247 OSL_ENSURE( !pD->HasTable( nScTab ), "ImportExcel8::Boundsheet - sheet exists already" );
248 pD->MakeTable( nScTab );
251 if( ( nGrbit & 0x0001 ) || ( nGrbit & 0x0002 ) )
252 pD->SetVisible( nScTab, false );
254 if( !pD->RenameTab( nScTab, aName ) )
256 pD->CreateValidTabName( aName );
257 pD->RenameTab( nScTab, aName );
260 nBdshtTab++;
263 void ImportExcel8::Scenman()
265 sal_uInt16 nLastDispl;
267 aIn.Ignore( 4 );
268 nLastDispl = aIn.ReaduInt16();
270 maScenList.nLastScenario = nLastDispl;
273 void ImportExcel8::Scenario()
275 maScenList.aEntries.push_back( o3tl::make_unique<ExcScenario>( aIn, *pExcRoot ) );
278 void ImportExcel8::Labelsst()
280 XclAddress aXclPos;
281 sal_uInt16 nXF;
282 sal_uInt32 nSst;
284 aIn >> aXclPos;
285 nXF = aIn.ReaduInt16();
286 nSst = aIn.ReaduInt32( );
288 ScAddress aScPos( ScAddress::UNINITIALIZED );
289 if( GetAddressConverter().ConvertAddress( aScPos, aXclPos, GetCurrScTab(), true ) )
291 GetXFRangeBuffer().SetXF( aScPos, nXF );
292 const XclImpString* pXclStr = GetSst().GetString(nSst);
293 if (pXclStr)
294 XclImpStringHelper::SetToDocument(GetDocImport(), aScPos, *this, *pXclStr, nXF);
298 void ImportExcel8::FeatHdr()
300 if (!readFrtHeader( aIn, 0x0867))
301 return;
303 // Feature type (isf) can be EXC_ISFPROTECTION, EXC_ISFFEC2 or
304 // EXC_ISFFACTOID.
305 sal_uInt16 nFeatureType = aIn.ReaduInt16();
306 if (nFeatureType != EXC_ISFPROTECTION)
307 // We currently only support import of enhanced protection data.
308 return;
310 aIn.Ignore(1); // always 1
312 GetSheetProtectBuffer().ReadOptions( aIn, GetCurrScTab() );
315 void ImportExcel8::Feat()
317 if (!readFrtHeader( aIn, 0x0868))
318 return;
320 // Feature type (isf) can be EXC_ISFPROTECTION, EXC_ISFFEC2 or
321 // EXC_ISFFACTOID.
322 sal_uInt16 nFeatureType = aIn.ReaduInt16();
323 if (nFeatureType != EXC_ISFPROTECTION)
324 // We currently only support import of enhanced protection data.
325 return;
327 aIn.Ignore(5); // reserved1 (1 byte) and reserved2 (4 bytes)
329 sal_uInt16 nCref = aIn.ReaduInt16(); // number of ref elements
330 aIn.Ignore(4); // size if EXC_ISFFEC2, else 0 and to be ignored
331 aIn.Ignore(2); // reserved3 (2 bytes)
333 ScEnhancedProtection aProt;
334 if (nCref)
336 XclRangeList aRefs;
337 aRefs.Read( aIn, true, nCref);
338 if (!aRefs.empty())
340 aProt.maRangeList = new ScRangeList;
341 GetAddressConverter().ConvertRangeList( *aProt.maRangeList, aRefs, GetCurrScTab(), false);
345 // FeatProtection structure follows in record.
347 aProt.mnAreserved = aIn.ReaduInt32();
348 aProt.mnPasswordVerifier = aIn.ReaduInt32();
349 aProt.maTitle = aIn.ReadUniString();
350 if ((aProt.mnAreserved & 0x00000001) == 0x00000001)
352 sal_uInt32 nCbSD = aIn.ReaduInt32();
353 // TODO: could here be some sanity check applied to not allocate 4GB?
354 aProt.maSecurityDescriptor.resize( nCbSD);
355 sal_Size nRead = aIn.Read( &aProt.maSecurityDescriptor.front(), nCbSD);
356 if (nRead < nCbSD)
357 aProt.maSecurityDescriptor.resize( nRead);
360 GetSheetProtectBuffer().AppendEnhancedProtection( aProt, GetCurrScTab() );
363 void ImportExcel8::ReadBasic()
365 SfxObjectShell* pShell = GetDocShell();
366 tools::SvRef<SotStorage> xRootStrg = GetRootStorage();
367 const SvtFilterOptions& rFilterOpt = SvtFilterOptions::Get();
368 if( pShell && xRootStrg.Is() ) try
370 // #FIXME need to get rid of this, we can also do this from within oox
371 // via the "ooo.vba.VBAGlobals" service
372 if( ( rFilterOpt.IsLoadExcelBasicCode() ||
373 rFilterOpt.IsLoadExcelBasicStorage() ) &&
374 rFilterOpt.IsLoadExcelBasicExecutable() )
376 // see if we have the XCB stream
377 tools::SvRef<SotStorageStream> xXCB = xRootStrg->OpenSotStream( "XCB", STREAM_STD_READ | StreamMode::NOCREATE );
378 if ( xXCB.Is()|| SVSTREAM_OK == xXCB->GetError() )
380 ScCTBWrapper wrapper;
381 if ( wrapper.Read( *xXCB ) )
383 #ifdef DEBUG_SC_EXCEL
384 wrapper.Print( stderr );
385 #endif
386 wrapper.ImportCustomToolBar( *pShell );
392 uno::Reference< uno::XComponentContext > aCtx( ::comphelper::getProcessComponentContext() );
393 SfxMedium& rMedium = GetMedium();
394 uno::Reference< io::XInputStream > xIn = rMedium.GetInputStream();
395 oox::ole::OleStorage root( aCtx, xIn, false );
396 oox::StorageRef vbaStg = root.openSubStorage( "_VBA_PROJECT_CUR", false );
397 if ( vbaStg.get() )
399 oox::ole::VbaProject aVbaPrj( aCtx, pShell->GetModel(), "Calc" );
400 // collect names of embedded form controls, as specified in the VBA project
401 uno::Reference< container::XNameContainer > xOleNameOverrideSink( new OleNameOverrideContainer );
402 aVbaPrj.setOleOverridesSink( xOleNameOverrideSink );
403 aVbaPrj.importVbaProject( *vbaStg );
404 GetObjectManager().SetOleNameOverrideInfo( xOleNameOverrideSink );
407 catch( uno::Exception& )
411 catch( uno::Exception& )
416 void ImportExcel8::EndSheet()
418 ImportExcel::EndSheet();
419 GetCondFormatManager().Apply();
420 GetValidationManager().Apply();
423 void ImportExcel8::PostDocLoad()
425 #if HAVE_FEATURE_SCRIPTING
426 // reading basic has been delayed until sheet objects (codenames etc.) are read
427 if( HasBasic() )
428 ReadBasic();
429 #endif
430 // #i11776# filtered ranges before outlines and hidden rows
431 if( pExcRoot->pAutoFilterBuffer )
432 pExcRoot->pAutoFilterBuffer->Apply();
434 GetWebQueryBuffer().Apply(); //TODO: test if extant
435 GetSheetProtectBuffer().Apply();
436 GetDocProtectBuffer().Apply();
438 ImportExcel::PostDocLoad();
440 // check scenarios; Attention: This increases the table count of the document!!
441 if( !pD->IsClipboard() && maScenList.aEntries.size() )
443 pD->UpdateChartListenerCollection(); // references in charts must be updated
445 maScenList.Apply( GetRoot() );
448 // read doc info (no docshell while pasting from clipboard)
449 LoadDocumentProperties();
451 // #i45843# Pivot tables are now handled outside of PostDocLoad, so they are available
452 // when formula cells are calculated, for the GETPIVOTDATA function.
455 void ImportExcel8::LoadDocumentProperties()
457 // no docshell while pasting from clipboard
458 if( SfxObjectShell* pShell = GetDocShell() )
460 // BIFF5+ without storage is possible
461 tools::SvRef<SotStorage> xRootStrg = GetRootStorage();
462 if( xRootStrg.Is() ) try
464 uno::Reference< document::XDocumentPropertiesSupplier > xDPS( pShell->GetModel(), uno::UNO_QUERY_THROW );
465 uno::Reference< document::XDocumentProperties > xDocProps( xDPS->getDocumentProperties(), uno::UNO_SET_THROW );
466 sfx2::LoadOlePropertySet( xDocProps, xRootStrg );
468 catch( uno::Exception& )
474 // autofilter
476 void ImportExcel8::FilterMode()
478 // The FilterMode record exists: if either the AutoFilter
479 // record exists or an Advanced Filter is saved and stored
480 // in the sheet. Thus if the FilterMode records only exists
481 // then the latter is true..
482 if( !pExcRoot->pAutoFilterBuffer ) return;
484 XclImpAutoFilterData* pData = pExcRoot->pAutoFilterBuffer->GetByTab( GetCurrScTab() );
485 if( pData )
486 pData->SetAutoOrAdvanced();
489 void ImportExcel8::AutoFilterInfo()
491 if( !pExcRoot->pAutoFilterBuffer ) return;
493 XclImpAutoFilterData* pData = pExcRoot->pAutoFilterBuffer->GetByTab( GetCurrScTab() );
494 if( pData )
496 pData->SetAdvancedRange( nullptr );
497 pData->Activate();
501 void ImportExcel8::AutoFilter()
503 if( !pExcRoot->pAutoFilterBuffer ) return;
505 XclImpAutoFilterData* pData = pExcRoot->pAutoFilterBuffer->GetByTab( GetCurrScTab() );
506 if( pData )
507 pData->ReadAutoFilter(aIn, GetDoc().GetSharedStringPool());
510 XclImpAutoFilterData::XclImpAutoFilterData( RootData* pRoot, const ScRange& rRange ) :
511 ExcRoot( pRoot ),
512 pCurrDBData(nullptr),
513 bActive( false ),
514 bCriteria( false ),
515 bAutoOrAdvanced(false)
517 aParam.nCol1 = rRange.aStart.Col();
518 aParam.nRow1 = rRange.aStart.Row();
519 aParam.nTab = rRange.aStart.Tab();
520 aParam.nCol2 = rRange.aEnd.Col();
521 aParam.nRow2 = rRange.aEnd.Row();
523 aParam.bInplace = true;
527 namespace {
529 OUString CreateFromDouble( double fVal )
531 return rtl::math::doubleToUString(fVal,
532 rtl_math_StringFormat_Automatic, rtl_math_DecimalPlaces_Max,
533 ScGlobal::pLocaleData->getNumDecimalSep()[0], true);
538 void XclImpAutoFilterData::SetCellAttribs()
540 ScDocument& rDoc = pExcRoot->pIR->GetDoc();
541 for ( SCCOL nCol = StartCol(); nCol <= EndCol(); nCol++ )
543 ScMF nFlag = static_cast<const ScMergeFlagAttr*>( rDoc.GetAttr( nCol, StartRow(), Tab(), ATTR_MERGE_FLAG ))->GetValue();
544 rDoc.ApplyAttr( nCol, StartRow(), Tab(), ScMergeFlagAttr( nFlag | ScMF::Auto) );
548 void XclImpAutoFilterData::InsertQueryParam()
550 if (pCurrDBData)
552 ScRange aAdvRange;
553 bool bHasAdv = pCurrDBData->GetAdvancedQuerySource( aAdvRange );
554 if( bHasAdv )
555 pExcRoot->pIR->GetDoc().CreateQueryParam( aAdvRange.aStart.Col(),
556 aAdvRange.aStart.Row(), aAdvRange.aEnd.Col(), aAdvRange.aEnd.Row(),
557 aAdvRange.aStart.Tab(), aParam );
559 pCurrDBData->SetQueryParam( aParam );
560 if( bHasAdv )
561 pCurrDBData->SetAdvancedQuerySource( &aAdvRange );
562 else
564 pCurrDBData->SetAutoFilter( true );
565 SetCellAttribs();
570 static void ExcelQueryToOooQuery( OUString& aStr, ScQueryEntry& rEntry )
572 if (rEntry.eOp != SC_EQUAL && rEntry.eOp != SC_NOT_EQUAL)
573 return;
575 sal_Int32 nLen = aStr.getLength();
576 sal_Unicode nStart = aStr[0];
577 sal_Unicode nEnd = aStr[ nLen-1 ];
578 if( nLen > 2 && nStart == '*' && nEnd == '*' )
580 aStr = aStr.copy( 1, nLen-2 );
581 rEntry.eOp = ( rEntry.eOp == SC_EQUAL ) ? SC_CONTAINS : SC_DOES_NOT_CONTAIN;
583 else if( nLen > 1 && nStart == '*' && nEnd != '*' )
585 aStr = aStr.copy( 1 );
586 rEntry.eOp = ( rEntry.eOp == SC_EQUAL ) ? SC_ENDS_WITH : SC_DOES_NOT_END_WITH;
588 else if( nLen > 1 && nStart != '*' && nEnd == '*' )
590 aStr = aStr.copy( 0, nLen-1 );
591 rEntry.eOp = ( rEntry.eOp == SC_EQUAL ) ? SC_BEGINS_WITH : SC_DOES_NOT_BEGIN_WITH;
593 else if( nLen == 2 && nStart == '*' && nEnd == '*' )
595 aStr = aStr.copy( 1 );
599 void XclImpAutoFilterData::ReadAutoFilter(
600 XclImpStream& rStrm, svl::SharedStringPool& rPool )
602 sal_uInt16 nCol, nFlags;
603 nCol = rStrm.ReaduInt16();
604 nFlags = rStrm.ReaduInt16();
606 ScQueryConnect eConn = ::get_flagvalue( nFlags, EXC_AFFLAG_ANDORMASK, SC_OR, SC_AND );
607 bool bSimple1 = ::get_flag(nFlags, EXC_AFFLAG_SIMPLE1);
608 bool bSimple2 = ::get_flag(nFlags, EXC_AFFLAG_SIMPLE2);
609 bool bTop10 = ::get_flag(nFlags, EXC_AFFLAG_TOP10);
610 bool bTopOfTop10 = ::get_flag(nFlags, EXC_AFFLAG_TOP10TOP);
611 bool bPercent = ::get_flag(nFlags, EXC_AFFLAG_TOP10PERC);
612 sal_uInt16 nCntOfTop10 = nFlags >> 7;
614 if( bTop10 )
616 ScQueryEntry& aEntry = aParam.AppendEntry();
617 ScQueryEntry::Item& rItem = aEntry.GetQueryItem();
618 aEntry.bDoQuery = true;
619 aEntry.nField = static_cast<SCCOLROW>(StartCol() + static_cast<SCCOL>(nCol));
620 aEntry.eOp = bTopOfTop10 ?
621 (bPercent ? SC_TOPPERC : SC_TOPVAL) : (bPercent ? SC_BOTPERC : SC_BOTVAL);
622 aEntry.eConnect = SC_AND;
624 rItem.meType = ScQueryEntry::ByString;
625 rItem.maString = rPool.intern(OUString::number(nCntOfTop10));
627 rStrm.Ignore(20);
628 return;
631 sal_uInt8 nType, nOper, nBoolErr, nVal;
632 sal_Int32 nRK;
633 double fVal;
635 sal_uInt8 nStrLen[2] = { 0, 0 };
636 ScQueryEntry aEntries[2];
638 for (size_t nE = 0; nE < 2; ++nE)
640 ScQueryEntry& rEntry = aEntries[nE];
641 ScQueryEntry::Item& rItem = rEntry.GetQueryItem();
642 bool bIgnore = false;
644 nType = rStrm.ReaduInt8();
645 nOper = rStrm.ReaduInt8();
646 switch( nOper )
648 case EXC_AFOPER_LESS:
649 rEntry.eOp = SC_LESS;
650 break;
651 case EXC_AFOPER_EQUAL:
652 rEntry.eOp = SC_EQUAL;
653 break;
654 case EXC_AFOPER_LESSEQUAL:
655 rEntry.eOp = SC_LESS_EQUAL;
656 break;
657 case EXC_AFOPER_GREATER:
658 rEntry.eOp = SC_GREATER;
659 break;
660 case EXC_AFOPER_NOTEQUAL:
661 rEntry.eOp = SC_NOT_EQUAL;
662 break;
663 case EXC_AFOPER_GREATEREQUAL:
664 rEntry.eOp = SC_GREATER_EQUAL;
665 break;
666 default:
667 rEntry.eOp = SC_EQUAL;
670 switch( nType )
672 case EXC_AFTYPE_RK:
673 nRK = rStrm.ReadInt32();
674 rStrm.Ignore( 4 );
675 rItem.maString = rPool.intern(
676 CreateFromDouble(XclTools::GetDoubleFromRK(nRK)));
677 break;
678 case EXC_AFTYPE_DOUBLE:
679 fVal = rStrm.ReadDouble();
680 rItem.maString = rPool.intern(CreateFromDouble(fVal));
681 break;
682 case EXC_AFTYPE_STRING:
683 rStrm.Ignore( 4 );
684 nStrLen[ nE ] = rStrm.ReaduInt8();
685 rStrm.Ignore( 3 );
686 rItem.maString = svl::SharedString();
687 break;
688 case EXC_AFTYPE_BOOLERR:
689 nBoolErr = rStrm.ReaduInt8();
690 nVal = rStrm.ReaduInt8();
691 rStrm.Ignore( 6 );
692 rItem.maString = rPool.intern(OUString::number(nVal));
693 bIgnore = (nBoolErr != 0);
694 break;
695 case EXC_AFTYPE_EMPTY:
696 rEntry.SetQueryByEmpty();
697 break;
698 case EXC_AFTYPE_NOTEMPTY:
699 rEntry.SetQueryByNonEmpty();
700 break;
701 default:
702 rStrm.Ignore( 8 );
703 bIgnore = true;
706 if (!bIgnore)
708 rEntry.bDoQuery = true;
709 rItem.meType = ScQueryEntry::ByString;
710 rEntry.nField = static_cast<SCCOLROW>(StartCol() + static_cast<SCCOL>(nCol));
711 rEntry.eConnect = nE ? eConn : SC_AND;
715 if (eConn == SC_AND)
717 for (size_t nE = 0; nE < 2; ++nE)
719 if (nStrLen[nE] && aEntries[nE].bDoQuery)
721 OUString aStr = rStrm.ReadUniString(nStrLen[nE]);
722 ExcelQueryToOooQuery(aStr, aEntries[nE]);
723 aEntries[nE].GetQueryItem().maString = rPool.intern(aStr);
724 aParam.AppendEntry() = aEntries[nE];
728 else
730 OSL_ASSERT(eConn == SC_OR);
731 // Import only when both conditions are for simple equality, else
732 // import only the 1st condition due to conflict with the ordering of
733 // conditions. #i39464#.
735 // Example: Let A1 be a condition of column A, and B1 and B2
736 // conditions of column B, connected with OR. Excel performs 'A1 AND
737 // (B1 OR B2)' in this case, but Calc would do '(A1 AND B1) OR B2'
738 // instead.
740 if (bSimple1 && bSimple2 && nStrLen[0] && nStrLen[1])
742 // Two simple OR'ed equal conditions. We can import this correctly.
743 ScQueryEntry& rEntry = aParam.AppendEntry();
744 rEntry.bDoQuery = true;
745 rEntry.eOp = SC_EQUAL;
746 rEntry.eConnect = SC_AND;
747 ScQueryEntry::QueryItemsType aItems;
748 aItems.reserve(2);
749 ScQueryEntry::Item aItem1, aItem2;
750 aItem1.maString = rPool.intern(rStrm.ReadUniString(nStrLen[0]));
751 aItem1.meType = ScQueryEntry::ByString;
752 aItem2.maString = rPool.intern(rStrm.ReadUniString(nStrLen[1]));
753 aItem2.meType = ScQueryEntry::ByString;
754 aItems.push_back(aItem1);
755 aItems.push_back(aItem2);
756 rEntry.GetQueryItems().swap(aItems);
758 else if (nStrLen[0] && aEntries[0].bDoQuery)
760 // Due to conflict, we can import only the first condition.
761 OUString aStr = rStrm.ReadUniString(nStrLen[0]);
762 ExcelQueryToOooQuery(aStr, aEntries[0]);
763 aEntries[0].GetQueryItem().maString = rPool.intern(aStr);
764 aParam.AppendEntry() = aEntries[0];
769 void XclImpAutoFilterData::SetAdvancedRange( const ScRange* pRange )
771 if (pRange)
773 aCriteriaRange = *pRange;
774 bCriteria = true;
776 else
777 bCriteria = false;
780 void XclImpAutoFilterData::SetExtractPos( const ScAddress& rAddr )
782 aParam.nDestCol = rAddr.Col();
783 aParam.nDestRow = rAddr.Row();
784 aParam.nDestTab = rAddr.Tab();
785 aParam.bInplace = false;
786 aParam.bDestPers = true;
789 void XclImpAutoFilterData::Apply()
791 CreateScDBData();
793 if( bActive )
795 InsertQueryParam();
799 void XclImpAutoFilterData::CreateScDBData()
802 // Create the ScDBData() object if the AutoFilter is activated
803 // or if we need to create the Advanced Filter.
804 if( bActive || bCriteria)
806 ScDocument& rDoc = pExcRoot->pIR->GetDocRef();
807 OUString aNewName(STR_DB_LOCAL_NONAME);
808 pCurrDBData = new ScDBData(aNewName , Tab(),
809 StartCol(),StartRow(), EndCol(),EndRow() );
810 if(bCriteria)
812 EnableRemoveFilter();
814 pCurrDBData->SetQueryParam( aParam );
815 pCurrDBData->SetAdvancedQuerySource(&aCriteriaRange);
817 else
818 pCurrDBData->SetAdvancedQuerySource(nullptr);
819 rDoc.SetAnonymousDBData(Tab(), pCurrDBData);
824 void XclImpAutoFilterData::EnableRemoveFilter()
826 // only if this is a saved Advanced filter
827 if( !bActive && bAutoOrAdvanced )
829 ScQueryEntry& aEntry = aParam.AppendEntry();
830 aEntry.bDoQuery = true;
833 // TBD: force the automatic activation of the
834 // "Remove Filter" by setting a virtual mouse click
835 // inside the advanced range
838 void XclImpAutoFilterBuffer::Insert( RootData* pRoot, const ScRange& rRange)
840 if( !GetByTab( rRange.aStart.Tab() ) )
841 maFilters.push_back( std::make_shared<XclImpAutoFilterData>( pRoot, rRange ));
844 void XclImpAutoFilterBuffer::AddAdvancedRange( const ScRange& rRange )
846 XclImpAutoFilterData* pData = GetByTab( rRange.aStart.Tab() );
847 if( pData )
848 pData->SetAdvancedRange( &rRange );
851 void XclImpAutoFilterBuffer::AddExtractPos( const ScRange& rRange )
853 XclImpAutoFilterData* pData = GetByTab( rRange.aStart.Tab() );
854 if( pData )
855 pData->SetExtractPos( rRange.aStart );
858 void XclImpAutoFilterBuffer::Apply()
860 for( const auto& rFilterPtr : maFilters )
861 rFilterPtr->Apply();
864 XclImpAutoFilterData* XclImpAutoFilterBuffer::GetByTab( SCTAB nTab )
866 for( const auto& rFilterPtr : maFilters )
868 if( rFilterPtr->Tab() == nTab )
869 return rFilterPtr.get();
871 return nullptr;
874 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */