Using Documents as Data Source

GroupDocs.Assembly for Node.js via Java can use a table from another document as a data source. The following classes are used:

ClassPurpose
DocumentTableA single table loaded from a spreadsheet worksheet, a word processing table or a presentation table.
DocumentTableOptionsOptions for loading a table: whether the first row contains column names, and the row and column index limits.
DocumentTableSetAll tables of a document loaded as a single operation, with optional relations between them.

A DocumentTable is passed to the engine like any other data source and is treated as a list of rows in templates:

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

const options = new groupdocs.DocumentTableOptions();
options.setFirstRowContainsColumnNames(true);

// Load the first table (worksheet) of the document.
const table = new groupdocs.DocumentTable('Contracts Data.xlsx', 0, options);

const assembler = new groupdocs.DocumentAssembler();
assembler.assembleDocument('template.docx', 'report.docx',
    new groupdocs.DataSourceInfo(table, 'contracts'));

process.exit(0);

Key points:

  • The second constructor argument is the zero-based index of the table in the document (for spreadsheets, the index of the worksheet).
  • When setFirstRowContainsColumnNames(true) is not set, the first row is treated as data and default column names are used: A, B, C, … for spreadsheets and Column1, Column2, … for other documents.
  • Spaces in column names are replaced with underscores, for example Contract Price becomes Contract_Price.
  • For spreadsheets, a column type is detected from cell values (for example, double when all cells are numeric). For other documents, columns are java.lang.String by default. See Changing Automatically Detected Types of DocumentTable Columns.
  • setMinRowIndex, setMaxRowIndex, setMinColumnIndex and setMaxColumnIndex of DocumentTableOptions limit the loaded range.
  • DocumentTable and DocumentTableSet also have constructors that accept a Java InputStream instead of a file path.

Articles in this section

Close
Loading

Analyzing your prompt, please hold on...

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