[Libreoffice-commits] core.git: 2 commits - sc/qa sc/source
Markus Mohrhard
markus.mohrhard at googlemail.com
Mon Aug 14 22:59:04 UTC 2017
sc/qa/unit/datatransformation_test.cxx | 54 ++++++++++++++++++++
sc/source/ui/dataprovider/datatransformation.cxx | 60 +++++++++++++++++++++++
sc/source/ui/inc/datatransformation.hxx | 28 ++++++++++
3 files changed, 142 insertions(+)
New commits:
commit 46aba1db9a8e43da03f4db580b8dc9de7b850b00
Author: Markus Mohrhard <markus.mohrhard at googlemail.com>
Date: Mon Aug 14 23:45:17 2017 +0200
external data: add merge column transformation
Change-Id: I112650bbb0ed279b386389e7df6b372bde080b62
Reviewed-on: https://gerrit.libreoffice.org/41157
Tested-by: Jenkins <ci at libreoffice.org>
Reviewed-by: Markus Mohrhard <markus.mohrhard at googlemail.com>
diff --git a/sc/qa/unit/datatransformation_test.cxx b/sc/qa/unit/datatransformation_test.cxx
index bcd8c9a88a7a..785b7024a74b 100644
--- a/sc/qa/unit/datatransformation_test.cxx
+++ b/sc/qa/unit/datatransformation_test.cxx
@@ -30,10 +30,12 @@ public:
void testColumnRemove();
void testColumnSplit();
+ void testColumnMerge();
CPPUNIT_TEST_SUITE(ScDataTransformationTest);
CPPUNIT_TEST(testColumnRemove);
CPPUNIT_TEST(testColumnSplit);
+ CPPUNIT_TEST(testColumnMerge);
CPPUNIT_TEST_SUITE_END();
private:
@@ -95,6 +97,32 @@ void ScDataTransformationTest::testColumnSplit()
CPPUNIT_ASSERT_EQUAL(OUString("Test2,Test3"), m_pDoc->GetString(3, 3, 0));
}
+void ScDataTransformationTest::testColumnMerge()
+{
+ m_pDoc->SetString(2, 0, 0, "Berlin");
+ m_pDoc->SetString(2, 1, 0, "Brussels");
+ m_pDoc->SetString(2, 2, 0, "Paris");
+ m_pDoc->SetString(2, 3, 0, "Peking");
+
+ m_pDoc->SetString(4, 0, 0, "Germany");
+ m_pDoc->SetString(4, 1, 0, "Belgium");
+ m_pDoc->SetString(4, 2, 0, "France");
+ m_pDoc->SetString(4, 3, 0, "China");
+
+ sc::MergeColumnTransformation aTransform(2, 4, ", ");
+ aTransform.Transform(*m_pDoc);
+
+ CPPUNIT_ASSERT_EQUAL(OUString("Berlin, Germany"), m_pDoc->GetString(2, 0, 0));
+ CPPUNIT_ASSERT_EQUAL(OUString("Brussels, Belgium"), m_pDoc->GetString(2, 1, 0));
+ CPPUNIT_ASSERT_EQUAL(OUString("Paris, France"), m_pDoc->GetString(2, 2, 0));
+ CPPUNIT_ASSERT_EQUAL(OUString("Peking, China"), m_pDoc->GetString(2, 3, 0));
+
+ for (SCROW nRow = 0; nRow <= 3; ++nRow)
+ {
+ CPPUNIT_ASSERT(m_pDoc->GetString(4, nRow, 0).isEmpty());
+ }
+}
+
ScDataTransformationTest::ScDataTransformationTest() :
ScBootstrapFixture( "/sc/qa/unit/data/dataprovider" ),
m_pDoc(nullptr)
diff --git a/sc/source/ui/dataprovider/datatransformation.cxx b/sc/source/ui/dataprovider/datatransformation.cxx
index e5e65dc43344..2c2fe72bbcf4 100644
--- a/sc/source/ui/dataprovider/datatransformation.cxx
+++ b/sc/source/ui/dataprovider/datatransformation.cxx
@@ -68,6 +68,29 @@ void SplitColumnTransformation::Transform(ScDocument& rDoc)
}
}
+MergeColumnTransformation::MergeColumnTransformation(SCCOL nCol1, SCCOL nCol2, const OUString& rMergeString):
+ mnCol1(nCol1),
+ mnCol2(nCol2),
+ maMergeString(rMergeString)
+{
+}
+
+void MergeColumnTransformation::Transform(ScDocument& rDoc)
+{
+ SCROW nEndRow1 = getLastRow(rDoc, mnCol1);
+ SCROW nEndRow2 = getLastRow(rDoc, mnCol2);
+ SCROW nEndRow = std::max(nEndRow1, nEndRow2);
+
+ for (SCROW nRow = 0; nRow <= nEndRow; ++nRow)
+ {
+ OUString aStr1 = rDoc.GetString(mnCol1, nRow, 0);
+ OUString aStr2 = rDoc.GetString(mnCol2, nRow, 0);
+ rDoc.SetString(mnCol1, nRow, 0, aStr1 + maMergeString + aStr2);
+ }
+
+ rDoc.DeleteCol(0, 0, MAXROW, 0, mnCol2, 1);
+}
+
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sc/source/ui/inc/datatransformation.hxx b/sc/source/ui/inc/datatransformation.hxx
index 4cb55e72298d..09c677b56aa4 100644
--- a/sc/source/ui/inc/datatransformation.hxx
+++ b/sc/source/ui/inc/datatransformation.hxx
@@ -53,6 +53,18 @@ public:
virtual void Transform(ScDocument& rDoc) override;
};
+class SC_DLLPUBLIC MergeColumnTransformation : public DataTransformation
+{
+ SCCOL mnCol1;
+ SCCOL mnCol2;
+ OUString maMergeString;
+
+public:
+
+ MergeColumnTransformation(SCCOL nCol1, SCCOL nCol2, const OUString& rMergeString);
+ virtual void Transform(ScDocument& rDoc) override;
+};
+
}
#endif
commit 3ecec2dbcff2870c385b0814203b1883887eddec
Author: Markus Mohrhard <markus.mohrhard at googlemail.com>
Date: Mon Aug 14 23:27:45 2017 +0200
external data: add split column transformation
Change-Id: I7a06f1a5c3f28b47288b0a2b191a34cd81efb439
Reviewed-on: https://gerrit.libreoffice.org/41156
Tested-by: Jenkins <ci at libreoffice.org>
Reviewed-by: Markus Mohrhard <markus.mohrhard at googlemail.com>
diff --git a/sc/qa/unit/datatransformation_test.cxx b/sc/qa/unit/datatransformation_test.cxx
index a894edbaa3c8..bcd8c9a88a7a 100644
--- a/sc/qa/unit/datatransformation_test.cxx
+++ b/sc/qa/unit/datatransformation_test.cxx
@@ -29,9 +29,11 @@ public:
virtual void tearDown() override;
void testColumnRemove();
+ void testColumnSplit();
CPPUNIT_TEST_SUITE(ScDataTransformationTest);
CPPUNIT_TEST(testColumnRemove);
+ CPPUNIT_TEST(testColumnSplit);
CPPUNIT_TEST_SUITE_END();
private:
@@ -69,6 +71,30 @@ void ScDataTransformationTest::testColumnRemove()
}
}
+void ScDataTransformationTest::testColumnSplit()
+{
+ m_pDoc->SetString(2, 0, 0, "Test1,Test2");
+ m_pDoc->SetString(2, 1, 0, "Test1,");
+ m_pDoc->SetString(2, 2, 0, ",Test1");
+ m_pDoc->SetString(2, 3, 0, "Test1,Test2,Test3");
+ m_pDoc->SetString(3, 0, 0, "AnotherString");
+
+ sc::SplitColumnTransformation aTransform(2, ',');
+ aTransform.Transform(*m_pDoc);
+
+ CPPUNIT_ASSERT_EQUAL(OUString("AnotherString"), m_pDoc->GetString(4, 0, 0));
+
+ CPPUNIT_ASSERT_EQUAL(OUString("Test1"), m_pDoc->GetString(2, 0, 0));
+ CPPUNIT_ASSERT_EQUAL(OUString("Test1"), m_pDoc->GetString(2, 1, 0));
+ CPPUNIT_ASSERT_EQUAL(OUString(""), m_pDoc->GetString(2, 2, 0));
+ CPPUNIT_ASSERT_EQUAL(OUString("Test1"), m_pDoc->GetString(2, 3, 0));
+
+ CPPUNIT_ASSERT_EQUAL(OUString("Test2"), m_pDoc->GetString(3, 0, 0));
+ CPPUNIT_ASSERT_EQUAL(OUString(""), m_pDoc->GetString(3, 1, 0));
+ CPPUNIT_ASSERT_EQUAL(OUString("Test1"), m_pDoc->GetString(3, 2, 0));
+ CPPUNIT_ASSERT_EQUAL(OUString("Test2,Test3"), m_pDoc->GetString(3, 3, 0));
+}
+
ScDataTransformationTest::ScDataTransformationTest() :
ScBootstrapFixture( "/sc/qa/unit/data/dataprovider" ),
m_pDoc(nullptr)
diff --git a/sc/source/ui/dataprovider/datatransformation.cxx b/sc/source/ui/dataprovider/datatransformation.cxx
index 5c581782e220..e5e65dc43344 100644
--- a/sc/source/ui/dataprovider/datatransformation.cxx
+++ b/sc/source/ui/dataprovider/datatransformation.cxx
@@ -17,6 +17,15 @@ DataTransformation::~DataTransformation()
{
}
+SCROW DataTransformation::getLastRow(ScDocument& rDoc, SCCOL nCol)
+{
+ SCROW nStartRow = 0;
+ SCROW nEndRow = MAXROW;
+ rDoc.ShrinkToDataArea(0, nCol, nStartRow, nCol, nEndRow);
+
+ return nEndRow;
+}
+
ColumnRemoveTransformation::ColumnRemoveTransformation(SCCOL nCol):
mnCol(nCol)
{
@@ -31,6 +40,34 @@ void ColumnRemoveTransformation::Transform(ScDocument& rDoc)
rDoc.DeleteCol(0, 0, MAXROW, 0, mnCol, 1);
}
+SplitColumnTransformation::SplitColumnTransformation(SCCOL nCol, sal_Unicode cSeparator):
+ mnCol(nCol),
+ mcSeparator(cSeparator)
+{
+}
+
+void SplitColumnTransformation::Transform(ScDocument& rDoc)
+{
+ rDoc.InsertCol(0, 0, MAXROW, 0, mnCol + 1, 1);
+
+ SCROW nEndRow = getLastRow(rDoc, mnCol);
+ for (SCROW nRow = 0; nRow <= nEndRow; ++nRow)
+ {
+ CellType eType;
+ rDoc.GetCellType(mnCol, nRow, 0, eType);
+ if (eType == CELLTYPE_STRING)
+ {
+ OUString aStr = rDoc.GetString(mnCol, nRow, 0);
+ sal_Int32 nIndex = aStr.indexOf(mcSeparator);
+ if (nIndex != -1)
+ {
+ rDoc.SetString(mnCol + 1, nRow, 0, aStr.copy(nIndex + 1));
+ rDoc.SetString(mnCol, nRow, 0, aStr.copy(0, nIndex));
+ }
+ }
+ }
+}
+
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sc/source/ui/inc/datatransformation.hxx b/sc/source/ui/inc/datatransformation.hxx
index 44882f0f09d3..4cb55e72298d 100644
--- a/sc/source/ui/inc/datatransformation.hxx
+++ b/sc/source/ui/inc/datatransformation.hxx
@@ -19,10 +19,15 @@ namespace sc {
class SC_DLLPUBLIC DataTransformation
{
+protected:
+
+ static SCROW getLastRow(ScDocument& rDoc, SCCOL nCol);
+
public:
virtual ~DataTransformation();
virtual void Transform(ScDocument& rDoc) = 0;
+
};
class SC_DLLPUBLIC ColumnRemoveTransformation : public DataTransformation
@@ -37,6 +42,17 @@ public:
virtual void Transform(ScDocument& rDoc) override;
};
+class SC_DLLPUBLIC SplitColumnTransformation : public DataTransformation
+{
+ SCCOL mnCol;
+ sal_Unicode mcSeparator;
+
+public:
+
+ SplitColumnTransformation(SCCOL nCol, sal_Unicode cSeparator);
+ virtual void Transform(ScDocument& rDoc) override;
+};
+
}
#endif
More information about the Libreoffice-commits
mailing list