sc/qa/unit/datatransformation_test.cxx | 70 ++++++++++++++++ sc/source/ui/dataprovider/datatransformation.cxx | 95 +++++++++++++++++++++++ sc/source/ui/inc/datatransformation.hxx | 16 +++ 3 files changed, 180 insertions(+), 1 deletion(-)
New commits: commit 483ca6e6bb9c920cc96212c7042e13fbbb5fa767 Author: Vikas <vikasmaha...@gmail.com> Date: Mon Jul 2 01:29:09 2018 +0530 Added text transformations for external data - Uppercase, which sets all text in the selected columns to upper case. - Lowercase, which sets all text in the selected columns to lower case. - Capitalize Each Word, which makes all words in the selected column start with a capital letter and sets all subsequent letters in a word to lower case. - Trim, which removes any leading or trailing whitespace characters from text. Change-Id: I6da37bec9d820887a155da57d0b0c75f36e8ac69 Reviewed-on: https://gerrit.libreoffice.org/56780 Tested-by: Jenkins 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 d4c0e11a2642..8cec163eeb43 100644 --- a/sc/qa/unit/datatransformation_test.cxx +++ b/sc/qa/unit/datatransformation_test.cxx @@ -31,11 +31,19 @@ public: void testColumnRemove(); void testColumnSplit(); void testColumnMerge(); + void testTextToLower(); + void testTextToUpper(); + void testTextCapitalize(); + void testTextTrim(); CPPUNIT_TEST_SUITE(ScDataTransformationTest); CPPUNIT_TEST(testColumnRemove); CPPUNIT_TEST(testColumnSplit); CPPUNIT_TEST(testColumnMerge); + CPPUNIT_TEST(testTextToLower); + CPPUNIT_TEST(testTextToUpper); + CPPUNIT_TEST(testTextCapitalize); + CPPUNIT_TEST(testTextTrim); CPPUNIT_TEST_SUITE_END(); private: @@ -123,6 +131,68 @@ void ScDataTransformationTest::testColumnMerge() } } +void ScDataTransformationTest::testTextToLower() +{ + 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"); + + sc::TextTransformation aTransform(2, sc::TEXT_TRANSFORM_TYPE::TO_LOWER); + aTransform.Transform(*m_pDoc); + + CPPUNIT_ASSERT_EQUAL(OUString("berlin"), m_pDoc->GetString(2, 0, 0)); + CPPUNIT_ASSERT_EQUAL(OUString("brussels"), m_pDoc->GetString(2, 1, 0)); + CPPUNIT_ASSERT_EQUAL(OUString("paris"), m_pDoc->GetString(2, 2, 0)); + CPPUNIT_ASSERT_EQUAL(OUString("peking"), m_pDoc->GetString(2, 3, 0)); +} + +void ScDataTransformationTest::testTextToUpper() +{ + 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"); + + sc::TextTransformation aTransform(2, sc::TEXT_TRANSFORM_TYPE::TO_UPPER); + aTransform.Transform(*m_pDoc); + + CPPUNIT_ASSERT_EQUAL(OUString("BERLIN"), m_pDoc->GetString(2, 0, 0)); + CPPUNIT_ASSERT_EQUAL(OUString("BRUSSELS"), m_pDoc->GetString(2, 1, 0)); + CPPUNIT_ASSERT_EQUAL(OUString("PARIS"), m_pDoc->GetString(2, 2, 0)); + CPPUNIT_ASSERT_EQUAL(OUString("PEKING"), m_pDoc->GetString(2, 3, 0)); +} + +void ScDataTransformationTest::testTextCapitalize() +{ + m_pDoc->SetString(2, 0, 0, "hello woRlD"); + m_pDoc->SetString(2, 1, 0, "qUe vA"); + m_pDoc->SetString(2, 2, 0, "si tu la ves"); + m_pDoc->SetString(2, 3, 0, "cUaNdO mE EnAmOro"); + + sc::TextTransformation aTransform(2, sc::TEXT_TRANSFORM_TYPE::CAPITALIZE); + aTransform.Transform(*m_pDoc); + + CPPUNIT_ASSERT_EQUAL(OUString("Hello World"), m_pDoc->GetString(2, 0, 0)); + CPPUNIT_ASSERT_EQUAL(OUString("Que Va"), m_pDoc->GetString(2, 1, 0)); + CPPUNIT_ASSERT_EQUAL(OUString("Si Tu La Ves"), m_pDoc->GetString(2, 2, 0)); + CPPUNIT_ASSERT_EQUAL(OUString("Cuando Me Enamoro"), m_pDoc->GetString(2, 3, 0)); +} + +void ScDataTransformationTest::testTextTrim() +{ + m_pDoc->SetString(2, 0, 0, " Berlin"); + m_pDoc->SetString(2, 1, 0, "Brussels "); + m_pDoc->SetString(2, 2, 0, " Paris "); + + sc::TextTransformation aTransform(2, sc::TEXT_TRANSFORM_TYPE::TRIM); + aTransform.Transform(*m_pDoc); + + CPPUNIT_ASSERT_EQUAL(OUString("Berlin"), m_pDoc->GetString(2, 0, 0)); + CPPUNIT_ASSERT_EQUAL(OUString("Brussels"), m_pDoc->GetString(2, 1, 0)); + CPPUNIT_ASSERT_EQUAL(OUString("Paris"), m_pDoc->GetString(2, 2, 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 09370a8e82c7..84df313aacdc 100644 --- a/sc/source/ui/dataprovider/datatransformation.cxx +++ b/sc/source/ui/dataprovider/datatransformation.cxx @@ -173,6 +173,101 @@ ScSortParam SortTransformation::getSortParam() const return maSortParam; } +TextTransformation::TextTransformation(SCCOL nCol, const TEXT_TRANSFORM_TYPE rType): + mnCol(nCol), + maType(rType) +{ +} + +void TextTransformation::Transform(ScDocument& rDoc) const +{ + SCROW nEndRow = getLastRow(rDoc, mnCol); + + switch (maType) + { + case TEXT_TRANSFORM_TYPE::TO_LOWER: + { + 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); + rDoc.SetString(mnCol, nRow, 0, ScGlobal::pCharClass->lowercase(aStr)); + } + } + } + break; + case TEXT_TRANSFORM_TYPE::TO_UPPER: + { + 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); + rDoc.SetString(mnCol, nRow, 0, ScGlobal::pCharClass->uppercase(aStr)); + } + } + } + break; + case TEXT_TRANSFORM_TYPE::CAPITALIZE: + { + sal_Unicode separator = sal_Unicode(U' '); + 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 length = aStr.getLength(); + + if(length != 0) + aStr = aStr.replaceAt(0, 1, ScGlobal::pCharClass->uppercase(OUString(aStr[0]))); + + for (sal_Int32 i = 1; i < length; i++){ + if (aStr[i-1] == separator) + { + aStr = aStr.replaceAt(i, 1, ScGlobal::pCharClass->uppercase(OUString(aStr[i]))); + } + else + { + aStr = aStr.replaceAt(i, 1, ScGlobal::pCharClass->lowercase(OUString(aStr[i]))); + } + } + rDoc.SetString(mnCol, nRow, 0, aStr); + } + } + } + break; + case TEXT_TRANSFORM_TYPE::TRIM: + { + 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); + rDoc.SetString(mnCol, nRow, 0, aStr.trim()); + } + } + } + break; + default: + break; + } +} + +TransformationType TextTransformation::getTransformationType() const +{ + return TransformationType::TEXT_TRANSFORMATION; +} + } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sc/source/ui/inc/datatransformation.hxx b/sc/source/ui/inc/datatransformation.hxx index 8dbae9600384..522c039851ae 100644 --- a/sc/source/ui/inc/datatransformation.hxx +++ b/sc/source/ui/inc/datatransformation.hxx @@ -26,9 +26,12 @@ enum class TransformationType MERGE_TRANSFORMATION, SPLIT_TRANSFORMATION, DELETE_TRANSFORMATION, - SORT_TRANSFORMATION + SORT_TRANSFORMATION, + TEXT_TRANSFORMATION }; +enum class TEXT_TRANSFORM_TYPE { TO_LOWER, TO_UPPER, CAPITALIZE, TRIM }; + class SC_DLLPUBLIC DataTransformation { protected: @@ -96,6 +99,17 @@ public: ScSortParam getSortParam() const; }; +class SC_DLLPUBLIC TextTransformation : public DataTransformation +{ + SCCOL mnCol; + TEXT_TRANSFORM_TYPE maType; + + public: + TextTransformation(SCCOL nCol, const TEXT_TRANSFORM_TYPE rType); + virtual void Transform(ScDocument& rDoc) const override; + virtual TransformationType getTransformationType() const override; +}; + } #endif _______________________________________________ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits