Help us shape the future of Go! Take the 2026 Go Developer Survey

sheet

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package sheet reads and writes the spreadsheets APIs upload and export: CSV, and XLSX (Office Open XML). A cell is the text of its value; an XLSX cell also has a type, a number format and a formula (Cell), and the rest of a workbook (styles, the worksheets apart from their cells) is kept to be compared as it is.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsPartLike

func IsPartLike(name string) bool

IsPartLike reports whether a bare name in a path is in the form of a part name (sheet1), which paths no longer use (PathName).

func IsXLSX

func IsXLSX(data []byte) bool

IsXLSX reports whether data is a ZIP archive with a workbook in it.

func QuoteName

func QuoteName(name string) string

QuoteName writes a workbook's name of a worksheet in a path (PathName).

func Ref

func Ref(row, col int) string

Ref writes the reference of a cell; row and column count from 0.

func Refs

func Refs(a, b Sheet) []string

Refs lists the references of the cells of both sheets, with a value or only a formula or a style (Meta), row by row and column by column.

func WriteWorkbook

func WriteWorkbook(sheets ...Rows) []byte

WriteWorkbook writes a workbook with a worksheet for each of sheets, in that order, holding the rows as text cells; the n-th one is the part xl/worksheets/sheet<n>.xml. Like WriteXLSX, the same sheets give the same bytes.

func WriteXLSX

func WriteXLSX(rows [][]string) []byte

WriteXLSX writes a workbook with one worksheet, Sheet1, holding rows as text cells. The output is the same bytes for the same rows: stored entries without times.

Types

type Cell

type Cell struct {
	// Type is the type of the value: number, string, bool, error or date.
	// A shared, inline and formula string are all string: how a writer
	// stores a text is not its type.
	Type string
	// Format is the number format: its format code, "builtin N" for a
	// built-in format the workbook does not write out, "" for General.
	Format string
	// Formula is the formula's text without "=" (of a shared formula, the
	// text of the cell that defines it); "" for a plain value.
	Formula string
	// Style is the index of the cell's style (font, fill, borders and the
	// number format) in the workbook's styles; "" for the default.
	Style string
}

Cell is what an XLSX cell has besides the text of its value.

type Part

type Part struct {
	Name string
	Data []byte
}

Part is a part of a workbook: its name and content.

type Rows

type Rows struct {
	Name string
	Rows [][]string
}

Rows is a worksheet to write: its name in the workbook and its rows.

type Sheet

type Sheet struct {
	// Name is the name the workbook gives the worksheet ("Users"), or
	// its part name ("sheet1") when the workbook does not name it (Named
	// false); "csv" for a CSV file.
	Name  string
	Named bool
	Cells map[string]string
	// Meta holds what an XLSX cell has besides its value, for every cell
	// in Cells and every empty cell with a formula or a style of its own
	// (a cell without a value is not in Cells); nil for a CSV file.
	Meta map[string]Cell
	// Rich holds how the rich texts (a shared or inline string of runs in
	// a bold font, a color) are formatted, by cell: each run as <r len="N">,
	// its length in characters, with its properties; nil when no cell has
	// one. Kept apart from Meta, which every cell has.
	Rich map[string]string
}

Sheet is one worksheet: cell reference (A1) → value.

func ReadCSV

func ReadCSV(data []byte) (Sheet, error)

ReadCSV reads a CSV file (a UTF-8 byte order mark is skipped). Empty cells are left out, as in a spreadsheet.

func ReadXLSX

func ReadXLSX(data []byte) ([]Sheet, error)

ReadXLSX reads the cells of every worksheet (ReadWorkbook).

func (Sheet) PathName

func (s Sheet) PathName() string

PathName writes the worksheet in a drift or rule path (xlsx:<PathName>!B2). A name of letters, digits, _ and . stands as it is; any other is quoted as in a spreadsheet formula, with a quote inside doubled:

Users   Članovi   'Q1 2026!'   'O''Brien'   'sheet1'   [sheet1]

A worksheet the workbook does not name is its part name in brackets, [sheet1], which no workbook name can be ([ and ] are not allowed in one). A name written like a part name (sheet1) is quoted too: that bare form is what DriftGate wrote before it read the workbook's names, and is never written now.

func (Sheet) Rows

func (s Sheet) Rows() [][]string

Rows returns the cells of a sheet as rows, empty cells as "".

type Workbook

type Workbook struct {
	Sheets []Sheet
	// Rest is the workbook apart from its cells, by part name in order:
	// every part but the document properties (docProps/: times, author,
	// application), the shared strings and the calculation chain (both
	// follow from the cells), with the worksheets written without their
	// cells (withoutValues). Two workbooks with the same cells and the same
	// Rest look the same in a spreadsheet application.
	Rest []Part
}

Workbook is a read XLSX file: its worksheets, and the rest of it.

func ReadWorkbook

func ReadWorkbook(data []byte) (*Workbook, error)

ReadWorkbook reads the cells of every worksheet, in the order of the workbook's tabs and under the names the workbook gives them. When the workbook does not say which part holds which worksheet (xl/workbook.xml or its relationships are missing, cannot be read, or point at a part that is not there), every worksheet part is read in the order of its number and named after it (sheet1, Named false): one rule for the whole workbook, so a damaged workbook never mixes the two. Shared strings and inline strings are resolved; a number, boolean or formula result is the text stored in the cell. A row or cell without its optional reference (r) is where ECMA-376 puts it: the row after the previous row, the column after the previous cell of the row.

Jump to

Keyboard shortcuts

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