Work with Dynamic Array Formulas
- 6 minutes to read
A dynamic array formula is a formula in a single cell that returns an array of values. The formula cell is the anchor cell. The formula result spills into adjacent empty cells below and to the right of the anchor cell. These cells define the spill range. The spill range size depends on the result. When source data changes, the spill range grows or shrinks after calculation.
Dynamic array formulas can contain functions that return arrays (for example, SORT, SORTBY, FILTER, UNIQUE, or TRANSPOSE) and expressions that process cell ranges (for example, =A2:A7*C2:C7).
Refer to the following help topic for information about array formulas that occupy a fixed cell range: Work with Array Formulas.
Create Dynamic Array Formulas
Assign a formula to the Cell.DynamicArrayFormula property to create a dynamic array formula. If the formula does not start with an equal sign (=), the API adds this sign. The API does not calculate the formula when you assign it. Call the Workbook.Calculate() method to calculate formulas and fill spill ranges.
The following code snippet creates dynamic array formulas that list unique product categories and products with a price greater than 18 in descending price order and calculates the formulas:
using System.IO;
using DevExpress.Docs.Spreadsheet;
using var workbook = new Workbook();
var worksheet = workbook.Worksheets[0];
// Add sample data.
var products = new[] { "Chai", "Tofu", "Konbu", "Ikura", "Chang", "Pavlova" };
var categories = new[] { "Beverages", "Produce", "Seafood", "Seafood", "Beverages", "Confections" };
var prices = new[] { 18, 23.25, 6, 31, 19, 17.45 };
worksheet.Cells["A1"].Value = "Product";
worksheet.Cells["B1"].Value = "Category";
worksheet.Cells["C1"].Value = "Price";
for (var i = 0; i < products.Length; i++) {
worksheet.Cells[i + 1, 0].Value = products[i];
worksheet.Cells[i + 1, 1].Value = categories[i];
worksheet.Cells[i + 1, 2].Value = prices[i];
}
// Spill unique categories into cells starting from E2.
worksheet.Cells["E2"].DynamicArrayFormula = "=UNIQUE(B2:B7)";
// Spill products with a price greater than 18 into cells starting from G2.
worksheet.Cells["G2"].DynamicArrayFormula = "=SORT(FILTER(A2:C7,C2:C7>18),3,-1)";
// Calculate formulas to fill spill ranges.
workbook.Calculate();
You can also create a DynamicArrayFormula object and pass it to the DynamicArrayFormulaCollection.Add(formula) method.
The Worksheet.DynamicArrayFormulas property gets the collection of dynamic array formulas in a worksheet. The Add method creates the formula in the top-left cell of the specified range. The formula result determines the spill range size. The following code snippet adds a dynamic array formula that lists products in a row:
worksheet.DynamicArrayFormulas.Add(new DynamicArrayFormula(worksheet["A10"], "=TRANSPOSE(A2:A7)"));
A dynamic array formula cannot spill if cells in the required spill range contain data, are merged, or are in a table. In this instance, the formula returns an error, and the Cell.GetDynamicArrayFormulaRange method returns the anchor cell only.
If you anchor a dynamic array formula in the spill range of another dynamic array formula, the new formula blocks the existing spill range. After calculation, the existing formula returns the #VALUE! error, and the Cell.GetDynamicArrayFormulaRange method returns only the anchor cell of the existing formula. The new formula spills if cells in the new spill range are empty.
The Cell.Formula property does not create a dynamic array formula. If a formula that you assign to the Cell.Formula property returns an array, the cell contains a single value, and the result does not spill.
Obtain Dynamic Array Formulas
Use the following members to get dynamic array formulas in a worksheet:
| Member | Returns |
|---|---|
| DynamicArrayFormula | The formula text of the dynamic array formula that occupies the cell (the anchor cell or a spilled cell). If the cell is not in a spill range, the property gets null. |
| Cell.Formula | The formula text for the anchor cell. Spilled cells contain values only and return null. |
| Cell.GetDynamicArrayFormulaRange() | The cell range that the dynamic array formula occupies (the anchor cell and spilled cells), or null if the cell is not in a spill range. |
| DynamicArrayFormulaCollection.GetDynamicArrayFormula(rowIndex, columnIndex) | The DynamicArrayFormula object that occupies the specified cell, or null if the cell is not in a spill range. |
A DynamicArrayFormula object contains the formula text (DynamicArrayFormula.Formula) and the cell range that the formula occupies (DynamicArrayFormula.Range). Use the Cell.Value property to read spilled values.
The following code snippet gets the dynamic array formula that occupies cell E3, displays the formula text and values in the spill range, and applies bold text formatting to all spill ranges in the worksheet:
DynamicArrayFormula dynamicArrayFormula = worksheet.DynamicArrayFormulas.GetDynamicArrayFormula(2, 4);
if (dynamicArrayFormula != null) {
Console.WriteLine(dynamicArrayFormula.Formula);
foreach (Cell cell in dynamicArrayFormula.Range)
Console.WriteLine(cell.Value);
}
foreach (DynamicArrayFormula item in worksheet.DynamicArrayFormulas)
item.Range.ApplyFont(new FontFormat().WithBold(true));
Remove Dynamic Array Formulas
Use one of the following options to remove a dynamic array formula:
- Set the
Cell.DynamicArrayFormulaproperty of the anchor cell or a spilled cell tonull. - Call the DynamicArrayFormulaCollection.Remove(range) or DynamicArrayFormulaCollection.Remove(formula) method.
- Call the DynamicArrayFormulaCollection.RemoveAt(index) method to remove a dynamic array formula at the specified index.
- Call the DynamicArrayFormulaCollection.Clear() method to remove all dynamic array formulas from the worksheet.
When you remove a dynamic array formula, the API clears the anchor cell and spilled values.
The following code snippet removes the dynamic array formula that occupies cell E3 and then removes all other dynamic array formulas:
Dynamic Array Formulas and Structural Changes
When you insert or delete rows, columns, or cells above or to the left of a dynamic array formula, the anchor cell and spill range move, and the API adjusts references in the formula.
If you insert or delete rows or columns inside a range that the formula references, the API adjusts the reference (for example, =A1:A3*2 becomes =A1:A4*2 after you insert a row above row 2). The API does not recalculate formulas after structural changes. Call the Workbook.Calculate() method to update spilled values and the spill range.
If you delete the row or column that contains the anchor cell, the API removes the dynamic array formula and spilled values.