LCOV - code coverage report
Current view: top level - sc/source/filter/oox - formulabuffer.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 94 112 83.9 %
Date: 2012-08-25 Functions: 12 15 80.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 115 248 46.4 %

           Branch data     Line data    Source code
       1                 :            : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2                 :            : /*
       3                 :            :  * Copyright 2012 LibreOffice contributors.
       4                 :            :  *
       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/.
       8                 :            :  */
       9                 :            : 
      10                 :            : #include "formulabuffer.hxx"
      11                 :            : #include "formulaparser.hxx"
      12                 :            : #include <com/sun/star/sheet/XFormulaTokens.hpp>
      13                 :            : #include <com/sun/star/sheet/XArrayFormulaTokens.hpp>
      14                 :            : #include <com/sun/star/container/XIndexAccess.hpp>
      15                 :            : #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
      16                 :            : #include <com/sun/star/table/XCell2.hpp>
      17                 :            : #include "cell.hxx"
      18                 :            : #include "document.hxx"
      19                 :            : #include "convuno.hxx"
      20                 :            : 
      21                 :            : #include "rangelst.hxx"
      22                 :            : #include "autonamecache.hxx"
      23                 :            : #include "tokenuno.hxx"
      24                 :            : 
      25                 :            : namespace oox {
      26                 :            : namespace xls {
      27                 :            : 
      28                 :            : using namespace ::com::sun::star::uno;
      29                 :            : using namespace ::com::sun::star::table;
      30                 :            : using namespace ::com::sun::star::uno;
      31                 :            : using namespace ::com::sun::star::sheet;
      32                 :            : using namespace ::com::sun::star::container;
      33                 :            : 
      34 [ +  - ][ +  - ]:         24 : FormulaBuffer::FormulaBuffer( const WorkbookHelper& rHelper ) : WorkbookHelper( rHelper )
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
      35                 :            : {
      36                 :         24 : }
      37                 :            : 
      38                 :          0 : Reference< XCellRange > FormulaBuffer::getRange( const CellRangeAddress& rRange)
      39                 :            : {
      40                 :          0 :     Reference< XCellRange > xRange;
      41                 :            :     try
      42                 :            :     {
      43 [ #  # ][ #  # ]:          0 :         xRange = mxCurrSheet->getCellRangeByPosition( rRange.StartColumn, rRange.StartRow, rRange.EndColumn, rRange.EndRow );
         [ #  # ][ #  # ]
      44                 :            :     }
      45         [ #  # ]:          0 :     catch( Exception& )
      46                 :            :     {
      47                 :            :     }
      48                 :          0 :     return xRange;
      49                 :            : }
      50                 :            : 
      51                 :         24 : void FormulaBuffer::finalizeImport()
      52                 :            : {
      53 [ +  - ][ +  - ]:         24 :     ISegmentProgressBarRef xFormulaBar = getProgressBar().createSegment( getProgressBar().getFreeLength() );
         [ +  - ][ +  - ]
      54                 :            : 
      55         [ +  - ]:         24 :     ScDocument& rDoc = getScDocument();
      56 [ +  - ][ +  - ]:         24 :     Reference< XIndexAccess > xSheets( getDocument()->getSheets(), UNO_QUERY_THROW );
         [ +  - ][ +  - ]
      57 [ +  - ][ +  - ]:         24 :     rDoc.SetAutoNameCache( new ScAutoNameCache( &rDoc ) );
                 [ +  - ]
      58 [ +  - ][ +  - ]:         84 :     for ( sal_Int16 nTab = 0, nElem = xSheets->getCount(); nTab < nElem; ++nTab )
                 [ +  + ]
      59                 :            :     {
      60                 :         60 :         double fPosition = static_cast< double> (nTab + 1) /static_cast<double>(nElem);
      61         [ +  - ]:         60 :         xFormulaBar->setPosition( fPosition );
      62 [ +  - ][ +  - ]:         60 :         mxCurrSheet = getSheetFromDoc( nTab );
      63                 :            :         // process shared Formula
      64         [ +  - ]:         60 :         SheetToFormulaEntryMap::iterator sharedIt = sharedFormulas.find( nTab );
      65         [ +  + ]:         60 :         if ( sharedIt != sharedFormulas.end() )
      66                 :            :         {
      67                 :            :             // shared id ( to create the special shared names from )
      68                 :          3 :             std::vector<SharedFormulaEntry>& rSharedFormulas = sharedIt->second;
      69 [ +  - ][ +  + ]:          6 :             for ( std::vector<SharedFormulaEntry>::iterator it = rSharedFormulas.begin(), it_end = rSharedFormulas.end(); it != it_end; ++it )
      70                 :            :             {
      71         [ +  - ]:          3 :                  createSharedFormula( it->maAddress, it->mnSharedId, it->maTokenStr );
      72                 :            :             }
      73                 :            :         }
      74                 :            :         // now process any defined shared formulae
      75         [ +  - ]:         60 :         SheetToSharedFormulaid::iterator formulDescIt = sharedFormulaIds.find( nTab );
      76         [ +  - ]:         60 :         SheetToSharedIdToTokenIndex::iterator tokensIt = tokenIndexes.find( nTab );
      77 [ +  + ][ +  - ]:         60 :         if ( formulDescIt != sharedFormulaIds.end() && tokensIt !=  tokenIndexes.end() )
         [ +  - ][ +  + ]
         [ +  - ][ +  +  
             #  #  #  # ]
      78                 :            :         {
      79         [ +  - ]:          3 :             SharedIdToTokenIndex& rTokenIdMap = tokensIt->second;
      80                 :          3 :             std::vector< SharedFormulaDesc >& rVector = formulDescIt->second;
      81 [ +  - ][ +  + ]:         24 :             for ( std::vector< SharedFormulaDesc >::iterator it = rVector.begin(), it_end = rVector.end(); it != it_end; ++it )
      82                 :            :             {
      83                 :            :                 // see if we have a
      84                 :            :                 // resolved tokenId
      85                 :         21 :                 CellAddress& rAddress = it->first;
      86                 :         21 :                 sal_Int32& rnSharedId = it->second;
      87         [ +  - ]:         21 :                 SharedIdToTokenIndex::iterator itTokenId =  rTokenIdMap.find( rnSharedId );
      88 [ +  - ][ +  - ]:         21 :                 if ( itTokenId != rTokenIdMap.end() )
      89                 :            :                 {
      90 [ +  - ][ +  - ]:         21 :                     ApiTokenSequence aTokens =  getFormulaParser().convertNameToFormula( itTokenId->second );
                 [ +  - ]
      91 [ +  - ][ +  - ]:         21 :                     applyCellFormula( rDoc, aTokens, rAddress );
      92                 :            :                 }
      93                 :            :             }
      94                 :            :         }
      95                 :            : 
      96         [ +  - ]:         60 :         FormulaDataMap::iterator cellIt = cellFormulas.find( nTab );
      97         [ +  + ]:         60 :         if ( cellIt != cellFormulas.end() )
      98                 :            :         {
      99         [ +  - ]:         21 :             applyCellFormulas( cellIt->second );
     100                 :            :         }
     101                 :            : 
     102         [ +  - ]:         60 :         FormulaValueMap::iterator itValues = cellFormulaValues.find( nTab );
     103         [ +  + ]:         60 :         if ( itValues != cellFormulaValues.end() )
     104                 :            :         {
     105                 :         15 :             std::vector< ValueAddressPair > & rVector = itValues->second;
     106         [ +  - ]:         15 :             applyCellFormulaValues( rVector );
     107                 :            :         }
     108                 :            : 
     109         [ +  - ]:         60 :         ArrayFormulaDataMap::iterator itArray = cellArrayFormulas.find( nTab );
     110                 :            : 
     111         [ -  + ]:         60 :         if ( itArray != cellArrayFormulas.end() )
     112                 :            :         {
     113         [ #  # ]:          0 :             applyArrayFormulas( itArray->second );
     114                 :            :         }
     115                 :            :     }
     116         [ +  - ]:         24 :     rDoc.SetAutoNameCache( NULL );
     117 [ +  - ][ +  - ]:         24 :     xFormulaBar->setPosition( 1.0 );
     118                 :         24 : }
     119                 :        102 : void FormulaBuffer::applyCellFormula( ScDocument& rDoc, const ApiTokenSequence& rTokens, const ::com::sun::star::table::CellAddress& rAddress )
     120                 :            : {
     121         [ +  - ]:        102 :         ScTokenArray aTokenArray;
     122                 :        102 :         ScAddress aCellPos;
     123                 :        102 :         ScUnoConversion::FillScAddress( aCellPos, rAddress );
     124         [ +  - ]:        102 :         ScTokenConversion::ConvertToTokenArray( rDoc, aTokenArray, rTokens );
     125 [ +  - ][ +  - ]:        102 :         ScBaseCell* pNewCell = new ScFormulaCell( &rDoc, aCellPos, &aTokenArray );
                 [ +  - ]
     126 [ +  - ][ +  - ]:        102 :         rDoc.PutCell( aCellPos, pNewCell, sal_True );
     127                 :        102 : }
     128                 :            : 
     129                 :         21 : void FormulaBuffer::applyCellFormulas( const std::vector< TokenAddressItem >& rVector )
     130                 :            : {
     131                 :         21 :     ScDocument& rDoc = getScDocument();
     132 [ +  - ][ +  + ]:        102 :     for ( std::vector< TokenAddressItem >::const_iterator it = rVector.begin(), it_end = rVector.end(); it != it_end; ++it )
     133                 :            :     {
     134                 :         81 :         const ::com::sun::star::table::CellAddress& rAddress = it->maCellAddress;
     135 [ +  - ][ +  - ]:         81 :         ApiTokenSequence aTokens = getFormulaParser().importFormula( rAddress, it->maTokenStr );
     136         [ +  - ]:         81 :         applyCellFormula( rDoc, aTokens, rAddress );
     137         [ +  - ]:         81 :     }
     138                 :         21 : }
     139                 :            : 
     140                 :         15 : void FormulaBuffer::applyCellFormulaValues( const std::vector< ValueAddressPair >& rVector )
     141                 :            : {
     142                 :         15 :     ScDocument& rDoc = getScDocument();
     143 [ +  - ][ +  + ]:        108 :     for ( std::vector< ValueAddressPair >::const_iterator it = rVector.begin(), it_end = rVector.end(); it != it_end; ++it )
     144                 :            :     {
     145                 :         93 :         ScAddress aCellPos;
     146                 :         93 :         ScUnoConversion::FillScAddress( aCellPos, it->first );
     147         [ +  - ]:         93 :         ScBaseCell* pBaseCell = rDoc.GetCell( aCellPos );
     148         [ +  - ]:         93 :         if ( pBaseCell->GetCellType() == CELLTYPE_FORMULA )
     149                 :            :         {
     150         [ +  - ]:         93 :             ScFormulaCell* pCell = static_cast< ScFormulaCell* >( pBaseCell );
     151         [ +  - ]:         93 :             pCell->SetHybridDouble( it->second );
     152                 :         93 :             pCell->ResetDirty();
     153         [ +  - ]:         93 :             pCell->ResetChanged();
     154                 :            :         }
     155                 :            :     }
     156                 :         15 : }
     157                 :            : 
     158                 :          0 : void FormulaBuffer::applyArrayFormulas( const std::vector< TokenRangeAddressItem >& rVector )
     159                 :            : {
     160 [ #  # ][ #  # ]:          0 :     for ( std::vector< TokenRangeAddressItem >::const_iterator it = rVector.begin(), it_end = rVector.end(); it != it_end; ++it )
     161                 :            :     {
     162 [ #  # ][ #  # ]:          0 :         Reference< XArrayFormulaTokens > xTokens( getRange( it->maCellRangeAddress ), UNO_QUERY );
     163                 :            :         OSL_ENSURE( xTokens.is(), "SheetDataBuffer::finalizeArrayFormula - missing formula token interface" );
     164 [ #  # ][ #  # ]:          0 :         ApiTokenSequence aTokens = getFormulaParser().importFormula( it->maTokenAndAddress.maCellAddress, it->maTokenAndAddress.maTokenStr );
     165         [ #  # ]:          0 :         if( xTokens.is() )
     166 [ #  # ][ #  # ]:          0 :             xTokens->setArrayTokens( aTokens );
     167         [ #  # ]:          0 :     }
     168                 :          0 : }
     169                 :            : 
     170                 :          3 : void FormulaBuffer::createSharedFormulaMapEntry( const ::com::sun::star::table::CellAddress& rAddress, sal_Int32 nSharedId, const rtl::OUString& rTokens )
     171                 :            : {
     172         [ +  - ]:          3 :      std::vector<SharedFormulaEntry>& rSharedFormulas = sharedFormulas[ rAddress.Sheet ];
     173         [ +  - ]:          3 :     SharedFormulaEntry aEntry( rAddress, rTokens, nSharedId );
     174 [ +  - ][ +  - ]:          3 :     rSharedFormulas.push_back( aEntry );
     175                 :          3 : }
     176                 :            : 
     177                 :         81 : void FormulaBuffer::setCellFormula( const ::com::sun::star::table::CellAddress& rAddress, const rtl::OUString& rTokenStr )
     178                 :            : {
     179 [ +  - ][ +  - ]:         81 :     cellFormulas[ rAddress.Sheet ].push_back( TokenAddressItem( rTokenStr, rAddress ) );
     180                 :         81 : }
     181                 :            : 
     182                 :         21 : void FormulaBuffer::setCellFormula( const ::com::sun::star::table::CellAddress& rAddress, sal_Int32 nSharedId )
     183                 :            : {
     184 [ +  - ][ +  - ]:         21 :     sharedFormulaIds[ rAddress.Sheet ].push_back( SharedFormulaDesc( rAddress, nSharedId ) );
     185                 :         21 : }
     186                 :            : 
     187                 :          0 : void FormulaBuffer::setCellArrayFormula( const ::com::sun::star::table::CellRangeAddress& rRangeAddress, const ::com::sun::star::table::CellAddress& rTokenAddress, const rtl::OUString& rTokenStr )
     188                 :            : {
     189                 :            : 
     190                 :          0 :     TokenAddressItem tokenPair( rTokenStr, rTokenAddress );
     191 [ #  # ][ #  # ]:          0 :     cellArrayFormulas[ rRangeAddress.Sheet ].push_back( TokenRangeAddressItem( tokenPair, rRangeAddress ) );
     192                 :          0 : }
     193                 :            : 
     194                 :         93 : void FormulaBuffer::setCellFormulaValue( const ::com::sun::star::table::CellAddress& rAddress, double fValue )
     195                 :            : {
     196 [ +  - ][ +  - ]:         93 :     cellFormulaValues[ rAddress.Sheet ].push_back( ValueAddressPair( rAddress, fValue ) );
     197                 :         93 : }
     198                 :            : 
     199                 :          3 : void  FormulaBuffer::createSharedFormula( const ::com::sun::star::table::CellAddress& rAddress,  sal_Int32 nSharedId, const rtl::OUString& rTokenStr )
     200                 :            : {
     201 [ +  - ][ +  - ]:          3 :     ApiTokenSequence aTokens = getFormulaParser().importFormula( rAddress, rTokenStr );
     202         [ +  - ]:          6 :     rtl::OUString aName = rtl::OUStringBuffer().appendAscii( RTL_CONSTASCII_STRINGPARAM( "__shared_" ) ).
     203         [ +  - ]:          3 :         append( static_cast< sal_Int32 >( rAddress.Sheet + 1 ) ).
     204 [ +  - ][ +  - ]:          3 :         append( sal_Unicode( '_' ) ).append( nSharedId ).
     205 [ +  - ][ +  - ]:          9 :         append( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("_0") ) ).makeStringAndClear();
                 [ +  - ]
     206         [ +  - ]:          3 :     ScRangeData* pScRangeData  = createNamedRangeObject( aName, aTokens, 0  );
     207                 :            : 
     208                 :          3 :     pScRangeData->SetType(RT_SHARED);
     209                 :          3 :     sal_Int32 nTokenIndex = static_cast< sal_Int32 >( pScRangeData->GetIndex() );
     210                 :            : 
     211                 :            :         // store the token index in the map
     212 [ +  - ][ +  - ]:          3 :    tokenIndexes[  rAddress.Sheet ][ nSharedId ] = nTokenIndex;
                 [ +  - ]
     213                 :          3 : }
     214                 :            : } // namespace xls
     215 [ +  - ][ +  - ]:         24 : } // namespace oox
     216                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10