Formulas
OpenDocumentSpreadsheets reads and writes formulas in the notation LibreOffice shows in its formula bar: =SUM(A1:B2,1), =AVERAGE(A1:A10,'Other sheet'.B2). In the file, OpenDocument stores them in OpenFormula syntax (of:=SUM([.A1:.B2];1)), which raw = true reads and writes.
using OpenDocumentSpreadsheets
using OpenDocumentSpreadsheets: getFormula
ods = newods("Prices")
sh = ods["Prices"]
sh["A1:C1"] = ["Item" "Price" "With VAT"]
sh["A2:B4"] = ["Tea" 3.5; "Cake" 4.25; "Soup" 6]
for r in 2:4
setFormula(sh, "C$r"; val = "=B$r*1.2")
end
setFormula(sh, "C5"; val = "=SUM(C2:C4)")
setFormula(sh, "D5"; val = "of:=[.C5]/3", raw = true)
getFormula(sh, "C5") # "=SUM(C2:C4)"
getFormula(sh, "C5"; raw = true) # "of:=SUM([.C2:.C4])"
The notation
- English function names, with
,between arguments. - In inline arrays,
,between columns and;between rows ({1,2;3,4}). - References name their sheet with
.(Sheet2.A1), as in LibreOffice. - Functions that LibreOffice stores under another name are translated with LibreOffice's table of names:
IFSis stored asCOM.MICROSOFT.IFS,CEILING.XCLasCOM.MICROSOFT.CEILING, and so on.
getFormula and the getters on other pages are public but not exported: import them by name, as above, or write OpenDocumentSpreadsheets.getFormula.
Array formulas
An array formula is one formula over a range, whose results fill the range: in LibreOffice, it is entered by selecting the range and pressing Ctrl+Shift+Enter. array = true writes one.
using OpenDocumentSpreadsheets: getArrayFormulas
sh["F1"] = "Items"
setFormula(sh, "F2:F4"; val = "=SORT(A2:A4)", array = true) # Cake, Soup, Tea
setFormula(sh, "G2:G4"; val = "=B2:B4*1.2", array = true) # one formula, three results
setFormula(sh, "G5"; val = "=SUM(B2:B4*(B2:B4>4))", array = true) # one result: 10.25
getFormula(sh, "G3") # "{=B2:B4*1.2}": every cell of the range shows it in braces
getArrayFormulas(sh) # [F2:F4, G2:G4, G5:G5]- The range is fixed. LibreOffice fills the range it is given and doesn't spill into the cells next to it. Give a range the size of the results: in a larger one, LibreOffice shows
#N/Ain the cells left over, and a smaller one shows only part of the results. - Its cells change together, as in LibreOffice: writing to part of an array formula is an error, and writing over all of it replaces it (
sh["G2:G4"] = missingdeletes it). - Rows and columns can be inserted before or after it, and it moves. LibreOffice doesn't insert them inside it, or at its first row or column when it has more than one, and doesn't delete part of it: each of these is an error.
- Functions that return several values, such as
UNIQUE,SORT,FILTERandMMULT, give all their results in an array formula. In an ordinary formula, LibreOffice shows only the first.
Results
OpenDocumentSpreadsheets doesn't calculate formulas. A formula written here has no result until the file is opened in LibreOffice (or another spreadsheet program), which calculates it. A formula read from a file gives the result LibreOffice last saved with it.
Not available
- Formulas that spill, whose results take as many cells as they need: LibreOffice's array formulas have a fixed range, so
setFormula'sspillkeyword is an error.