Create Formulas and Use Cell References
- 3 minutes to read
A formula is an expression that calculates a single cell value. A formula can contain constants, operators, cell references, functions, defined names, and table references. The Spreadsheet Document API supports the same formula syntax as Microsoft Excel.
Assign a Formula to a Cell
Assign a formula string to the Cell.Formula property to add a formula to a cell. A formula string starts with the equal sign (=). If you omit the equal sign, the API adds it. Function names and cell references are case-insensitive. The Formula property gets them in uppercase (for example, =sum(a1:a2) becomes =SUM(A1:A2)).
Call the Workbook.Calculate() method to calculate formulas. The Cell.Value property gets the formula result.
The following code snippet assigns values to cells A1 and A2, adds formulas to cells A3 and A4, and displays formula results in the console:
using System;
using DevExpress.Docs.Spreadsheet;
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Cells["A1"].Value = 120;
worksheet.Cells["A2"].Value = 80;
worksheet.Cells["A3"].Formula = "=SUM(A1:A2)*0.9";
worksheet.Cells["A4"].Formula = "=IF(A3>150,\"High\",\"Low\")";
workbook.Calculate();
Console.WriteLine(worksheet.Cells["A3"].Value);
Console.WriteLine(worksheet.Cells["A4"].Value);
The Formula property accepts and gets formulas in the invariant culture. A comma (,) separates function arguments, a period (.) is the decimal separator, and function names are in English (for example, =IF(A1>1.5,"High","Low")). The current culture of the application (CultureInfo.CurrentCulture) does not affect the Formula property. If a formula contains separators of another culture (for example, =IF(A1>1,5;"High";"Low")), the API throws an ArgumentException.
To assign or get a formula in a specific culture, set the Workbook.Culture property and call Workbook.SetCellFormulaLocal(cell, formula) and Workbook.GetCellFormulaLocal(cell) methods.
If you assign a formula with a syntax error (for example, =SUM(A1:A2 without the closing parenthesis), the API throws an ArgumentException, and the cell keeps prior content. Refer to the following help topic for additional information about cell errors: Cell Error Values.
To remove a formula, set the Cell.Formula property to null (Nothing in VB.NET). The API clears the formula and the cell value. If you assign a value to the Cell.Value property, the API removes the cell formula.
Refer to the following help topic for additional information about calculation: Calculate Formulas.
Formula Operators
Formulas support the following operators:
| Operator Type | Operators |
|---|---|
| Arithmetic | +, -, *, /, %, ^ |
| Comparison | =, <>, >, <, >=, <= |
| Text concatenation | & |
| Reference | : (range), , (union), space (intersection) |
Enclose a union in parentheses when you pass it to a function as a single argument (for example, =SUM((B1:B2,C1))).
Use Cell References in Formulas
For more information about cell references you can use in formulas, refer to the following help topic: Work with Cell References.
Use Functions in Formulas
For more information about functions you can use in formulas, refer to the following help topic: Use Spreadsheet Functions.