Manage formulas of Excel files
Introduction
One of the of Microsoft Excel’s compelling features is its ability to process data with formulas and functions. Microsoft Excel provides a set of built-in functions and formulas that helps users to perform complex calculations quickly. Aspose.Cells for Python via .NET also provides a huge set of built-in functions and formulas that help developers compute values easily. Aspose.Cells for Python via .NET also supports add-in functions. Moreover, Aspose.Cells for Python via .NET supports array and R1C1 formulas.
How to Use Formulas and Functions
Aspose.Cells for Python via .NET provides a class, Workbook, that represents a Microsoft Excel file. The Workbook class contains a worksheets collection that allows access to each worksheet in the Excel file. A worksheet is represented by the Worksheet class. The Worksheet class provides a cells collection. Each item in the Cells collection represents an object of the Cell class.
It is possible to apply formulas to cells using properties and methods offered by the Cell class, discussed in more detail below.
- Using built-in functions.
- Using add-in functions.
- Working with array formulas.
- Creating a R1C1 formula.
How to Use Built-in Functions
Built-in functions or formulas are provided as ready-made functions to reduce developers' efforts and time. See a list of built-in functions supported by Aspose.Cells for Python via .NET. The functions are listed in alphabetical order. More functions will be supported in future.
Aspose.Cells for Python via .NET supports most of the formulas or functions offered by Microsoft Excel. Developers can use these formulas through the API or designer spreadsheet. Aspose.Cells for Python via .NET supports a huge set of mathematical, string, Boolean, date/time, statistical, database, lookup and reference formulas.
Use the Cell class' formula property to add a formula to a cell. Complex formulas, for example
= H7*(1+IF(P7 = $L$3,$M$3, (IF(P7=$L$4,$M$4,0))))
, are also supported in Aspose.Cells for Python via .NET. When applying a formula to a cell, always begin the string with an equal sign (=) as you do when creating a formula in Microsoft Excel and use a comma (,) to delimit function parameters.
In the example below, a complex formula is applied to the first cell of a worksheet’s cells collection. The formula uses a built-in IF function provided by Aspose.Cells for Python via .NET.
from aspose.cells import Workbook | |
from os import os, path | |
# 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 directory if it is not already present. | |
IsExists = path.isdir(dataDir) | |
if notIsExists: | |
os.makedirs(dataDir) | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Adding a new worksheet to the Excel object | |
sheetIndex = workbook.worksheets.add() | |
# Obtaining the reference of the newly added worksheet by passing its sheet index | |
worksheet = workbook.worksheets[0] | |
# Adding a value to "A1" cell | |
worksheet.cells.get("A1").put_value(1) | |
# Adding a value to "A2" cell | |
worksheet.cells.get("A2").put_value(2) | |
# Adding a value to "A3" cell | |
worksheet.cells.get("A3").put_value(3) | |
# Adding a SUM formula to "A4" cell | |
worksheet.cells.get("A4").formula = "=SUM(A1:A3)" | |
# Calculating the results of formulas | |
workbook.calculate_formula() | |
# Get the calculated value of the cell | |
value = str(worksheet.cells.get("A4").value) | |
# Saving the Excel file | |
workbook.save(dataDir + "output.xls") |
How to Use Add-in Functions
We can have some user defined formulas that we want to include as an excel add-in. When setting the cell.Formula function built-in functions work fine however there is a need to set the custom functions or formulas using the add-in functions.
Aspose.Cells for Python via .NET provides features to register add in functions using worksheets.register_add_in_function(). Afterwards when we set cell.Formula = anyFunctionFromAddIn, the output Excel file contains the calculated value from the AddIn function.
Following XLAM file shall be downloaded for registering the add in function in the below sample code. Similarly the output file “test_udf.xlsx” can be downloaded to check the output.
from aspose.cells import SaveFormat, Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Create empty workbook | |
workbook = Workbook() | |
# Register macro enabled add-in along with the function name | |
id = workbook.worksheets.register_add_in_function(sourceDir + str(r"TESTUDF.xlam"), "TEST_UDF", False) | |
# Register more functions in the file (if any) | |
workbook.worksheets.register_add_in_function(id, "TEST_UDF1") | |
# Access first worksheet | |
worksheet = workbook.worksheets[0] | |
# Access first cell | |
cell = worksheet.cells.get("A1") | |
# Set formula name present in the add-in | |
cell.formula = "=TEST_UDF()" | |
# Save workbook to output XLSX format. | |
workbook.save(outputDir + str(r"test_udf.xlsx"), SaveFormat.XLSX) |
How to Use Array Formula
Array formulas are formulas that take arrays, instead of individual numbers, as arguments to the functions that make up the formula. When an array formula is displayed, it is surrounded by braces ({}).
Some Microsoft Excel functions return arrays of values. To calculate multiple results with an array formula, enter the array into a range of cells with the same number of rows and columns as the array arguments.
It is possible to apply an array formula to a cell by calling the Cell class' set_array_formula method. The set_array_formula method takes the following parameters:
- Array Formula, the array formula.
- Number of Rows, the number of rows to populate result of the array formula.
- Number of Columns, the number of columns to populate result of the array formula.
from aspose.cells import Workbook | |
from os import os, path | |
# 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 directory if it is not already present. | |
IsExists = path.isdir(dataDir) | |
if notIsExists: | |
os.makedirs(dataDir) | |
# Instantiating a Workbook object | |
workbook = Workbook() | |
# Adding a new worksheet to the Excel object | |
sheetIndex = workbook.worksheets.add() | |
# Obtaining the reference of the newly added worksheet by passing its sheet index | |
worksheet = workbook.worksheets[sheetIndex] | |
# Adding a value to "A1" cell | |
worksheet.cells.get("A1").put_value(1) | |
# Adding a value to "A2" cell | |
worksheet.cells.get("A2").put_value(2) | |
# Adding a value to "A3" cell | |
worksheet.cells.get("A3").put_value(3) | |
# Adding a value to B1 | |
worksheet.cells.get("B1").put_value(4) | |
# Adding a value to "B2" cell | |
worksheet.cells.get("B2").put_value(5) | |
# Adding a value to "B3" cell | |
worksheet.cells.get("B3").put_value(6) | |
# Adding a value to C1 | |
worksheet.cells.get("C1").put_value(7) | |
# Adding a value to "C2" cell | |
worksheet.cells.get("C2").put_value(8) | |
# Adding a value to "C3" cell | |
worksheet.cells.get("C3").put_value(9) | |
# Adding a SUM formula to "A4" cell | |
worksheet.cells.get("A6").set_array_formula("=LINEST(A1:A3,B1:C3,TRUE,TRUE)", 5, 3) | |
# Calculating the results of formulas | |
workbook.calculate_formula() | |
# Get the calculated value of the cell | |
value = str(worksheet.cells.get("A6").value) | |
# Saving the Excel file | |
workbook.save(dataDir + "output.xls") |
How to Use R1C1 Formula
Add an R1C1 reference style formula to a cell with the Cell class' r1c1_formula property.
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(".") | |
# Instantiating a Workbook object | |
workbook = Workbook(dataDir + "Book1.xls") | |
worksheet = workbook.worksheets[0] | |
# Setting an R1C1 formula on the "A11" cell, | |
# Row and Column indeces are relative to destination index | |
worksheet.cells.get("A11").r1c1_formula = "=SUM(R[-10]C[0]:R[-7]C[0])" | |
# Saving the Excel file | |
workbook.save(dataDir + "output.xls") |
Advance topics
- Precedents and Dependents
- Set External Links in Formulas
- Propagate Formula in Table or List Object automatically while entering data in new rows
- Setting Formula for Named Range
- Setting Formulas - Notice for Non English Users
- Setting Shared Formula
- Specify Maximum Rows of Shared Formula
- Supported Excel Functions