Get Max Column Index in Row and Max Row Index in Column
Possible Usage Scenarios
When you only need to manipulate some data on the rows or columns, you need to know the data range of rows and columns. Aspose.Cells for Node.js via C++ offers this feature. To obtain the maximum column index on a row, you can obtain the Row.getLastCell() and Row.getLastDataCell() methods, and then use the Cell.getColumn() method to obtain the maximum column index and maximum data column index. But in order to obtain the maximum row index and maximum row data index on a column, you need to create a range on the column, then traverse the range to find the last cell, and finally call the Cell.getRow() method on the cell.
Aspose.Cells for Node.js via C++ provides the following properties and methods to help you achieve your goals.
Get Max Column Index in Row and Max Row Index in Column
This example shows how to:
- Load the sample file.
- Get the row that needs to get the maximum column index and maximum data column index.
- Call the Cell.getColumn() method on the cell.
- Create a range based on column.
- Get iterator and traverse range.
- Call the Cell.getRow() method on the cell.
const AsposeCells = require("aspose.cells.node"); | |
const path = require("path"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
const workbook = new AsposeCells.Workbook(dataDir +"sample.xlsx"); | |
const sheet = workbook.getWorksheets().get(0); | |
const cells = sheet.getCells(); | |
const row = cells.checkRow(1); | |
if (row != null) { | |
//get Maximum column index of Row which contains data or style. | |
console.log("Max column index in row: " + row.getLastCell().getColumn()); | |
//get Maximum column index of Row which contains data. | |
console.log("Max data column index in row: " + row.getLastDataCell().getColumn()); | |
} | |
// create the range of column B | |
const columnRange = cells.createRange(1, 1, true); | |
var max_row_index = cells.getMaxRow() + 1; | |
var maxRow = 0; | |
var maxDataRow = 0; | |
for (let row_index = 0; row_index < max_row_index; row_index++) | |
{ | |
var curr_cell = cells.get(row_index, 1); | |
if (curr_cell) | |
{ | |
if (curr_cell.getStringValue()) | |
{ | |
maxDataRow = curr_cell.getRow(); | |
} | |
if (curr_cell.getStringValue() || curr_cell.getHasCustomStyle()) | |
{ | |
maxRow = curr_cell.getRow(); | |
} | |
} | |
} | |
//Maximum row index of Column which contains data or style. | |
console.log("Max row index in Column: " + maxRow); | |
//Maximum row index of Column which contains data. | |
console.log("Max data row index in Column: " + maxDataRow); |