jmix icon indicating copy to clipboard operation
jmix copied to clipboard

XLSX formula value is not calulated when the report file is opened in LibreOffice

Open gorbunkov opened this issue 3 years ago • 1 comments

Add a formula that sums column values.

Screenshot 2022-04-14 at 10 41 10

Run the report.

If you open the report using MS Office, the formula is evaluated correctly.

If you open the report using LibreOffice, there will be a correct formula in the cell, but the value is not calculated.

Screenshot 2022-04-14 at 10 34 32

If you invoke Data - Calculate - Recalculate (F9) command in LibreOffice, the formula value will be refreshed.

See CUBA forum topic See CUBA issue

gorbunkov avatar Apr 14 '22 06:04 gorbunkov

The workaround is to post-process the report using Apache POI that can evaluate formulas:

        ReportOutputDocument document = reportRunner.byReportCode("contracts-report")
                .run();
        ByteArrayInputStream bis = new ByteArrayInputStream(document.getContent());
        Workbook wb = new XSSFWorkbook(bis);
        FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
        for (Sheet sheet : wb) {
            for (Row r : sheet) {
                for (Cell c : r) {
                    if (c.getCellType() == CellType.FORMULA) {
                        evaluator.evaluateFormulaCell(c);
                    }
                }
            }
        }
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        wb.write(bos);
        byte[] modifiedFileBytes = bos.toByteArray();

Maybe we can add this behavior to each report generation and enable/disable it by some feature flag.

gorbunkov avatar Apr 14 '22 09:04 gorbunkov

Added new property to force formulas evaluation - jmix.reports.formulas-post-processing-evaluation-enabled. True by default.

Gavrilov-Ivan avatar Dec 15 '22 11:12 Gavrilov-Ivan

Checklist A template xlsx containing the formula has been created Open the generated report using LibreOffice 7.4

rusiaikinat avatar Dec 27 '22 12:12 rusiaikinat

Updated: default value is 'false'.

Gavrilov-Ivan avatar Jan 17 '23 13:01 Gavrilov-Ivan

In most cases is will be enough to set LibreOffice property Tools - Options - LibreOffice Calc - Formula - Recalculation on File Load to 'Always recalculate'

Gavrilov-Ivan avatar Jan 19 '23 11:01 Gavrilov-Ivan