获取单元格索引
Contents
[
Hide
]
Excel 的默认视图是 A1 样式引用,每列定义为 A、B、C……,单元格命名为 A1、B2、C3……等等。
您可能想知道该单元格位于哪一行和列。
可能的使用场景
当您只需通过行和列索引操作工作表上的特定数据时,您需要了解该特定单元格的行索引和列索引。 Aspose.Cells for Node.js via C++ 提供此功能,根据行名、列名和单元格名获取对应的行和列索引。 Aspose.Cells for Node.js via C++提供以下属性和方法,帮助你实现目标。
- CellsHelper.cellNameToIndex(string)
- CellsHelper.columnIndexToName
- CellsHelper.columnNameToIndex
- CellsHelper.rowIndexToName
- CellsHelper.rowNameToIndex
注意:在 Aspose.Cells for Node.js via C++ 中索引从零开始,但在 MS Excel 中行的 ID 是从一开始的。
使用 Aspose.Cells for Node.js via C++ 获取单元格索引
此示例演示如何:
- 创建一个工作簿并添加一些数据。
- 在第一个工作表中查找特定单元格。
- 通过单元格名称获取行索引和列索引。
- 通过列名获取列索引。
- 通过行名获取行索引。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const AsposeCells = require("aspose.cells.node"); | |
var workbook = new AsposeCells.Workbook(AsposeCells.FileFormatType.Xlsx); | |
var cells = workbook.getWorksheets().get(0).getCells(); | |
var cell = cells.get("A1"); | |
cell.putValue("Fruit"); | |
cell = cells.get("B1"); | |
cell.putValue("Count"); | |
cell = cells.get("C1"); | |
cell.putValue("Price"); | |
cell = cells.get("A2"); | |
cell.putValue("Apple"); | |
cell = cells.get("A3"); | |
cell.putValue("Mango"); | |
cell = cells.get("A4"); | |
cell.putValue("Blackberry"); | |
cell = cells.get("A5"); | |
cell.putValue("Cherry"); | |
cell = cells.get("B2"); | |
cell.putValue(5); | |
cell = cells.get("B3"); | |
cell.putValue(3); | |
cell = cells.get("B4"); | |
cell.putValue(6); | |
cell = cells.get("B5"); | |
cell.putValue(4); | |
cell = cells.get("C2"); | |
cell.putValue(5); | |
cell = cells.get("C3"); | |
cell.putValue(20); | |
cell = cells.get("C4"); | |
cell.putValue(30); | |
cell = cells.get("C5"); | |
cell.putValue(60); | |
var curr = cells.find("Blackberry", null); | |
//var curr = cells.get("A4"); | |
console.log("Current Cell Name: " + curr.getName()); | |
//get row and column index of current cell | |
var rowCol = AsposeCells.CellsHelper.cellNameToIndex(curr.getName()); | |
var currRow = rowCol[0]; | |
var currCol = rowCol[1]; | |
console.log("Row Index: " + currRow + " Column Index: " + currCol); | |
//get column name by column index | |
var columnName = AsposeCells.CellsHelper.columnIndexToName(currCol); | |
//get row name by row index | |
var rowName = AsposeCells.CellsHelper.rowIndexToName(currRow); | |
console.log("Column Name: " + columnName + " Row Name: " + rowName); | |
//get column index by column name | |
var columnIndex = AsposeCells.CellsHelper.columnNameToIndex(columnName); | |
//get row index by row name | |
var rowIndex = AsposeCells.CellsHelper.rowNameToIndex(rowName); | |
console.log("Column Index: " + columnIndex + " Row Index: " + rowIndex); | |
console.log(columnIndex == currCol); | |
console.log(rowIndex == currRow); |