Définition de la formule pour une plage nommée
Définition de la formule pour une plage nommée
Comme l’application Excel, les API Aspose.Cells pour Python via .NET offrent la possibilité de spécifier une formule pour une plage nommée en utilisant sa propriété Range.refers_to. Il pourrait y avoir de nombreux scénarios d’utilisation pour cette fonctionnalité, dont quelques-uns sont détaillés ci-dessous.
Définir une formule simple pour une plage nommée
Une formule simple pourrait être une référence à une autre cellule dans la même feuille de calcul (ou dans une feuille de calcul différente). L’exemple suivant crée une plage nommée dans une nouvelle feuille et défini sa référence à une autre cellule.
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. | |
dataDir = RunExamples.GetDataDir(".") | |
# Create an instance of Workbook | |
book = Workbook() | |
# Get the WorksheetCollection | |
worksheets = book.worksheets | |
# Add a new Named Range with name "NewNamedRange" | |
index = worksheets.names.add("NewNamedRange") | |
# Access the newly created Named Range | |
name = worksheets.names[index] | |
# Set RefersTo property of the Named Range to a formula. Formula references another cell in the same worksheet | |
name.refers_to = "=Sheet1!$A$3" | |
# Set the formula in the cell A1 to the newly created Named Range | |
worksheets[0].cells.get("A1").formula = "NewNamedRange" | |
# Insert the value in cell A3 which is being referenced in the Named Range | |
worksheets[0].cells.get("A3").put_value("This is the value of A3") | |
# Calculate formulas | |
book.calculate_formula() | |
# Save the result in XLSX format | |
book.save(dataDir + "output_out.xlsx") |
Définir une formule complexe pour une plage nommée
Une formule complexe pourrait être n’importe quoi, comme une plage dynamique ou une formule s’étalant sur plusieurs cellules dans différentes feuilles de calcul. L’exemple suivant crée une plage dynamique en utilisant la fonction INDEX pour obtenir la valeur d’une liste en fonction de son emplacement.
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. | |
dataDir = RunExamples.GetDataDir(".") | |
# Create an instance of Workbook | |
book = Workbook() | |
# Get the WorksheetCollection | |
worksheets = book.worksheets | |
# Add a new Named Range with name "data" | |
index = worksheets.names.add("data") | |
# Access the newly created Named Range from the collection | |
data = worksheets.names[index] | |
# Set RefersTo property of the Named Range to a cell range in same worksheet | |
data.refers_to = "=Sheet1!$A$1:$A$10" | |
# Add another Named Range with name "range" | |
index = worksheets.names.add("range") | |
# Access the newly created Named Range from the collection | |
range = worksheets.names[index] | |
# Set RefersTo property to a formula using the Named Range data | |
range.refers_to = "=INDEX(data,Sheet1!$A$1,1):INDEX(data,Sheet1!$A$1,9)" | |
# Save the workbook | |
book.save(dataDir + "output_out.xlsx") |
Voici un autre exemple qui utilise une plage nommée pour additionner les valeurs de 2 cellules dans différentes feuilles de calcul.
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. | |
dataDir = RunExamples.GetDataDir(".") | |
# Create an instance of Workbook | |
book = Workbook() | |
# Get the WorksheetCollection | |
worksheets = book.worksheets | |
# Insert some data in cell A1 of Sheet1 | |
worksheets.get("Sheet1").cells.get("A1").put_value(10) | |
# Add a new Worksheet and insert a value to cell A1 | |
worksheets[worksheets.add()].cells.get("A1").put_value(10) | |
# Add a new Named Range with name "range" | |
index = worksheets.names.add("range") | |
# Access the newly created Named Range from the collection | |
range = worksheets.names[index] | |
# Set RefersTo property of the Named Range to a SUM function | |
range.refers_to = "=SUM(Sheet1!$A$1,Sheet2!$A$1)" | |
# Insert the Named Range as formula to 3rd worksheet | |
worksheets[worksheets.add()].cells.get("A1").formula = "range" | |
# Calculate formulas | |
book.calculate_formula() | |
# Save the result in XLSX format | |
book.save(dataDir + "output_out.xlsx") |