Bug 166102 - Exporting a Writer table formula with a sum of range to DOCX produces an invalid formula
Summary: Exporting a Writer table formula with a sum of range to DOCX produces an inva...
Status: RESOLVED FIXED
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: Writer (show other bugs)
Version:
(earliest affected)
7.1.0.3 release
Hardware: All All
: medium normal
Assignee: Xisco Faulí
URL:
Whiteboard: target:25.8.0 target:25.2.4
Keywords: bibisected, bisected
Depends on:
Blocks: Writer-Tables-Formulas
  Show dependency treegraph
 
Reported: 2025-04-09 11:37 UTC by Mike Kaganski
Modified: 2025-04-29 09:24 UTC (History)
2 users (show)

See Also:
Crash report or crash signature:


Attachments
A simple reproducer (1.04 KB, application/vnd.oasis.opendocument.text)
2025-04-09 11:37 UTC, Mike Kaganski
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Mike Kaganski 2025-04-09 11:37:42 UTC
Created attachment 200248 [details]
A simple reproducer

In the attachment, there is a table with "1", "2" and "3" in A1, A2, and A3; A4 has a formula

  =sum<A1:A3>

Saving the document to DOCX and reloading produces ** Expression is faulty ** in A4. Opening in Word, and updating the field in A4, produces "!Syntax error, :".

Started in 7.1; in 7.0, the formula was exported as simple value.
Comment 1 Mateusz Wlazłowski 2025-04-09 18:02:04 UTC
Could reproduce


Version: 25.2.2.2 (X86_64) / LibreOffice Community
Build ID: 7370d4be9e3cf6031a51beef54ff3bda878e3fac
CPU threads: 8; OS: Linux 6.11; UI render: default; VCL: gtk3
Locale: en-US (en_US.UTF-8); UI: en-US
Flatpak
Calc: threaded
Comment 2 Xisco Faulí 2025-04-10 08:29:37 UTC
if the formula is changed from '=sum<A1:A3>' to '=sum(<A1:A3>)' before saving then it works
Comment 3 Xisco Faulí 2025-04-10 08:44:44 UTC
The issue started to happen after

commit cf596c43315bb96b5e7256a82256f1ccb8c9c4d0	[log]
author	László Németh <nemeth@numbertext.org>	Thu Aug 13 12:05:20 2020 +0200
committer	László Németh <nemeth@numbertext.org>	Fri Aug 14 09:13:03 2020 +0200
tree aa81fa39cefaeb670db98fc5278ea0bdadecc9d3
parent 6457d46967f8dbb41199b750d59edde839f24b5d [diff]

tdf#133163 DOCX: export formula cells
Comment 4 Mike Kaganski 2025-04-10 09:13:41 UTC
Regarding formula syntax, see https://bugs.documentfoundation.org/show_bug.cgi?id=133647#c8
Comment 5 Xisco Faulí 2025-04-10 10:52:59 UTC
My take on it: https://gerrit.libreoffice.org/c/core/+/183967
Comment 6 Xisco Faulí 2025-04-10 17:51:36 UTC
after a second thought, this is an import issue, and not a export issue. it has to be fixed in convertFieldFormula in sw/source/writerfilter/dmapper/DomainMapper_Impl.cxx
Comment 7 Mike Kaganski 2025-04-10 17:55:38 UTC
(In reply to Xisco Faulí from comment #6)

I'm sorry, how can a formula exported as "=sumA1:A3" be *imported* with correction? The token "sumA1" is a valid name, and is *not* the same as "sum(A1" or "sum A1".
Comment 8 Xisco Faulí 2025-04-10 18:14:58 UTC
(In reply to Mike Kaganski from comment #7)
> (In reply to Xisco Faulí from comment #6)
> 
> I'm sorry, how can a formula exported as "=sumA1:A3" be *imported* with
> correction? The token "sumA1" is a valid name, and is *not* the same as
> "sum(A1" or "sum A1".

Hi Mike,
Can you give an example where "sumA1" could be a valid name ?
Comment 9 Mike Kaganski 2025-04-10 18:27:41 UTC
(In reply to Xisco Faulí from comment #8)

You may create arbitrary variables in Writer, using "Define variable" field [1], and there you define the names (e.g., sumA1); and then you may use these names in formulas, including table formulas.

In Word, there is no fields like that, but there is a programmatic way to define document variables [2]. However, I don't know if these variables may be used like that in "formula" fields.

But even if the above didn't exist:

* Word can't read that formula correctly, so it *is* exported incorrectly;
* We can't assume that tomorrow, there won't appear a new function with a name that clashes with something that we decide to parse like what you suggest.

And really, that "this is an import issue, and not a export issue" was shocking for me :)

[1] https://help.libreoffice.org/25.2/en-US/text/swriter/01/04090005.html
[2] https://support.microsoft.com/en-us/topic/how-to-store-and-retrieve-variables-in-word-documents-3a912a35-42a0-7a28-7f2d-787ed6afb566
Comment 10 Xisco Faulí 2025-04-10 18:39:40 UTC
before I (In reply to Mike Kaganski from comment #9)
> * Word can't read that formula correctly, so it *is* exported incorrectly;

I think this is not correct. if you revert a586ce188086ff27b08b8a4de4672c07ddf8ed7c causing bug 166125, then Word is able to read the formula correctly and 6 is displayed ( at least with Office 2016 ) that's why I assumed it was a import issue and not an export issue.
Replacing < and > with spaces would cause many tests to fail ( https://ci.libreoffice.org/job/gerrit_linux_gcc_release/186458/consoleFull#1682007923d893063f-7f3d-4b7e-b56f-4e0f225817cd ) so we either adapt those tests as well or we also have to add a regexmatcher to convertFieldFormula to remove the spaces
Comment 11 Mike Kaganski 2025-04-10 18:41:08 UTC
(In reply to Xisco Faulí from comment #10)
> (In reply to Mike Kaganski from comment #9)
> > * Word can't read that formula correctly, so it *is* exported incorrectly;
> 
> I think this is not correct. if you revert
> a586ce188086ff27b08b8a4de4672c07ddf8ed7c causing bug 166125, then Word is
> able to read the formula correctly and 6 is displayed ( at least with Office
> 2016 )

... until you refresh the field - which means, it only shows a cached value, and that is unrelated to the correctness of formula handling :-)
Comment 12 Xisco Faulí 2025-04-10 19:26:57 UTC
(In reply to Mike Kaganski from comment #11)
> (In reply to Xisco Faulí from comment #10)
> > (In reply to Mike Kaganski from comment #9)
> > > * Word can't read that formula correctly, so it *is* exported incorrectly;
> > 
> > I think this is not correct. if you revert
> > a586ce188086ff27b08b8a4de4672c07ddf8ed7c causing bug 166125, then Word is
> > able to read the formula correctly and 6 is displayed ( at least with Office
> > 2016 )
> 
> ... until you refresh the field - which means, it only shows a cached value,
> and that is unrelated to the correctness of formula handling :-)

indeed. I missed that step. Thanks!!
Comment 13 Xisco Faulí 2025-04-10 19:32:51 UTC
it seems using spaces also caused a Syntax error, so the way to go is by using parentheses
Comment 14 Xisco Faulí 2025-04-10 19:34:03 UTC
(In reply to Xisco Faulí from comment #13)
> it seems using spaces also caused a Syntax error, so the way to go is by
> using parentheses

* causes a Syntax error in Word after updating the field
Comment 15 Commit Notification 2025-04-11 19:10:09 UTC
Xisco Fauli committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/7f8602e4feac16b5d0f6b80b93d5be6378f0aa11

tdf#166102: replace < and > with parentheses

It will be available in 25.8.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 16 Commit Notification 2025-04-29 09:24:39 UTC
Xisco Fauli committed a patch related to this issue.
It has been pushed to "libreoffice-25-2":

https://git.libreoffice.org/core/commit/9bbd80c06ed0b563184882873fbe129b245504e5

tdf#166102: replace < and > with parentheses

It will be available in 25.2.4.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.