AutoFit Rows and Columns with Node.js via C++
Auto Fitting
Aspose.Cells provides a Workbook class that represents a Microsoft Excel file. The Workbook class contains a Workbook.getWorksheets() collection that allows access to each worksheet in an Excel file. A worksheet is represented by the Worksheet class. The Worksheet class provides a wide range of properties and methods for managing a worksheet. This article looks at using the Worksheet class to autofit rows or columns.
AutoFit Row - Simple
The most straightforward approach to auto-sizing the width and height of a row is to call the Worksheet class autoFitRow method. The autoFitRow method takes a row index (of the row to be resized) as a parameter.
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const inputPath = path.join(dataDir, "Book1.xlsx");
// Reading the Excel file into a buffer
const fs = require("fs");
const fileBuffer = fs.readFileSync(inputPath);
// Opening the Excel file through the buffer
const workbook = new AsposeCells.Workbook(fileBuffer);
// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);
// Auto-fitting the 3rd row of the worksheet
worksheet.autoFitRow(1);
// Saving the modified Excel file
const outputPath = path.join(dataDir, "output.xlsx");
workbook.save(outputPath);
How to AutoFit Row in a Range of Cells
A row is composed of many columns. Aspose.Cells allows developers to auto-fit a row based on the content in a range of cells within the row by calling an overloaded version of the autoFitRow method. It takes the following parameters:
- Row index, the index of the row about to be auto-fitted.
- First column index, the index of the row’s first column.
- Last column index, the index of the row’s last column.
The autoFitRow method checks the contents of all the columns in the row and then auto-fits the row.
const AsposeCells = require("aspose.cells.node");
const path = require("path");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const inputPath = path.join(dataDir, "Book1.xlsx");
// Reading the Excel file into a buffer
const fs = require("fs");
const fileData = fs.readFileSync(inputPath);
// Opening the Excel file through the buffer
const workbook = new AsposeCells.Workbook(fileData);
// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);
// Auto-fitting the 3rd row of the worksheet
worksheet.autoFitRow(1, 0, 5);
// Saving the modified Excel file
workbook.save(path.join(dataDir, "output.xlsx"));
How to AutoFit Column in a Range of Cells
A column is composed of many rows. It is possible to auto-fit a column based on the content in a range of cells in the column by calling an overloaded version of autoFitColumn method that takes the following parameters:
- Column index, the index of the column about to be auto-fitted.
- First row index, the index of the column’s first row.
- Last row index, the index of the column’s last row.
The autoFitColumn method checks the contents of all rows in the column and then auto-fits the column.
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const inputPath = path.join(dataDir, "Book1.xlsx");
// Creating a file stream containing the Excel file to be opened
const fs = require("fs");
const workbook = new AsposeCells.Workbook(fs.readFileSync(inputPath));
// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);
// Auto-fitting the Column of the worksheet
worksheet.autoFitColumn(4);
// Saving the modified Excel file
workbook.save(path.join(dataDir, "output.xlsx"));
How to AutoFit Rows for Merged Cells
With Aspose.Cells, it is possible to autofit rows even for cells that have been merged using the AutoFitterOptions API. The AutoFitterOptions class provides AutoFitterOptions.getAutoFitMergedCellsType() property that can be used to autofit rows for merged cells. AutoFitterOptions.getAutoFitMergedCellsType() accepts AutoFitMergedCellsType enumerable which has the following members.
- None: Ignore merged cells.
- FirstLine: Only expands the height of the first row.
- LastLine: Only expands the height of the last row.
- EachLine: Only expands the height of each row.
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const outputDir = path.join(dataDir, "output");
// Instantiate a new Workbook
const wb = new AsposeCells.Workbook();
// Get the first (default) worksheet
const worksheet = wb.getWorksheets().get(0);
// Create a range A1:B1
const range = worksheet.getCells().createRange(0, 0, 1, 2);
// Merge the cells
range.merge();
// Insert value to the merged cell A1
worksheet.getCells().get(0, 0).setValue("A quick brown fox jumps over the lazy dog. A quick brown fox jumps over the lazy dog....end");
// Create a style object
const style = worksheet.getCells().get(0, 0).getStyle();
// Set wrapping text on
style.setIsTextWrapped(true);
// Apply the style to the cell
worksheet.getCells().get(0, 0).setStyle(style);
// Create an object for AutoFitterOptions
const options = new AsposeCells.AutoFitterOptions();
// Set auto-fit for merged cells
options.setAutoFitMergedCellsType(AsposeCells.AutoFitMergedCellsType.EachLine);
// Autofit rows in the sheet (including the merged cells)
worksheet.autoFitRows(options);
// Save the Excel file
wb.save(path.join(outputDir, "AutofitRowsforMergedCells.xlsx"));
You may also try to use the overloaded versions of autoFitRows & autoFitColumns methods accepting a range of rows/columns and an instance of AutoFitterOptions to auto-fit the selected rows/columns with your desired AutoFitterOptions accordingly.
The signatures of the aforesaid methods are as follows:
- autoFitRows(int startRow, int endRow, AutoFitterOptions options)
- autoFitColumns(int firstColumn, int lastColumn, AutoFitterOptions options)