You can add a formula to a cell or range of cells. You can also add a formula to all the cells in a row or column. The formula is a string with the expression of the formula, typically containing a combination of functions, operators, and constants.
When assigning a formula to the Row class or Column class, you are assigning a default formula for that row or column. In other words, the formula is used for every cell in the row or column (assuming that the formula is not overridden at the cell level). For a formula in a row or column, Spread uses the first cell in the row or column as the base location. The formula evaluates to a different result for each cell in column A if you use relative addressing. If you want each cell in column A to evaluate to the sum of the values in C2 and D2 (and not the value in the C and D columns for each row) then you would need to use the formula $C$2+$D$2, which uses absolute address. For examples of formulas that use cell references, refer to Specifying a Cell Reference in a Formula.
You can add a formula by specifying the Formula property for the object or by entering it in the Spread Designer. The procedures for using code are given below. For instructions on using Spread Designer to enter a formula, refer to Entering a Formula in Spread Designer. You can allow end users to enter formulas by allowing them to type the equals sign and then the formula; refer to Allowing the User to Enter Formulas.
Be careful of the type of cell in which the data is found, and whether you use the Text or Value property when assigning data that is used in a formula. When you assign cell data using the Text property, the spreadsheet uses the cell type to parse an assigned string into the needed data type. For example, a number cell type parses a string into a double data type. When you assign the cell data using the Value property, the spreadsheet accepts the assigned object as is and no parsing occurs, so if you set it with a string, it remains a string. Some numeric functions (for example, SUM) ignore non-numeric values in a cell range. For example, if the cell range A1:A3 contains the values {1, "2", 3}, then the formula SUM(A1:A3) evaluates to 4 because the SUM function ignores the string "2". Be sure that you set the value correctly for any cells used in the calculation of a formula and that you set them with the correct data type.
A string constant in a formula can contain special characters such as the new line character (that is, '\n'). Make sure that you enclose the string constant in quotes in the text representation of the formula. The following C# code creates a multiple-line text cell and assigns a formula that contains a string constant that contains a new line character.
C# |
Copy Code
|
---|---|
TextCellType ct = new TextCellType(); ct.Multiline = true; fpspread1.Sheets[0].Cells[0,0].Formula = "\"line1\nline2\""; |
Add a formula to a cell, row, or column by specifying the Formula property for that cell, row, or column.
This example shows how to specify a formula that finds the product of five times the value in the first cell, and puts the result in another cell. Then it finds the sum of a range of cells (A1 through A4) and puts the result in every cell of the fourth column.
C# |
Copy Code
|
---|---|
fpSpread1.ActiveSheet.Cells[2, 0].Formula = "PRODUCT(A1,5)"; fpSpread1.ActiveSheet.Columns[3].Formula = "SUM(A1:A4)"; |
VB |
Copy Code
|
---|---|
fpSpread1.ActiveSheet.Cells(2, 0).Formula = "PRODUCT(A1,5)" fpSpread1.ActiveSheet.Columns(3).Formula = "SUM(A1:A4)" |
This example shows how to specify a formula that sums two cells, doubles it, and puts the result in a third cell.
C# |
Copy Code
|
---|---|
FarPoint.Win.Spread.Cell mycell;
mycell = fpSpread1.Cells[2, 0];
mycell.Formula = "SUM(A1:A2) * 2";
|
VB |
Copy Code
|
---|---|
Dim mycell As FarPoint.Win.Spread.Cell mycell = fpSpread1.ActiveSheet.Cells(2, 0) mycell.Formula = "SUM(A1:A2) * 2" |
You can retain the suffix and prefix whitespace characters of the formula expression by using the ReserveFormulaWhiteSpaces enumeration. The whitespace characters can include many characters such as space, enter, new-line etc.
This example shows how to keep the whitespace characters in the expression.
C# |
Copy Code
|
---|---|
fpSpread1.AsWorkbook().WorkbookSet.CalculationEngine.CalcFeatures |= CalcFeatures.ReserveFormulaWhiteSpaces; sheet1.Cells["C1"].Formula2 = "NOW( )"; sheet1.Cells["C2"].Formula2 = "SUM( Sheet1!A1:A2 )"; fpSpread1.AllowUserFormulas = true; |
VB |
Copy Code
|
---|---|
fpSpread1.AsWorkbook().WorkbookSet.CalculationEngine.CalcFeatures = fpSpread1.AsWorkbook().WorkbookSet.CalculationEngine.CalcFeatures Or CalcFeatures.ReserveFormulaWhiteSpaces sheet1.Cells("C1").Formula2 = "NOW( )" sheet1.Cells("C2").Formula2 = "SUM( Sheet1!A1:A2 )" fpSpread1.AllowUserFormulas = True |
At design time, you can enter formulas in cells using either the Formula bar or the Formula Editor, both of which are available from the Spread Designer. The Formula Editor is also available from the Properties Window. For more information, refer to Entering a Formula in Spread Designer. For details on the functions and operators that can be used to create a formula, refer to the Formula Reference.