ワークシートからシナリオを作成、操作、または削除する
Contents
[
Hide
]
時々、スプレッドシートでシナリオを作成、操作、または削除する必要があります。 シナリオは、1つまたは複数の数式によってリンクされた変数入力セルを含む名前付きのwhat-ifモデルです。 シナリオを作成する前に、異なる値を挿入できるセルに依存する少なくとも1つの数式を含むワークシートを設計します。 以下の例では、Aspose.Cells APIを使用してワークシートからシナリオを作成および削除する方法を示します。
Aspose.Cellsは、 ScenarioCollection、Scenario、ScenarioInputCellCollection、およびScenarioInputCellなどの便利なクラスを提供します。 また、Worksheet.Scenariosプロパティも提供しています。 以下のサンプルコードでは、(いくつかのシナリオを含む)XLSX Excelファイルを開いて、既存のシナリオをワークシートから削除し、Excelファイルを保存する前に新しいシナリオを追加します。 これは、シナリオを含む非常にシンプルなテンプレートファイルを使用します。
コードを実行すると、既存のシナリオが削除され、新しいシナリオがワークシートに追加されます。
出力ファイル
This file contains 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
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(CreateScenariosfromWorksheets.class); | |
// Instantiate the Workbook | |
// Load an Excel file | |
Workbook workbook = new Workbook(dataDir + "Bk_scenarios.xlsx"); | |
// Access first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Remove the existing first scenario from the sheet | |
worksheet.getScenarios().removeAt(0); | |
// Create a scenario | |
int i = worksheet.getScenarios().add("MyScenario"); | |
// Get the scenario | |
Scenario scenario = worksheet.getScenarios().get(i); | |
// Add comment to it | |
scenario.setComment("Test sceanrio is created."); | |
// Get the input cells for the scenario | |
ScenarioInputCellCollection sic = scenario.getInputCells(); | |
// Add the scenario on B4 (as changing cell) with default value | |
sic.add(3, 1, "1100000"); | |
// Save the Excel file. | |
workbook.save(dataDir + "outBk_scenarios1.xlsx"); |