Spread Windows Forms 6.0 Product Documentation
Placing a Formula in Cells
Support Options
Spread Windows Forms 6.0 Product Documentation > Developer's Guide > Customizing Interaction in Cells > Managing Formulas in Cells > Placing a Formula in Cells

Glossary Item Box

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 ever 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 the Spread Designer Guide. 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 which contains a string constant that contains a new line character.

    TextCellType ct = new TextCellType();

    ct.Multiline = true;

    fpspread1.Sheets[0].Cells[0,0].Formula = "\"line1\nline2\"";

Return to the overview of Managing Formulas in Cells.

Using a Shortcut

This way of adding a formula is done in code and is not seen until run time. For dot notation, you specify the formula as a property of that cell, row, or column.

Add a formula to a cell, row, or column by specifying the Formula property for that cell, row, or column.

Example

This example shows how to specify a formula that finds the product of fifty 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 third 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)"
 

Using Code

This way of adding a formula is done in code and is not seen until run time. This uses the longer form of code instruction

  1. Specify the cell, row, or column.
  2. Add a formula to the cell, row, or column.

Example

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.Cells(2, 0)
 
mycell.Formula = "SUM(A1:A2) * 2"
 

Using the Formula Editor

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 the Spread Designer Guide. For details on the functions and operators that can be used to create a formula, refer to the Formula Reference.

  1. Select the sheet tab of the sheet that contains the cells in which to place formulas.
  2. Select the cell or cells in the sheet.
  3. In the Formula property, click the arrow button. This opens the Formula Editor.
  4. In the Formula Editor, you may type in the formula in the edit box. To assist in entering functions in the formula, you may double-click on a function name to have it appear in the edit box. Functions are organized by category. You may also type operators and constants to construct your formula.
  5. When done, click Apply. Click OK to close the editor.
  6. If you were working from within the Spread Designer, from the File menu choose Apply and Exit to apply your changes to the component and exit Spread Designer.
© 2002-2012 ComponentOne, a division of GrapeCity. All Rights Reserved.