tdf#158185: fix Excel's Range.Find and Range.Replace wildcard recognition

VBAToRegexp is wrong, using Basic wildcards. The functionality introduced
in tdf#72196 matches the required wildcard repertoire.

This introduces a new WildcardEscapeCharacter property to SearchDescriptor
service. Interestingly, the default value there was backslash.

TODO: also fix Range.AutoFilter, and drop VBAToRegexp. To do that, a new
property is needed in service SheetFilterDescriptor, to use wildcards,
in addition to "UseRegularExpressions" property.

Change-Id: Idebcf00d4b7376e5d7feaf8ae800fb19d05689d4
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/159391
Tested-by: Jenkins
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
This commit is contained in:
Mike Kaganski 2023-11-13 19:53:40 +03:00
parent 9520c38b0b
commit c60a9db1f2
5 changed files with 29 additions and 7 deletions

View file

@ -138,6 +138,9 @@ public:
inline bool GetWildcard() const;
void SetWildcard( bool bVal );
inline sal_Int32 GetWildcardEscapeCharacter() const;
inline void SetWildcardEscapeCharacter(sal_Int32 val);
bool GetPattern() const { return m_bPattern; }
void SetPattern(bool bNewPattern) { m_bPattern = bNewPattern; }
@ -247,6 +250,16 @@ bool SvxSearchItem::GetWildcard() const
return m_aSearchOpt.AlgorithmType2 == css::util::SearchAlgorithms2::WILDCARD ;
}
sal_Int32 SvxSearchItem::GetWildcardEscapeCharacter() const
{
return m_aSearchOpt.WildcardEscapeCharacter;
}
void SvxSearchItem::SetWildcardEscapeCharacter(sal_Int32 val)
{
m_aSearchOpt.WildcardEscapeCharacter = val;
}
bool SvxSearchItem::IsLEVRelaxed() const
{
return 0 != (m_aSearchOpt.searchFlag & css::util::SearchFlags::LEV_RELAXED);

View file

@ -123,6 +123,12 @@ published service SearchDescriptor
*/
[optional, property] boolean SearchWildcard;
/** Specifices the character used to escape special characters in wildcards.
@since LibreOffice 24.2
*/
[optional, property] long WildcardEscapeCharacter;
};

View file

@ -395,6 +395,7 @@ inline constexpr OUString SC_UNO_SRCHTYPE = u"SearchType"_ustr;
inline constexpr OUString SC_UNO_SRCHWORDS = u"SearchWords"_ustr;
inline constexpr OUString SC_UNO_SRCHFILTERED = u"SearchFiltered"_ustr;
inline constexpr OUString SC_UNO_SRCHFORMATTED = u"SearchFormatted"_ustr;
inline constexpr OUString SC_UNO_SRCHWCESCCHAR = u"WildcardEscapeCharacter"_ustr;
// old (5.2) property names for page styles - for compatibility only!
inline constexpr OUString OLD_UNO_PAGE_BACKCOLOR = u"BackgroundColor"_ustr;

View file

@ -49,6 +49,7 @@ static std::span<const SfxItemPropertyMapEntry> lcl_GetSearchPropertyMap()
{ SC_UNO_SRCHSTYLES, 0, cppu::UnoType<bool>::get(), 0, 0},
{ SC_UNO_SRCHTYPE, 0, cppu::UnoType<sal_Int16>::get(), 0, 0}, // enum TableSearch is gone
{ SC_UNO_SRCHWORDS, 0, cppu::UnoType<bool>::get(), 0, 0},
{ SC_UNO_SRCHWCESCCHAR, 0, cppu::UnoType<sal_Int32>::get(), 0, 0 },
};
return aSearchPropertyMap_Impl;
}
@ -144,6 +145,7 @@ void SAL_CALL ScCellSearchObj::setPropertyValue(
else if (aPropertyName == SC_UNO_SRCHTYPE) pSearchItem->SetCellType( static_cast<SvxSearchCellType>(ScUnoHelpFunctions::GetInt16FromAny( aValue )) );
else if (aPropertyName == SC_UNO_SRCHFILTERED) pSearchItem->SetSearchFiltered( ScUnoHelpFunctions::GetBoolFromAny(aValue) );
else if (aPropertyName == SC_UNO_SRCHFORMATTED) pSearchItem->SetSearchFormatted( ScUnoHelpFunctions::GetBoolFromAny(aValue) );
else if (aPropertyName == SC_UNO_SRCHWCESCCHAR) pSearchItem->SetWildcardEscapeCharacter( ScUnoHelpFunctions::GetInt32FromAny(aValue) );
}
uno::Any SAL_CALL ScCellSearchObj::getPropertyValue( const OUString& aPropertyName )
@ -166,6 +168,7 @@ uno::Any SAL_CALL ScCellSearchObj::getPropertyValue( const OUString& aPropertyNa
else if (aPropertyName == SC_UNO_SRCHTYPE) aRet <<= static_cast<sal_Int16>(pSearchItem->GetCellType());
else if (aPropertyName == SC_UNO_SRCHFILTERED) aRet <<= pSearchItem->IsSearchFiltered();
else if (aPropertyName == SC_UNO_SRCHFORMATTED) aRet <<= pSearchItem->IsSearchFormatted();
else if (aPropertyName == SC_UNO_SRCHWCESCCHAR) aRet <<= pSearchItem->GetWildcardEscapeCharacter();
return aRet;
}

View file

@ -3108,7 +3108,6 @@ ScVbaRange::Replace( const OUString& What, const OUString& Replacement, const un
// sanity check required params
if ( What.isEmpty() )
throw uno::RuntimeException("Range::Replace, missing params" );
OUString sWhat = VBAToRegexp( What);
// #TODO #FIXME SearchFormat & ReplacesFormat are not processed
// What do we do about MatchByte... we don't seem to support that
const SvxSearchItem& globalSearchOptions = ScGlobal::GetSearchItem();
@ -3120,8 +3119,9 @@ ScVbaRange::Replace( const OUString& What, const OUString& Replacement, const un
uno::Reference< util::XReplaceDescriptor > xDescriptor =
xReplace->createReplaceDescriptor();
xDescriptor->setSearchString( sWhat);
xDescriptor->setPropertyValue( SC_UNO_SRCHREGEXP, uno::Any( true ) );
xDescriptor->setSearchString(What);
xDescriptor->setPropertyValue(SC_UNO_SRCHWILDCARD, uno::Any(true));
xDescriptor->setPropertyValue(SC_UNO_SRCHWCESCCHAR, uno::Any(sal_Int32('~')));
xDescriptor->setReplaceString( Replacement);
if ( LookAt.hasValue() )
{
@ -3215,8 +3215,6 @@ ScVbaRange::Find( const uno::Any& What, const uno::Any& After, const uno::Any& L
else
throw uno::RuntimeException("Range::Find, missing search-for-what param" );
OUString sSearch = VBAToRegexp( sWhat );
const SvxSearchItem& globalSearchOptions = ScGlobal::GetSearchItem();
SvxSearchItem newOptions( globalSearchOptions );
@ -3224,8 +3222,9 @@ ScVbaRange::Find( const uno::Any& What, const uno::Any& After, const uno::Any& L
if( xSearch.is() )
{
uno::Reference< util::XSearchDescriptor > xDescriptor = xSearch->createSearchDescriptor();
xDescriptor->setSearchString( sSearch );
xDescriptor->setPropertyValue( SC_UNO_SRCHREGEXP, uno::Any( true ) );
xDescriptor->setSearchString(sWhat);
xDescriptor->setPropertyValue(SC_UNO_SRCHWILDCARD, uno::Any(true));
xDescriptor->setPropertyValue(SC_UNO_SRCHWCESCCHAR, uno::Any(sal_Int32('~')));
uno::Reference< excel::XRange > xAfterRange;
uno::Reference< table::XCellRange > xStartCell;