tdf166413 sc: escape quotes in XLSX cond. formatting fixed formula

...during export, otherwise Excel fails to open file

Change-Id: I50bc16ff4064a84ae3601cfa539d7b90f5441607
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/184852
Tested-by: Jenkins
Reviewed-by: Aron Budea <aron.budea@collabora.com>
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
This commit is contained in:
Aron Budea
2025-05-01 11:55:10 +02:00
parent 52ad257d44
commit 55169df2f6
3 changed files with 42 additions and 5 deletions
Binary file not shown.
+35
View File
@@ -2241,6 +2241,41 @@ CPPUNIT_TEST_FIXTURE(ScExportTest4, testTdf165886)
assertXPathContent(pSheet, "/x:worksheet/x:sheetData/x:row[4]/x:c[2]/x:f", u"OR(D4=0,D4<>’’)");
}
CPPUNIT_TEST_FIXTURE(ScExportTest4, testTdf166413)
{
createScDoc("xlsx/tdf166413.xlsx");
save(u"Calc Office Open XML"_ustr);
xmlDocUniquePtr pSheet = parseExport(u"xl/worksheets/sheet1.xml"_ustr);
CPPUNIT_ASSERT(pSheet);
// Without the accompanying fix in place, this test would have failed with
// - Expected: NOT(ISERROR(SEARCH("""ABC""",A1)))
// - Actual : NOT(ISERROR(SEARCH(""ABC"",A1)))
assertXPathContent(pSheet,
"/x:worksheet/x:conditionalFormatting[@sqref=\"A1:C1\"]/x:cfRule/x:formula",
u"NOT(ISERROR(SEARCH(\"\"\"ABC\"\"\",A1)))");
// Similarly
// - Expected: ISERROR(SEARCH("""ABC""",A2))
// - Actual : ISERROR(SEARCH(""ABC"",A2))
assertXPathContent(pSheet,
"/x:worksheet/x:conditionalFormatting[@sqref=\"A2:C2\"]/x:cfRule/x:formula",
u"ISERROR(SEARCH(\"\"\"ABC\"\"\",A2))");
// Similarly
// - Expected: LEFT(A3,LEN("""ABC"""))="""ABC"""
// - Actual : LEFT(A3,LEN(""ABC""))=""ABC""
assertXPathContent(pSheet,
"/x:worksheet/x:conditionalFormatting[@sqref=\"A3:C3\"]/x:cfRule/x:formula",
u"LEFT(A3,LEN(\"\"\"ABC\"\"\"))=\"\"\"ABC\"\"\"");
// Similarly
// - Expected: RIGHT(A4,LEN("""ABC"""))="""ABC"""
// - Actual : RIGHT(A4,LEN(""ABC""))=""ABC""
assertXPathContent(pSheet,
"/x:worksheet/x:conditionalFormatting[@sqref=\"A4:C4\"]/x:cfRule/x:formula",
u"RIGHT(A4,LEN(\"\"\"ABC\"\"\"))=\"\"\"ABC\"\"\"");
}
CPPUNIT_PLUGIN_IMPLEMENT();
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
+7 -5
View File
@@ -1001,11 +1001,13 @@ bool RequiresFixedFormula(ScConditionMode eMode)
return false;
}
OString GetFixedFormula(ScConditionMode eMode, const ScAddress& rAddress, std::string_view rText)
OString GetFixedFormula(ScConditionMode eMode, const ScAddress& rAddress, const OString& rText)
{
OStringBuffer aBuffer;
XclXmlUtils::ToOString(aBuffer, rAddress);
OString aPos = aBuffer.makeStringAndClear();
// double quotes in rText need to be escaped
const OString aText = rText.replaceAll("\""_ostr, "\"\""_ostr);
switch (eMode)
{
case ScConditionMode::Error:
@@ -1013,13 +1015,13 @@ OString GetFixedFormula(ScConditionMode eMode, const ScAddress& rAddress, std::s
case ScConditionMode::NoError:
return OString("NOT(ISERROR(" + aPos + "))") ;
case ScConditionMode::BeginsWith:
return OString("LEFT(" + aPos + ",LEN(\"" + rText + "\"))=\"" + rText + "\"");
return OString("LEFT(" + aPos + ",LEN(\"" + aText + "\"))=\"" + aText + "\"");
case ScConditionMode::EndsWith:
return OString("RIGHT(" + aPos +",LEN(\"" + rText + "\"))=\"" + rText + "\"");
return OString("RIGHT(" + aPos +",LEN(\"" + aText + "\"))=\"" + aText + "\"");
case ScConditionMode::ContainsText:
return OString(OString::Concat("NOT(ISERROR(SEARCH(\"") + rText + "\"," + aPos + ")))");
return OString(OString::Concat("NOT(ISERROR(SEARCH(\"") + aText + "\"," + aPos + ")))");
case ScConditionMode::NotContainsText:
return OString(OString::Concat("ISERROR(SEARCH(\"") + rText + "\"," + aPos + "))");
return OString(OString::Concat("ISERROR(SEARCH(\"") + aText + "\"," + aPos + "))");
default:
break;
}