Updating Fields while Assembling Word Processing Documents

Updating fields

By default, fields in a Word Processing template (for example, =SUM(ABOVE) formulas in tables) keep the result values stored in the template. Apply the DocumentAssemblyOptions.UPDATE_FIELDS_AND_FORMULAS option to make the engine recalculate them while assembling the report.

The recipe

  • Set up the source template path
  • Set up the destination report path
  • Instantiate the DocumentAssembler class
  • Set the DocumentAssemblyOptions.UPDATE_FIELDS_AND_FORMULAS option
  • Generate the report

The template

The template contains a list of clients built from data and a table with a formula field in the last cell:

We provide support for the following clients:
<<foreach [in clients]>><<[Name]>>
<</foreach>>
ModelPrice
Lumia 640 XL18000
Lumia 55012500
Total{ =SUM(ABOVE) \# "#,##0" }

The data is a JSON array:

[ { "Name": "A Company" }, { "Name": "B Ltd." }, { "Name": "C & D" } ]

The code

const groupdocs = require('@groupdocs/groupdocs.assembly');

const templatePath = "Update field.docx";
const reportPath = "Update field_report.docx";

const assembler = new groupdocs.DocumentAssembler();
// Recalculate fields and formulas while assembling
assembler.setOptions(groupdocs.DocumentAssemblyOptions.UPDATE_FIELDS_AND_FORMULAS);

const dataSource = new groupdocs.JsonDataSource("clients.json");
assembler.assembleDocument(templatePath, reportPath,
    new groupdocs.DataSourceInfo(dataSource, "clients"));

process.exit(0);

In the report, the Total cell shows the recalculated value 30,500 (the number format follows the current culture). Without the option, the field keeps the value that was stored in the template.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.