Format Rows and Columns with Node.js via C++
Working with Rows
How to Adjust Row Height
Aspose.Cells provides a class, Workbook, that represents a Microsoft Excel file. The Workbook class contains a WorksheetCollection that allows access to each worksheet in the Excel file. A worksheet is represented by the Worksheet class. The Worksheet class provides a Cells collection that represents all cells in the worksheet.
The Cells collection provides several methods to manage rows or columns in a worksheet. Some of these are discussed below in more detail.
How to Set the Height of a Row
It is possible to set the height of a single row by calling the Cells collection’s setRowHeight(number, number) method. The setRowHeight(number, number) method takes the following parameters as follows:
- Row index, the index of the row that you’re changing the height of.
- Row height, the row height to apply on the row.
try {
const path = require("path");
const fs = require("fs");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "book1.xls");
// Creating a file stream containing the Excel file to be opened
const fstream = fs.createReadStream(filePath);
// Reading the file stream into a buffer
const chunks = [];
fstream.on('data', chunk => chunks.push(chunk));
fstream.on('end', () => {
const buffer = Buffer.concat(chunks);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
const workbook = new AsposeCells.Workbook(buffer);
// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);
// Setting the height of the second row to 13
worksheet.getCells().setRowHeight(1, 13);
// Saving the modified Excel file
workbook.save(path.join(dataDir, "output.out.xls"));
// Closing the file stream to free all resources
fstream.close();
How to Set the Height of All Rows in a Worksheet
If developers need to set the same row height for all rows in the worksheet, they can do it by using the getStandardHeight() property of the Cells collection.
Example:
const fs = require("fs");
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, "book1.xls");
// Creating a file stream containing the Excel file to be opened
const fstream = fs.readFileSync(filePath);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
const workbook = new AsposeCells.Workbook(fstream);
// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);
// Setting the height of all rows in the worksheet to 15
worksheet.getCells().setStandardHeight(15);
// Saving the modified Excel file
workbook.save(path.join(dataDir, "output.out.xls"));
// Closing the file stream to free all resources
// (Note: Closing the file stream is unnecessary in this context as it's a
// synchronous read performed using fs.readFileSync, which does not require
// explicit closure, but if using fs.createReadStream, you would handle it accordingly)
Working with Columns
How to Set the Width of a Column
Set the width of a column by calling the Cells collection’s setColumnWidth(number, number) method. The setColumnWidth(number, number) method takes the following parameters:
- Column index, the index of the column that you’re changing the width of.
- Column width, the desired column width.
const fs = require("fs");
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, "book1.xls");
// Creating a file stream containing the Excel file to be opened
const fstream = fs.readFileSync(filePath);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
const workbook = new AsposeCells.Workbook(fstream);
// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);
// Setting the width of the second column to 17.5
worksheet.getCells().setColumnWidth(1, 17.5);
// Saving the modified Excel file
workbook.save(path.join(dataDir, "output.out.xls"));
// Closing the file stream to free all resources
fstream; // Note: No explicit close needed for fs.readFileSync
How to Set Column Width in Pixels
Set the width of a column by calling the Cells collection’s setColumnWidthPixel(number, number) method. The setColumnWidthPixel(number, number) method takes the following parameters:
- Column index, the index of the column that you’re changing the width of.
- Column width, the desired column width in pixels.
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// Source directory
const sourceDir = path.join(__dirname, "data");
const outDir = path.join(__dirname, "output");
// Load source Excel file
const workbook = new AsposeCells.Workbook(path.join(sourceDir, "Book1.xlsx"));
// Access first worksheet
const worksheet = workbook.getWorksheets().get(0);
// Set the width of the column in pixels
worksheet.getCells().setColumnWidthPixel(7, 200);
workbook.save(path.join(outDir, "SetColumnWidthInPixels_Out.xlsx"));
How to Set the Width of All Columns in a Worksheet
To set the same column width for all columns in the worksheet, use the Cells collection’s getStandardWidth() property.
const path = require("path");
const fs = require("fs");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Creating a file stream containing the Excel file to be opened
const filePath = path.join(dataDir, "book1.xls");
// Instantiating a Workbook object
// Opening the Excel file
const workbook = new AsposeCells.Workbook(filePath);
// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);
// Setting the width of all columns in the worksheet to 20.5
worksheet.getCells().setStandardWidth(20.5);
// Saving the modified Excel file
workbook.save(path.join(dataDir, "output.out.xls"));
// Closing the file stream to free all resources
// No explicit close needed in Node.js
Advance topics
- AutoFit Rows and Columns
- Convert Text to Columns using Aspose.Cells
- Copying Rows and Columns
- Delete Blank Rows and Columns in a Worksheet
- Grouping and Ungrouping Rows and Columns
- Hiding and Showing Rows and Columns
- Insert or Delete Rows in an Excel Worksheet
- Inserting and Deleting Rows and Columns of Excel file
- Remove duplicate rows in a Worksheet
- Update references in other worksheets while deleting blank columns and rows in a worksheet