sc/qa/unit/datatransformation_test.cxx           |    4 -
 sc/source/ui/dataprovider/datatransformation.cxx |   50 ++++++++++++++++-------
 sc/source/ui/inc/datatransformation.hxx          |   11 ++---
 sc/source/ui/miscdlgs/dataproviderdlg.cxx        |    3 -
 4 files changed, 46 insertions(+), 22 deletions(-)

New commits:
commit 8ef47e605372aba103d19f6b8ee8a00dab40fdab
Author: Markus Mohrhard <markus.mohrh...@googlemail.com>
Date:   Thu Feb 8 22:44:46 2018 +0100

    support multiple columns in column data transformations
    
    Change-Id: Ic911fb7d5f99f692c960ef5267e37b263e798672
    Reviewed-on: https://gerrit.libreoffice.org/49457
    Tested-by: Jenkins <c...@libreoffice.org>
    Reviewed-by: Markus Mohrhard <markus.mohrh...@googlemail.com>

diff --git a/sc/qa/unit/datatransformation_test.cxx 
b/sc/qa/unit/datatransformation_test.cxx
index 4639d4e087d5..d4c0e11a2642 100644
--- a/sc/qa/unit/datatransformation_test.cxx
+++ b/sc/qa/unit/datatransformation_test.cxx
@@ -53,7 +53,7 @@ void ScDataTransformationTest::testColumnRemove()
         }
     }
 
-    sc::ColumnRemoveTransformation aTransformation(5);
+    sc::ColumnRemoveTransformation aTransformation({5});
     aTransformation.Transform(*m_pDoc);
 
     for (SCROW nRow = 0; nRow < 10; ++nRow)
@@ -109,7 +109,7 @@ void ScDataTransformationTest::testColumnMerge()
     m_pDoc->SetString(4, 2, 0, "France");
     m_pDoc->SetString(4, 3, 0, "China");
 
-    sc::MergeColumnTransformation aTransform(2, 4, ", ");
+    sc::MergeColumnTransformation aTransform({2, 4}, ", ");
     aTransform.Transform(*m_pDoc);
 
     CPPUNIT_ASSERT_EQUAL(OUString("Berlin, Germany"), m_pDoc->GetString(2, 0, 
0));
diff --git a/sc/source/ui/dataprovider/datatransformation.cxx 
b/sc/source/ui/dataprovider/datatransformation.cxx
index 039c17209304..bbb9bd068c7f 100644
--- a/sc/source/ui/dataprovider/datatransformation.cxx
+++ b/sc/source/ui/dataprovider/datatransformation.cxx
@@ -26,8 +26,8 @@ SCROW DataTransformation::getLastRow(const ScDocument& rDoc, 
SCCOL nCol)
     return nEndRow;
 }
 
-ColumnRemoveTransformation::ColumnRemoveTransformation(SCCOL nCol):
-    mnCol(nCol)
+ColumnRemoveTransformation::ColumnRemoveTransformation(const std::set<SCCOL>& 
rColumns):
+    maColumns(rColumns)
 {
 }
 
@@ -37,7 +37,10 @@ ColumnRemoveTransformation::~ColumnRemoveTransformation()
 
 void ColumnRemoveTransformation::Transform(ScDocument& rDoc) const
 {
-    rDoc.DeleteCol(0, 0, MAXROW, 0, mnCol, 1);
+    for (auto& rCol : maColumns)
+    {
+        rDoc.DeleteCol(0, 0, MAXROW, 0, rCol, 1);
+    }
 }
 
 SplitColumnTransformation::SplitColumnTransformation(SCCOL nCol, sal_Unicode 
cSeparator):
@@ -68,27 +71,46 @@ void SplitColumnTransformation::Transform(ScDocument& rDoc) 
const
     }
 }
 
-MergeColumnTransformation::MergeColumnTransformation(SCCOL nCol1, SCCOL nCol2, 
const OUString& rMergeString):
-    mnCol1(nCol1),
-    mnCol2(nCol2),
+MergeColumnTransformation::MergeColumnTransformation(const std::set<SCCOL>& 
rColumns, const OUString& rMergeString):
+    maColumns(rColumns),
     maMergeString(rMergeString)
 {
 }
 
 void MergeColumnTransformation::Transform(ScDocument& rDoc) const
 {
-    SCROW nEndRow1 = getLastRow(rDoc, mnCol1);
-    SCROW nEndRow2 = getLastRow(rDoc, mnCol2);
-    SCROW nEndRow = std::max(nEndRow1, nEndRow2);
+    if (maColumns.empty())
+        return;
 
-    for (SCROW nRow = 0; nRow <= nEndRow; ++nRow)
+    SCROW nMaxRow = 0;
+    for (auto& itr : maColumns)
     {
-        OUString aStr1 = rDoc.GetString(mnCol1, nRow, 0);
-        OUString aStr2 = rDoc.GetString(mnCol2, nRow, 0);
-        rDoc.SetString(mnCol1, nRow, 0, aStr1 + maMergeString + aStr2);
+        nMaxRow = getLastRow(rDoc, itr);
     }
 
-    rDoc.DeleteCol(0, 0, MAXROW, 0, mnCol2, 1);
+    SCCOL nTargetCol = *maColumns.begin();
+
+
+    for (SCROW nRow = 0; nRow <= nMaxRow; ++nRow)
+    {
+        OUStringBuffer aStr = rDoc.GetString(nTargetCol, nRow, 0);
+        for (auto& itr : maColumns)
+        {
+            if (itr != nTargetCol)
+            {
+                aStr.append(maMergeString).append(rDoc.GetString(itr, nRow, 
0));
+            }
+        }
+        rDoc.SetString(nTargetCol, nRow, 0, aStr.makeStringAndClear());
+    }
+
+    for (auto& itr : maColumns)
+    {
+        if (itr == nTargetCol)
+            continue;
+
+        rDoc.DeleteCol(0, 0, MAXROW, 0, itr, 1);
+    }
 }
 
 SortTransformation::SortTransformation(const ScSortParam& rSortParam):
diff --git a/sc/source/ui/inc/datatransformation.hxx 
b/sc/source/ui/inc/datatransformation.hxx
index d647fecb8e5d..251397ca8304 100644
--- a/sc/source/ui/inc/datatransformation.hxx
+++ b/sc/source/ui/inc/datatransformation.hxx
@@ -15,6 +15,8 @@
 
 #include <sortparam.hxx>
 
+#include <set>
+
 class ScDocument;
 
 namespace sc {
@@ -34,11 +36,11 @@ public:
 
 class SC_DLLPUBLIC ColumnRemoveTransformation : public DataTransformation
 {
-    SCCOL mnCol;
+    std::set<SCCOL> maColumns;
 
 public:
 
-    ColumnRemoveTransformation(SCCOL nCol);
+    ColumnRemoveTransformation(const std::set<SCCOL>& rColumns);
     virtual ~ColumnRemoveTransformation() override;
 
     virtual void Transform(ScDocument& rDoc) const override;
@@ -57,13 +59,12 @@ public:
 
 class SC_DLLPUBLIC MergeColumnTransformation : public DataTransformation
 {
-    SCCOL mnCol1;
-    SCCOL mnCol2;
+    std::set<SCCOL> maColumns;
     OUString maMergeString;
 
 public:
 
-    MergeColumnTransformation(SCCOL nCol1, SCCOL nCol2, const OUString& 
rMergeString);
+    MergeColumnTransformation(const std::set<SCCOL>& rColumns, const OUString& 
rMergeString);
     virtual void Transform(ScDocument& rDoc) const  override;
 };
 
diff --git a/sc/source/ui/miscdlgs/dataproviderdlg.cxx 
b/sc/source/ui/miscdlgs/dataproviderdlg.cxx
index d5f36083e4f6..11fa52ee2927 100644
--- a/sc/source/ui/miscdlgs/dataproviderdlg.cxx
+++ b/sc/source/ui/miscdlgs/dataproviderdlg.cxx
@@ -327,7 +327,8 @@ 
ScMergeColumnTransformationControl::ScMergeColumnTransformationControl(vcl::Wind
 
 std::shared_ptr<sc::DataTransformation> 
ScMergeColumnTransformationControl::getTransformation()
 {
-    return std::make_shared<sc::MergeColumnTransformation>(0, 1, ",");
+    std::set<SCCOL> maColumns = {0, 1};
+    return std::make_shared<sc::MergeColumnTransformation>(maColumns, ",");
 }
 
 }
_______________________________________________
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to