Read and write values
Use this when a program fills cells from its own data or reads results back out of a sheet. Rows and columns are 0-based, and every call that takes a cell also takes an A1 string.
Read a cell as a native value
getValue returns null for a blank cell, a number, a boolean or a string, and an ExcelError for an Excel error such as #DIV/0!. Python reads a cell with ws["B2"] or ws.get_value("B2"), and numbers come back as float. In the default calculation mode a read computes dirty cells first, so no recalc() call sits between a write and a read.
import createFormulon from '@libraz/formulon'
const formulon = await createFormulon()
const wb = formulon.Workbook.createDefault()
try {
const ws = wb.sheet(0)
ws.setValues('A1', [['Item', 'Qty'], ['Widget', 3]])
ws.setFormula('C2', '=B2*2')
ws.setFormula('D2', '=B2/0')
console.log(ws.getValue('B2')) // 3
console.log(ws.getValue('C2')) // 6
console.log(ws.getValue('E9')) // null
console.log(String(ws.getValue('D2'))) // #DIV/0!
} finally {
wb.dispose()
}from formulon import Workbook
with Workbook.create_default() as wb:
ws = wb[0]
ws.set_values("A1", [["Item", "Qty"], ["Widget", 3]])
ws.set_formula("C2", "=B2*2")
ws.set_formula("D2", "=B2/0")
print(ws["B2"]) # 3.0
print(ws["C2"]) # 6.0
print(ws["E9"]) # None
print(ws["D2"]) # #DIV/0!getTypedValue (Python get_typed_value) returns the tagged Value when the kind matters, for example to tell a blank cell from an empty string. API conventions lists the native and tagged forms.
Read and write a block
setValues writes a rectangular array of numbers, strings, booleans and null starting at a cell, and getValues reads a range back as rows. In Python, ws["A1:B3"] reads and writes the same lists of rows.
import createFormulon from '@libraz/formulon'
const formulon = await createFormulon()
const wb = formulon.Workbook.createDefault()
try {
const ws = wb.sheet(0)
ws.setValues('A1', [
['Region', 'Sales'],
['East', 120],
['West', 90],
])
console.log(JSON.stringify(ws.getValues('A1:B3'))) // [["Region","Sales"],["East",120],["West",90]]
} finally {
wb.dispose()
}from formulon import Workbook
with Workbook.create_default() as wb:
ws = wb[0]
ws.set_values("A1", [
["Region", "Sales"],
["East", 120],
["West", 90],
])
print(ws["A1:B3"]) # [['Region', 'Sales'], ['East', 120.0], ['West', 90.0]]A string that starts with = in the array is entered as a formula; see Keep text as text.
Read a whole sheet
getUsedRange() returns the rectangle that holds every cell with a value or a formula, or null for an empty sheet. iterRows(range?) yields one row of native values at a time and covers the used range when called without an argument, so it suits a sheet of unknown size.
import createFormulon from '@libraz/formulon'
const formulon = await createFormulon()
const wb = formulon.Workbook.createDefault()
try {
const ws = wb.sheet(0)
ws.setValues('A1', [['Region', 'Sales'], ['East', 120], ['West', 90]])
const used = ws.getUsedRange()
console.log(`${used.lastRow} ${used.lastCol}`) // 2 1
const rows = [...ws.iterRows()]
console.log(JSON.stringify(rows)) // [["Region","Sales"],["East",120],["West",90]]
} finally {
wb.dispose()
}from formulon import Workbook
with Workbook.create_default() as wb:
ws = wb[0]
ws.set_values("A1", [["Region", "Sales"], ["East", 120], ["West", 90]])
used = ws.get_used_range()
print(used.last_row, used.last_col) # 2 1
rows = list(ws.iter_rows())
print(rows) # [['Region', 'Sales'], ['East', 120.0], ['West', 90.0]]listCells lists only the cells that hold something, and getCellsInRange pages through a large range with a cursor. Sheets, cells and structure compares the four readers.
Hand rows to a data library
Formulon has no data-frame dependency. Produce the rows on the Formulon side and pass them on: the first row of iterRows is the header, and the rest are the records. In Python those two lists are what pandas.DataFrame(body, columns=header) takes.
import createFormulon from '@libraz/formulon'
const formulon = await createFormulon()
const wb = formulon.Workbook.createDefault()
try {
const ws = wb.sheet(0)
ws.setValues('A1', [['Region', 'Sales'], ['East', 120], ['West', 90]])
const [header, ...body] = ws.iterRows()
const records = body.map((row) => Object.fromEntries(header.map((key, i) => [key, row[i]])))
console.log(JSON.stringify(records)) // [{"Region":"East","Sales":120},{"Region":"West","Sales":90}]
} finally {
wb.dispose()
}from formulon import Workbook
with Workbook.create_default() as wb:
ws = wb[0]
ws.set_values("A1", [["Region", "Sales"], ["East", 120], ["West", 90]])
header, *body = ws.iter_rows()
print(header) # ['Region', 'Sales']
print(body) # [['East', 120.0], ['West', 90.0]]Keep text as text
A string that starts with = passed to setValue, setValues or ws[...] = is a formula, the same as setFormula. A string from a user, a CSV file or a database goes through setText (Python set_text), which always stores text and never reads it as a formula. +, - and @ at the start of a string are text on every path.
import createFormulon from '@libraz/formulon'
const formulon = await createFormulon()
const wb = formulon.Workbook.createDefault()
try {
const ws = wb.sheet(0)
ws.setText('A1', '=SUM(1,2)')
ws.setValue('A2', '=SUM(1,2)')
ws.setText('A3', '00123')
console.log(ws.getValue('A1')) // =SUM(1,2)
console.log(ws.getValue('A2')) // 3
console.log(ws.getValue('A3')) // 00123
} finally {
wb.dispose()
}from formulon import Workbook
with Workbook.create_default() as wb:
ws = wb[0]
ws.set_text("A1", "=SUM(1,2)")
ws["A2"] = "=SUM(1,2)"
ws.set_text("A3", "00123")
print(ws["A1"]) # =SUM(1,2)
print(ws["A2"]) # 3.0
print(ws["A3"]) # 00123Read and write dates
A JavaScript Date (its local components) and a Python date, datetime or time are stored as serial numbers in the workbook's date system. A cell in the General format also gets a date format, so it displays as a date. getDate (Python get_date) reads the cell back as a Date (datetime), and returns null (None) for an empty cell. getValue still gives the serial, because a date is a number with a format.
import createFormulon from '@libraz/formulon'
const formulon = await createFormulon()
const wb = formulon.Workbook.createDefault()
try {
const ws = wb.sheet(0)
ws.setValue('A1', new Date(2026, 10, 7, 13, 30))
ws.updateCellFormat('A1', { numberFormat: 'yyyy-mm-dd' })
const back = ws.getDate('A1')
console.log(back.getFullYear(), back.getMonth() + 1, back.getDate(), back.getHours(), back.getMinutes()) // 2026 11 7 13 30
console.log(ws.getValue('A1')) // 46333.5625
console.log(ws.getDisplayText('A1').text) // 2026-11-07
} finally {
wb.dispose()
}from datetime import datetime
from formulon import Workbook
with Workbook.create_default() as wb:
ws = wb[0]
ws["A1"] = datetime(2026, 11, 7, 13, 30)
ws.update_cell_format("A1", number_format="yyyy-mm-dd")
print(ws.get_date("A1")) # 2026-11-07 13:30:00
print(ws["A1"]) # 46333.5625
print(ws.get_display_text("A1").text) # 2026-11-07The 1904 date system (getDate1904()) shifts every serial by 1,462 days; Date and datetime values follow whichever system the workbook uses. API conventions gives the exact rules.
Apply a number format and read the display text
updateCellFormat(range, { numberFormat }) (Python update_cell_format(range, number_format=...)) sets the format code of every cell in the range. A whole-column range such as "D:D" formats the column itself, so cells typed there later take the format. getDisplayText(cell).text returns the value as Excel would show it, and wb.formatValue(value, code) renders a value under any format code without touching a cell.
import createFormulon from '@libraz/formulon'
const formulon = await createFormulon()
const wb = formulon.Workbook.createDefault()
try {
const ws = wb.sheet(0)
ws.setValues('A1', [[1234.5, 0.125]])
ws.updateCellFormat('A1', { numberFormat: '#,##0.00' })
ws.updateCellFormat('B:B', { numberFormat: '0.0%' })
ws.setValue('B2', 0.5)
console.log(ws.getDisplayText('A1').text) // 1,234.50
console.log(ws.getDisplayText('B1').text) // 12.5%
console.log(ws.getDisplayText('B2').text) // 50.0%
console.log(ws.getCellFormat('B9').numberFormat) // 0.0%
console.log(wb.formatValue(1234.5, '0.0E+00').text) // 1.2E+03
} finally {
wb.dispose()
}from formulon import Workbook
with Workbook.create_default() as wb:
ws = wb[0]
ws["A1:B1"] = [[1234.5, 0.125]]
ws.update_cell_format("A1", number_format="#,##0.00")
ws.update_cell_format("B:B", number_format="0.0%")
ws["B2"] = 0.5
print(ws.get_display_text("A1").text) # 1,234.50
print(ws.get_display_text("B1").text) # 12.5%
print(ws.get_display_text("B2").text) # 50.0%
print(ws.get_cell_format("B9").number_format) # 0.0%
print(wb.format_value(1234.5, "0.0E+00").text) # 1.2E+03The same patch sets the rest of the cell format, as the next section shows.
Format cells
The same updateCellFormat(range, patch) changes the font, fill, border, alignment and protection of every cell in the range. A key left out keeps its current value, and null (Python None) clears a key that can be cleared. A colour is a "#RRGGBB" string, and getCellFormat(cell) reads the format back.
import createFormulon, { BorderStyle, HorizontalAlign } from '@libraz/formulon'
const formulon = await createFormulon()
const wb = formulon.Workbook.createDefault()
try {
const ws = wb.sheet(0)
ws.setValues('A1', [['Region', 'Sales'], ['East', 120], ['West', 90]])
ws.updateCellFormat('A1:B1', {
font: { bold: true, color: '#FFFFFF' },
fill: { foreground: '#2F6FED' },
alignment: { horizontal: HorizontalAlign.Center },
border: { bottom: { style: BorderStyle.Thin, color: '#000000' } },
})
ws.updateCellFormat('B2:B3', { numberFormat: '#,##0.00' })
console.log(ws.getCellFormat('A1').font.bold) // true
console.log(ws.getDisplayText('B2').text) // 120.00
} finally {
wb.dispose()
}from formulon import BorderStyle, HorizontalAlign, Workbook
with Workbook.create_default() as wb:
ws = wb[0]
ws.set_values("A1", [["Region", "Sales"], ["East", 120], ["West", 90]])
ws.update_cell_format(
"A1:B1",
font={"bold": True, "color": "#FFFFFF"},
fill={"foreground": "#2F6FED"},
alignment={"horizontal": HorizontalAlign.CENTER},
border={"bottom": {"style": BorderStyle.THIN, "color": "#000000"}},
)
ws.update_cell_format("B2:B3", number_format="#,##0.00")
print(ws.get_cell_format("A1").font.bold) # True
print(ws.get_display_text("B2").text) # 120.00Equal formats share one style record, so formatting many cells does not grow the style table. Styles and workbook settings covers named cell styles and the style-record layer.
Size and format whole columns and rows
A span string names whole columns or rows: "B:B", "B:D", "3:5". setColumnWidth (in characters) and setRowHeight (in points) take the span or a pair of indices. updateCellFormat formats the column or row itself when the range is a whole column or row, so cells typed there later take the format.
import createFormulon from '@libraz/formulon'
const formulon = await createFormulon()
const wb = formulon.Workbook.createDefault()
try {
const ws = wb.sheet(0)
ws.setColumnWidth('B:D', 12)
ws.setRowHeight('1', 24)
ws.updateCellFormat('D:D', { numberFormat: '0.0%' })
ws.updateCellFormat('1:1', { font: { bold: true } })
ws.setValue('D5', 0.25)
console.log(ws.getCellFormat('D5').numberFormat) // 0.0%
console.log(ws.getDisplayText('D5').text) // 25.0%
console.log(ws.getCellFormat('F1').font.bold) // true
} finally {
wb.dispose()
}from formulon import Workbook
with Workbook.create_default() as wb:
ws = wb[0]
ws.set_column_width("B:D", 12)
ws.set_row_height("1", 24)
ws.update_cell_format("D:D", number_format="0.0%")
ws.update_cell_format("1:1", font={"bold": True})
ws["D5"] = 0.25
print(ws.get_cell_format("D5").number_format) # 0.0%
print(ws.get_display_text("D5").text) # 25.0%
print(ws.get_cell_format("F1").font.bold) # TrueresetColumnWidth and resetRowHeight return a span to the default size. Presentation and geometry explains the width and height units.
Read next
- Formulas and recalculation: formulas, calculation modes and display text.
- API conventions: native values, ranges and partial updates.
- WASM API and Python API: the block access members.