diff options
author | Markus Mohrhard <markus.mohrhard@googlemail.com> | 2017-08-14 23:27:45 +0200 |
---|---|---|
committer | Markus Mohrhard <markus.mohrhard@googlemail.com> | 2017-08-15 00:57:44 +0200 |
commit | 3ecec2dbcff2870c385b0814203b1883887eddec (patch) | |
tree | a47b28a5b3a336ac9372767fa6ca2cc4c84f038a /sc | |
parent | a19dc104ec008f0bb44676d63d4764c6d7442a0d (diff) |
external data: add split column transformation
Change-Id: I7a06f1a5c3f28b47288b0a2b191a34cd81efb439
Reviewed-on: https://gerrit.libreoffice.org/41156
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Markus Mohrhard <markus.mohrhard@googlemail.com>
Diffstat (limited to 'sc')
-rw-r--r-- | sc/qa/unit/datatransformation_test.cxx | 26 | ||||
-rw-r--r-- | sc/source/ui/dataprovider/datatransformation.cxx | 37 | ||||
-rw-r--r-- | sc/source/ui/inc/datatransformation.hxx | 16 |
3 files changed, 79 insertions, 0 deletions
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 |