diff options
-rw-r--r-- | sc/inc/dbdata.hxx | 3 | ||||
-rw-r--r-- | sc/source/core/tool/dbdata.cxx | 59 | ||||
-rw-r--r-- | sc/source/filter/excel/xedbdata.cxx | 12 |
3 files changed, 69 insertions, 5 deletions
diff --git a/sc/inc/dbdata.hxx b/sc/inc/dbdata.hxx index 2b6dfaec97a8..bacf0999357b 100644 --- a/sc/inc/dbdata.hxx +++ b/sc/inc/dbdata.hxx @@ -114,6 +114,9 @@ public: void SetTableColumnNames( const ::std::vector< OUString >& rNames ) { maTableColumnNames = rNames; } const ::std::vector< OUString >& GetTableColumnNames() const { return maTableColumnNames; } + /** Refresh/update the column names with the header row's cell contents. */ + SC_DLLPUBLIC void RefreshTableColumnNames( ScDocument* pDoc ); + /** Finds the column named rName and returns the corresponding offset within the table. @returns -1 if not found. diff --git a/sc/source/core/tool/dbdata.cxx b/sc/source/core/tool/dbdata.cxx index 2c1f20791d2b..d87351d4618c 100644 --- a/sc/source/core/tool/dbdata.cxx +++ b/sc/source/core/tool/dbdata.cxx @@ -31,6 +31,7 @@ #include "globstr.hrc" #include "subtotalparam.hxx" #include "sortparam.hxx" +#include "dociter.hxx" #include <memory> #include <utility> @@ -621,6 +622,64 @@ void ScDBData::AdjustTableColumnNames( UpdateRefMode eUpdateRefMode, SCCOL nDx, aNewNames.swap( maTableColumnNames); } +void ScDBData::RefreshTableColumnNames( ScDocument* pDoc ) +{ + if (!HasHeader()) + return; // sorry, but.. + + ::std::vector<OUString> aNewNames; + aNewNames.resize( nEndCol - nStartCol + 1); + ScHorizontalCellIterator aIter( pDoc, nTable, nStartCol, nStartRow, nEndCol, nStartRow); // header row only + ScRefCellValue* pCell; + SCCOL nCol, nLastColFilled = nStartCol - 1; + SCROW nRow; + bool bHaveEmpty = false; + for (size_t i=0; (pCell = aIter.GetNext( nCol, nRow)) != nullptr; ++i) + { + if (pCell->hasString()) + { + const OUString& rStr = pCell->getString( pDoc); + aNewNames[nCol-nStartCol] = rStr; + if (rStr.isEmpty()) + bHaveEmpty = true; + else if (nLastColFilled < nCol-1) + bHaveEmpty = true; + nLastColFilled = nCol; + } + else + bHaveEmpty = true; + } + + // Try to not have empty names and remember previous name that might had + // been used to compile formulas, but only if same number of columns and no + // duplicates. + /* TODO: formula references' create string should be adapted to look for + * the column name here if the TableRef column header cell is empty. */ + if (bHaveEmpty && aNewNames.size() == maTableColumnNames.size()) + { + for (size_t i=0, n=aNewNames.size(); i < n; ++i) + { + if (aNewNames[i].isEmpty()) + { + bool bCopy = true; + const OUString& rStr = maTableColumnNames[i]; + for (size_t j=0; j < n; ++j) + { + if (ScGlobal::GetpTransliteration()->isEqual( aNewNames[j], rStr)) + { + bCopy = false; + break; // for + } + } + if (bCopy) + aNewNames[i] = rStr; + } + } + } + + aNewNames.swap( maTableColumnNames); +} + namespace { class TableColumnNameSearch : public unary_function<ScDBData, bool> { diff --git a/sc/source/filter/excel/xedbdata.cxx b/sc/source/filter/excel/xedbdata.cxx index 6522b522ba90..f4c404bce93f 100644 --- a/sc/source/filter/excel/xedbdata.cxx +++ b/sc/source/filter/excel/xedbdata.cxx @@ -111,19 +111,21 @@ XclExpTablesManager::~XclExpTablesManager() void XclExpTablesManager::Initialize() { - const ScDocument& rDoc = GetDoc(); - const ScDBCollection* pDBColl = rDoc.GetDBCollection(); + // All non-const to be able to call RefreshTableColumnNames(). + ScDocument& rDoc = GetDoc(); + ScDBCollection* pDBColl = rDoc.GetDBCollection(); if (!pDBColl) return; - const ScDBCollection::NamedDBs& rDBs = pDBColl->getNamedDBs(); + ScDBCollection::NamedDBs& rDBs = pDBColl->getNamedDBs(); if (rDBs.empty()) return; sal_Int32 nTableId = 0; - for (ScDBCollection::NamedDBs::const_iterator itDB(rDBs.begin()); itDB != rDBs.end(); ++itDB) + for (ScDBCollection::NamedDBs::iterator itDB(rDBs.begin()); itDB != rDBs.end(); ++itDB) { - const ScDBData* pDBData = itDB->get(); + ScDBData* pDBData = itDB->get(); + pDBData->RefreshTableColumnNames( &rDoc); // currently not in sync, so refresh ScRange aRange( ScAddress::UNINITIALIZED); pDBData->GetArea( aRange); SCTAB nTab = aRange.aStart.Tab(); |