From 03c2b0a9be7e23033c29e8a59a1764f45d348af7 Mon Sep 17 00:00:00 2001 From: Stefan Rijnhart Date: Tue, 9 Feb 2021 14:03:42 +0100 Subject: [PATCH] [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. --- report_xlsx_helper/report/report_xlsx_abstract.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/report_xlsx_helper/report/report_xlsx_abstract.py b/report_xlsx_helper/report/report_xlsx_abstract.py index b4eb0dfcb..6d67daa1a 100644 --- a/report_xlsx_helper/report/report_xlsx_abstract.py +++ b/report_xlsx_helper/report/report_xlsx_abstract.py @@ -723,6 +723,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 @@ -735,6 +736,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, "", "eval")