Node.jsを使ってワークシートからシナリオを作成、操作、削除する(C++経由)
Contents
[
Hide
]
時折、スプレッドシートでシナリオを作成、操作、または削除する必要があります。シナリオとは、1つ以上の数式によってリンクされた可変の入力セルを含む名前付きの’仮定’モデルです。シナリオを作成する前に、異なる値が挿入できるセルに依存する1つ以上の数式を含むワークシートの設計を行います。以下の例は、Aspose.CellsのAPIを使用してワークシートからシナリオを作成および削除する方法を示しています。
Aspose.Cellsには、例えば、ScenarioCollection、Scenario、ScenarioInputCellCollection、およびScenarioInputCellのクラスなど、いくつかの便利なクラスが提供されています。また、Worksheet.getScenarios()プロパティも提供されています。以下のサンプルコードは、いくつかのシナリオを含むXLSX形式のExcelファイルを開き、既存のシナリオを削除し、Excelファイルを保存する前にワークシートに新しいシナリオを追加します。この例では、シナリオを含む非常にシンプルなテンプレートファイルが使用されています。
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Instantiate the Workbook
// Load an Excel file
const workbook = new AsposeCells.Workbook(path.join(dataDir, "aspose-sample.xlsx"));
// Access first worksheet
const worksheet = workbook.getWorksheets().get(0);
if (worksheet.getScenarios().getCount() > 0) {
// Remove the existing first scenario from the sheet
worksheet.getScenarios().removeAt(0);
// Create a scenario
const i = worksheet.getScenarios().add("MyScenario");
// Get the scenario
const scenario = worksheet.getScenarios().get(i);
// Add comment to it
scenario.setComment("Test scenario is created.");
// Get the input cells for the scenario
const sic = scenario.getInputCells();
// Add the scenario on B4 (as changing cell) with default value
sic.add(3, 1, "1100000");
const outputFilePath = path.join(dataDir, "outBk_scenarios1.out.xlsx");
// Save the Excel file.
workbook.save(outputFilePath);
console.log("\nProcess completed successfully.\nFile saved at " + outputFilePath);
}