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
|
/* -*- 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 <config_features.h>
#include "excimp8.hxx"
#include <scitems.hxx>
#include <comphelper/processfactory.hxx>
#include <comphelper/sequence.hxx>
#include <unotools/fltrcfg.hxx>
#include <vcl/wmf.hxx>
#include <editeng/eeitem.hxx>
#include <sfx2/docfile.hxx>
#include <sfx2/objsh.hxx>
#include <sfx2/request.hxx>
#include <sfx2/app.hxx>
#include <sfx2/docinf.hxx>
#include <sfx2/frame.hxx>
#include <editeng/brushitem.hxx>
#include <editeng/editdata.hxx>
#include <editeng/editeng.hxx>
#include <editeng/editobj.hxx>
#include <editeng/editstat.hxx>
#include <editeng/colritem.hxx>
#include <editeng/udlnitem.hxx>
#include <editeng/wghtitem.hxx>
#include <editeng/postitem.hxx>
#include <editeng/crossedoutitem.hxx>
#include <editeng/flditem.hxx>
#include <svx/xflclit.hxx>
#include <vcl/graph.hxx>
#include <vcl/bitmapaccess.hxx>
#include <sot/exchange.hxx>
#include <svl/stritem.hxx>
#include <svl/sharedstringpool.hxx>
#include <rtl/math.hxx>
#include <rtl/ustring.hxx>
#include <unotools/localedatawrapper.hxx>
#include <unotools/charclass.hxx>
#include <drwlayer.hxx>
#include "formulacell.hxx"
#include "document.hxx"
#include "patattr.hxx"
#include "docpool.hxx"
#include "attrib.hxx"
#include "conditio.hxx"
#include "dbdata.hxx"
#include "globalnames.hxx"
#include "editutil.hxx"
#include "markdata.hxx"
#include "rangenam.hxx"
#include "docoptio.hxx"
#include "globstr.hrc"
#include "fprogressbar.hxx"
#include "xltracer.hxx"
#include "xihelper.hxx"
#include "xipage.hxx"
#include "xicontent.hxx"
#include "xilink.hxx"
#include "xiescher.hxx"
#include "xipivot.hxx"
#include "excform.hxx"
#include "scextopt.hxx"
#include "stlpool.hxx"
#include "stlsheet.hxx"
#include "detfunc.hxx"
#include "macromgr.hxx"
#include "queryentry.hxx"
#include <com/sun/star/document/XDocumentProperties.hpp>
#include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
#include <com/sun/star/script/ModuleInfo.hpp>
#include <com/sun/star/container/XIndexContainer.hpp>
#include <cppuhelper/component_context.hxx>
#include <cppuhelper/implbase.hxx>
#include "xltoolbar.hxx"
#include <oox/ole/vbaproject.hxx>
#include <oox/ole/olestorage.hxx>
#include <unotools/streamwrap.hxx>
#include <o3tl/make_unique.hxx>
using namespace com::sun::star;
using namespace ::comphelper;
//OleNameOverrideContainer
class OleNameOverrideContainer : public ::cppu::WeakImplHelper< container::XNameContainer >
{
private:
typedef std::unordered_map< OUString, uno::Reference< container::XIndexContainer >, OUStringHash > NamedIndexToOleName;
NamedIndexToOleName IdToOleNameHash;
::osl::Mutex m_aMutex;
public:
// XElementAccess
virtual uno::Type SAL_CALL getElementType( ) throw (uno::RuntimeException, std::exception) override { return cppu::UnoType<container::XIndexContainer>::get(); }
virtual sal_Bool SAL_CALL hasElements( ) throw (uno::RuntimeException, std::exception) override
{
::osl::MutexGuard aGuard( m_aMutex );
return ( !IdToOleNameHash.empty() );
}
// XNameAcess
virtual uno::Any SAL_CALL getByName( const OUString& aName ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception) override
{
::osl::MutexGuard aGuard( m_aMutex );
if ( !hasByName(aName) )
throw container::NoSuchElementException();
return uno::makeAny( IdToOleNameHash[ aName ] );
}
virtual uno::Sequence< OUString > SAL_CALL getElementNames( ) throw (uno::RuntimeException, std::exception) override
{
::osl::MutexGuard aGuard( m_aMutex );
return comphelper::mapKeysToSequence( IdToOleNameHash);
}
virtual sal_Bool SAL_CALL hasByName( const OUString& aName ) throw (uno::RuntimeException, std::exception) override
{
::osl::MutexGuard aGuard( m_aMutex );
return ( IdToOleNameHash.find( aName ) != IdToOleNameHash.end() );
}
// XNameContainer
virtual void SAL_CALL insertByName( const OUString& aName, const uno::Any& aElement ) throw(lang::IllegalArgumentException, container::ElementExistException, lang::WrappedTargetException, uno::RuntimeException, std::exception) override
{
::osl::MutexGuard aGuard( m_aMutex );
if ( hasByName( aName ) )
throw container::ElementExistException();
uno::Reference< container::XIndexContainer > xElement;
if ( ! ( aElement >>= xElement ) )
throw lang::IllegalArgumentException();
IdToOleNameHash[ aName ] = xElement;
}
virtual void SAL_CALL removeByName( const OUString& aName ) throw(container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception) override
{
::osl::MutexGuard aGuard( m_aMutex );
if ( !hasByName( aName ) )
throw container::NoSuchElementException();
IdToOleNameHash.erase( IdToOleNameHash.find( aName ) );
}
virtual void SAL_CALL replaceByName( const OUString& aName, const uno::Any& aElement ) throw(lang::IllegalArgumentException, container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception) override
{
::osl::MutexGuard aGuard( m_aMutex );
if ( !hasByName( aName ) )
throw container::NoSuchElementException();
uno::Reference< container::XIndexContainer > xElement;
if ( ! ( aElement >>= xElement ) )
throw lang::IllegalArgumentException();
IdToOleNameHash[ aName ] = xElement;
}
};
namespace {
/** Future Record Type header.
@return whether read rt matches nRecordID
*/
bool readFrtHeader( XclImpStream& rStrm, sal_uInt16 nRecordID )
{
sal_uInt16 nRt = rStrm.ReaduInt16();
rStrm.Ignore(10); // grbitFrt (2 bytes) and reserved (8 bytes)
return nRt == nRecordID;
}
}
ImportExcel8::ImportExcel8( XclImpRootData& rImpData, SvStream& rStrm ) :
ImportExcel( rImpData, rStrm )
{
// replace BIFF2-BIFF5 formula importer with BIFF8 formula importer
delete pFormConv;
pFormConv = pExcRoot->pFmlaConverter = new ExcelToSc8( GetRoot() );
}
ImportExcel8::~ImportExcel8()
{
}
void ImportExcel8::Calccount()
{
ScDocOptions aOpt = pD->GetDocOptions();
aOpt.SetIterCount( aIn.ReaduInt16() );
pD->SetDocOptions( aOpt );
}
void ImportExcel8::Precision()
{
ScDocOptions aOpt = pD->GetDocOptions();
aOpt.SetCalcAsShown( aIn.ReaduInt16() == 0 );
pD->SetDocOptions( aOpt );
}
void ImportExcel8::Delta()
{
ScDocOptions aOpt = pD->GetDocOptions();
aOpt.SetIterEps( aIn.ReadDouble() );
pD->SetDocOptions( aOpt );
}
void ImportExcel8::Iteration()
{
ScDocOptions aOpt = pD->GetDocOptions();
aOpt.SetIter( aIn.ReaduInt16() == 1 );
pD->SetDocOptions( aOpt );
}
void ImportExcel8::Boundsheet()
{
sal_uInt8 nLen;
sal_uInt16 nGrbit;
aIn.DisableDecryption();
maSheetOffsets.push_back( aIn.ReaduInt32() );
aIn.EnableDecryption();
nGrbit = aIn.ReaduInt16();
nLen = aIn.ReaduInt8();
OUString aName( aIn.ReadUniString( nLen ) );
GetTabInfo().AppendXclTabName( aName, nBdshtTab );
SCTAB nScTab = static_cast< SCTAB >( nBdshtTab );
if( nScTab > 0 )
{
OSL_ENSURE( !pD->HasTable( nScTab ), "ImportExcel8::Boundsheet - sheet exists already" );
pD->MakeTable( nScTab );
}
if( ( nGrbit & 0x0001 ) || ( nGrbit & 0x0002 ) )
pD->SetVisible( nScTab, false );
if( !pD->RenameTab( nScTab, aName ) )
{
pD->CreateValidTabName( aName );
pD->RenameTab( nScTab, aName );
}
nBdshtTab++;
}
void ImportExcel8::Scenman()
{
sal_uInt16 nLastDispl;
aIn.Ignore( 4 );
nLastDispl = aIn.ReaduInt16();
maScenList.nLastScenario = nLastDispl;
}
void ImportExcel8::Scenario()
{
maScenList.aEntries.push_back( o3tl::make_unique<ExcScenario>( aIn, *pExcRoot ) );
}
void ImportExcel8::Labelsst()
{
XclAddress aXclPos;
sal_uInt16 nXF;
sal_uInt32 nSst;
aIn >> aXclPos;
nXF = aIn.ReaduInt16();
nSst = aIn.ReaduInt32( );
ScAddress aScPos( ScAddress::UNINITIALIZED );
if( GetAddressConverter().ConvertAddress( aScPos, aXclPos, GetCurrScTab(), true ) )
{
GetXFRangeBuffer().SetXF( aScPos, nXF );
const XclImpString* pXclStr = GetSst().GetString(nSst);
if (pXclStr)
XclImpStringHelper::SetToDocument(GetDocImport(), aScPos, *this, *pXclStr, nXF);
}
}
void ImportExcel8::FeatHdr()
{
if (!readFrtHeader( aIn, 0x0867))
return;
// Feature type (isf) can be EXC_ISFPROTECTION, EXC_ISFFEC2 or
// EXC_ISFFACTOID.
sal_uInt16 nFeatureType = aIn.ReaduInt16();
if (nFeatureType != EXC_ISFPROTECTION)
// We currently only support import of enhanced protection data.
return;
aIn.Ignore(1); // always 1
GetSheetProtectBuffer().ReadOptions( aIn, GetCurrScTab() );
}
void ImportExcel8::Feat()
{
if (!readFrtHeader( aIn, 0x0868))
return;
// Feature type (isf) can be EXC_ISFPROTECTION, EXC_ISFFEC2 or
// EXC_ISFFACTOID.
sal_uInt16 nFeatureType = aIn.ReaduInt16();
if (nFeatureType != EXC_ISFPROTECTION)
// We currently only support import of enhanced protection data.
return;
aIn.Ignore(5); // reserved1 (1 byte) and reserved2 (4 bytes)
sal_uInt16 nCref = aIn.ReaduInt16(); // number of ref elements
aIn.Ignore(4); // size if EXC_ISFFEC2, else 0 and to be ignored
aIn.Ignore(2); // reserved3 (2 bytes)
ScEnhancedProtection aProt;
if (nCref)
{
XclRangeList aRefs;
aRefs.Read( aIn, true, nCref);
if (!aRefs.empty())
{
aProt.maRangeList = new ScRangeList;
GetAddressConverter().ConvertRangeList( *aProt.maRangeList, aRefs, GetCurrScTab(), false);
}
}
// FeatProtection structure follows in record.
aProt.mnAreserved = aIn.ReaduInt32();
aProt.mnPasswordVerifier = aIn.ReaduInt32();
aProt.maTitle = aIn.ReadUniString();
if ((aProt.mnAreserved & 0x00000001) == 0x00000001)
{
sal_uInt32 nCbSD = aIn.ReaduInt32();
// TODO: could here be some sanity check applied to not allocate 4GB?
aProt.maSecurityDescriptor.resize( nCbSD);
sal_Size nRead = aIn.Read( &aProt.maSecurityDescriptor.front(), nCbSD);
if (nRead < nCbSD)
aProt.maSecurityDescriptor.resize( nRead);
}
GetSheetProtectBuffer().AppendEnhancedProtection( aProt, GetCurrScTab() );
}
void ImportExcel8::ReadBasic()
{
SfxObjectShell* pShell = GetDocShell();
tools::SvRef<SotStorage> xRootStrg = GetRootStorage();
const SvtFilterOptions& rFilterOpt = SvtFilterOptions::Get();
if( pShell && xRootStrg.Is() ) try
{
// #FIXME need to get rid of this, we can also do this from within oox
// via the "ooo.vba.VBAGlobals" service
if( ( rFilterOpt.IsLoadExcelBasicCode() ||
rFilterOpt.IsLoadExcelBasicStorage() ) &&
rFilterOpt.IsLoadExcelBasicExecutable() )
{
// see if we have the XCB stream
tools::SvRef<SotStorageStream> xXCB = xRootStrg->OpenSotStream( "XCB", STREAM_STD_READ | StreamMode::NOCREATE );
if ( xXCB.Is()|| SVSTREAM_OK == xXCB->GetError() )
{
ScCTBWrapper wrapper;
if ( wrapper.Read( *xXCB ) )
{
#ifdef DEBUG_SC_EXCEL
wrapper.Print( stderr );
#endif
wrapper.ImportCustomToolBar( *pShell );
}
}
}
try
{
uno::Reference< uno::XComponentContext > aCtx( ::comphelper::getProcessComponentContext() );
SfxMedium& rMedium = GetMedium();
uno::Reference< io::XInputStream > xIn = rMedium.GetInputStream();
oox::ole::OleStorage root( aCtx, xIn, false );
oox::StorageRef vbaStg = root.openSubStorage( "_VBA_PROJECT_CUR", false );
if ( vbaStg.get() )
{
oox::ole::VbaProject aVbaPrj( aCtx, pShell->GetModel(), "Calc" );
// collect names of embedded form controls, as specified in the VBA project
uno::Reference< container::XNameContainer > xOleNameOverrideSink( new OleNameOverrideContainer );
aVbaPrj.setOleOverridesSink( xOleNameOverrideSink );
aVbaPrj.importVbaProject( *vbaStg );
GetObjectManager().SetOleNameOverrideInfo( xOleNameOverrideSink );
}
}
catch( uno::Exception& )
{
}
}
catch( uno::Exception& )
{
}
}
void ImportExcel8::EndSheet()
{
ImportExcel::EndSheet();
GetCondFormatManager().Apply();
GetValidationManager().Apply();
}
void ImportExcel8::PostDocLoad()
{
#if HAVE_FEATURE_SCRIPTING
// reading basic has been delayed until sheet objects (codenames etc.) are read
if( HasBasic() )
ReadBasic();
#endif
// #i11776# filtered ranges before outlines and hidden rows
if( pExcRoot->pAutoFilterBuffer )
pExcRoot->pAutoFilterBuffer->Apply();
GetWebQueryBuffer().Apply(); //TODO: test if extant
GetSheetProtectBuffer().Apply();
GetDocProtectBuffer().Apply();
ImportExcel::PostDocLoad();
// check scenarios; Attention: This increases the table count of the document!!
if( !pD->IsClipboard() && maScenList.aEntries.size() )
{
pD->UpdateChartListenerCollection(); // references in charts must be updated
maScenList.Apply( GetRoot() );
}
// read doc info (no docshell while pasting from clipboard)
LoadDocumentProperties();
// #i45843# Pivot tables are now handled outside of PostDocLoad, so they are available
// when formula cells are calculated, for the GETPIVOTDATA function.
}
void ImportExcel8::LoadDocumentProperties()
{
// no docshell while pasting from clipboard
if( SfxObjectShell* pShell = GetDocShell() )
{
// BIFF5+ without storage is possible
tools::SvRef<SotStorage> xRootStrg = GetRootStorage();
if( xRootStrg.Is() ) try
{
uno::Reference< document::XDocumentPropertiesSupplier > xDPS( pShell->GetModel(), uno::UNO_QUERY_THROW );
uno::Reference< document::XDocumentProperties > xDocProps( xDPS->getDocumentProperties(), uno::UNO_SET_THROW );
sfx2::LoadOlePropertySet( xDocProps, xRootStrg );
}
catch( uno::Exception& )
{
}
}
}
// autofilter
void ImportExcel8::FilterMode()
{
// The FilterMode record exists: if either the AutoFilter
// record exists or an Advanced Filter is saved and stored
// in the sheet. Thus if the FilterMode records only exists
// then the latter is true..
if( !pExcRoot->pAutoFilterBuffer ) return;
XclImpAutoFilterData* pData = pExcRoot->pAutoFilterBuffer->GetByTab( GetCurrScTab() );
if( pData )
pData->SetAutoOrAdvanced();
}
void ImportExcel8::AutoFilterInfo()
{
if( !pExcRoot->pAutoFilterBuffer ) return;
XclImpAutoFilterData* pData = pExcRoot->pAutoFilterBuffer->GetByTab( GetCurrScTab() );
if( pData )
{
pData->SetAdvancedRange( nullptr );
pData->Activate();
}
}
void ImportExcel8::AutoFilter()
{
if( !pExcRoot->pAutoFilterBuffer ) return;
XclImpAutoFilterData* pData = pExcRoot->pAutoFilterBuffer->GetByTab( GetCurrScTab() );
if( pData )
pData->ReadAutoFilter(aIn, GetDoc().GetSharedStringPool());
}
XclImpAutoFilterData::XclImpAutoFilterData( RootData* pRoot, const ScRange& rRange ) :
ExcRoot( pRoot ),
pCurrDBData(nullptr),
bActive( false ),
bCriteria( false ),
bAutoOrAdvanced(false)
{
aParam.nCol1 = rRange.aStart.Col();
aParam.nRow1 = rRange.aStart.Row();
aParam.nTab = rRange.aStart.Tab();
aParam.nCol2 = rRange.aEnd.Col();
aParam.nRow2 = rRange.aEnd.Row();
aParam.bInplace = true;
}
namespace {
OUString CreateFromDouble( double fVal )
{
return rtl::math::doubleToUString(fVal,
rtl_math_StringFormat_Automatic, rtl_math_DecimalPlaces_Max,
ScGlobal::pLocaleData->getNumDecimalSep()[0], true);
}
}
void XclImpAutoFilterData::SetCellAttribs()
{
ScDocument& rDoc = pExcRoot->pIR->GetDoc();
for ( SCCOL nCol = StartCol(); nCol <= EndCol(); nCol++ )
{
ScMF nFlag = static_cast<const ScMergeFlagAttr*>( rDoc.GetAttr( nCol, StartRow(), Tab(), ATTR_MERGE_FLAG ))->GetValue();
rDoc.ApplyAttr( nCol, StartRow(), Tab(), ScMergeFlagAttr( nFlag | ScMF::Auto) );
}
}
void XclImpAutoFilterData::InsertQueryParam()
{
if (pCurrDBData)
{
ScRange aAdvRange;
bool bHasAdv = pCurrDBData->GetAdvancedQuerySource( aAdvRange );
if( bHasAdv )
pExcRoot->pIR->GetDoc().CreateQueryParam( aAdvRange.aStart.Col(),
aAdvRange.aStart.Row(), aAdvRange.aEnd.Col(), aAdvRange.aEnd.Row(),
aAdvRange.aStart.Tab(), aParam );
pCurrDBData->SetQueryParam( aParam );
if( bHasAdv )
pCurrDBData->SetAdvancedQuerySource( &aAdvRange );
else
{
pCurrDBData->SetAutoFilter( true );
SetCellAttribs();
}
}
}
static void ExcelQueryToOooQuery( OUString& aStr, ScQueryEntry& rEntry )
{
if (rEntry.eOp != SC_EQUAL && rEntry.eOp != SC_NOT_EQUAL)
return;
sal_Int32 nLen = aStr.getLength();
sal_Unicode nStart = aStr[0];
sal_Unicode nEnd = aStr[ nLen-1 ];
if( nLen > 2 && nStart == '*' && nEnd == '*' )
{
aStr = aStr.copy( 1, nLen-2 );
rEntry.eOp = ( rEntry.eOp == SC_EQUAL ) ? SC_CONTAINS : SC_DOES_NOT_CONTAIN;
}
else if( nLen > 1 && nStart == '*' && nEnd != '*' )
{
aStr = aStr.copy( 1 );
rEntry.eOp = ( rEntry.eOp == SC_EQUAL ) ? SC_ENDS_WITH : SC_DOES_NOT_END_WITH;
}
else if( nLen > 1 && nStart != '*' && nEnd == '*' )
{
aStr = aStr.copy( 0, nLen-1 );
rEntry.eOp = ( rEntry.eOp == SC_EQUAL ) ? SC_BEGINS_WITH : SC_DOES_NOT_BEGIN_WITH;
}
else if( nLen == 2 && nStart == '*' && nEnd == '*' )
{
aStr = aStr.copy( 1 );
}
}
void XclImpAutoFilterData::ReadAutoFilter(
XclImpStream& rStrm, svl::SharedStringPool& rPool )
{
sal_uInt16 nCol, nFlags;
nCol = rStrm.ReaduInt16();
nFlags = rStrm.ReaduInt16();
ScQueryConnect eConn = ::get_flagvalue( nFlags, EXC_AFFLAG_ANDORMASK, SC_OR, SC_AND );
bool bSimple1 = ::get_flag(nFlags, EXC_AFFLAG_SIMPLE1);
bool bSimple2 = ::get_flag(nFlags, EXC_AFFLAG_SIMPLE2);
bool bTop10 = ::get_flag(nFlags, EXC_AFFLAG_TOP10);
bool bTopOfTop10 = ::get_flag(nFlags, EXC_AFFLAG_TOP10TOP);
bool bPercent = ::get_flag(nFlags, EXC_AFFLAG_TOP10PERC);
sal_uInt16 nCntOfTop10 = nFlags >> 7;
if( bTop10 )
{
ScQueryEntry& aEntry = aParam.AppendEntry();
ScQueryEntry::Item& rItem = aEntry.GetQueryItem();
aEntry.bDoQuery = true;
aEntry.nField = static_cast<SCCOLROW>(StartCol() + static_cast<SCCOL>(nCol));
aEntry.eOp = bTopOfTop10 ?
(bPercent ? SC_TOPPERC : SC_TOPVAL) : (bPercent ? SC_BOTPERC : SC_BOTVAL);
aEntry.eConnect = SC_AND;
rItem.meType = ScQueryEntry::ByString;
rItem.maString = rPool.intern(OUString::number(nCntOfTop10));
rStrm.Ignore(20);
return;
}
sal_uInt8 nType, nOper, nBoolErr, nVal;
sal_Int32 nRK;
double fVal;
sal_uInt8 nStrLen[2] = { 0, 0 };
ScQueryEntry aEntries[2];
for (size_t nE = 0; nE < 2; ++nE)
{
ScQueryEntry& rEntry = aEntries[nE];
ScQueryEntry::Item& rItem = rEntry.GetQueryItem();
bool bIgnore = false;
nType = rStrm.ReaduInt8();
nOper = rStrm.ReaduInt8();
switch( nOper )
{
case EXC_AFOPER_LESS:
rEntry.eOp = SC_LESS;
break;
case EXC_AFOPER_EQUAL:
rEntry.eOp = SC_EQUAL;
break;
case EXC_AFOPER_LESSEQUAL:
rEntry.eOp = SC_LESS_EQUAL;
break;
case EXC_AFOPER_GREATER:
rEntry.eOp = SC_GREATER;
break;
case EXC_AFOPER_NOTEQUAL:
rEntry.eOp = SC_NOT_EQUAL;
break;
case EXC_AFOPER_GREATEREQUAL:
rEntry.eOp = SC_GREATER_EQUAL;
break;
default:
rEntry.eOp = SC_EQUAL;
}
switch( nType )
{
case EXC_AFTYPE_RK:
nRK = rStrm.ReadInt32();
rStrm.Ignore( 4 );
rItem.maString = rPool.intern(
CreateFromDouble(XclTools::GetDoubleFromRK(nRK)));
break;
case EXC_AFTYPE_DOUBLE:
fVal = rStrm.ReadDouble();
rItem.maString = rPool.intern(CreateFromDouble(fVal));
break;
case EXC_AFTYPE_STRING:
rStrm.Ignore( 4 );
nStrLen[ nE ] = rStrm.ReaduInt8();
rStrm.Ignore( 3 );
rItem.maString = svl::SharedString();
break;
case EXC_AFTYPE_BOOLERR:
nBoolErr = rStrm.ReaduInt8();
nVal = rStrm.ReaduInt8();
rStrm.Ignore( 6 );
rItem.maString = rPool.intern(OUString::number(nVal));
bIgnore = (nBoolErr != 0);
break;
case EXC_AFTYPE_EMPTY:
rEntry.SetQueryByEmpty();
break;
case EXC_AFTYPE_NOTEMPTY:
rEntry.SetQueryByNonEmpty();
break;
default:
rStrm.Ignore( 8 );
bIgnore = true;
}
if (!bIgnore)
{
rEntry.bDoQuery = true;
rItem.meType = ScQueryEntry::ByString;
rEntry.nField = static_cast<SCCOLROW>(StartCol() + static_cast<SCCOL>(nCol));
rEntry.eConnect = nE ? eConn : SC_AND;
}
}
if (eConn == SC_AND)
{
for (size_t nE = 0; nE < 2; ++nE)
{
if (nStrLen[nE] && aEntries[nE].bDoQuery)
{
OUString aStr = rStrm.ReadUniString(nStrLen[nE]);
ExcelQueryToOooQuery(aStr, aEntries[nE]);
aEntries[nE].GetQueryItem().maString = rPool.intern(aStr);
aParam.AppendEntry() = aEntries[nE];
}
}
}
else
{
OSL_ASSERT(eConn == SC_OR);
// Import only when both conditions are for simple equality, else
// import only the 1st condition due to conflict with the ordering of
// conditions. #i39464#.
//
// Example: Let A1 be a condition of column A, and B1 and B2
// conditions of column B, connected with OR. Excel performs 'A1 AND
// (B1 OR B2)' in this case, but Calc would do '(A1 AND B1) OR B2'
// instead.
if (bSimple1 && bSimple2 && nStrLen[0] && nStrLen[1])
{
// Two simple OR'ed equal conditions. We can import this correctly.
ScQueryEntry& rEntry = aParam.AppendEntry();
rEntry.bDoQuery = true;
rEntry.eOp = SC_EQUAL;
rEntry.eConnect = SC_AND;
ScQueryEntry::QueryItemsType aItems;
aItems.reserve(2);
ScQueryEntry::Item aItem1, aItem2;
aItem1.maString = rPool.intern(rStrm.ReadUniString(nStrLen[0]));
aItem1.meType = ScQueryEntry::ByString;
aItem2.maString = rPool.intern(rStrm.ReadUniString(nStrLen[1]));
aItem2.meType = ScQueryEntry::ByString;
aItems.push_back(aItem1);
aItems.push_back(aItem2);
rEntry.GetQueryItems().swap(aItems);
}
else if (nStrLen[0] && aEntries[0].bDoQuery)
{
// Due to conflict, we can import only the first condition.
OUString aStr = rStrm.ReadUniString(nStrLen[0]);
ExcelQueryToOooQuery(aStr, aEntries[0]);
aEntries[0].GetQueryItem().maString = rPool.intern(aStr);
aParam.AppendEntry() = aEntries[0];
}
}
}
void XclImpAutoFilterData::SetAdvancedRange( const ScRange* pRange )
{
if (pRange)
{
aCriteriaRange = *pRange;
bCriteria = true;
}
else
bCriteria = false;
}
void XclImpAutoFilterData::SetExtractPos( const ScAddress& rAddr )
{
aParam.nDestCol = rAddr.Col();
aParam.nDestRow = rAddr.Row();
aParam.nDestTab = rAddr.Tab();
aParam.bInplace = false;
aParam.bDestPers = true;
}
void XclImpAutoFilterData::Apply()
{
CreateScDBData();
if( bActive )
{
InsertQueryParam();
}
}
void XclImpAutoFilterData::CreateScDBData()
{
// Create the ScDBData() object if the AutoFilter is activated
// or if we need to create the Advanced Filter.
if( bActive || bCriteria)
{
ScDocument& rDoc = pExcRoot->pIR->GetDocRef();
OUString aNewName(STR_DB_LOCAL_NONAME);
pCurrDBData = new ScDBData(aNewName , Tab(),
StartCol(),StartRow(), EndCol(),EndRow() );
if(bCriteria)
{
EnableRemoveFilter();
pCurrDBData->SetQueryParam( aParam );
pCurrDBData->SetAdvancedQuerySource(&aCriteriaRange);
}
else
pCurrDBData->SetAdvancedQuerySource(nullptr);
rDoc.SetAnonymousDBData(Tab(), pCurrDBData);
}
}
void XclImpAutoFilterData::EnableRemoveFilter()
{
// only if this is a saved Advanced filter
if( !bActive && bAutoOrAdvanced )
{
ScQueryEntry& aEntry = aParam.AppendEntry();
aEntry.bDoQuery = true;
}
// TBD: force the automatic activation of the
// "Remove Filter" by setting a virtual mouse click
// inside the advanced range
}
void XclImpAutoFilterBuffer::Insert( RootData* pRoot, const ScRange& rRange)
{
if( !GetByTab( rRange.aStart.Tab() ) )
maFilters.push_back( std::make_shared<XclImpAutoFilterData>( pRoot, rRange ));
}
void XclImpAutoFilterBuffer::AddAdvancedRange( const ScRange& rRange )
{
XclImpAutoFilterData* pData = GetByTab( rRange.aStart.Tab() );
if( pData )
pData->SetAdvancedRange( &rRange );
}
void XclImpAutoFilterBuffer::AddExtractPos( const ScRange& rRange )
{
XclImpAutoFilterData* pData = GetByTab( rRange.aStart.Tab() );
if( pData )
pData->SetExtractPos( rRange.aStart );
}
void XclImpAutoFilterBuffer::Apply()
{
for( const auto& rFilterPtr : maFilters )
rFilterPtr->Apply();
}
XclImpAutoFilterData* XclImpAutoFilterBuffer::GetByTab( SCTAB nTab )
{
for( const auto& rFilterPtr : maFilters )
{
if( rFilterPtr->Tab() == nTab )
return rFilterPtr.get();
}
return nullptr;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|