Insert, delete and move
Use this when a program changes the layout of a sheet or the order of sheets and the formulas in the workbook have to follow, as they do in Excel.
Insert and delete rows and columns
insertRows(first, last) inserts last - first + 1 rows before row first, and deleteRows, insertColumns and deleteColumns take the same closed span. A span string such as '3:5' or 'C:E' works in place of the two indices. References below or to the right of the edit shift, and a range that spans the edit grows or shrinks.
import createFormulon 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.setFormula('C1', '=SUM(A1:A3)')
ws.setFormula('C2', '=A3*10')
ws.insertRows(1, 2) // two rows before row index 1
console.log(ws.getFormula('C1'), ws.getValue('C1')) // =SUM(A1:A5) 6
console.log(ws.getFormula('C4')) // =A5*10
ws.deleteRows('2:3') // the two blank rows again
console.log(ws.getFormula('C1'), ws.getFormula('C2')) // =SUM(A1:A3) =A3*10
ws.insertColumns('A:A')
console.log(ws.getFormula('D1')) // =SUM(B1:B3)
} finally {
wb.dispose()
}from formulon import Workbook
with Workbook.create_default() as wb:
ws = wb[0]
ws["A1:A3"] = [[1], [2], [3]]
ws.set_formula("C1", "=SUM(A1:A3)")
ws.set_formula("C2", "=A3*10")
ws.insert_rows(1, 2) # two rows before row index 1
print(ws.get_formula("C1"), ws["C1"]) # =SUM(A1:A5) 6.0
print(ws.get_formula("C4")) # =A5*10
ws.delete_rows("2:3") # the two blank rows again
print(ws.get_formula("C1"), ws.get_formula("C2")) # =SUM(A1:A3) =A3*10
ws.insert_columns("A:A")
print(ws.get_formula("D1")) # =SUM(B1:B3)An inserted row takes the format and height of the row above it, and an inserted column the format and width of the column to its left. An edit that cuts through a legacy array block, a data table or a PivotTable throws StatusCode.PreconditionFailed and changes nothing.
See what becomes #REF!
A reference whose cells are deleted cannot move anywhere, so it becomes #REF! instead of pointing at whatever moved into the gap. A range survives as long as part of it does, and shrinks.
import createFormulon 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.setFormula('C1', '=SUM(A1:A3)')
ws.setFormula('C2', '=A3*10')
ws.deleteRows(2, 2)
console.log(ws.getFormula('C1'), ws.getValue('C1')) // =SUM(A1:A2) 3
console.log(ws.getFormula('C2'), String(ws.getValue('C2'))) // =#REF!*10 #REF!
ws.deleteColumns(0, 0)
console.log(ws.getFormula('B1')) // =SUM(#REF!)
} finally {
wb.dispose()
}from formulon import Workbook
with Workbook.create_default() as wb:
ws = wb[0]
ws["A1:A3"] = [[1], [2], [3]]
ws.set_formula("C1", "=SUM(A1:A3)")
ws.set_formula("C2", "=A3*10")
ws.delete_rows(2, 2)
print(ws.get_formula("C1"), ws["C1"]) # =SUM(A1:A2) 3.0
print(ws.get_formula("C2"), ws["C2"]) # =#REF!*10 #REF!
ws.delete_columns(0, 0)
print(ws.get_formula("B1")) # =SUM(#REF!)Delete a row or column that a formula reads and compare the formulas before and after.
Insert and delete rows and columns
Inserting or deleting rows and columns rewrites the formulas that move with them. The lists below are read back out of the workbook either side of the call, so a reference that ended up inside deleted space shows the #REF! the engine actually stored.
Powered by the real Formulon engine (WASM) — it runs entirely in your browser, nothing is uploaded.
Defined names, conditional formats, validations, tables and chart series follow the same rules. A picture, chart, shape, form control or OLE object that moves and sizes with cells is removed when a delete covers all of its rows or all of its columns (Drawing anchors). Sheets, cells and structure lists what else moves with the cells.
Insert and delete cells
insertCells(range, shift) and deleteCells(range, shift) (Python insert_cells, delete_cells) move part of a sheet instead of whole rows or columns. CellShift.Down and Right insert, and Up and Left delete. Only the band the range covers moves: the columns of the range for a vertical shift, its rows for a horizontal one.
import createFormulon, { CellShift } from '@libraz/formulon'
const formulon = await createFormulon()
const wb = formulon.Workbook.createDefault()
try {
const ws = wb.sheet(0)
ws.setValues('A1', [[1, 10], [2, 20], [3, 30]])
ws.setFormula('D1', '=SUM(A1:A3)')
ws.setFormula('D2', '=SUM(B1:B3)')
ws.insertCells('A2', CellShift.Down)
console.log(JSON.stringify(ws.getValues('A1:B4'))) // [[1,10],[null,20],[2,30],[3,null]]
console.log(ws.getFormula('D1'), ws.getFormula('D2')) // =SUM(A1:A4) =SUM(B1:B3)
ws.deleteCells('A1:A2', CellShift.Up)
console.log(JSON.stringify(ws.getValues('A1:A2')), ws.getFormula('D1')) // [[2],[3]] =SUM(A1:A2)
} finally {
wb.dispose()
}from formulon import CellShift, Workbook
with Workbook.create_default() as wb:
ws = wb[0]
ws["A1:B3"] = [[1, 10], [2, 20], [3, 30]]
ws.set_formula("D1", "=SUM(A1:A3)")
ws.set_formula("D2", "=SUM(B1:B3)")
ws.insert_cells("A2", CellShift.DOWN)
print(ws["A1:B4"]) # [[1.0, 10.0], [None, 20.0], [2.0, 30.0], [3.0, None]]
print(ws.get_formula("D1"), ws.get_formula("D2")) # =SUM(A1:A4) =SUM(B1:B3)
ws.delete_cells("A1:A2", CellShift.UP)
print(ws["A1:A2"], ws.get_formula("D1")) # [[2.0], [3.0]] =SUM(A1:A2)A reference follows the move only when it lies inside the band. A reference that crosses the band, or a whole-column or whole-row reference, keeps its address. An Up or Left shift for an insert, or a Down or Right shift for a delete, throws StatusCode.InvalidArgument. Insert and delete cells gives the full rules.
Move, copy, rename and remove sheets
wb.copySheet(source, { insertBefore, newName }) duplicates a sheet and returns its handle; without newName the copy is named Sheet1 (2) style. ws.move(insertBefore) changes a sheet's position, and ws.rename(name) rewrites every formula that names the sheet. A Sheet handle follows its sheet through all of these. Removing a sheet turns references to it into #REF!.
import createFormulon from '@libraz/formulon'
const formulon = await createFormulon()
const wb = formulon.Workbook.createDefault()
try {
const summary = wb.sheet(0)
const data = wb.addSheet('Data')
data.setValue('A1', 7)
summary.setFormula('A1', '=Data!A1*2')
const copy = wb.copySheet('Data', { newName: 'Data 2025' })
data.move(0)
console.log(wb.listSheets().map((s) => s.name).join()) // Data,Sheet1,Data 2025
console.log(data.index, copy.index) // 0 2
data.rename('Inputs')
console.log(summary.getFormula('A1'), summary.getValue('A1')) // =Inputs!A1*2 14
wb.removeSheet('Inputs')
console.log(summary.getFormula('A1')) // =#REF!A1*2
} finally {
wb.dispose()
}from formulon import Workbook
with Workbook.create_default() as wb:
summary = wb[0]
data = wb.add_sheet("Data")
data["A1"] = 7
summary.set_formula("A1", "=Data!A1*2")
copy = wb.copy_sheet("Data", new_name="Data 2025")
data.move(0)
print(",".join(s.name for s in wb.list_sheets())) # Data,Sheet1,Data 2025
print(data.index, copy.index) # 0 2
data.rename("Inputs")
print(summary.get_formula("A1"), summary["A1"]) # =Inputs!A1*2 14.0
wb.remove_sheet("Inputs")
print(summary.get_formula("A1")) # =#REF!A1*2insertBefore is read in the sheet list as it is before the move, and null (Python None) appends. A name that collides with another sheet, case-insensitively, throws StatusCode.AlreadyExists; an empty name, a forbidden character or a name over 31 characters throws StatusCode.InvalidSheetName. Removing the only sheet throws StatusCode.PreconditionFailed.
Read next
- Sheets, cells and structure: the structural rules in full.
- Errors and diagnostics: telling a refused edit from a wrong call.
- WASM API and Python API: the sheet and structure members.