tdf#159483 sc HTML copy: handle data-sheets-formula attribute
When a formula cell gets copied from Calc to google docs, only the
formula result was copied, not the formula.
There is a data-sheets-formula attribute on <td> that can describe the
formula we have.
Fix the problem by extending ScHTMLExport::WriteCell() to emit that.
This is more or less the export equivalent of commit
7812adb2ed11a3e08be24d3f2f94d14bfd740c55 (tdf#159483 sc HTML paste:
handle data-sheets-formula attribute, 2024-02-12).
Change-Id: Iab373ce8a028deb6a2874a8c690e928edf5d79f4
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/164363
Reviewed-by: Miklos Vajna <[email protected]>
Tested-by: Jenkins
diff --git a/sc/qa/filter/html/html.cxx b/sc/qa/filter/html/html.cxx
index 916dd23..391806b 100644
--- a/sc/qa/filter/html/html.cxx
+++ b/sc/qa/filter/html/html.cxx
@@ -276,6 +276,32 @@ CPPUNIT_TEST_FIXTURE(Test, testCopyFormattedNumber)
assertXPath(pHtmlDoc, "(//td)[2]"_ostr, "data-sheets-numberformat"_ostr,
"{ \"1\": 2, \"2\": \"#,##0.00\", \"3\": 1}");
}
CPPUNIT_TEST_FIXTURE(Test, testCopyFormula)
{
// Given a document with a formula in A3:
createScDoc();
ScDocument* pDoc = getScDoc();
ScAddress aCellPos1(/*nColP=*/0, /*nRowP=*/0, /*nTabP=*/0);
pDoc->SetString(aCellPos1, "1000");
ScAddress aCellPos2(/*nColP=*/0, /*nRowP=*/1, /*nTabP=*/0);
pDoc->SetString(aCellPos2, "2000");
ScAddress aCellPos3(/*nColP=*/0, /*nRowP=*/2, /*nTabP=*/0);
pDoc->SetFormula(aCellPos3, "=SUM(A1:A2)", pDoc->GetGrammar());
// When copying those cells:
ScImportExport aExporter(*pDoc, ScRange(aCellPos1, aCellPos3));
SvMemoryStream aStream;
CPPUNIT_ASSERT(aExporter.ExportStream(aStream, OUString(), SotClipboardFormatId::HTML));
// Then make sure the formula is exported in A3:
aStream.Seek(0);
htmlDocUniquePtr pHtmlDoc = parseHtmlStream(&aStream);
// Without the accompanying fix in place, this test would have failed with:
// - XPath '(//td)[3]' no attribute 'data-sheets-formula' exist
// i.e. only the formula result was exported, not the formula.
assertXPath(pHtmlDoc, "(//td)[3]"_ostr, "data-sheets-formula"_ostr, "=SUM(R[-2]C:R[-1]C)");
}
}
CPPUNIT_PLUGIN_IMPLEMENT();
diff --git a/sc/source/filter/html/htmlexp.cxx b/sc/source/filter/html/htmlexp.cxx
index c2554b7..4413d66 100644
--- a/sc/source/filter/html/htmlexp.cxx
+++ b/sc/source/filter/html/htmlexp.cxx
@@ -1172,6 +1172,15 @@ void ScHTMLExport::WriteCell( sc::ColumnBlockPosition& rBlockPos, SCCOL nCol, SC
}
}
}
if (aCell.getType() == CELLTYPE_FORMULA)
{
// If it's a formula, then also emit that, grammar is R1C1 reference style.
OUString aFormula = aCell.getFormula()->GetFormula(
formula::FormulaGrammar::GRAM_ENGLISH_XL_R1C1);
aStrTD.append(" " OOO_STRING_SVTOOLS_HTML_O_DSformula "=\""
+ HTMLOutFuncs::ConvertStringToHTML(aFormula) + "\"");
}
}
else
{