From dbc8b7a93bed16df11a16a928a9638f01f73966d Mon Sep 17 00:00:00 2001 From: Markus Mohrhard Date: Fri, 29 Apr 2011 01:41:45 +0200 Subject: support export of anonymous db data to OOXML --- sc/source/filter/excel/xeformula.cxx | 8 ---- sc/source/filter/excel/xename.cxx | 90 ++++++------------------------------ sc/source/filter/excel/xltools.cxx | 7 +++ sc/source/filter/inc/xltools.hxx | 5 ++ 4 files changed, 26 insertions(+), 84 deletions(-) diff --git a/sc/source/filter/excel/xeformula.cxx b/sc/source/filter/excel/xeformula.cxx index 834592e68..9dbb277b4 100644 --- a/sc/source/filter/excel/xeformula.cxx +++ b/sc/source/filter/excel/xeformula.cxx @@ -418,7 +418,6 @@ private: void ProcessExternalRangeRef( const XclExpScToken& rTokData ); void ProcessDefinedName( const XclExpScToken& rTokData ); void ProcessExternalName( const XclExpScToken& rTokData ); - void ProcessDatabaseArea( const XclExpScToken& rTokData ); // token vector ----------------------------------------------------------- @@ -1184,7 +1183,6 @@ XclExpScToken XclExpFmlaCompImpl::Factor( XclExpScToken aTokData ) case ocBad: ProcessBad( aTokData ); break; case ocOpen: ProcessParentheses( aTokData ); break; case ocName: ProcessDefinedName( aTokData ); break; - case ocDBArea: ProcessDatabaseArea( aTokData ); break; case ocFalse: case ocTrue: ProcessBoolean( aTokData ); break; case ocDde: ProcessDdeLink( aTokData ); break; @@ -2138,12 +2136,6 @@ void XclExpFmlaCompImpl::ProcessExternalName( const XclExpScToken& rTokData ) AppendErrorToken( EXC_ERR_NAME, rTokData.mnSpaces ); } -void XclExpFmlaCompImpl::ProcessDatabaseArea( const XclExpScToken& rTokData ) -{ - sal_uInt16 nNameIdx = GetNameManager().InsertDBRange( rTokData.mpScToken->GetIndex() ); - AppendNameToken( nNameIdx, rTokData.mnSpaces ); -} - // token vector --------------------------------------------------------------- void XclExpFmlaCompImpl::PushOperandPos( sal_uInt16 nTokPos ) diff --git a/sc/source/filter/excel/xename.cxx b/sc/source/filter/excel/xename.cxx index c5c2ea3ba..b4610837d 100644 --- a/sc/source/filter/excel/xename.cxx +++ b/sc/source/filter/excel/xename.cxx @@ -38,6 +38,7 @@ #include "dbcolect.hxx" #include "xehelper.hxx" #include "xelink.hxx" +#include "globalnames.hxx" // for filter manager #include "excrecds.hxx" @@ -138,11 +139,10 @@ public: /** Inserts the Calc name with the passed index and returns the Excel NAME index. */ sal_uInt16 InsertName( SCTAB nTab, sal_uInt16 nScNameIdx ); - /** Inserts the Calc database range with the passed index and returns the Excel NAME index. */ - sal_uInt16 InsertDBRange( sal_uInt16 nScDBRangeIdx ); /** Inserts a new built-in defined name. */ sal_uInt16 InsertBuiltInName( sal_Unicode cBuiltIn, XclTokenArrayRef xTokArr, SCTAB nScTab ); + sal_uInt16 InsertBuiltInName( sal_Unicode cBuiltIn, XclTokenArrayRef xTokArr, const ScRange& aRange ); /** Inserts a new defined name. Sets another unused name, if rName already exists. */ sal_uInt16 InsertUniqueName( const String& rName, XclTokenArrayRef xTokArr, SCTAB nScTab ); /** Returns index of an existing name, or creates a name without definition. */ @@ -179,10 +179,6 @@ private: */ sal_uInt16 FindNamedExpIndex( SCTAB nTab, sal_uInt16 nScIdx ); - /** - * Find the index of a NAME record from Calc's database range index. - */ - sal_uInt16 FindDBNameIdx( sal_uInt16 nScIdx ) const; /** Returns the index of an existing built-in NAME record with the passed definition, otherwise 0. */ sal_uInt16 FindBuiltInNameIdx( const String& rName, const XclTokenArray& rTokArr, bool bDBRange ) const; @@ -203,8 +199,6 @@ private: void CreateBuiltInNames(); /** Creates NAME records for all user-defined names in the document. */ void CreateUserNames(); - /** Creates NAME records for all database ranges in the document. */ - void CreateDatabaseNames(); private: /** @@ -214,8 +208,6 @@ private: */ NamedExpIndexMap maNamedExpMap; XclExpNameList maNameList; /// List of NAME records. - XclExpIndexMap maDBRangeMap; /// Maps Calc database ranges to Excel NAME records. - String maUnnamedDBName; /// Name of the hidden unnamed database range. size_t mnFirstUserIdx; /// List index of first user-defined NAME record. }; @@ -254,9 +246,11 @@ XclExpName::XclExpName( const XclExpRoot& rRoot, sal_Unicode cBuiltIn ) : { String aName( XclTools::GetXclBuiltInDefName( EXC_BUILTIN_FILTERDATABASE ) ); mxName = XclExpStringHelper::CreateString( rRoot, aName, EXC_STR_8BITLENGTH ); + maOrigName = XclTools::GetXclBuiltInDefName( cBuiltIn ); } else { + maOrigName = XclTools::GetBuiltInDefNameXml( cBuiltIn ) ; mxName = XclExpStringHelper::CreateString( rRoot, cBuiltIn, EXC_STR_8BITLENGTH ); ::set_flag( mnFlags, EXC_NAME_BUILTIN ); } @@ -336,10 +330,6 @@ void XclExpName::Save( XclExpStream& rStrm ) void XclExpName::SaveXml( XclExpXmlStream& rStrm ) { - // For some reason, AutoFilter creates exportable names where maOrigName=="" - if( maOrigName.Len() == 0 ) - return; - sax_fastparser::FSHelperPtr& rWorkbook = rStrm.GetCurrentStream(); rWorkbook->startElement( XML_definedName, // OOXTODO: XML_comment, "", @@ -383,7 +373,6 @@ void XclExpName::WriteBody( XclExpStream& rStrm ) XclExpNameManagerImpl::XclExpNameManagerImpl( const XclExpRoot& rRoot ) : XclExpRoot( rRoot ), - maUnnamedDBName( ScGlobal::GetRscString( STR_DB_NONAME ) ), mnFirstUserIdx( 0 ) { } @@ -393,7 +382,6 @@ void XclExpNameManagerImpl::Initialize() CreateBuiltInNames(); mnFirstUserIdx = maNameList.GetSize(); CreateUserNames(); - CreateDatabaseNames(); } sal_uInt16 XclExpNameManagerImpl::InsertName( SCTAB nTab, sal_uInt16 nScNameIdx ) @@ -413,13 +401,15 @@ sal_uInt16 XclExpNameManagerImpl::InsertName( SCTAB nTab, sal_uInt16 nScNameIdx return nNameIdx; } -sal_uInt16 XclExpNameManagerImpl::InsertDBRange( sal_uInt16 nScDBRangeIdx ) +sal_uInt16 XclExpNameManagerImpl::InsertBuiltInName( sal_Unicode cBuiltIn, XclTokenArrayRef xTokArr, const ScRange& aRange ) { - sal_uInt16 nNameIdx = FindDBNameIdx( nScDBRangeIdx ); - if( nNameIdx == 0 ) - if( const ScDBData* pDBData = GetDatabaseRanges().FindIndex( nScDBRangeIdx ) ) - nNameIdx = CreateName( *pDBData ); - return nNameIdx; + XclExpNameRef xName( new XclExpName( GetRoot(), cBuiltIn ) ); + xName->SetTokenArray( xTokArr ); + xName->SetLocalTab( aRange.aStart.Tab() ); + String sSymbol; + aRange.Format( sSymbol, SCR_ABS_3D, GetDocPtr(), ScAddress::Details( ::formula::FormulaGrammar::CONV_XL_A1 ) ); + xName->SetSymbol( sSymbol ); + return Append( xName ); } sal_uInt16 XclExpNameManagerImpl::InsertBuiltInName( sal_Unicode cBuiltIn, XclTokenArrayRef xTokArr, SCTAB nScTab ) @@ -515,18 +505,12 @@ sal_uInt16 XclExpNameManagerImpl::FindNamedExpIndex( SCTAB nTab, sal_uInt16 nScI return (itr == maNamedExpMap.end()) ? 0 : itr->second; } -sal_uInt16 XclExpNameManagerImpl::FindDBNameIdx( sal_uInt16 nScIdx ) const -{ - XclExpIndexMap::const_iterator aIt = maDBRangeMap.find( nScIdx ); - return (aIt == maDBRangeMap.end()) ? 0 : aIt->second; -} - sal_uInt16 XclExpNameManagerImpl::FindBuiltInNameIdx( const String& rName, const XclTokenArray& rTokArr, bool bDBRange ) const { /* Get built-in index from the name. Special case: the database range 'unnamed' will be mapped to Excel's built-in '_FilterDatabase' name. */ - sal_Unicode cBuiltIn = (bDBRange && (rName == maUnnamedDBName)) ? + sal_Unicode cBuiltIn = (bDBRange && (rName == String(RTL_CONSTASCII_USTRINGPARAM(STR_DB_LOCAL_NONAME)))) ? EXC_BUILTIN_FILTERDATABASE : XclTools::GetBuiltInDefNameIndex( rName ); if( cBuiltIn < EXC_BUILTIN_UNKNOWN ) @@ -597,11 +581,6 @@ sal_uInt16 XclExpNameManagerImpl::CreateName( SCTAB nTab, const ScRangeData& rRa { XclTokenArrayRef xTokArr = GetFormulaCompiler().CreateFormula( EXC_FMLATYPE_NAME, *pScTokArr ); xName->SetTokenArray( xTokArr ); - - String sSymbol; - rRangeData.GetSymbol( sSymbol, formula::FormulaGrammar::GRAM_ENGLISH_XL_A1 ); - xName->SetSymbol( sSymbol ); - /* Try to replace by existing built-in name - complete token array is needed for comparison, and due to the recursion problem above this cannot be done earlier. If a built-in name is found, the created NAME @@ -622,29 +601,6 @@ sal_uInt16 XclExpNameManagerImpl::CreateName( SCTAB nTab, const ScRangeData& rRa return nNameIdx; } -sal_uInt16 XclExpNameManagerImpl::CreateName( const ScDBData& rDBData ) -{ - // get name and source range, and create the definition formula - const String& rName = rDBData.GetName(); - ScRange aRange; - rDBData.GetArea( aRange ); - XclTokenArrayRef xTokArr = GetFormulaCompiler().CreateFormula( EXC_FMLATYPE_NAME, aRange ); - - // try to use an existing built-in name - sal_uInt16 nNameIdx = FindBuiltInNameIdx( rName, *xTokArr, true ); - if( nNameIdx == 0 ) - { - // insert a new name into the list - XclExpNameRef xName( new XclExpName( GetRoot(), GetUnusedName( rName ) ) ); - xName->SetTokenArray( xTokArr ); - nNameIdx = Append( xName ); - } - - // store the index of the NAME record in the lookup map - maDBRangeMap[ rDBData.GetIndex() ] = nNameIdx; - return nNameIdx; -} - void XclExpNameManagerImpl::CreateBuiltInNames() { ScDocument& rDoc = GetDoc(); @@ -718,19 +674,6 @@ void XclExpNameManagerImpl::CreateUserNames() } } -void XclExpNameManagerImpl::CreateDatabaseNames() -{ - const ScDBCollection& rDBRanges = GetDatabaseRanges(); - for( sal_uInt16 nDBIdx = 0, nDBCount = rDBRanges.GetCount(); nDBIdx < nDBCount; ++nDBIdx ) - { - const ScDBData* pDBData = rDBRanges[ nDBIdx ]; - DBG_ASSERT( pDBData, "XclExpNameManagerImpl::CreateDatabaseNames - missing database range" ); - // skip hidden "unnamed" range - if( pDBData && (pDBData->GetName() != maUnnamedDBName) && !FindDBNameIdx( pDBData->GetIndex() ) ) - CreateName( *pDBData ); - } -} - // ---------------------------------------------------------------------------- XclExpNameManager::XclExpNameManager( const XclExpRoot& rRoot ) : @@ -753,15 +696,10 @@ sal_uInt16 XclExpNameManager::InsertName( SCTAB nTab, sal_uInt16 nScNameIdx ) return mxImpl->InsertName( nTab, nScNameIdx ); } -sal_uInt16 XclExpNameManager::InsertDBRange( sal_uInt16 nScDBRangeIdx ) -{ - return mxImpl->InsertDBRange( nScDBRangeIdx ); -} - sal_uInt16 XclExpNameManager::InsertBuiltInName( sal_Unicode cBuiltIn, const ScRange& rRange ) { XclTokenArrayRef xTokArr = GetFormulaCompiler().CreateFormula( EXC_FMLATYPE_NAME, rRange ); - return mxImpl->InsertBuiltInName( cBuiltIn, xTokArr, rRange.aStart.Tab() ); + return mxImpl->InsertBuiltInName( cBuiltIn, xTokArr, rRange ); } sal_uInt16 XclExpNameManager::InsertBuiltInName( sal_Unicode cBuiltIn, const ScRangeList& rRangeList ) diff --git a/sc/source/filter/excel/xltools.cxx b/sc/source/filter/excel/xltools.cxx index 903655b06..7bc13b4f7 100644 --- a/sc/source/filter/excel/xltools.cxx +++ b/sc/source/filter/excel/xltools.cxx @@ -469,6 +469,8 @@ String XclTools::GetXclFontName( const String& rFontName ) const String XclTools::maDefNamePrefix( RTL_CONSTASCII_USTRINGPARAM( "Excel_BuiltIn_" ) ); +const String XclTools::maDefNamePrefixXml ( RTL_CONSTASCII_USTRINGPARAM( "_xlnm." ) ); + static const sal_Char* const ppcDefNames[] = { "Consolidate_Area", @@ -504,6 +506,11 @@ String XclTools::GetBuiltInDefName( sal_Unicode cBuiltIn ) return String( maDefNamePrefix ).Append( GetXclBuiltInDefName( cBuiltIn ) ); } +String XclTools::GetBuiltInDefNameXml( sal_Unicode cBuiltIn ) +{ + return String( maDefNamePrefixXml ).Append( GetXclBuiltInDefName( cBuiltIn ) ); +} + sal_Unicode XclTools::GetBuiltInDefNameIndex( const String& rDefName ) { xub_StrLen nPrefixLen = maDefNamePrefix.Len(); diff --git a/sc/source/filter/inc/xltools.hxx b/sc/source/filter/inc/xltools.hxx index bfe284f19..e9e306e2f 100644 --- a/sc/source/filter/inc/xltools.hxx +++ b/sc/source/filter/inc/xltools.hxx @@ -187,6 +187,10 @@ public: @descr Adds a prefix to the representation returned by GetXclBuiltInDefName(). @param cBuiltIn Excel index of the built-in name. */ static String GetBuiltInDefName( sal_Unicode cBuiltIn ); + /** Returns the Excel built-in name with OOXML prefix + @descr Adds the "_xlnm." prefix to the representation returned by GetXclBuiltInDefName() + @param cBuiltIn Excel index of the built in name.*/ + static String GetBuiltInDefNameXml( sal_Unicode cBuiltIn ); /** Returns the Excel built-in name index of the passed defined name from Calc. @descr Ignores any characters following a valid representation of a built-in name. @param pcBuiltIn (out-param) If not 0, the index of the built-in name will be returned here. @@ -245,6 +249,7 @@ public: // ------------------------------------------------------------------------ private: static const String maDefNamePrefix; /// Prefix for built-in defined names. + static const String maDefNamePrefixXml; /// Prefix for built-in defined names for OOX static const String maStyleNamePrefix1; /// Prefix for built-in cell style names. static const String maStyleNamePrefix2; /// Prefix for built-in cell style names from OOX filter. static const String maCFStyleNamePrefix1; /// Prefix for cond. formatting style names. -- cgit v1.2.3