Spill a dynamic array
Use this when a formula such as SORT, FILTER or SEQUENCE returns more than one value, or when a workbook carries legacy Ctrl+Shift+Enter array formulas.
Write a formula that spills
Write the formula into one cell, the anchor. The result fills the cells below and to the right of it, and every one of them reads back through getValue and getValues. Only the anchor holds a formula: getFormula on another cell of the spill returns null.
import createFormulon from '@libraz/formulon'
const formulon = await createFormulon()
const wb = formulon.Workbook.createDefault()
try {
const ws = wb.sheet(0)
ws.setValues('A1', [[30], [10], [20]])
ws.setFormula('C1', '=SORT(A1:A3)')
console.log(JSON.stringify(ws.getValues('C1:C3'))) // [[10],[20],[30]]
console.log(ws.getFormula('C1')) // =SORT(A1:A3)
console.log(ws.getFormula('C2')) // null
} finally {
wb.dispose()
}from formulon import Workbook
with Workbook.create_default() as wb:
ws = wb[0]
ws["A1:A3"] = [[30], [10], [20]]
ws.set_formula("C1", "=SORT(A1:A3)")
print(ws["C1:C3"]) # [[10.0], [20.0], [30.0]]
print(ws.get_formula("C1")) # =SORT(A1:A3)
print(ws.get_formula("C2")) # NoneAnother formula refers to the whole result with the spill operator, C1#, and follows it when the result changes size.
Find the spill range
getSpill(cell) (Python get_spill) returns a record for any cell of a spill, the anchor included: anchor is the formula cell, range is the rectangle the result covers, and blocked is false. It returns null for a cell outside every spill, unless the cell is a PivotTable report's anchor, which reads the record pivot.getSpill() returns.
import createFormulon from '@libraz/formulon'
const formulon = await createFormulon()
const wb = formulon.Workbook.createDefault()
try {
const ws = wb.sheet(0)
ws.setValues('A1', [[30], [10], [20]])
ws.setFormula('C1', '=SORT(A1:A3)')
ws.setFormula('E1', '=SUM(C1#)')
const spill = ws.getSpill('C2')
console.log(spill.blocked) // false
console.log(JSON.stringify(spill.range)) // {"firstRow":0,"firstCol":2,"lastRow":2,"lastCol":2}
console.log(ws.getSpill('D1')) // null
console.log(ws.getValue('E1')) // 60
} finally {
wb.dispose()
}from formulon import Workbook
with Workbook.create_default() as wb:
ws = wb[0]
ws["A1:A3"] = [[30], [10], [20]]
ws.set_formula("C1", "=SORT(A1:A3)")
ws.set_formula("E1", "=SUM(C1#)")
spill = ws.get_spill("C2")
print(spill.blocked) # False
print(spill.range) # CellRect(first_row=0, first_col=2, last_row=2, last_col=2)
print(ws.get_spill("D1")) # None
print(ws["E1"]) # 60.0Handle a #SPILL! collision
When a cell inside the spill rectangle holds a value, the anchor returns #SPILL! and none of the result is written. getSpill on the anchor then reports blocked: true with the rectangle the result would fill, which is where to look for the obstruction. Its cause says what is in the way:
SpillCause.Mergedfor a merge.SpillCause.BeyondEdgefor a rectangle running past the sheet edge, whoserangeis then clipped to the grid.SpillCause.Occupiedfor anything else.
A result larger than one cell is blocked by any merge it touches, including one anchored at the formula cell; a one-cell result at the top-left cell of a merge shows in the merged cell.
Clearing the obstruction lets the formula spill again at the next calculation.
import createFormulon from '@libraz/formulon'
const formulon = await createFormulon()
const wb = formulon.Workbook.createDefault()
try {
const ws = wb.sheet(0)
ws.setValues('A1', [[30], [10], [20]])
ws.setFormula('C1', '=SORT(A1:A3)')
ws.setValue('C3', 'note')
console.log(String(ws.getValue('C1'))) // #SPILL!
console.log(ws.getSpill('C1').blocked) // true
ws.setBlank('C3')
console.log(ws.getValue('C3'), ws.getSpill('C1').blocked) // 30 false
} finally {
wb.dispose()
}from formulon import Workbook
with Workbook.create_default() as wb:
ws = wb[0]
ws["A1:A3"] = [[30], [10], [20]]
ws.set_formula("C1", "=SORT(A1:A3)")
ws["C3"] = "note"
print(ws["C1"]) # #SPILL!
print(ws.get_spill("C1").blocked) # True
ws.set_blank("C3")
print(ws["C3"], ws.get_spill("C1").blocked) # 30.0 FalseA result of more than one cell also returns #SPILL! when its rectangle touches a worksheet table. Dynamic arrays lists the other spill rules.
Work with a legacy array formula
A legacy CSE array formula is one formula entered over a fixed block. setArrayFormula({ range, formula }) (Python set_array_formula(ArrayFormula(...))) enters one, and getArrayFormula(cell) returns the block and its formula for any cell of it. A write to a cell inside the block other than its top-left cell throws StatusCode.PreconditionFailed. A write to the top-left cell replaces the whole block, and that is how a block is cleared.
import createFormulon, { StatusCode } from '@libraz/formulon'
const formulon = await createFormulon()
const wb = formulon.Workbook.createDefault()
try {
const ws = wb.sheet(0)
ws.setValues('A1', [[1], [2], [3]])
ws.setArrayFormula({ range: 'C1:C3', formula: '=A1:A3*10' })
console.log(JSON.stringify(ws.getValues('C1:C3'))) // [[10],[20],[30]]
console.log(ws.getArrayFormula('C2').formula) // =A1:A3*10
try {
ws.setValue('C2', 0)
} catch (e) {
console.log(e.code === StatusCode.PreconditionFailed) // true
}
ws.setBlank('C1')
console.log(ws.getArrayFormula('C2'), JSON.stringify(ws.getValues('C1:C3'))) // null [[null],[null],[null]]
} finally {
wb.dispose()
}from formulon import ArrayFormula, FormulonError, StatusCode, Workbook
with Workbook.create_default() as wb:
ws = wb[0]
ws["A1:A3"] = [[1], [2], [3]]
ws.set_array_formula(ArrayFormula(range="C1:C3", formula="=A1:A3*10"))
print(ws["C1:C3"]) # [[10.0], [20.0], [30.0]]
print(ws.get_array_formula("C2").formula) # =A1:A3*10
try:
ws["C2"] = 0
except FormulonError as e:
print(e.code == StatusCode.PRECONDITION_FAILED) # True
ws.set_blank("C1")
print(ws.get_array_formula("C2"), ws["C1:C3"]) # None [[None], [None], [None]]FORMULATEXT on any cell of a block returns the formula in braces, {=A1:A3*10}. A legacy block keeps its size when its result changes: a smaller result leaves #N/A in the cells it does not reach.
Read next
- Dynamic arrays: spill rules, the array size limit and recalculation.
- Formulas and recalculation: entering formulas and reading their values.
- WASM API and Python API:
getSpilland the array-formula members.