Reading and Writing Query Table of Worksheet with Node.js via C++

Reading and Writing Query Table of Worksheet

The following sample code reads the first QueryTable of the first worksheet, prints both of the QueryTable properties, and then sets the QueryTable.preserveFormatting property to true.

You can download the source Excel file used in this code and the output Excel file generated by the code from the following links.

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "Sample_queries.xlsx");
// Create workbook from source excel file
const workbook = new AsposeCells.Workbook(filePath);

// Access first worksheet
const worksheet = workbook.getWorksheets().get(0);

// Access first Query Table
const qt = worksheet.getQueryTables().get(0);

// Print Query Table Data
console.log("Adjust Column Width: " + qt.getAdjustColumnWidth());
console.log("Preserve Formatting: " + qt.getPreserveFormatting());

// Now set Preserve Formatting to true
qt.setPreserveFormatting(true);

// Save the workbook
workbook.save(path.join(dataDir, "Output_out.xlsx"));

Console Output

Here is the console output of the above sample code:

Adjust Column Width: True
Preserve Formatting: False

Retrieve Query Table Result Range

Aspose.Cells provides an option to read the address, i.e., the result range of cells, for a query table. The following code demonstrates this feature by reading the address of the result range for a query table. The sample file can be downloaded here.

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "Sample_queries.xlsx");

// Create workbook from source excel file
const wb = new AsposeCells.Workbook(filePath);

// Display the address (range) of the result range of the query table
console.log(wb.getWorksheets().get(0).getQueryTables().get(0).getResultRange().getAddress());