sc/qa/extras/macros-test.cxx    |   34 ++++++++++++++++++++++++++++++++++
 sc/source/ui/unoobj/viewuno.cxx |    9 ++++++++-
 2 files changed, 42 insertions(+), 1 deletion(-)

New commits:
commit bf39a7a45a9e6d1292a4425e5d25f9ce6551205a
Author:     Andreas Heinisch <andreas.heini...@yahoo.de>
AuthorDate: Thu Jan 12 07:46:43 2023 +0100
Commit:     Xisco Fauli <xiscofa...@libreoffice.org>
CommitDate: Mon Jan 16 11:19:45 2023 +0000

    tdf#147122 - Return cell object when a simple selection is merged
    
    Change-Id: I4ddd3b3a804f8300a5ec15526f4c9c77aaf45fc5
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/145378
    Tested-by: Jenkins
    Reviewed-by: Andreas Heinisch <andreas.heini...@yahoo.de>
    (cherry picked from commit b9411e587586750f36ba9009b5f1e29fe461d8b5)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/145452
    Reviewed-by: Xisco Fauli <xiscofa...@libreoffice.org>

diff --git a/sc/qa/extras/macros-test.cxx b/sc/qa/extras/macros-test.cxx
index f53389f40459..24379362bed5 100644
--- a/sc/qa/extras/macros-test.cxx
+++ b/sc/qa/extras/macros-test.cxx
@@ -71,6 +71,7 @@ public:
     void testTdf107572();
     void testShapeLayerId();
     void testFunctionAccessIndirect();
+    void testTdf147122();
 
     CPPUNIT_TEST_SUITE(ScMacrosTest);
     CPPUNIT_TEST(testStarBasic);
@@ -105,6 +106,7 @@ public:
     CPPUNIT_TEST(testTdf107572);
     CPPUNIT_TEST(testShapeLayerId);
     CPPUNIT_TEST(testFunctionAccessIndirect);
+    CPPUNIT_TEST(testTdf147122);
 
     CPPUNIT_TEST_SUITE_END();
 };
@@ -898,6 +900,38 @@ void ScMacrosTest::testFunctionAccessIndirect()
     CPPUNIT_ASSERT_EQUAL(css::uno::Any(OUString("a1")), aResult);
 }
 
+void ScMacrosTest::testTdf147122()
+{
+    mxComponent = loadFromDesktop("private:factory/scalc");
+
+    css::uno::Reference<css::document::XEmbeddedScripts> xDocScr(mxComponent, 
UNO_QUERY_THROW);
+    auto xLibs = xDocScr->getBasicLibraries();
+    auto xLibrary = xLibs->createLibrary("TestLibrary");
+    xLibrary->insertByName(
+        "TestModule",
+        uno::Any(
+            OUString("Function TestMergedSelection\n"
+                     // Insert test string into cell A1
+                     "  oActiveSheet = 
ThisComponent.CurrentController.ActiveSheet\n"
+                     "  oActiveCell = 
oActiveSheet.getCellRangeByName(\"A1\")\n"
+                     "  oActiveCell.setString(\"This is a test\")\n"
+                     // Merge A1:B2 cell range and return the content of the 
merged range
+                     "  oRange = oActiveSheet.getCellRangeByName(\"A1:B2\")\n"
+                     "  ThisComponent.getCurrentController.Select(oRange)\n"
+                     "  oActiveCell = ThisComponent.CurrentSelection\n"
+                     "  oActiveCell.Merge(True)\n"
+                     "  TestMergedSelection = 
ThisComponent.getCurrentSelection().getString()\n"
+                     "End Function\n")));
+
+    Any aRet = 
executeMacro("vnd.sun.Star.script:TestLibrary.TestModule.TestMergedSelection?"
+                            "language=Basic&location=document");
+    // Without the fix in place, this test would have failed with
+    // - Expression: false
+    // - Unexpected dialog: Error: BASIC runtime error.
+    // Property or method not found: getString.
+    CPPUNIT_ASSERT_EQUAL(Any(OUString("This is a test")), aRet);
+}
+
 ScMacrosTest::ScMacrosTest()
       : UnoApiXmlTest("/sc/qa/extras/testdocuments")
 {
diff --git a/sc/source/ui/unoobj/viewuno.cxx b/sc/source/ui/unoobj/viewuno.cxx
index 5a7a8a391427..f8940594746c 100644
--- a/sc/source/ui/unoobj/viewuno.cxx
+++ b/sc/source/ui/unoobj/viewuno.cxx
@@ -70,6 +70,7 @@
 #include <svx/sdrhittesthelper.hxx>
 #include <formatsh.hxx>
 #include <sfx2/app.hxx>
+#include <scitems.hxx>
 
 using namespace com::sun::star;
 
@@ -872,7 +873,13 @@ uno::Any SAL_CALL ScTabViewObj::getSelection()
         ScMarkType eMarkType = rViewData.GetSimpleArea(aRange);
         if ( nTabs == 1 && (eMarkType == SC_MARK_SIMPLE) )
         {
-            if (aRange.aStart == aRange.aEnd)
+            // tdf#147122 - return cell object when a simple selection is 
merged
+            ScDocument& rDoc = pDocSh->GetDocument();
+            const ScPatternAttr* pMarkPattern = rDoc.GetPattern(aRange.aStart);
+            if (aRange.aStart == aRange.aEnd
+                || (pMarkPattern
+                    && pMarkPattern->GetItemSet().GetItemState(ATTR_MERGE, 
false)
+                           == SfxItemState::SET))
                 pObj = new ScCellObj( pDocSh, aRange.aStart );
             else
                 pObj = new ScCellRangeObj( pDocSh, aRange );

Reply via email to