Loading Multiple DocumentTable Objects from a Single File as a Single Operation
Loading Multiple DocumentTable Objects from a Single File as a Single Operation
Leave feedback
On this page
DocumentTableSet loads all tables of a document as a single operation. The loaded tables are available through getTables(), which returns a collection of DocumentTable objects accessible by index or by name.
Each DocumentTable provides:
getName() / setName() – the table name. By default, tables are named Table1, Table2, and so on; for spreadsheets, names are taken from worksheet names.
getIndexInDocument() – the zero-based index of the table in the document.
To control how each table is loaded, pass an implementation of the Java interface com.groupdocs.assembly.IDocumentTableLoadHandler. In JavaScript, create it with java.newProxy from the java package. The handler’s handle(args) method receives a DocumentTableLoadArgs object with the following members:
getTableIndex() – the index of the table being loaded.
isLoaded(false) – skips loading of the table.
getOptions() / setOptions() – the DocumentTableOptions used to load the table.
The following handler loads the first table with default options, skips the second table, and extracts column names from the first row of the third table:
constjava=require('java');constgroupdocs=require('@groupdocs/groupdocs.assembly');constcustomDocumentTableLoadHandler=java.newProxy('com.groupdocs.assembly.IDocumentTableLoadHandler',{handle:function(args){switch(args.getTableIndex()){case0:// Do nothing. The table is loaded with default options.
break;case1:// Discard loading of the table completely.
args.isLoaded(false);break;case2:// Load the table with custom options.
args.setOptions(newgroupdocs.DocumentTableOptions());args.getOptions().setFirstRowContainsColumnNames(true);break;}}});// Load document tables using custom options.
consttableSet=newgroupdocs.DocumentTableSet('Multiple Tables Data.docx',customDocumentTableLoadHandler);// Ensure that the second table is not loaded.
consttables=tableSet.getTables();console.log(tables.getCount());// 2
console.log(tables.get(0).getName());// Table1
console.log(tables.get(1).getName());// Table3
// Ensure that default options are used to load the first table (default column names are used).
constfirstColumns=tables.get(0).getColumns();console.log(firstColumns.get(0).getName(),firstColumns.get(1).getName());// Column1 Column2
// Ensure that custom options are used to load the third table (column names are extracted).
constthirdColumns=tables.get(1).getColumns();console.log(thirdColumns.get(0).getName(),thirdColumns.get(1).getName());// Name Address
process.exit(0);
Using DocumentTableSet as Data Source
A DocumentTableSet can be passed to the engine as a data source without a name. Its tables are then referenced in the template by their names:
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('Multiple Tables Data.docx',columnNameExtractingDocumentTableLoadHandler);// Set table names for convenience.
tableSet.getTables().get(0).setName('Planets');tableSet.getTables().get(1).setName('Persons');tableSet.getTables().get(2).setName('Companies');// Pass DocumentTableSet as a data source.
constassembler=newgroupdocs.DocumentAssembler();assembler.assembleDocument('Using Document Table Set as Data Source.pptx','out.pptx',newgroupdocs.DataSourceInfo(tableSet));process.exit(0);
Note
In evaluation mode, text read from PPTX templates is truncated, which can make template tags invalid. Apply a license to process presentations without this limitation, or use a template of another format. See Evaluation Limitations and Licensing.