Node.js経由でC++を使ってMicrosoft Excelファイルのワークシートを管理
Aspose.Cellsは、Excelファイルを表すクラスWorkbookを提供します。Workbookクラスには、Excelファイル内の各ワークシートにアクセスできるWorkbook.getWorksheets()コレクションが含まれています。
ワークシートはWorksheetクラスで表されます。Worksheetクラスは、ワークシートの管理のためのさまざまなプロパティとメソッドを提供します。
新しいExcelファイルにワークシートを追加する
プログラムで新しいExcelファイルを作成するには:
- Workbookクラスのオブジェクトを作成します。
- WorksheetCollectionクラスのWorksheetCollection.add(SheetType)メソッドを呼び出します。空のワークシートが自動的に追加されます。新しいワークシートのシート番号を渡してWorkbook.getWorksheets()コレクションから参照できます。
- ワークシートの参照を取得します。
- ワークシートで作業を実行します。
- WorkbookクラスのWorkbook.save(string, SaveFormat)メソッドを呼び出して、新しいワークシートを含むExcelファイルを保存します。
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();
// Adding a new worksheet to the Workbook object
const i = workbook.getWorksheets().getCount();
workbook.getWorksheets().add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
const worksheet = workbook.getWorksheets().get(i);
// Setting the name of the newly added worksheet
worksheet.setName("My Worksheet");
// Saving the Excel file
workbook.save(path.join(dataDir, "output.out.xls"));
デザイナースプレッドシートにワークシートを追加する
デザイナスプレッドシートにワークシートを追加する操作は、新しいワークシートの追加と同じですが、既存のExcelファイルを開く必要があります。Workbookクラスを使用してデザイナスプレッドシートを開くことができます。
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 inputPath = path.join(dataDir, "book1.xlsx");
// Creating a file stream containing the Excel file to be opened
const fstream = fs.readFileSync(inputPath);
// Opening the Excel file through the file stream
const workbook = new AsposeCells.Workbook(fstream);
// Adding a new worksheet to the Workbook object
const i = workbook.getWorksheets().add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
const worksheet = workbook.getWorksheets().get(i);
// Setting the name of the newly added worksheet
worksheet.setName("My Worksheet");
// Saving the Excel file
workbook.save(path.join(dataDir, "output.xlsx"));
シート名を使用してワークシートにアクセスする
名前またはインデックスを指定して任意のワークシートにアクセスできます。
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 inputPath = path.join(dataDir, "book1.xlsx");
// Creating a file stream containing the Excel file to be opened
const fstream = fs.readFileSync(inputPath);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
const workbook = new AsposeCells.Workbook(fstream);
// Accessing a worksheet using its sheet name
const worksheet = workbook.getWorksheets().get("Sheet1");
const cell = worksheet.getCells().get("A1");
console.log(cell.getValue());
シート名を使用してワークシートを削除する
ファイルからワークシートを削除するには、WorksheetCollectionクラスのWorksheetCollection.removeAt(string)メソッドを呼び出します。シート名を渡して特定のワークシートを削除できます。
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 fstream = fs.readFileSync(filePath);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
const workbook = new AsposeCells.Workbook(fstream);
// Removing a worksheet using its sheet name
workbook.getWorksheets().removeAt("Sheet1");
// Save workbook
workbook.save(path.join(dataDir, "output.out.xls"));
Sheet Indexを使用してワークシートを削除する
名前でワークシートを削除するのは、ワークシートの名前がわかっている場合に効果的です。ワークシート名が不明な場合は、シート番号を指定するオーバーロードされたWorksheetCollection.removeAt(string)メソッドを使用してください。
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 fstream = fs.readFileSync(filePath);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
const workbook = new AsposeCells.Workbook(fstream);
// Removing a worksheet using its sheet index
workbook.getWorksheets().removeAt(0);
// Save workbook
workbook.save(path.join(dataDir, "output.out.xls"));
シートのアクティブ化およびワークシート内のアクティブセルを作成します
時には、Microsoft Excel ファイルを開いたときに特定のワークシートをアクティブにして表示させる必要があります。同様に、特定のセルをアクティブにしてスクロールバーをそのセルに合わせることもあります。Aspose.Cellsはこれらすべてのタスクを実行できます。
アクティブなシートとは、作業中のシートのことです。タブ上のアクティブなシートの名前は、デフォルトで太字になります。
アクティブなセルは選択されたセルであり、タイプを始めるとデータが入力されるセルです。1度に1つのセルがアクティブです。アクティブなセルは太い枠で強調表示されます。
シートのアクティブ化とセルをアクティブにする
Aspose.Cellsはシートとセルをアクティブにするための特定のAPI呼び出しを提供します。例えば、WorksheetCollection.getActiveSheetIndex()プロパティはワークブック内のアクティブなシートを設定するのに役立ちます。同様に、Worksheet.getActiveCell()プロパティはワークシート内でアクティブなセルを設定および取得するために使用されます。
特定のデータを表示させるために水平または垂直スクロールバーを目的の行と列のインデックス位置に合わせるには、Worksheet.getFirstVisibleRow()とWorksheet.getFirstVisibleColumn()プロパティを使用します。
次の例は、ワークシートをアクティブ化し、その中のアクティブなセルにします。生成された出力では、スクロールバーは、2行と2列を最初に表示されるようにスクロールします。
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Instantiate a new Workbook.
const workbook = new AsposeCells.Workbook();
// Add a worksheet if collection is empty
const worksheets = workbook.getWorksheets();
if (worksheets.getCount() === 0) {
worksheets.add();
}
// Get the first worksheet in the workbook.
const worksheet1 = worksheets.get(0);
// Get the cells in the worksheet.
const cells = worksheet1.getCells();
// Input data into B2 cell.
cells.get(1, 1).putValue("Hello World!");
// Set the first sheet as an active sheet.
workbook.getWorksheets().setActiveSheetIndex(0);
// Set B2 cell as an active cell in the worksheet.
worksheet1.setActiveCell("B2");
// Set the B column as the first visible column in the worksheet.
worksheet1.setFirstVisibleColumn(1);
// Set the 2nd row as the first visible row in the worksheet.
worksheet1.setFirstVisibleRow(1);
// Save the excel file.
workbook.save(path.join(dataDir, "output.xls"));