Document Solutions for Excel, .NET Edition | Document Solutions
Features / Formulas / Set Formula to Range
In This Topic
    Set Formula to Range
    In This Topic

    You can set formula to a cell range using the Formula property of the IRange interface.

    Refer to the following example code to add custom names and set formula to a range in a worksheet. For more information on how to add custom names, see Defined Names.

    C#
    Copy Code
    // Add custom name and set formula to range
    worksheet.Names.Add("test1", "=Sheet1!$A$1");
    worksheet.Names.Add("test2", "=Sheet1!test1*2");
    
    worksheet.Range["A1"].Value = 1;
    //C6's value is 1.
    worksheet.Range["C6"].Formula = "=test1";
    //C7's value is 3.
    worksheet.Range["C7"].Formula = "=test1 + test2";
    //C8's value is 6.283185307
    worksheet.Range["C8"].Formula = "=test2*PI()";

    Note: The value calculated by the formula is stored in a cache. Users can verify the cached value by invoking the Dirty method of the IRange interface. This method clears the cached value of the specified range and all the ranges dependent on it, or the entire workbook.

    Reference style

    DsExcel .NET supports the RIC1 reference style to allow users to perform calculations in a much easier and quicker way. To set reference style, you can use the Reference Style property of the IWorkbook interface.

    Refer to the following example code to see how reference style can be set in a workbook.

    C#
    Copy Code
    //set workbook's reference style to R1C1.
    workbook.ReferenceStyle = ReferenceStyle.R1C1;


    Defer the Update of Dirty State for Formula Cells

    The value calculated by a formula is stored in cache first and the cached result is returned upon retrieving the cell value. When a worksheet contains huge amount of data which depends on the result of formulas and the value of a cell is changed, all the formula cells are recalculated and the cached values are stored again which could degrade the performance of worksheet.

    Hence, DsExcel provides DeferUpdateDirtyState property in Workbook class, which when set to true does not update the dirty state of formula cells immediately when the value of a cell is changed.

    Refer to the following example code to defer the update of dirty state for formula cells.

    C#
    Copy Code
    Workbook wb = new Workbook();
    wb.Open("formulas.xlsx");
    //Defer the update of dirty cell state
    wb.DeferUpdateDirtyState = true;
    for (int i = 0; i < 1000; i++)
    {
        wb.Worksheets[0].Range[i, 0].Value = i;
    }
    //Resume the update of dirty cell state
    wb.DeferUpdateDirtyState = false;


    Limitation

    When Workbook.DeferUpdateDirtyState is set to True, DsExcel does not update the dirty state of formula cells immediately. At this point the referred ranges for other features (such as chart etc.) won't be dirty, so their caches would not be updated. If you retrieve the state of such features, they may not be correct at that particular point of time.