Calculating IFNA Function with Python.NET using Aspose.Cells
Contents
[
Hide
]
Aspose.Cells for Python.NET supports calculating the IFNA Excel function. The IFNA function returns a specified value if a formula results in #N/A error; otherwise returns the formula’s result.
Calculating IFNA Function in Python.NET
The following code sample demonstrates how to calculate the IFNA function using Aspose.Cells for Python.NET:
Console Output
The above code will produce the following console output:
Not found
Orange
Key Steps Explanation
- Create a new
Workbook
instance - Access worksheet and cells collection
- Populate source data in column A
- Set VLOOKUP formulas that may produce #N/A errors
- Use IFNA to handle potential errors
- Calculate formulas using
calculate_formula()
- Retrieve and display results from error-handled cells
- Save modified workbook with calculation results
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")
# Create new workbook
workbook = Workbook()
# Access first worksheet
worksheet = workbook.worksheets[0]
# Add data for VLOOKUP
cells = worksheet.cells
cells.get("A1").put_value("Apple")
cells.get("A2").put_value("Orange")
cells.get("A3").put_value("Banana")
# Access cell A5 and A6
cell_a5 = worksheet.cells.get("A5")
cell_a6 = worksheet.cells.get("A6")
# Assign IFNA formula to A5 and A6
cell_a5.formula = "=IFNA(VLOOKUP(\"Pear\",$A$1:$A$3,1,0),\"Not found\")"
cell_a6.formula = "=IFNA(VLOOKUP(\"Orange\",$A$1:$A$3,1,0),\"Not found\")"
# Calculate the formula of workbook
workbook.calculate_formula()
# Print the values of A5 and A6
print(cell_a5.string_value)
print(cell_a6.string_value)