[FIX] report_xlsx_helper: force Libreoffice to recompute formulas

As per https://xlsxwriter.readthedocs.io/faq.html:

Q. Why do my formulas show a zero result in some, non-Excel applications?
A. [...] Or, you can set a blank result in the formula, which will also force
recalculation.
pull/482/head
Stefan Rijnhart 2021-02-09 14:03:42 +01:00
parent f95a30c90d
commit 2d5945dbed
1 changed files with 10 additions and 0 deletions

View File

@ -636,6 +636,7 @@ class ReportXlsxAbstract(models.AbstractModel):
if isinstance(cell_format, CodeType):
cell_format = self._eval(cell_format, render_space)
args_data.append(cell_format)
self._apply_formula_quirk(args_data, cell_type, cell_format)
if colspan > 1:
args_pos += [row_pos, pos + colspan - 1]
args = args_pos + args_data
@ -648,6 +649,15 @@ class ReportXlsxAbstract(models.AbstractModel):
return row_pos + 1
@staticmethod
def _apply_formula_quirk(args_data, cell_type, cell_format):
""" Insert empty value to force LibreOffice to recompute the value """
if cell_type == "formula":
if not cell_format:
# Insert positional argument for missing format
args_data.append(None)
args_data.append("")
@staticmethod
def _render(code):
return compile(code, "<string>", "eval")