summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/svtools/htmlkywd.hxx1
-rw-r--r--include/svtools/htmltokn.h1
-rw-r--r--sc/qa/filter/html/data/formula.html7
-rw-r--r--sc/qa/filter/html/html.cxx25
-rw-r--r--sc/source/filter/html/htmlpars.cxx14
-rw-r--r--sc/source/filter/inc/eeparser.hxx2
-rw-r--r--sc/source/filter/rtf/eeimpars.cxx5
-rw-r--r--svtools/source/svhtml/htmlkywd.cxx1
8 files changed, 56 insertions, 0 deletions
diff --git a/include/svtools/htmlkywd.hxx b/include/svtools/htmlkywd.hxx
index 80e47b100187..36e853960feb 100644
--- a/include/svtools/htmlkywd.hxx
+++ b/include/svtools/htmlkywd.hxx
@@ -448,6 +448,7 @@
#define OOO_STRING_SVTOOLS_HTML_O_DSval "data-sheets-value"
#define OOO_STRING_SVTOOLS_HTML_O_SDnum "sdnum"
#define OOO_STRING_SVTOOLS_HTML_O_DSnum "data-sheets-numberformat"
+#define OOO_STRING_SVTOOLS_HTML_O_DSformula "data-sheets-formula"
#define OOO_STRING_SVTOOLS_HTML_O_sdlibrary "sdlibrary"
#define OOO_STRING_SVTOOLS_HTML_O_sdmodule "sdmodule"
#define OOO_STRING_SVTOOLS_HTML_O_sdevent "sdevent-"
diff --git a/include/svtools/htmltokn.h b/include/svtools/htmltokn.h
index 21fcec800a9a..89d8ee868535 100644
--- a/include/svtools/htmltokn.h
+++ b/include/svtools/htmltokn.h
@@ -347,6 +347,7 @@ STRING_START = BOOL_END,
DSVAL,
SDNUM, // StarDiv NumberFormat
DSNUM,
+ DSFORMULA,
SDLIBRARY,
SDMODULE,
STRING_END,
diff --git a/sc/qa/filter/html/data/formula.html b/sc/qa/filter/html/data/formula.html
new file mode 100644
index 000000000000..f6c9245d4c02
--- /dev/null
+++ b/sc/qa/filter/html/data/formula.html
@@ -0,0 +1,7 @@
+<table>
+ <tr>
+ <td data-sheets-value="{&quot;1&quot;:3,&quot;3&quot;:1}">1</td>
+ <td data-sheets-value="{&quot;1&quot;:3,&quot;3&quot;:2}">2</td>
+ <td data-sheets-value="{&quot;1&quot;:3,&quot;3&quot;:3}" data-sheets-formula="=SUM(R[0]C[-2]:R[0]C[-1])">3</td>
+ </tr>
+</table>
diff --git a/sc/qa/filter/html/html.cxx b/sc/qa/filter/html/html.cxx
index b66976396f99..83e35d9f8281 100644
--- a/sc/qa/filter/html/html.cxx
+++ b/sc/qa/filter/html/html.cxx
@@ -143,6 +143,31 @@ CPPUNIT_TEST_FIXTURE(Test, testPasteTdAsFormattedNumber)
CPPUNIT_ASSERT_EQUAL(static_cast<double>(1000),
pDoc->GetValue(/*col=*/0, /*row=*/0, /*tab=*/0));
}
+
+CPPUNIT_TEST_FIXTURE(Test, testPasteTdAsFormula)
+{
+ // Given an empty document:
+ createScDoc();
+
+ // When pasting HTML with cells containing a formula:
+ ScDocument* pDoc = getScDoc();
+ ScAddress aCellPos(/*nColP=*/0, /*nRowP=*/0, /*nTabP=*/0);
+ ScImportExport aImporter(*pDoc, aCellPos);
+ SvFileStream aFile(createFileURL(u"formula.html"), StreamMode::READ);
+ SvMemoryStream aMemory;
+ aMemory.WriteStream(aFile);
+ aMemory.Seek(0);
+ CPPUNIT_ASSERT(aImporter.ImportStream(aMemory, OUString(), SotClipboardFormatId::HTML));
+
+ // Then make sure C1 is a sum and it evaluates to 3:
+ // Without the accompanying fix in place, this test would have failed with:
+ // - Expected: =SUM(A1:B1)
+ // - Actual :
+ // i.e. only the formula result was imported, not the formula.
+ CPPUNIT_ASSERT_EQUAL(OUString("=SUM(A1:B1)"),
+ pDoc->GetFormula(/*col=*/2, /*row=*/0, /*tab=*/0));
+ CPPUNIT_ASSERT_EQUAL(static_cast<double>(3), pDoc->GetValue(/*col=*/2, /*row=*/0, /*tab=*/0));
+}
}
CPPUNIT_PLUGIN_IMPLEMENT();
diff --git a/sc/source/filter/html/htmlpars.cxx b/sc/source/filter/html/htmlpars.cxx
index 78aaafdad70b..5db879db75dd 100644
--- a/sc/source/filter/html/htmlpars.cxx
+++ b/sc/source/filter/html/htmlpars.cxx
@@ -155,6 +155,14 @@ void ParseDataSheetsNumberformat(const OUString& rDataSheetsValue, std::optional
}
}
}
+
+/// data-sheets-formula from google sheets, grammar is R1C1 reference style.
+void ParseDataSheetsFormula(const OUString& rDataSheetsFormula, std::optional<OUString>& rVal,
+ std::optional<formula::FormulaGrammar::Grammar>& rGrammar)
+{
+ rVal = rDataSheetsFormula;
+ rGrammar = formula::FormulaGrammar::GRAM_ENGLISH_XL_R1C1;
+}
}
ScHTMLStyles::ScHTMLStyles() : maEmpty() {}
@@ -1074,6 +1082,12 @@ void ScHTMLLayoutParser::TableDataOn( HtmlImportInfo* pInfo )
ParseDataSheetsNumberformat(rOption.GetString(), mxActEntry->pNumStr);
}
break;
+ case HtmlOptionId::DSFORMULA:
+ {
+ ParseDataSheetsFormula(rOption.GetString(), mxActEntry->moFormulaStr,
+ mxActEntry->moFormulaGrammar);
+ }
+ break;
default: break;
}
}
diff --git a/sc/source/filter/inc/eeparser.hxx b/sc/source/filter/inc/eeparser.hxx
index ebc383e32dac..e890a80dd111 100644
--- a/sc/source/filter/inc/eeparser.hxx
+++ b/sc/source/filter/inc/eeparser.hxx
@@ -56,6 +56,8 @@ struct ScEEParseEntry
ESelection aSel; // Selection in EditEngine
std::optional<OUString>
pValStr; // HTML possibly SDVAL string
+ std::optional<OUString> moFormulaStr;
+ std::optional<formula::FormulaGrammar::Grammar> moFormulaGrammar;
std::optional<OUString>
pNumStr; // HTML possibly SDNUM string
std::optional<OUString>
diff --git a/sc/source/filter/rtf/eeimpars.cxx b/sc/source/filter/rtf/eeimpars.cxx
index 70562308120f..57d81598bfd9 100644
--- a/sc/source/filter/rtf/eeimpars.cxx
+++ b/sc/source/filter/rtf/eeimpars.cxx
@@ -339,6 +339,11 @@ void ScEEImport::WriteToDocument( bool bSizeColsRows, double nOutputFactor, SvNu
if (!aValStr.isEmpty())
mpDoc->SetValue( nCol, nRow, nTab, fVal );
+ else if (pE->moFormulaStr && pE->moFormulaGrammar)
+ {
+ mpDoc->SetFormula(ScAddress(nCol, nRow, nTab), *pE->moFormulaStr,
+ *pE->moFormulaGrammar);
+ }
else if ( !pE->aSel.HasRange() )
{
// maybe ALT text of IMG or similar
diff --git a/svtools/source/svhtml/htmlkywd.cxx b/svtools/source/svhtml/htmlkywd.cxx
index a3dff93f9b4f..434e1cb9e488 100644
--- a/svtools/source/svhtml/htmlkywd.cxx
+++ b/svtools/source/svhtml/htmlkywd.cxx
@@ -527,6 +527,7 @@ static HTML_OptionEntry aHTMLOptionTab[] = {
{std::u16string_view(u"" OOO_STRING_SVTOOLS_HTML_O_DSval), HtmlOptionId::DSVAL},
{std::u16string_view(u"" OOO_STRING_SVTOOLS_HTML_O_SDnum), HtmlOptionId::SDNUM}, // StarDiv NumberFormat
{std::u16string_view(u"" OOO_STRING_SVTOOLS_HTML_O_DSnum), HtmlOptionId::DSNUM},
+ {std::u16string_view(u"" OOO_STRING_SVTOOLS_HTML_O_DSformula), HtmlOptionId::DSFORMULA},
{std::u16string_view(u"" OOO_STRING_SVTOOLS_HTML_O_sdlibrary), HtmlOptionId::SDLIBRARY},
{std::u16string_view(u"" OOO_STRING_SVTOOLS_HTML_O_sdmodule), HtmlOptionId::SDMODULE},