tdf#154803 - Check if range is entirely merged

Regression from commit b9411e587586750f36ba9009b5f1e29fe461d8b5 where I
missinterpreted the check to get merged cells.

Regression:
tdf#147122 - Return cell object when a simple selection is merged
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/145378

Change-Id: I2e39599a206cf102b1da8c7fc4bb2d8c0a4b106c
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/150412
Tested-by: Jenkins
Reviewed-by: Andreas Heinisch <andreas.heinisch@yahoo.de>
Signed-off-by: Xisco Fauli <xiscofauli@libreoffice.org>
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/150581
(cherry picked from commit 752f0aa9d25a286d70a5627d466ce9e76c84fba6)
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/150570
Reviewed-by: Stéphane Guillou <stephane.guillou@libreoffice.org>
Reviewed-by: Eike Rathke <erack@redhat.com>
diff --git a/sc/qa/extras/macros-test.cxx b/sc/qa/extras/macros-test.cxx
index d1af2ae..f224127 100644
--- a/sc/qa/extras/macros-test.cxx
+++ b/sc/qa/extras/macros-test.cxx
@@ -73,6 +73,7 @@ public:
    void testShapeLayerId();
    void testFunctionAccessIndirect();
    void testTdf147122();
    void testTdf154803();

    CPPUNIT_TEST_SUITE(ScMacrosTest);
    CPPUNIT_TEST(testStarBasic);
@@ -109,6 +110,7 @@ public:
    CPPUNIT_TEST(testShapeLayerId);
    CPPUNIT_TEST(testFunctionAccessIndirect);
    CPPUNIT_TEST(testTdf147122);
    CPPUNIT_TEST(testTdf154803);

    CPPUNIT_TEST_SUITE_END();
};
@@ -960,6 +962,38 @@ void ScMacrosTest::testTdf147122()
    CPPUNIT_ASSERT_EQUAL(Any(OUString("This is a test")), aRet);
}

void ScMacrosTest::testTdf154803()
{
    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 TestExtendedMergedSelection\n"
                     // Merge A1:B2 cell range
                     "  oActiveSheet = ThisComponent.CurrentController.ActiveSheet\n"
                     "  oRange = oActiveSheet.getCellRangeByName(\"A1:B2\")\n"
                     "  ThisComponent.getCurrentController.Select(oRange)\n"
                     "  oActiveCell = ThisComponent.CurrentSelection\n"
                     "  oActiveCell.Merge(True)\n"
                     // Select A1:B3 range and check for its implementation name
                     "  oRange = oActiveSheet.getCellRangeByName(\"A1:B3\")\n"
                     "  ThisComponent.getCurrentController.Select(oRange)\n"
                     "  TestExtendedMergedSelection = ThisComponent.CurrentSelection.ImplementationName\n"
                     "End Function\n")));

    Any aRet = executeMacro("vnd.sun.Star.script:TestLibrary.TestModule.TestExtendedMergedSelection?"
                            "language=Basic&location=document");
    // Without the fix in place, this test would have failed with
    // - Expected : ScCellRangeObj
    // - Actual   : ScCellObj
    // i.e. the selection was interpreted as a single cell instead of a range
    CPPUNIT_ASSERT_EQUAL(Any(OUString("ScCellRangeObj")), 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 f894059..f58bf378 100644
--- a/sc/source/ui/unoobj/viewuno.cxx
+++ b/sc/source/ui/unoobj/viewuno.cxx
@@ -54,6 +54,7 @@
#include <prevwsh.hxx>
#include <docsh.hxx>
#include <drwlayer.hxx>
#include <attrib.hxx>
#include <drawview.hxx>
#include <fupoor.hxx>
#include <sc.hrc>
@@ -873,13 +874,20 @@ uno::Any SAL_CALL ScTabViewObj::getSelection()
        ScMarkType eMarkType = rViewData.GetSimpleArea(aRange);
        if ( nTabs == 1 && (eMarkType == SC_MARK_SIMPLE) )
        {
            // tdf#147122 - return cell object when a simple selection is merged
            // tdf#154803 - check if range is entirely merged
            ScDocument& rDoc = pDocSh->GetDocument();
            const ScPatternAttr* pMarkPattern = rDoc.GetPattern(aRange.aStart);
            const ScMergeAttr* pMergeAttr = rDoc.GetAttr(aRange.aStart, ATTR_MERGE);
            SCCOL nColSpan = 1;
            SCROW nRowSpan = 1;
            if (pMergeAttr && pMergeAttr->IsMerged())
            {
                nColSpan = pMergeAttr->GetColMerge();
                nRowSpan = pMergeAttr->GetRowMerge();
            }
            // tdf#147122 - return cell object when a simple selection is entirely merged
            if (aRange.aStart == aRange.aEnd
                || (pMarkPattern
                    && pMarkPattern->GetItemSet().GetItemState(ATTR_MERGE, false)
                           == SfxItemState::SET))
                || (aRange.aEnd.Col() - aRange.aStart.Col() == nColSpan - 1
                    && aRange.aEnd.Row() - aRange.aStart.Row() == nRowSpan - 1))
                pObj = new ScCellObj( pDocSh, aRange.aStart );
            else
                pObj = new ScCellRangeObj( pDocSh, aRange );