Szenarien in Arbeitsblättern erstellen, manipulieren oder entfernen mit Python via .NET
Contents
[
Hide
]
Manchmal müssen Sie Szenarien in Tabellenkalkulationen erstellen, manipulieren oder löschen. Ein Szenario ist ein benanntes „Was-wäre-wenn“-Modell, das variable Eingabezellen umfasst, die durch eine oder mehrere Formeln verbunden sind. Bevor Sie ein Szenario erstellen, gestalten Sie das Arbeitsblatt so, dass es mindestens eine Formel enthält, die von Zellen abhängt, die unterschiedliche Werte annehmen können. Dieses Beispiel zeigt, wie Sie Szenarien mit Aspose.Cells für Python via .NET verwalten.
Aspose.Cells stellt mehrere Klassen für die Arbeit mit Szenarien bereit:
Verwenden Sie die Eigenschaft Worksheet.scenarios, um auf Szenarien zuzugreifen. Das folgende Beispiel zeigt, wie man:
-
Eine Excel-Datei mit Szenarien öffnet
-
Ein bestehendes Szenario entfernt
-
Ein neues Szenario hinzufügt
-
Die modifizierte Arbeitsmappe speichert
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}")