tablexlsx

package
v1.135.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 24, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package tablexlsx writes an Excel workbook from a workbook described as data (#1849): named sheets of columns and rows, a small vocabulary of column types, and the few presentation facts a report needs -- a bold header row, column widths, a frozen pane and an optional title row. It is deliberately not a styling API: a sheet says what its columns ARE, and the writer decides how each type is shown.

Numbers the script computed exactly stay exact. A currency or decimal cell is written from its decimal text into the cell's value, so "1234.50" is the number the file carries, not the nearest float64 to it. See cells.go.

Index

Constants

View Source
const (
	// MaxSheets bounds the sheets of one workbook. Excel has no fixed limit;
	// a workbook with more sheets than this is several outputs.
	MaxSheets = 255
	// MaxCells bounds the cells of one workbook, across its sheets. The
	// writer holds the workbook in memory while it builds it, so the bound is
	// on what is built, not only on the bytes the result compresses to.
	MaxCells = 2_000_000
)

Limits a workbook is held to before a byte is written.

View Source
const ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

ContentType is the media type of an Excel workbook.

View Source
const Extension = ".xlsx"

Extension is the file extension of an Excel workbook.

Variables

This section is empty.

Functions

func CheckSheetName

func CheckSheetName(name string) error

CheckSheetName holds a sheet name to Excel's rules: 1 to 31 characters (UTF-16 units, as Excel counts them), none of []:*?/\, not beginning or ending with an apostrophe, and no character Excel cannot store.

Types

type ColumnType

type ColumnType string

ColumnType is one entry of the vocabulary a sheet's column_types uses.

const (
	TypeString   ColumnType = "string"
	TypeInteger  ColumnType = "integer"
	TypeDecimal  ColumnType = "decimal"
	TypeCurrency ColumnType = "currency"
	TypeDate     ColumnType = "date"
	TypeDateTime ColumnType = "datetime"
	TypePercent  ColumnType = "percent"
)

The column types a sheet may declare. A column that declares none is written from its values: text as text, a number as a number, a bool as a bool.

type Sheet

type Sheet struct {
	Name    string
	Title   string
	Columns []string
	// Types is the declared type of each column that declared one.
	Types map[string]ColumnType
	// Widths is the declared width of each column that declared one, in
	// Excel's character units.
	Widths map[string]float64
	// Freeze is the top-left cell of the scrolling pane, "" for none.
	Freeze string
	// Rows holds each row's values, positional against Columns.
	Rows [][]any
}

Sheet is one sheet of a workbook: its columns in order, each row's values in that order, and the presentation facts it declared.

type SheetShape

type SheetShape struct {
	Name string `json:"name"`
	Rows int    `json:"rows"`
}

SheetShape is what a report says about one sheet: its name and how many data rows it holds (the header and title rows are not counted).

type Workbook

type Workbook struct {
	Sheets []Sheet
}

Workbook is a parsed, validated workbook description.

func Parse

func Parse(body any, columnsFor func(sheet int) []string) (*Workbook, error)

Parse reads a workbook description: a map with one key, "sheets", a list of sheet maps. Values are the plain Go values a JSON decode or a script's conversion produces (string, int64, float64, bool, nil, []any, map[string]any).

columnsFor supplies the column order for a sheet whose description names no "columns": the caller that still holds the rows in their written order (a script's dicts keep insertion order; a Go map does not) reads it there. It may be nil, in which case such a sheet is refused.

func (*Workbook) RowCount

func (w *Workbook) RowCount() int

RowCount is the data rows across every sheet.

func (*Workbook) Shape

func (w *Workbook) Shape() []SheetShape

Shape reports each sheet's name and data row count, in order.

func (*Workbook) Write

func (w *Workbook) Write(maxBytes int) ([]byte, error)

Write renders the workbook as an .xlsx file and refuses a result larger than maxBytes. A value a column cannot hold fails the write naming its sheet, row and column, and nothing is returned.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL