bump product version to 4.1.6.2
[LibreOffice.git] / sc / source / filter / excel / excimp8.cxx
blob5b849bea9ea484d8e32398b6559830b93b88c4b0
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 "excimp8.hxx"
22 #include <boost/bind.hpp>
24 #include <scitems.hxx>
25 #include <comphelper/processfactory.hxx>
26 #include <comphelper/mediadescriptor.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/bmpacc.hxx>
55 #include <sot/exchange.hxx>
57 #include <svl/stritem.hxx>
59 #include <tools/string.hxx>
60 #include <rtl/math.hxx>
61 #include <unotools/localedatawrapper.hxx>
62 #include <unotools/charclass.hxx>
63 #include <drwlayer.hxx>
65 #include <boost/scoped_array.hpp>
67 #include "formulacell.hxx"
68 #include "document.hxx"
69 #include "patattr.hxx"
70 #include "docpool.hxx"
71 #include "attrib.hxx"
72 #include "conditio.hxx"
73 #include "dbdata.hxx"
74 #include "globalnames.hxx"
75 #include "editutil.hxx"
76 #include "markdata.hxx"
77 #include "rangenam.hxx"
78 #include "docoptio.hxx"
79 #include "globstr.hrc"
80 #include "fprogressbar.hxx"
81 #include "xltracer.hxx"
82 #include "xihelper.hxx"
83 #include "xipage.hxx"
84 #include "xicontent.hxx"
85 #include "xilink.hxx"
86 #include "xiescher.hxx"
87 #include "xipivot.hxx"
89 #include "excform.hxx"
90 #include "scextopt.hxx"
91 #include "stlpool.hxx"
92 #include "stlsheet.hxx"
93 #include "detfunc.hxx"
94 #include "macromgr.hxx"
95 #include "queryentry.hxx"
97 #include <com/sun/star/document/XDocumentProperties.hpp>
98 #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
99 #include <com/sun/star/script/ModuleInfo.hpp>
100 #include <com/sun/star/container/XIndexContainer.hpp>
101 #include <cppuhelper/component_context.hxx>
102 #include "xltoolbar.hxx"
103 #include <oox/ole/vbaproject.hxx>
104 #include <oox/ole/olestorage.hxx>
105 #include <unotools/streamwrap.hxx>
107 using namespace com::sun::star;
108 using namespace ::comphelper;
110 //OleNameOverrideContainer
112 typedef ::cppu::WeakImplHelper1< container::XNameContainer > OleNameOverrideContainer_BASE;
114 class OleNameOverrideContainer : public OleNameOverrideContainer_BASE
116 private:
117 typedef boost::unordered_map< OUString, uno::Reference< container::XIndexContainer >, OUStringHash,
118 ::std::equal_to< OUString > > NamedIndexToOleName;
119 NamedIndexToOleName IdToOleNameHash;
120 ::osl::Mutex m_aMutex;
121 public:
122 // XElementAccess
123 virtual uno::Type SAL_CALL getElementType( ) throw (uno::RuntimeException) { return container::XIndexContainer::static_type(0); }
124 virtual ::sal_Bool SAL_CALL hasElements( ) throw (uno::RuntimeException)
126 ::osl::MutexGuard aGuard( m_aMutex );
127 return ( IdToOleNameHash.size() > 0 );
129 // XNameAcess
130 virtual uno::Any SAL_CALL getByName( const OUString& aName ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
132 ::osl::MutexGuard aGuard( m_aMutex );
133 if ( !hasByName(aName) )
134 throw container::NoSuchElementException();
135 return uno::makeAny( IdToOleNameHash[ aName ] );
137 virtual uno::Sequence< OUString > SAL_CALL getElementNames( ) throw (uno::RuntimeException)
139 ::osl::MutexGuard aGuard( m_aMutex );
140 uno::Sequence< OUString > aResult( IdToOleNameHash.size() );
141 NamedIndexToOleName::iterator it = IdToOleNameHash.begin();
142 NamedIndexToOleName::iterator it_end = IdToOleNameHash.end();
143 OUString* pName = aResult.getArray();
144 for (; it != it_end; ++it, ++pName )
145 *pName = it->first;
146 return aResult;
148 virtual ::sal_Bool SAL_CALL hasByName( const OUString& aName ) throw (uno::RuntimeException)
150 ::osl::MutexGuard aGuard( m_aMutex );
151 return ( IdToOleNameHash.find( aName ) != IdToOleNameHash.end() );
154 // XElementAccess
155 virtual ::sal_Int32 SAL_CALL getCount( ) throw (uno::RuntimeException)
157 ::osl::MutexGuard aGuard( m_aMutex );
158 return IdToOleNameHash.size();
160 // XNameContainer
161 virtual void SAL_CALL insertByName( const OUString& aName, const uno::Any& aElement ) throw(lang::IllegalArgumentException, container::ElementExistException, lang::WrappedTargetException, uno::RuntimeException)
163 ::osl::MutexGuard aGuard( m_aMutex );
164 if ( hasByName( aName ) )
165 throw container::ElementExistException();
166 uno::Reference< container::XIndexContainer > xElement;
167 if ( ! ( aElement >>= xElement ) )
168 throw lang::IllegalArgumentException();
169 IdToOleNameHash[ aName ] = xElement;
171 virtual void SAL_CALL removeByName( const OUString& aName ) throw(container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
173 ::osl::MutexGuard aGuard( m_aMutex );
174 if ( !hasByName( aName ) )
175 throw container::NoSuchElementException();
176 IdToOleNameHash.erase( IdToOleNameHash.find( aName ) );
178 virtual void SAL_CALL replaceByName( const OUString& aName, const uno::Any& aElement ) throw(lang::IllegalArgumentException, container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
180 ::osl::MutexGuard aGuard( m_aMutex );
181 if ( !hasByName( aName ) )
182 throw container::NoSuchElementException();
183 uno::Reference< container::XIndexContainer > xElement;
184 if ( ! ( aElement >>= xElement ) )
185 throw lang::IllegalArgumentException();
186 IdToOleNameHash[ aName ] = xElement;
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() );
199 ImportExcel8::~ImportExcel8()
204 void ImportExcel8::Calccount( void )
206 ScDocOptions aOpt = pD->GetDocOptions();
207 aOpt.SetIterCount( aIn.ReaduInt16() );
208 pD->SetDocOptions( aOpt );
212 void ImportExcel8::Precision( void )
214 ScDocOptions aOpt = pD->GetDocOptions();
215 aOpt.SetCalcAsShown( aIn.ReaduInt16() == 0 );
216 pD->SetDocOptions( aOpt );
220 void ImportExcel8::Delta( void )
222 ScDocOptions aOpt = pD->GetDocOptions();
223 aOpt.SetIterEps( aIn.ReadDouble() );
224 pD->SetDocOptions( aOpt );
228 void ImportExcel8::Iteration( void )
230 ScDocOptions aOpt = pD->GetDocOptions();
231 aOpt.SetIter( aIn.ReaduInt16() == 1 );
232 pD->SetDocOptions( aOpt );
236 void ImportExcel8::Boundsheet( void )
238 sal_uInt8 nLen;
239 sal_uInt16 nGrbit;
241 aIn.DisableDecryption();
242 maSheetOffsets.push_back( aIn.ReaduInt32() );
243 aIn.EnableDecryption();
244 aIn >> nGrbit >> nLen;
246 OUString aName( aIn.ReadUniString( nLen ) );
247 GetTabInfo().AppendXclTabName( aName, nBdshtTab );
249 SCTAB nScTab = static_cast< SCTAB >( nBdshtTab );
250 if( nScTab > 0 )
252 OSL_ENSURE( !pD->HasTable( nScTab ), "ImportExcel8::Boundsheet - sheet exists already" );
253 pD->MakeTable( nScTab );
256 if( ( nGrbit & 0x0001 ) || ( nGrbit & 0x0002 ) )
257 pD->SetVisible( nScTab, false );
259 if( !pD->RenameTab( nScTab, aName ) )
261 pD->CreateValidTabName( aName );
262 pD->RenameTab( nScTab, aName );
265 nBdshtTab++;
269 void ImportExcel8::Scenman( void )
271 sal_uInt16 nLastDispl;
273 aIn.Ignore( 4 );
274 aIn >> nLastDispl;
276 maScenList.nLastScenario = nLastDispl;
280 void ImportExcel8::Scenario( void )
282 maScenList.aEntries.push_back( new ExcScenario( aIn, *pExcRoot ) );
286 void ImportExcel8::Labelsst( void )
288 XclAddress aXclPos;
289 sal_uInt16 nXF;
290 sal_uInt32 nSst;
292 aIn >> aXclPos >> nXF >> nSst;
294 ScAddress aScPos( ScAddress::UNINITIALIZED );
295 if( GetAddressConverter().ConvertAddress( aScPos, aXclPos, GetCurrScTab(), true ) )
297 GetXFRangeBuffer().SetXF( aScPos, nXF );
298 const XclImpString* pXclStr = GetSst().GetString(nSst);
299 if (pXclStr)
300 XclImpStringHelper::SetToDocument(GetDoc(), aScPos, *this, *pXclStr, nXF);
305 void ImportExcel8::SheetProtection( void )
307 GetSheetProtectBuffer().ReadOptions( aIn, GetCurrScTab() );
310 void ImportExcel8::ReadBasic( void )
312 SfxObjectShell* pShell = GetDocShell();
313 SotStorageRef xRootStrg = GetRootStorage();
314 const SvtFilterOptions& rFilterOpt = SvtFilterOptions::Get();
315 if( pShell && xRootStrg.Is() ) try
317 // #FIXME need to get rid of this, we can also do this from within oox
318 // via the "ooo.vba.VBAGlobals" service
319 if( ( rFilterOpt.IsLoadExcelBasicCode() ||
320 rFilterOpt.IsLoadExcelBasicStorage() ) &&
321 rFilterOpt.IsLoadExcelBasicExecutable() )
323 // see if we have the XCB stream
324 SvStorageStreamRef xXCB = xRootStrg->OpenSotStream( String( RTL_CONSTASCII_USTRINGPARAM( "XCB" ) ), STREAM_STD_READ | STREAM_NOCREATE );
325 if ( xXCB.Is()|| SVSTREAM_OK == xXCB->GetError() )
327 ScCTBWrapper wrapper;
328 if ( wrapper.Read( *xXCB ) )
330 #if OSL_DEBUG_LEVEL > 1
331 wrapper.Print( stderr );
332 #endif
333 wrapper.ImportCustomToolBar( *pShell );
339 uno::Reference< uno::XComponentContext > aCtx( ::comphelper::getProcessComponentContext() );
340 SfxMedium& rMedium = GetMedium();
341 uno::Reference< io::XInputStream > xIn = rMedium.GetInputStream();
342 oox::ole::OleStorage root( aCtx, xIn, false );
343 oox::StorageRef vbaStg = root.openSubStorage( "_VBA_PROJECT_CUR", false );
344 if ( vbaStg.get() )
346 oox::ole::VbaProject aVbaPrj( aCtx, pShell->GetModel(), "Calc" );
347 // collect names of embedded form controls, as specified in the VBA project
348 uno::Reference< container::XNameContainer > xOleNameOverrideSink( new OleNameOverrideContainer );
349 aVbaPrj.setOleOverridesSink( xOleNameOverrideSink );
350 aVbaPrj.importVbaProject( *vbaStg );
351 GetObjectManager().SetOleNameOverrideInfo( xOleNameOverrideSink );
354 catch( uno::Exception& )
358 catch( uno::Exception& )
364 void ImportExcel8::EndSheet( void )
366 ImportExcel::EndSheet();
367 GetCondFormatManager().Apply();
368 GetValidationManager().Apply();
372 void ImportExcel8::PostDocLoad( void )
374 #ifndef DISABLE_SCRIPTING
375 // reading basic has been delayed until sheet objects (codenames etc.) are read
376 if( HasBasic() )
377 ReadBasic();
378 #endif
379 // #i11776# filtered ranges before outlines and hidden rows
380 if( pExcRoot->pAutoFilterBuffer )
381 pExcRoot->pAutoFilterBuffer->Apply();
383 GetWebQueryBuffer().Apply(); //! test if extant
384 GetSheetProtectBuffer().Apply();
385 GetDocProtectBuffer().Apply();
387 ImportExcel::PostDocLoad();
389 // Scenarien bemachen! ACHTUNG: Hier wird Tabellen-Anzahl im Dokument erhoeht!!
390 if( !pD->IsClipboard() && maScenList.aEntries.size() )
392 pD->UpdateChartListenerCollection(); // references in charts must be updated
394 maScenList.Apply( GetRoot() );
397 // read doc info (no docshell while pasting from clipboard)
398 LoadDocumentProperties();
400 // #i45843# Pivot tables are now handled outside of PostDocLoad, so they are available
401 // when formula cells are calculated, for the GETPIVOTDATA function.
404 void ImportExcel8::LoadDocumentProperties()
406 // no docshell while pasting from clipboard
407 if( SfxObjectShell* pShell = GetDocShell() )
409 // BIFF5+ without storage is possible
410 SotStorageRef xRootStrg = GetRootStorage();
411 if( xRootStrg.Is() ) try
413 uno::Reference< document::XDocumentPropertiesSupplier > xDPS( pShell->GetModel(), uno::UNO_QUERY_THROW );
414 uno::Reference< document::XDocumentProperties > xDocProps( xDPS->getDocumentProperties(), uno::UNO_SET_THROW );
415 sfx2::LoadOlePropertySet( xDocProps, xRootStrg );
417 catch( uno::Exception& )
423 //___________________________________________________________________
424 // autofilter
426 void ImportExcel8::FilterMode( void )
428 // The FilterMode record exists: if either the AutoFilter
429 // record exists or an Advanced Filter is saved and stored
430 // in the sheet. Thus if the FilterMode records only exists
431 // then the latter is true..
432 if( !pExcRoot->pAutoFilterBuffer ) return;
434 XclImpAutoFilterData* pData = pExcRoot->pAutoFilterBuffer->GetByTab( GetCurrScTab() );
435 if( pData )
436 pData->SetAutoOrAdvanced();
439 void ImportExcel8::AutoFilterInfo( void )
441 if( !pExcRoot->pAutoFilterBuffer ) return;
443 XclImpAutoFilterData* pData = pExcRoot->pAutoFilterBuffer->GetByTab( GetCurrScTab() );
444 if( pData )
446 pData->SetAdvancedRange( NULL );
447 pData->Activate();
451 void ImportExcel8::AutoFilter( void )
453 if( !pExcRoot->pAutoFilterBuffer ) return;
455 XclImpAutoFilterData* pData = pExcRoot->pAutoFilterBuffer->GetByTab( GetCurrScTab() );
456 if( pData )
457 pData->ReadAutoFilter( aIn );
462 XclImpAutoFilterData::XclImpAutoFilterData( RootData* pRoot, const ScRange& rRange ) :
463 ExcRoot( pRoot ),
464 pCurrDBData(NULL),
465 bActive( false ),
466 bCriteria( false ),
467 bAutoOrAdvanced(false)
469 aParam.nCol1 = rRange.aStart.Col();
470 aParam.nRow1 = rRange.aStart.Row();
471 aParam.nTab = rRange.aStart.Tab();
472 aParam.nCol2 = rRange.aEnd.Col();
473 aParam.nRow2 = rRange.aEnd.Row();
475 aParam.bInplace = sal_True;
479 void XclImpAutoFilterData::CreateFromDouble( OUString& rStr, double fVal )
481 rStr += ::rtl::math::doubleToUString(fVal,
482 rtl_math_StringFormat_Automatic, rtl_math_DecimalPlaces_Max,
483 ScGlobal::pLocaleData->getNumDecimalSep()[0], true);
486 void XclImpAutoFilterData::SetCellAttribs()
488 ScDocument& rDoc = pExcRoot->pIR->GetDoc();
489 for ( SCCOL nCol = StartCol(); nCol <= EndCol(); nCol++ )
491 sal_Int16 nFlag = ((ScMergeFlagAttr*) rDoc.GetAttr( nCol, StartRow(), Tab(), ATTR_MERGE_FLAG ))->GetValue();
492 rDoc.ApplyAttr( nCol, StartRow(), Tab(), ScMergeFlagAttr( nFlag | SC_MF_AUTO) );
496 void XclImpAutoFilterData::InsertQueryParam()
498 if (pCurrDBData)
500 ScRange aAdvRange;
501 sal_Bool bHasAdv = pCurrDBData->GetAdvancedQuerySource( aAdvRange );
502 if( bHasAdv )
503 pExcRoot->pIR->GetDoc().CreateQueryParam( aAdvRange.aStart.Col(),
504 aAdvRange.aStart.Row(), aAdvRange.aEnd.Col(), aAdvRange.aEnd.Row(),
505 aAdvRange.aStart.Tab(), aParam );
507 pCurrDBData->SetQueryParam( aParam );
508 if( bHasAdv )
509 pCurrDBData->SetAdvancedQuerySource( &aAdvRange );
510 else
512 pCurrDBData->SetAutoFilter( sal_True );
513 SetCellAttribs();
518 static void ExcelQueryToOooQuery( ScQueryEntry& rEntry )
520 if (rEntry.eOp != SC_EQUAL && rEntry.eOp != SC_NOT_EQUAL)
521 return;
523 String aStr = rEntry.GetQueryItem().maString;
524 xub_StrLen nLen = aStr.Len();
525 sal_Unicode nStart = aStr.GetChar( 0 );
526 sal_Unicode nEnd = aStr.GetChar( nLen-1 );
527 if( nLen >2 && nStart == '*' && nEnd == '*' )
529 aStr.Erase( nLen-1, 1 );
530 aStr.Erase( 0, 1 );
531 rEntry.eOp = ( rEntry.eOp == SC_EQUAL ) ? SC_CONTAINS : SC_DOES_NOT_CONTAIN;
533 else if( nLen > 1 && nStart == '*' && nEnd != '*' )
535 aStr.Erase( 0, 1 );
536 rEntry.eOp = ( rEntry.eOp == SC_EQUAL ) ? SC_ENDS_WITH : SC_DOES_NOT_END_WITH;
538 else if( nLen > 1 && nStart != '*' && nEnd == '*' )
540 aStr.Erase( nLen-1, 1 );
541 rEntry.eOp = ( rEntry.eOp == SC_EQUAL ) ? SC_BEGINS_WITH : SC_DOES_NOT_BEGIN_WITH;
543 else if( nLen == 2 && nStart == '*' && nEnd == '*' )
545 aStr.Erase( 0, 1 );
547 rEntry.GetQueryItem().maString = aStr;
550 void XclImpAutoFilterData::ReadAutoFilter( XclImpStream& rStrm )
552 sal_uInt16 nCol, nFlags;
553 rStrm >> nCol >> nFlags;
555 ScQueryConnect eConn = ::get_flagvalue( nFlags, EXC_AFFLAG_ANDORMASK, SC_OR, SC_AND );
556 bool bSimple1 = ::get_flag(nFlags, EXC_AFFLAG_SIMPLE1);
557 bool bSimple2 = ::get_flag(nFlags, EXC_AFFLAG_SIMPLE2);
558 bool bTop10 = ::get_flag(nFlags, EXC_AFFLAG_TOP10);
559 bool bTopOfTop10 = ::get_flag(nFlags, EXC_AFFLAG_TOP10TOP);
560 bool bPercent = ::get_flag(nFlags, EXC_AFFLAG_TOP10PERC);
561 sal_uInt16 nCntOfTop10 = nFlags >> 7;
563 if( bTop10 )
565 ScQueryEntry& aEntry = aParam.AppendEntry();
566 ScQueryEntry::Item& rItem = aEntry.GetQueryItem();
567 aEntry.bDoQuery = true;
568 aEntry.nField = static_cast<SCCOLROW>(StartCol() + static_cast<SCCOL>(nCol));
569 aEntry.eOp = bTopOfTop10 ?
570 (bPercent ? SC_TOPPERC : SC_TOPVAL) : (bPercent ? SC_BOTPERC : SC_BOTVAL);
571 aEntry.eConnect = SC_AND;
573 rItem.meType = ScQueryEntry::ByString;
574 rItem.maString = OUString::number(nCntOfTop10);
576 rStrm.Ignore(20);
577 return;
580 sal_uInt8 nType, nOper, nBoolErr, nVal;
581 sal_Int32 nRK;
582 double fVal;
583 bool bIgnore;
585 sal_uInt8 nStrLen[2] = { 0, 0 };
586 ScQueryEntry aEntries[2];
588 for (size_t nE = 0; nE < 2; ++nE)
590 ScQueryEntry& rEntry = aEntries[nE];
591 ScQueryEntry::Item& rItem = rEntry.GetQueryItem();
592 bIgnore = false;
594 rStrm >> nType >> nOper;
595 switch( nOper )
597 case EXC_AFOPER_LESS:
598 rEntry.eOp = SC_LESS;
599 break;
600 case EXC_AFOPER_EQUAL:
601 rEntry.eOp = SC_EQUAL;
602 break;
603 case EXC_AFOPER_LESSEQUAL:
604 rEntry.eOp = SC_LESS_EQUAL;
605 break;
606 case EXC_AFOPER_GREATER:
607 rEntry.eOp = SC_GREATER;
608 break;
609 case EXC_AFOPER_NOTEQUAL:
610 rEntry.eOp = SC_NOT_EQUAL;
611 break;
612 case EXC_AFOPER_GREATEREQUAL:
613 rEntry.eOp = SC_GREATER_EQUAL;
614 break;
615 default:
616 rEntry.eOp = SC_EQUAL;
619 switch( nType )
621 case EXC_AFTYPE_RK:
622 rStrm >> nRK;
623 rStrm.Ignore( 4 );
624 CreateFromDouble(
625 rItem.maString, XclTools::GetDoubleFromRK(nRK));
626 break;
627 case EXC_AFTYPE_DOUBLE:
628 rStrm >> fVal;
629 CreateFromDouble(rItem.maString, fVal);
630 break;
631 case EXC_AFTYPE_STRING:
632 rStrm.Ignore( 4 );
633 rStrm >> nStrLen[ nE ];
634 rStrm.Ignore( 3 );
635 rItem.maString = OUString();
636 break;
637 case EXC_AFTYPE_BOOLERR:
638 rStrm >> nBoolErr >> nVal;
639 rStrm.Ignore( 6 );
640 rItem.maString = OUString::number(nVal);
641 bIgnore = (nBoolErr != 0);
642 break;
643 case EXC_AFTYPE_EMPTY:
644 rEntry.SetQueryByEmpty();
645 break;
646 case EXC_AFTYPE_NOTEMPTY:
647 rEntry.SetQueryByNonEmpty();
648 break;
649 default:
650 rStrm.Ignore( 8 );
651 bIgnore = true;
654 if (!bIgnore)
656 rEntry.bDoQuery = true;
657 rItem.meType = ScQueryEntry::ByString;
658 rEntry.nField = static_cast<SCCOLROW>(StartCol() + static_cast<SCCOL>(nCol));
659 rEntry.eConnect = nE ? eConn : SC_AND;
663 if (eConn == SC_AND)
665 for (size_t nE = 0; nE < 2; ++nE)
667 if (nStrLen[nE] && aEntries[nE].bDoQuery)
669 aEntries[nE].GetQueryItem().maString = rStrm.ReadUniString(nStrLen[nE]);
670 ExcelQueryToOooQuery(aEntries[nE]);
671 aParam.AppendEntry() = aEntries[nE];
675 else
677 OSL_ASSERT(eConn == SC_OR);
678 // Import only when both conditions are for simple equality, else
679 // import only the 1st condition due to conflict with the ordering of
680 // conditions. #i39464#.
682 // Example: Let A1 be a condition of column A, and B1 and B2
683 // conditions of column B, connected with OR. Excel performs 'A1 AND
684 // (B1 OR B2)' in this case, but Calc would do '(A1 AND B1) OR B2'
685 // instead.
687 if (bSimple1 && bSimple2 && nStrLen[0] && nStrLen[1])
689 // Two simple OR'ed equal conditions. We can import this correctly.
690 ScQueryEntry& rEntry = aParam.AppendEntry();
691 rEntry.bDoQuery = true;
692 rEntry.eOp = SC_EQUAL;
693 rEntry.eConnect = SC_AND;
694 ScQueryEntry::QueryItemsType aItems;
695 aItems.reserve(2);
696 ScQueryEntry::Item aItem1, aItem2;
697 aItem1.maString = rStrm.ReadUniString(nStrLen[0]);
698 aItem1.meType = ScQueryEntry::ByString;
699 aItem2.maString = rStrm.ReadUniString(nStrLen[1]);
700 aItem2.meType = ScQueryEntry::ByString;
701 aItems.push_back(aItem1);
702 aItems.push_back(aItem2);
703 rEntry.GetQueryItems().swap(aItems);
705 else if (nStrLen[0] && aEntries[0].bDoQuery)
707 // Due to conflict, we can import only the first condition.
708 aEntries[0].GetQueryItem().maString = rStrm.ReadUniString(nStrLen[0]);
709 ExcelQueryToOooQuery(aEntries[0]);
710 aParam.AppendEntry() = aEntries[0];
715 void XclImpAutoFilterData::SetAdvancedRange( const ScRange* pRange )
717 if (pRange)
719 aCriteriaRange = *pRange;
720 bCriteria = true;
722 else
723 bCriteria = false;
726 void XclImpAutoFilterData::SetExtractPos( const ScAddress& rAddr )
728 aParam.nDestCol = rAddr.Col();
729 aParam.nDestRow = rAddr.Row();
730 aParam.nDestTab = rAddr.Tab();
731 aParam.bInplace = false;
732 aParam.bDestPers = sal_True;
735 void XclImpAutoFilterData::Apply()
737 CreateScDBData();
739 if( bActive )
741 InsertQueryParam();
743 // #i38093# rows hidden by filter need extra flag, but CR_FILTERED is not set here yet
744 // SCROW nRow1 = StartRow();
745 // SCROW nRow2 = EndRow();
746 // size_t nRows = nRow2 - nRow1 + 1;
747 // boost::scoped_array<sal_uInt8> pFlags( new sal_uInt8[nRows]);
748 // pExcRoot->pDoc->GetRowFlagsArray( Tab()).FillDataArray( nRow1, nRow2,
749 // pFlags.get());
750 // for (size_t j=0; j<nRows; ++j)
751 // {
752 // if ((pFlags[j] & CR_HIDDEN) && !(pFlags[j] & CR_FILTERED))
753 // pExcRoot->pDoc->SetRowFlags( nRow1 + j, Tab(),
754 // pFlags[j] | CR_FILTERED );
755 // }
759 void XclImpAutoFilterData::CreateScDBData()
762 // Create the ScDBData() object if the AutoFilter is activated
763 // or if we need to create the Advanced Filter.
764 if( bActive || bCriteria)
766 ScDocument* pDoc = pExcRoot->pIR->GetDocPtr();
767 String aNewName = OUString(STR_DB_LOCAL_NONAME);
768 pCurrDBData = new ScDBData(aNewName , Tab(),
769 StartCol(),StartRow(), EndCol(),EndRow() );
770 if(bCriteria)
772 EnableRemoveFilter();
774 pCurrDBData->SetQueryParam( aParam );
775 pCurrDBData->SetAdvancedQuerySource(&aCriteriaRange);
777 else
778 pCurrDBData->SetAdvancedQuerySource(NULL);
779 pDoc->SetAnonymousDBData(Tab(), pCurrDBData);
784 void XclImpAutoFilterData::EnableRemoveFilter()
786 // only if this is a saved Advanced filter
787 if( !bActive && bAutoOrAdvanced )
789 ScQueryEntry& aEntry = aParam.AppendEntry();
790 aEntry.bDoQuery = true;
793 // TBD: force the automatic activation of the
794 // "Remove Filter" by setting a virtual mouse click
795 // inside the advanced range
798 void XclImpAutoFilterBuffer::Insert( RootData* pRoot, const ScRange& rRange)
800 if( !GetByTab( rRange.aStart.Tab() ) )
801 maFilters.push_back( new XclImpAutoFilterData( pRoot, rRange) );
804 void XclImpAutoFilterBuffer::AddAdvancedRange( const ScRange& rRange )
806 XclImpAutoFilterData* pData = GetByTab( rRange.aStart.Tab() );
807 if( pData )
808 pData->SetAdvancedRange( &rRange );
811 void XclImpAutoFilterBuffer::AddExtractPos( const ScRange& rRange )
813 XclImpAutoFilterData* pData = GetByTab( rRange.aStart.Tab() );
814 if( pData )
815 pData->SetExtractPos( rRange.aStart );
818 void XclImpAutoFilterBuffer::Apply()
820 std::for_each(maFilters.begin(),maFilters.end(),
821 boost::bind(&XclImpAutoFilterData::Apply,_1));
824 XclImpAutoFilterData* XclImpAutoFilterBuffer::GetByTab( SCTAB nTab )
826 boost::ptr_vector<XclImpAutoFilterData>::iterator it;
827 for( it = maFilters.begin(); it != maFilters.end(); ++it )
829 if( it->Tab() == nTab )
830 return const_cast<XclImpAutoFilterData*>(&(*it));
832 return NULL;
835 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */