/** An ordinary calculated sheet: per-row formulas plus aggregates. */
private Workbook calculate() {
Workbook wb = invoice();
Sheet sheet = wb.sheet(0);
for (int row = 1; row <= 5; row++) {
sheet.cell(row, 3).setFormula("B" + (row + 1) + "*C" + (row + 1));
}
sheet.cell(7, 0).setValue(CellValue.text("Subtotal"));
sheet.cell(7, 3).setFormula("SUM(D2:D6)");
sheet.cell(8, 0).setValue(CellValue.text("VAT 20%"));
sheet.cell(8, 3).setFormula("D8*0.2");
sheet.cell(9, 0).setValue(CellValue.text("Total"));
sheet.cell(9, 3).setFormula("D8+D9");
sheet.range("A8:D10").setStyle(CellStyle.builder().bold().build());
sheet.range("D2:D10").setStyle(CellStyle.builder().numberFormat("#,##0.00").build());
wb.recalculate();
return wb;
}
Formulas are assigned as text and remain inert until a recalculation is performed. The second
button makes this explicit by reading the same cell before and after recalculate is
invoked.
The engine can also evaluate an expression ad hoc, with no cell involved, which is useful for
validating expressions supplied by a user. The dynamic array button spills the results of functions
such as SEQUENCE and UNIQUE across the adjacent cells, and the final
button illustrates what manual calculation mode defers.