Defining Relations Between DocumentTable Instances Loaded from a Single Document
Defining Relations Between DocumentTable Instances Loaded from a Single Document
Leave feedback
On this page
Relations can be defined between DocumentTable instances loaded from a single document into a DocumentTableSet. DocumentTableSet.getRelations() returns a DocumentTableRelationCollection; its add(parentColumn, childColumn) method creates a DocumentTableRelation between a column of a parent table and a column of a child table.
Once a relation is defined, a template can navigate from a child row to the related parent row by the parent table name (for example, Manager.Name), and from a parent row to its child rows.
The template contains a three-column table. The cells of its data row contain:
<<foreach [in Contract]>><<[Manager.Name]>>
<<[Client.Name]>>
<<[Price]>><</foreach>>
Using Document Table Relations
constjava=require('java');constgroupdocs=require('@groupdocs/groupdocs.assembly');// Extract column names from the first row of every table.
constcolumnNameExtractingDocumentTableLoadHandler=java.newProxy('com.groupdocs.assembly.IDocumentTableLoadHandler',{handle:function(args){args.setOptions(newgroupdocs.DocumentTableOptions());args.getOptions().setFirstRowContainsColumnNames(true);}});consttableSet=newgroupdocs.DocumentTableSet('Related Tables Data.xlsx',columnNameExtractingDocumentTableLoadHandler);// Define relations between tables.
// NOTE: For spreadsheet documents, table names are extracted from worksheet names.
consttables=tableSet.getTables();tableSet.getRelations().add(tables.get('CLIENT').getColumns().get('ID'),tables.get('CONTRACT').getColumns().get('CLIENT_ID'));tableSet.getRelations().add(tables.get('MANAGER').getColumns().get('ID'),tables.get('CONTRACT').getColumns().get('MANAGER_ID'));// Pass DocumentTableSet as a data source.
constassembler=newgroupdocs.DocumentAssembler();assembler.assembleDocument('Using Document Table Relations.docx','out.docx',newgroupdocs.DataSourceInfo(tableSet));process.exit(0);
The report lists every contract with its manager name, client name and price, for example:
Manager
Client
Contract Price
John Smith
A Company
1200000.0
John Smith
B Ltd.
750000.0
Tony Anderson
E Corp.
650000.0
Table and column names in template expressions are case-insensitive, so Contract, Manager.Name and Price match the CONTRACT table and its NAME and PRICE columns.