セルのインデックスの取得
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は1ベースです。
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); |