Skip to content

Spreadsheet

A complete spreadsheet: a grid of cells with formulas (146 functions), number formats, conditional formatting, dropdowns and checkboxes, sorting and filters, frozen panes, merged cells, several sheets, undo, autofill and copy and paste with Sheets and Excel, plus a toolbar, menus and sheet tabs. The data lives in a pz.Workbook, which your code reads and writes (book["B2"], book.records()) and which opens and saves .xlsx and CSV files. It has its own section: Spreadsheet.

ui.Spreadsheet · Category: Data · Full reference →

SpreadsheetSpreadsheet
  • When users enter, calculate or review tabular data: budgets, inventories, grades, price lists, imports.
  • To show records that are only read and sorted, a Table is simpler.
import pyzed as pz
from pyzed import ui
class Budget(pz.App):
def __init__(self):
self.book = pz.Workbook()
self.book.write([["Item", "Cost"], ["Rent", 950], ["Food", 320]])
self.book["B4"] = "=SUM(B2:B3)"
def changed(self, event):
print(event.value.action, event.value.range, self.book["B4"])
def view(self):
return ui.Spreadsheet(book=self.book, on_change=self.changed)

Without the toolbar and the tabs it is an editable table with formulas.

Spreadsheet: Just the gridSpreadsheet: Just the grid

Spreadsheet has 8 events of its own, listed below. Like every widget it also has the 18 common events (click, double_click, right_click, mouse_enter, drag, key_down, resize…), attached with .on(...): see Events and Events of every component.

It is a pz.Component, so it also raises on_mount and on_unmount, and its handlers get a pz.Event whose value holds the data.

Event Of Handler gets When
on_change ui.Spreadsheet, pz.sheet (event) Receives event.value with action, range and sheet when the user changes something.
on_select ui.Spreadsheet, pz.sheet (event) Receives event.value with range and sheet when the selection changes.
on_context pz.sheet (data) Called on a right click with kind ("cell", "row" or "column"), cell, x and y (relative to the grid): show a menu there.
on_dropdown pz.sheet (data) Called when a list cell’s dropdown should open: cell, options, x, y, width.
on_filter pz.sheet (data) Called when a filter button of the header is clicked: column, x, y.
on_find pz.sheet (data) Called on Ctrl+F or Ctrl+H: mode ("find" or "replace").
on_invalid pz.sheet (data) Called when a typed value breaks a strict validation rule: cell, value, message.
on_edit pz.sheet (data) Called when typing in a cell starts or ends: editing.

Handler gets is what Pyzed passes to your callable: () means nothing. Any handler can also be a message (a value that is not callable), which goes to update().

The same events written three ways. Imperative: methods of the app change self. Declarative: the handlers produce messages and one update() decides. Functional: a pure function draws an immutable state and asks for changes with set_state(...). See Imperative, declarative and functional.

class Budget(pz.App):
def __init__(self):
self.book = pz.Workbook()
self.book.write([["Item", "Cost"], ["Rent", 950], ["Food", 320]])
self.book["B4"] = "=SUM(B2:B3)"
self.total = self.book["B4"]
def changed(self, event): # on_change: event.value has action, range and sheet
self.total = self.book["B4"]
def view(self):
return pz.column([
ui.label(f"Total: {self.total}"),
ui.Spreadsheet(book=self.book, toolbar=False, on_change=self.changed),
], spacing=8)
class Spreadsheet

A component: the whole spreadsheet, with the grid and its formula bar, a toolbar, right-click menus, a find bar, panels for conditional formatting, validation and notes, sheet tabs and a status bar with the selection’s sum. Everything the user does goes to book. See The Spreadsheet component.

Property Default Description
book None The pz.Workbook to show and edit (a new empty one when None). Keep it in your app so its data survives view rebuilds.
toolbar True Shows the toolbar.
formula_bar True Shows the name box and the formula bar.
tabs True Shows the sheet tabs and the status bar.
headers True Shows the column letters and row numbers.
readonly False Users can select, copy and find, but not change anything.
text_size 13 Pixel size of 10 pt text.
width "fill" Width: pixels, "fill", "shrink" or "fill:N" (proportion).
height "fill" Height: pixels, "fill", "shrink" or "fill:N" (proportion).

Events: on_change, on_select (events).

Full reference →

class Workbook

A spreadsheet workbook: sheets of cells with values, formulas and formats.

Full reference →

sheet(
book,
*,
colors=None,
text_size=13.0,
formula_bar=True,
headers=True,
width=None,
height=None,
readonly=False,
on_change=None,
on_select=None,
on_context=None,
on_dropdown=None,
on_filter=None,
on_find=None,
on_invalid=None,
on_edit=None,
)

The spreadsheet grid on its own: cells, headers and formula bar, with selection, editing, formulas, the fill handle, copy and paste and the keyboard shortcuts of Sheets and Excel. It draws only the cells in view. The grid doesn’t draw menus: its events say where the user clicked. See The grid on its own.

Property Default Description
book required The pz.Workbook to show.
colors None A dict of colors (background, grid, header, text, accent, selection…); ui.sheet_colors(theme) makes one from a theme.
text_size 13.0 Pixel size of 10 pt text.
formula_bar True Shows the name box and the formula bar.
headers True Shows the column letters and row numbers.
width None Width: pixels, "fill", "shrink" or "fill:N" (proportion).
height None Height: pixels, "fill", "shrink" or "fill:N" (proportion).
readonly False Select and copy only.

Events: on_change, on_select, on_context, on_dropdown, on_filter, on_find, on_invalid, on_edit (events).

Full reference →

sheet_colors(theme=None)

The colors ui.Spreadsheet gives its grid for a theme, as the dict pz.sheet(colors=...) takes.

Property Default Description
theme None A theme (the current one by default).

Full reference →