[Libreoffice-commits] core.git: sc/inc sc/source

Eike Rathke erack at redhat.com
Mon Aug 31 14:40:52 PDT 2015


 sc/inc/dbdata.hxx                   |    3 +
 sc/source/core/tool/dbdata.cxx      |   59 ++++++++++++++++++++++++++++++++++++
 sc/source/filter/excel/xedbdata.cxx |   12 ++++---
 3 files changed, 69 insertions(+), 5 deletions(-)

New commits:
commit bc712935a05dd34142b4c17dd6686761d9b9d5e1
Author: Eike Rathke <erack at redhat.com>
Date:   Mon Aug 31 23:35:00 2015 +0200

    TableRef: refresh table column names before saving
    
    ... to match reality and be able to re-load structured references in
    range names once we save TableRefs as well.
    
    Change-Id: I5f8fc858be2a773d7841816c3e4bf21590d37e64

diff --git a/sc/inc/dbdata.hxx b/sc/inc/dbdata.hxx
index 2b6dfae..bacf099 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 2c1f207..d87351d 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 6522b52..f4c404b 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();


More information about the Libreoffice-commits mailing list