获取行中最大列索引和列中最大行索引

可能的使用场景

当你只需要操作某些行或列中的数据时,需要知道行和列的数据范围。Aspose.Cells for Node.js via C++提供了此功能。要获得某行的最大列索引,可以调用Row.getLastCell()Row.getLastDataCell()方法,然后使用Cell.getColumn()方法获取最大列索引和最大数据列索引。但为了获取某列的最大行索引和最大行数据索引,你需要在该列创建一个范围,然后遍历该范围找到最后一个单元格,最后调用该单元格的Cell.getRow()方法。

Aspose.Cells for Node.js via C++提供以下属性和方法,帮助你实现目标。

获取行中的最大列索引和列中的最大行索引

此示例演示如何:

  1. 加载示例文件
  2. 获取需要获取最大列索引和最大数据列索引的行。
  3. 调用单元格的Cell.getColumn()方法。
  4. 根据列创建一个范围。
  5. 获取迭代器并遍历范围。
  6. 调用单元格的Cell.getRow()方法。
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);