diff --git a/sc/qa/unit/uicalc/uicalc2.cxx b/sc/qa/unit/uicalc/uicalc2.cxx index bfa33dec83a4..6e1cc804841f 100644 --- a/sc/qa/unit/uicalc/uicalc2.cxx +++ b/sc/qa/unit/uicalc/uicalc2.cxx @@ -11,6 +11,8 @@ #include #include +#include +#include #include #include #include @@ -2295,6 +2297,41 @@ CPPUNIT_TEST_FIXTURE(ScUiCalcTest2, testSpillMatrixContractionOnEdit) CPPUNIT_ASSERT_EQUAL(CELLTYPE_NONE, pDoc->GetCellType(ScAddress(3, 4, 0))); // D5 cleared } +CPPUNIT_TEST_FIXTURE(ScUiCalcTest2, testSetHyperlinkKeepSelection) +{ + // Given a Calc doc with "foo" in cell A1 and the cell cursor on A1: + createScDoc(); + ScDocument* pDoc = getScDoc(); + pDoc->SetString(ScAddress(0, 0, 0), u"foo"_ustr); + goToCell(u"A1"_ustr); + + // When dispatching .uno:SetHyperlink with Hyperlink.Text differing from the cell content, + // but marked as a hint: + uno::Sequence aArgs = { + comphelper::makePropertyValue(u"Hyperlink.Text"_ustr, uno::Any(u"mytext"_ustr)), + comphelper::makePropertyValue(u"Hyperlink.TextIsHint"_ustr, uno::Any(true)), + comphelper::makePropertyValue(u"Hyperlink.URL"_ustr, + uno::Any(u"http://www.example.com"_ustr)), + }; + dispatchCommand(mxComponent, u".uno:SetHyperlink"_ustr, aArgs); + + // Commit the input mode that InsertURLField left the cell in. + ScModule::get()->InputEnterHandler(); + + // Then make sure the cell's existing content is preserved as the URL field's + // displayed text, not replaced by Hyperlink.Text: + const EditTextObject* pEditText = pDoc->GetEditText(ScAddress(0, 0, 0)); + CPPUNIT_ASSERT(pEditText); + const SvxFieldData* pField = pEditText->GetFieldData(0, 0, css::text::textfield::Type::URL); + auto pURLField = dynamic_cast(pField); + CPPUNIT_ASSERT(pURLField); + // Without the accompanying fix in place, this test would have failed with: + // - Expected: foo + // - Actual : mytext + // i.e. the cell content was lost, the provided text hint was used. + CPPUNIT_ASSERT_EQUAL(u"foo"_ustr, pURLField->GetRepresentation()); +} + CPPUNIT_PLUGIN_IMPLEMENT(); /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sc/source/ui/view/cellsh1.cxx b/sc/source/ui/view/cellsh1.cxx index 944f38342530..1e1928916833 100644 --- a/sc/source/ui/view/cellsh1.cxx +++ b/sc/source/ui/view/cellsh1.cxx @@ -3128,7 +3128,20 @@ void ScCellShell::ExecuteEdit( SfxRequest& rReq ) const OUString& rTarget = pHyper->GetTargetFrame(); sal_uInt16 nType = static_cast(pHyper->GetInsertMode()); - pTabViewShell->InsertURL( rName, rURL, rTarget, nType ); + // When the provided text is just a hint, prefer the cell's existing content + // over it; if both are missing, use the URL. + ScViewData& rData = GetViewData(); + OUString aCellText = rData.GetDocument().GetString( + rData.GetCurX(), rData.GetCurY(), rData.GetTabNumber()); + OUString sLinkText; + if (pHyper->GetTextIsHint() && !aCellText.isEmpty()) + sLinkText = aCellText; + else if (!rName.isEmpty()) + sLinkText = rName; + else + sLinkText = rURL; + + pTabViewShell->InsertURL( sLinkText, rURL, rTarget, nType ); rReq.Done(); } else