Python via .NETを使用してワークシートからシナリオを作成、操作または削除する方法
Contents
[
Hide
]
時には、スプレッドシートでシナリオを作成、操作、削除する必要があります。シナリオは、名前付きの「もしも?」モデルで、変数入力セルとそれにリンクされた1つ以上の数式を含みます。シナリオを作成する前に、少なくとも1つの数式を含み、さまざまな値を受け入れるセルに依存するワークシートを設計してください。この例は、Aspose.Cells for Python via .NETを使用してワークシートのシナリオを管理する方法を示します。
Aspose.Cellsは、シナリオを操作するためのいくつかのクラスを提供します:
シナリオにアクセスするにはWorksheet.scenariosプロパティを使用します。以下のコードは次のことを示します:
- シナリオが含まれるExcelファイルを開く
- 既存のシナリオを削除する 新しいシナリオを追加
変更されたワークブックを保存します
import os
from aspose.cells import Workbook
# For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
current_dir = os.path.dirname(os.path.abspath(__file__))
data_dir = os.path.join(current_dir, "data")
# Instantiate the Workbook and load an Excel file
workbook = Workbook(os.path.join(data_dir, "aspose-sample.xlsx"))
# Access first worksheet
worksheet = workbook.worksheets[0]
if len(worksheet.scenarios) > 0:
# Remove the existing first scenario from the sheet
worksheet.scenarios.remove_at(0)
# Create a scenario
i = worksheet.scenarios.add("MyScenario")
# Get the scenario
scenario = worksheet.scenarios[i]
# Add comment to it
scenario.comment = "Test sceanrio is created."
# Get the input cells for the scenario
sic = scenario.input_cells
# Add the scenario on B4 (as changing cell) with default value
sic.add(3, 1, "1100000")
output_path = os.path.join(data_dir, "outBk_scenarios1.out.xlsx")
# Save the Excel file
workbook.save(output_path)
print(f"\nProcess completed successfully.\nFile saved at {output_path}")