tdf#118247 - Correctly handle xlCellTypeConstants parameter

Change-Id: I5cc281d6193bcf03d2da3c594d427ec340c2cfc1
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/139239
Tested-by: Jenkins
Reviewed-by: Andreas Heinisch <andreas.heinisch@yahoo.de>
diff --git a/sc/qa/extras/testdocuments/tdf118247.xlsm b/sc/qa/extras/testdocuments/tdf118247.xlsm
new file mode 100755
index 0000000..73de66a
--- /dev/null
+++ b/sc/qa/extras/testdocuments/tdf118247.xlsm
Binary files differ
diff --git a/sc/qa/extras/vba-macro-test.cxx b/sc/qa/extras/vba-macro-test.cxx
index 71c3b90..24f8ddb 100644
--- a/sc/qa/extras/vba-macro-test.cxx
+++ b/sc/qa/extras/vba-macro-test.cxx
@@ -29,7 +29,10 @@
#include <com/sun/star/ui/XUIConfigurationManager.hpp>
#include <com/sun/star/awt/KeyModifier.hpp>

#include <ooo/vba/excel/XlSpecialCellsValue.hpp>

using namespace css;
using namespace ooo::vba;

class VBAMacroTest : public UnoApiTest
{
@@ -71,6 +74,7 @@ public:
    void testTdf107902();
    void testTdf90278();
    void testTdf149531();
    void testTdf118247();

    CPPUNIT_TEST_SUITE(VBAMacroTest);
    CPPUNIT_TEST(testSimpleCopyAndPaste);
@@ -92,6 +96,7 @@ public:
    CPPUNIT_TEST(testTdf107902);
    CPPUNIT_TEST(testTdf90278);
    CPPUNIT_TEST(testTdf149531);
    CPPUNIT_TEST(testTdf118247);
    CPPUNIT_TEST_SUITE_END();
};

@@ -928,6 +933,48 @@ void VBAMacroTest::testTdf149531()

    pDocSh->DoClose();
}

void VBAMacroTest::testTdf118247()
{
    OUString aFileName;
    createFileURL(u"tdf118247.xlsm", aFileName);
    auto xComponent = loadFromDesktop(aFileName, "com.sun.star.sheet.SpreadsheetDocument");

    uno::Any aRet;
    uno::Sequence<sal_Int16> aOutParamIndex;
    uno::Sequence<uno::Any> aOutParam;
    uno::Sequence<uno::Any> aParams;

    SfxObjectShell::CallXScript(
        xComponent,
        "vnd.sun.Star.script:VBAProject.Module1.testXlSpecialCellsValuesConstantsEmpty?"
        "language=Basic&location=document",
        aParams, aRet, aOutParamIndex, aOutParam);

    OUString aReturnValue;
    aRet >>= aReturnValue;
    CPPUNIT_ASSERT_EQUAL(OUString("$A$1:$A$3"), aReturnValue);

    const std::vector<std::pair<sal_Int32, OUString>> aTestParams(
        { { excel::XlSpecialCellsValue::xlNumbers, "$A$1:$A$2" },
          { excel::XlSpecialCellsValue::xlTextValues, "$A$3" },
          { excel::XlSpecialCellsValue::xlLogical, "$A$1:$A$2" },
          { excel::XlSpecialCellsValue::xlErrors, "$A$1:$A$4" } });

    for (auto & [ nXlSpecialCellsValue, sRange ] : aTestParams)
    {
        aParams = { uno::Any(nXlSpecialCellsValue) };
        SfxObjectShell::CallXScript(
            xComponent,
            "vnd.sun.Star.script:VBAProject.Module1.testXlSpecialCellsValuesConstants?"
            "language=Basic&location=document",
            aParams, aRet, aOutParamIndex, aOutParam);
        aRet >>= aReturnValue;
        CPPUNIT_ASSERT_EQUAL(sRange, aReturnValue);
    }
    css::uno::Reference<css::util::XCloseable> xCloseable(xComponent, css::uno::UNO_QUERY_THROW);
    xCloseable->close(true);
}
CPPUNIT_TEST_SUITE_REGISTRATION(VBAMacroTest);

CPPUNIT_PLUGIN_IMPLEMENT();
diff --git a/sc/source/ui/vba/vbarange.cxx b/sc/source/ui/vba/vbarange.cxx
index 279a4a5..d2bd37d 100644
--- a/sc/source/ui/vba/vbarange.cxx
+++ b/sc/source/ui/vba/vbarange.cxx
@@ -5536,6 +5536,27 @@ ScVbaRange::SpecialCells( const uno::Any& _oType, const uno::Any& _oValue)
    return pRangeToUse->SpecialCellsImpl( nType, _oValue );
}

static sal_Int32 getContentResultFlags(const uno::Any& aValue)
{
    if (sal_Int32 aType; aValue >>= aType)
    {
        switch (aType)
        {
            case excel::XlSpecialCellsValue::xlNumbers:
                return sheet::CellFlags::VALUE | sheet::CellFlags::DATETIME;
            case excel::XlSpecialCellsValue::xlTextValues:
                return sheet::CellFlags::STRING;
            case excel::XlSpecialCellsValue::xlLogical:
                return sheet::CellFlags::VALUE | sheet::CellFlags::DATETIME;
            case excel::XlSpecialCellsValue::xlErrors:
                return 0;
            default:
                DebugHelper::basicexception(ERRCODE_BASIC_BAD_PARAMETER, {});
        }
    }
    return sheet::CellFlags::VALUE | sheet::CellFlags::STRING | sheet::CellFlags::DATETIME;
}

/// @throws script::BasicErrorException
static sal_Int32 lcl_getFormulaResultFlags(const uno::Any& aType)
{
@@ -5589,7 +5610,7 @@ ScVbaRange::SpecialCellsImpl( sal_Int32 nType, const uno::Any& _oValue)
                xLocSheetCellRanges = xQuery->queryContentCells(sheet::CellFlags::ANNOTATION);
                break;
            case excel::XlCellType::xlCellTypeConstants:
                xLocSheetCellRanges = xQuery->queryContentCells(23);
                xLocSheetCellRanges = xQuery->queryContentCells(getContentResultFlags(_oValue));
                break;
            case excel::XlCellType::xlCellTypeFormulas:
            {