diff options
author | Miklos Vajna <vmiklos@collabora.com> | 2024-03-04 15:00:51 +0100 |
---|---|---|
committer | Caolán McNamara <caolan.mcnamara@collabora.com> | 2024-03-06 09:40:31 +0100 |
commit | e7d23a513b58d2a9dc5b303d9ca6484ed676b195 (patch) | |
tree | 32c56f1db1b5596d24d3c3357e7078e3c9a2e3f1 /sc | |
parent | 7405a5eb7e727148d9b063f1f01d073565b9f092 (diff) |
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).
(cherry picked from commit 2efe362c99a9fa6e9a71b9b675b025c64b6c7f9d)
Change-Id: Iab373ce8a028deb6a2874a8c690e928edf5d79f4
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/164417
Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoffice@gmail.com>
Reviewed-by: Caolán McNamara <caolan.mcnamara@collabora.com>
Diffstat (limited to 'sc')
-rw-r--r-- | sc/qa/filter/html/html.cxx | 26 | ||||
-rw-r--r-- | sc/source/filter/html/htmlexp.cxx | 9 |
2 files changed, 35 insertions, 0 deletions
diff --git a/sc/qa/filter/html/html.cxx b/sc/qa/filter/html/html.cxx index 916dd23981ec..391806be0333 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 c2554b7612f1..4413d668b428 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 { |