Skip to main content
You are viewing help content for pre-release software. This document and the features it describes are subject to change.
All docs
V26.2
  • Work with Array Formulas

    • 6 minutes to read

    An array formula performs calculations on one or more sets of values (arrays) and returns a single value or multiple values. Array formula arguments can be cell ranges or array constants (for example, {1,2,3}).

    The Spreadsheet Document API supports two array formula types:

    Type Description API
    Array formula (legacy) You specify the cell range. The range size does not depend on the formula result. Microsoft Excel creates such formulas when you press Ctrl+Shift+Enter. These array formulas are used in Microsoft Excel 2019 and in earlier versions. CellRange.ArrayFormula, Worksheet.ArrayFormulas
    Dynamic array formula You specify a single cell. The formula result spills into adjacent cells.
    Refer to the following help topic for information about dynamic array formulas: Work with Dynamic Array Formulas.
    Cell.DynamicArrayFormula, Worksheet.DynamicArrayFormulas

    Create Array Formulas

    Assign a formula to the CellRange.ArrayFormula property to create an array formula in a cell range. The range must be a single rectangular area. If the formula does not start with an equal sign (=), the API adds this sign.

    An array formula can return the following results:

    • Multiple values. Each cell in the array formula range contains one element of the result array.
    • A single value. The formula processes arrays and aggregates the result with a function (for example, SUM, AVERAGE, or MAX). Assign such a formula to a single cell.

    The following code snippet creates a multi-cell array formula that calculates the cost of each order item, and a single-cell array formula that calculates the total cost:

    using System.IO;
    using DevExpress.Docs.Spreadsheet;
    
    using Workbook workbook = new Workbook();
    Worksheet worksheet = workbook.Worksheets[0];
    
    // Add sample data.
    worksheet.Cells["A1"].Value = "Quantity";
    worksheet.Cells["B1"].Value = "Price";
    worksheet.Cells["C1"].Value = "Cost";
    worksheet.Cells["A2"].Value = 5;
    worksheet.Cells["A3"].Value = 2;
    worksheet.Cells["A4"].Value = 10;
    worksheet.Cells["B2"].Value = 12.5;
    worksheet.Cells["B3"].Value = 40;
    worksheet.Cells["B4"].Value = 3.2;
    
    // Create a multi-cell array formula.
    worksheet["C2:C4"].ArrayFormula = "=A2:A4*B2:B4";
    
    // Create a single-cell array formula.
    worksheet.Cells["B5"].Value = "Total";
    worksheet["C5"].ArrayFormula = "=SUM(A2:A4*B2:B4)";
    

    Note

    If the array formula range is larger than the result array, a result row or column that contains a single value is repeated to fill the range. Other extra cells display the #N/A error, as in Microsoft Excel. For additional information about cell errors, refer to the following help topic: Cell Error Values.

    You can also create an ArrayFormula object and pass it to the ArrayFormulaCollection.Add method. The Worksheet.ArrayFormulas property returns the collection of array formulas in a worksheet.

    The following code snippet adds an array formula that calculates a 20% discount for each order item:

    worksheet.ArrayFormulas.Add(new ArrayFormula(worksheet["D2:D4"], "=C2:C4*0.2"));
    

    The CellRange.Formula property does not create an array formula. The Formula property assigns a separate formula to each cell in the range and adjusts relative references for each cell.

    Obtain Array Formulas

    Use the following members to get array formulas in a worksheet:

    Member Returns
    CellRange.ArrayFormula The array formula for the cell range. If the range is a part of an array formula range (for example, a single cell), the property returns the array formula. If the range is not inside an array formula range, the property returns null.
    Cell.Formula The formula text. Each cell in an array formula range returns the array formula text.
    Cell.GetArrayFormulaRange() The array formula range that contains the cell, or null if the cell is not in an array formula range.
    ArrayFormulaCollection.GetArrayFormula(rowIndex, columnIndex) The ArrayFormula object that contains the specified cell, or null if the cell is not in an array formula range. An ArrayFormula object contains the formula text (ArrayFormula.Formula) and the cell range (ArrayFormula.Range).

    The following code snippet obtains the array formula that contains cell C3 and applies bold text formatting to all array formula ranges in the worksheet:

    ArrayFormula arrayFormula = worksheet.ArrayFormulas.GetArrayFormula(2, 2);
    if (arrayFormula != null)
        Console.WriteLine(arrayFormula.Formula);
    
    foreach (ArrayFormula item in worksheet.ArrayFormulas)
        item.Range.ApplyFont(new FontFormat().WithBold(true));
    

    Modify Array Formulas

    A multi-cell array formula range acts as a single unit. You cannot modify only a part of the range. If you assign a value or formula to a single cell in a multi-cell array formula range or clear part of the range, the API throws an InvalidOperationException. You can apply formatting settings to individual cells in the range.

    To modify an array formula, assign a new formula to the entire array formula range. You can also assign an array formula to a larger range that contains the entire array formula range. The new array formula replaces the previous array formula.

    The following code snippet modifies the array formula that contains cell C3:

    CellRange arrayRange = worksheet.Cells["C3"].GetArrayFormulaRange();
    if (arrayRange != null)
        arrayRange.ArrayFormula = "=A2:A4*B2:B4*1.1";
    

    You can assign a formula or value to a single-cell array formula. The new formula or value replaces the array formula.

    Remove Array Formulas

    Use one of the following options to remove an array formula:

    When you set ArrayFormula to null or call Remove(range), the API removes all array formulas that are entirely inside the specified range and clears their values. If the range does not contain an entire array formula (for example, it includes only part of an array formula range), the API throws an InvalidOperationException.

    The following code snippet removes the array formula that contains cell C3 and then removes all other array formulas:

    CellRange arrayRange = worksheet.Cells["C3"].GetArrayFormulaRange();
    if (arrayRange != null)
        arrayRange.ArrayFormula = null;
    
    worksheet.ArrayFormulas.Clear();
    

    Array Formulas and Structural Changes

    When you insert or delete rows, columns, or cells above or to the left of an array formula, the array formula range moves, and the API adjusts references in the formula. If you delete all rows or columns of an array formula range, the API removes the array formula.

    The API does not recalculate formulas after structural changes. Call the Workbook.Calculate method to update cell values. For additional information, refer to the following help topic: Calculate Formulas.

    Array Formulas in Tables

    A table column can contain a calculated column array formula and a total row array formula. Use the following properties to specify these formulas:

    Refer to the following help topic for additional information: Create and Manage Tables.

    Next Steps