Hiding and Showing Rows and Columns with Node.js via C++
Controlling the Visibility of Rows and Columns
Aspose.Cells for Node.js via C++ provides a class, Workbook, that represents a Microsoft Excel file. The Workbook class contains a WorksheetCollection that allows developers to access 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 for managing rows or columns in a worksheet. A few of these are discussed below.
Hiding Rows and Columns
Developers can hide a row or column by calling the hideRow(number) and hideColumn(number) methods of the Cells collection respectively. Both methods take the row and column index as a parameter to hide the specific row or column.
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");
// Reading the Excel file into a buffer
const fileBuffer = fs.readFileSync(filePath);
// Instantiating a Workbook object with Uint8Array
const workbook = new AsposeCells.Workbook(new Uint8Array(fileBuffer));
// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);
// Hiding the 3rd row of the worksheet
worksheet.getCells().hideRow(2);
// Hiding the 2nd column of the worksheet
worksheet.getCells().hideColumn(1);
// Saving the modified Excel file
workbook.save(path.join(dataDir, "output.out.xls"));
Showing Rows and Columns
Developers can show any hidden row or column by calling the unhideRow(number, number) and unhideColumn(number, number) methods of the Cells collection respectively. Both methods take two parameters:
- Row or column index - the index of a row or column that is used to show the specific row or column.
- Row height or column width - the row height or column width assigned to the row or column after unhiding.
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");
// Read the Excel file into a Buffer (Uint8Array)
const fileBuffer = fs.readFileSync(filePath);
// Instantiating a Workbook object with file buffer
const workbook = new AsposeCells.Workbook(fileBuffer);
// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);
// Unhiding the 3rd row and setting its height to 13.5
worksheet.getCells().unhideRow(2, 13.5);
// Unhiding the 2nd column and setting its width to 8.5
worksheet.getCells().unhideColumn(1, 8.5);
// Saving the modified Excel file
workbook.save(path.join(dataDir, "output.xls"));
Hiding Multiple Rows and Columns
Developers can hide multiple rows or columns at once by calling the hideRows(number, number) and hideColumns(number, number) methods of the Cells collection respectively. Both methods take the starting row or column index and the number of rows or columns that should be hidden as parameters.
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 fileStream = fs.readFileSync(filePath);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
const workbook = new AsposeCells.Workbook(fileStream);
// Accessing the first worksheet in the Excel file
const worksheet = workbook.getWorksheets().get(0);
// Hiding 3, 4, and 5 rows in the worksheet
worksheet.getCells().hideRows(2, 3);
// Hiding 2 and 3 columns in the worksheet
worksheet.getCells().hideColumns(1, 2);
// Saving the modified Excel file
workbook.save(path.join(dataDir, "outputxls"));