Cookbook
Short answers to common questions, each with the code to do it. Every recipe links to the guide page that covers the topic in full.
The recipes run in order: most use the sales DataFrame from the first recipe, and the sales.ods and report.ods files written by the first two. They need these packages (DataFrames is installed with Pkg.add("DataFrames")):
using OpenDocumentSpreadsheets, DataFrames, DatesReading and writing data
How do I write a DataFrame to a new file?
sales = DataFrame(region = ["North", "South", "East", "West"], revenue = [1000, 1500, 900, 1200])
writetable("sales.ods", sales; sheetname = "Sales", overwrite = true)The column names become the header row. overwrite = true replaces the file if it exists. Any other Tables.jl table works the same way, such as a named tuple of columns. See Creating and editing files.
How do I write several DataFrames to separate sheets?
Give each sheet's name and its table as a pair:
costs = DataFrame(item = ["Rent", "Staff", "Travel"], amount = [400, 2500, 320])
writetable("report.ods", "Sales" => sales, "Costs" => costs; overwrite = true)How do I read a sheet into a DataFrame?
df = DataFrame(readtable("report.ods", "Costs"))The header row gives the column names, and each column gets the narrowest type of its values. To read only some columns, or a table that starts lower down, give its columns and its first row:
df = DataFrame(readtable("report.ods", "Sales", "A:B"; first_row = 1))missing_strings = ["NA"] reads such cells as missing; empty cells are missing already. See the Tutorial's Reading a table.
How do I write a DataFrame into an existing file, below other cells?
openods("report.ods"; mode = "rw") do ods
s = addsheet!(ods, "Summary")
s["A1"] = "Costs, 2025"
writetable!(s, costs; anchor_cell = "A3") # the header in row 3, the rows below it
endThe file is saved when the block ends. writetable! writes into any sheet, with its top-left cell at anchor_cell.
How do I read a cell or a range?
readdata("report.ods", "Sales.B2:B5") # a 4×1 matrix: 1000, 1500, 900, 1200
ods = readods("report.ods")
ods["Sales"]["A2"] # "North"A reference names its sheet with a dot, as LibreOffice writes it: Sales.B2. See Addressing cells and ranges.
How do I add a sheet to an existing file?
openods("report.ods"; mode = "rw") do ods
notes = addsheet!(ods, "Notes")
notes["A1"] = "Figures are provisional."
endThe file is saved when the block ends. See Managing sheets.
How do I name a range and read it by its name?
openods("report.ods"; mode = "rw") do ods
addDefinedName(ods, "Revenue", "Sales.B2:B5") # Sheet ▸ Named Ranges and Expressions
end
revenue = vec(readods("report.ods")["Revenue"])Indexing a file with a name gives the cells it refers to as a matrix; vec makes it a vector. The name can be used in formulas too: =SUM(Revenue). See Named ranges.
How do I add a formula?
openods("report.ods"; mode = "rw") do ods
s = ods["Sales"]
s["A6"] = "Total"
setFormula(s, "B6"; val = "=SUM(B2:B5)")
s["C1"] = "Share"
setFormula(s, "C2:C5"; val = "=B2:B5/B6", array = true) # one array formula over C2:C5
endarray = true enters the formula over the whole range at once, as Ctrl+Shift+Enter does. The formulas have no values until LibreOffice opens the file and calculates them. See Formulas.
Formatting
How do I format a header row?
Make a cell style for it, and give the header that style. The style is listed in LibreOffice's Styles deck, and changing it there changes every cell that uses it:
ods = openods("sales.ods"; mode = "rw")
s = ods["Sales"]
addCellStyle(ods, "Header"; font = (bold = true, color = "FFFFFF"),
fill = (pattern = "solid", fgColor = "4472C4"),
border = (bottom = ["style" => "medium", "color" => "000000"],))
setCellStyle(s, "A1:B1", "Header")
setBorder(s, "A1:B5"; outside = ["style" => "thin", "color" => "000000"]) # a box round the table
setColumnWidth(s, "A:B"; width = 14)
writeods("sales_formatted.ods", ods)
setFont, setFill, setBorder and setAlignment format cells directly, as Format ▸ Cells does. See Cell formats.
How do I show numbers as currency, percentages or dates?
ods = newods("Formats")
s = ods["Formats"]
s["A1:A4"] = ["Price"; "Share"; "Date"; "Count";;]
s["B1:B4"] = Any[1234.5; 0.256; Date(2025, 3, 9); 9876543;;]
setFormat(s, "B1"; format = "Currency")
setFormat(s, "B2"; format = "0.0%")
setFormat(s, "B3"; format = "D MMMM YYYY")
setFormat(s, "B4"; format = "#,##0")
setColumnWidth(s, "A:B"; width = 16)
writeods("formats.ods", ods)
The value in each cell is unchanged; only how it is shown. A format code is written as in Format ▸ Cells ▸ Numbers ▸ Format code, and "Currency" uses the currency of whoever opens the file. See Number formats.
How do I highlight values above a threshold?
A conditional format, which LibreOffice applies again whenever the values change. Here it gives the cells LibreOffice's built-in Good style:
ods = openods("sales.ods"; mode = "rw")
setConditionalFormat(ods["Sales"], "B2:B5", :cellIs; operator = "greaterThan", value = "1100",
style = "Good")
writeods("sales_highlight.ods", ods)
See Conditional formats for data bars, colour scales, icon sets and the other conditions.
How do I set column widths?
openods("report.ods"; mode = "rw") do ods
setColumnWidth(ods["Costs"], "A"; width = 20) # in characters of the default font
autofitColumns(ods["Sales"], "A:C") # LibreOffice's optimal width
endSee Column width and row height.
LibreOffice features
How do I make a database range with a totals row?
A database range (Data ▸ Define Range) is a named block of rows under a header row, with filter buttons. writetable makes one as it writes a DataFrame:
writetable("sales_table.ods", sales; sheetname = "Sales", as_table = true, table_name = "Sales",
totals = ["region" => "Total", "revenue" => :sum], freeze_header = true)
The totals row, A6:B6, holds Total and a formula summing the revenue. To make a database range of cells already in a sheet, use addtable! and settotals!. Database ranges have no styles of their own: format one with cell styles. See Database ranges.
How do I make a chart?
using OpenDocumentSpreadsheets.Charts
ods = openods("sales.ods"; mode = "rw")
addChart(ods["Sales"], :column; anchor = "D2:K17", data = "A1:B5", title = "Revenue by region",
firstRowLabels = true, firstColumnLabels = true)
writeods("sales_chart.ods", ods)
data is the whole range, as LibreOffice's chart wizard takes it, with the first row and column as labels. LibreOffice draws the chart from the cells, so it follows them when they change. See Creating charts.
How do I put a small chart in a cell?
A sparkline: a line or column chart of a row or column of values, drawn inside one cell.
ods = newods("Trend")
s = ods["Trend"]
s["A1:F1"] = ["Region" "Q1" "Q2" "Q3" "Q4" "Trend"]
s["A2:E3"] = ["North" 10 14 12 18; "South" 20 17 15 11]
addSparklines(s, "F2:F3", "B2:E3") # one per row
writeods("trend.ods", ods)See Sparklines.
How do I make a drop-down list?
openods("sales.ods"; mode = "rw") do ods
s = ods["Sales"]
s["C1"] = "Status"
addDataValidation(s, "C2:C5"; type = :list, values = ["Open", "Closed", "On hold"])
endThis is Data ▸ Validity, with List allowed. For a longer list, put the items in cells and give their range as formula1. See Validity.
How do I freeze the header row?
openods("sales.ods"; mode = "rw") do ods
freezePanes(ods["Sales"]; nrows = 1) # row 1 stays in view as the sheet scrolls
endThis is View ▸ Freeze Cells ▸ Freeze First Row. See Panes, the view and tab colours.
How do I group detail rows so they can be collapsed?
ods = newods("Budget")
s = ods["Budget"]
s["A1:A7"] = ["Item"; "Rent"; "Rates"; "Premises total"; "Salaries"; "Pensions"; "Staff total";;]
s["B1:B7"] = Any["Cost"; 400; 120; 520; 2500; 300; 2800;;]
groupRows(s, 2:3) # shown, with a button beside them
groupRows(s, 5:6; collapsed = true) # hidden, with a button to show them
writeods("budget_grouped.ods", ods)This is Data ▸ Group and Outline ▸ Group. See Hiding and grouping.
How do I insert a picture or logo?
openods("report.ods"; mode = "rw") do ods
addImage(ods["Notes"], "D2", "logo.png"; alt = "The company logo") # PNG, JPEG or GIF
endThe picture is placed at its own size, anchored to D2. See Pictures, including how to insert a plot made in Julia.
How do I link to another sheet?
openods("report.ods"; mode = "rw") do ods
addHyperlink(ods["Notes"], "A3"; location = "Costs.A1", display = "See the costs")
end
A url links to a web page instead. See Hyperlinks.
How do I add a comment to a cell?
openods("report.ods"; mode = "rw") do ods
addComment(ods["Costs"], "B3", "Includes overtime"; author = "Finance")
endThis is Insert ▸ Comment. visible = true shows the comment always, not only when the pointer is on the cell. See Notes and comments.
Sheets, protection, printing and saving
How do I make a copy of a sheet for each region?
ods = newods("Template")
template = ods["Template"]
template["A1"] = "Region:"
for region in ["North", "South", "East"]
sheet = copysheet!(template, region)
sheet["B1"] = region
end
setSheetVisibility(template, :hidden)
setTabColor(ods["North"], "4472C4")
writeods("regions.ods", ods)See Managing sheets.
How do I protect a sheet but leave some cells editable?
ods = newods("Form")
s = ods["Form"]
s["A1:A3"] = ["Name"; "Email"; "Phone";;]
setCellProtection(s, "B1:B3"; locked = false) # the cells to fill in
protectSheet(s; password = "secret")
writeods("form.ods", ods)This is Tools ▸ Protect Sheet. Sheet protection prevents mistakes; it is not security. See Protecting sheets.
How do I set a sheet up for printing?
openods("report.ods"; mode = "rw") do ods
s = ods["Sales"]
setPageSetup(s; orientation = :landscape, paperSize = :A4, fitToWidth = 1, fitToHeight = 0)
setPrintTitles(s; rows = 1) # the header row on every page
endThese are Format ▸ Page Style and Format ▸ Print Ranges. See Page setup and printing.
How do I save a template or a flat file?
The file's extension says what is written:
writeods("form.ots", readods("form.ods")) # a template: LibreOffice opens each use as a new document
writeods("sales.fods", readods("sales.ods"); overwrite = true) # flat XML: one file, no zipA flat file is easy to read and compare, so git can show what changed in it. It holds everything the .ods did, its pictures and charts included, as LibreOffice's Save As ▸ Flat XML ODF Spreadsheet writes it, and saves back to .ods the same way. See Saving and large files.
How do I write a file too large to hold in memory?
A large DataFrame is written row by row, without building the file in memory first:
big = DataFrame(n = 1:1_000_000, square = (1:1_000_000) .^ 2)
writetable("big_df.ods", big; sheetname = "data", streaming = true, overwrite = true)Rows that are made one at a time are written as they come:
streamods("big.ods"; overwrite = true) do s
sh = addsheet!(s, "data"; columns = ["n", "square"])
for n in 1:1_000_000
writerow!(sh, (n, n^2))
end
endSee Large files.
Not here
- Longer tasks, done from start to finish, are on the Examples page.
- Pivot tables (Insert ▸ Pivot Table) can't be made or read.
- Table styles: database ranges have none; format them with cell styles, as above.