Updating Fields while Assembling Word Processing Documents

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Assembly MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-assembly -- dnx GroupDocs.Assembly.Mcp --yes
Folders, licensing and more →

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.