ods

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2025 License: MIT Imports: 8 Imported by: 2

README

rechenbrett

A go library for building Open Document spreadsheet files.

It can create 'normal' ods (Open Document Spreadsheet) files (*.ods) and 'flat' Open Document Spreadsheet files (*.fods).

ods files are zipped and can be opened with various commercial and open source spreadsheet applications. This is the default file format used by LibreOffice Calc when saving data.

fods files are plain xml files without compression. They contain the same information than their zipped counterparts, but wrap everything in one large xml document. Due to their plain text nature, they work well with version control systems such as git. For example, if you want to keep track of your bank account statements, which you might get in some sort of complex xml or json structure, you could use rechenbrett to convert them into a clean flat ods structure which can be version controlled and produce meaningful diffs.

Sadly, if you save fods files using LibreOffice Calc, it changes the file in many places which makes it harder to diff two versions of the same file in a meaningful way. Post processing the file using flat-odf-cleanup.py can mitigate the issue, but does not fully resolve it.

Example usage

package main

import (
	"os"

	rb "github.com/fwilhe2/rechenbrett"
)

func main() {
	inputCells := [][]rb.Cell{
		{
			rb.MakeCell("ABBA", "string"),
			rb.MakeCell("42.3324", "float"),
			rb.MakeCell("2022-02-02", "date"),
			rb.MakeCell("2.2.2022", "date"),
			rb.MakeCell("19:03:00", "time"),
			rb.MakeCell("2.22", "currency"),
			rb.MakeCell("-2.22", "currency"),
			rb.MakeCell("0.4223", "percentage"),
		},
	}

	spreadsheet := rb.MakeSpreadsheet(inputCells)

	// create ods file
	buff := rb.MakeOds(spreadsheet)
	archive, err := os.Create("myfile.ods")
	if err != nil {
		panic(err)
	}
	archive.Write(buff.Bytes())
	archive.Close()

	// create fods file
	flatOdsString := rb.MakeFlatOds(spreadsheet)
	os.WriteFile("myfile.fods", []byte(flatOdsString), 0o644)
}

json-to-ods is a simple go wrapper for rechenbrett to make it usable as a cli tool

mkods-demo shows how mkods can be used in combination with node.js to transform complex json structures into clean spreadsheets

csv-to-ods converts csv files to ods with optional type hints

kalkulationsbogen is similar to rechenbrett, but written in TypeScript for node.js

License

This software is written by Florian Wilhelm and available under the MIT license (see LICENSE for details)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MakeFlatOds

func MakeFlatOds(spreadsheet Spreadsheet) string

func MakeOds

func MakeOds(spreadsheet Spreadsheet) *bytes.Buffer

Types

type AutomaticStyles

type AutomaticStyles struct {
	XMLName      xml.Name      `xml:"office:automatic-styles"`
	NumberStyles []interface{} `xml:"number:number-style"`
	Styles       []Style       `xml:"style:style"`
}

type Body

type Body struct {
	XMLName     xml.Name    `xml:"office:body"`
	Spreadsheet Spreadsheet `xml:"office:spreadsheet"`
}

type Cell

type Cell struct {
	XMLName     xml.Name `xml:"table:table-cell"`
	Text        string   `xml:"text:p,omitempty"`
	ValueType   string   `xml:"office:value-type,attr,omitempty"`
	CalcExtType string   `xml:"calcext:value-type,attr,omitempty"`
	Value       string   `xml:"office:value,attr,omitempty"`
	DateValue   string   `xml:"office:date-value,attr,omitempty"`
	TimeValue   string   `xml:"office:time-value,attr,omitempty"`
	Currency    string   `xml:"office:currency,attr,omitempty"`
	StyleName   string   `xml:"table:style-name,attr,omitempty"`
	Formula     string   `xml:"table:formula,attr,omitempty"`
	Range       string   `xml:"-"`
}

func MakeCell

func MakeCell(value, valueType string) Cell

func MakeRangeCell

func MakeRangeCell(value, valueType, rangeName string) Cell

type CellData

type CellData struct {
	Value     string `json:"value"`
	ValueType string `json:"valueType"`
	Range     string `json:"range,omitempty"`
}

type CurrencyStyle

type CurrencyStyle struct {
	XMLName        xml.Name        `xml:"number:currency-style"`
	Name           string          `xml:"style:name,attr"`
	Volatile       string          `xml:"style:volatile,attr,omitempty"`
	Language       string          `xml:"number:language,attr"`
	Country        string          `xml:"number:country,attr"`
	Texts          []TextElement   `xml:"number:text"`
	Number         NumberFormat    `xml:"number:number"`
	CurrencySymbol CurrencySymbol  `xml:"number:currency-symbol"`
	TextProperties *TextProperties `xml:"style:text-properties,omitempty"`
	StyleMap       *StyleMap       `xml:"style:map,omitempty"`
}

type CurrencySymbol

type CurrencySymbol struct {
	Language string `xml:"number:language,attr"`
	Country  string `xml:"number:country,attr"`
	Symbol   string `xml:",chardata"`
}

type DateStyle

type DateStyle struct {
	XMLName xml.Name      `xml:"number:date-style"`
	Name    string        `xml:"style:name,attr"`
	Parts   []interface{} `xml:"number:text"`
}

type FileEntry

type FileEntry struct {
	FullPath  string `xml:"manifest:full-path,attr"`
	Version   string `xml:"manifest:version,attr,omitempty"`
	MediaType string `xml:"manifest:media-type,attr"`
}

type FlatOds

type FlatOds struct {
	XMLName         xml.Name        `xml:"office:document"`
	XMLNSOffice     string          `xml:"xmlns:office,attr"`
	XMLNSTable      string          `xml:"xmlns:table,attr"`
	XMLNSText       string          `xml:"xmlns:text,attr"`
	XMLNSStyle      string          `xml:"xmlns:style,attr"`
	XMLNSFo         string          `xml:"xmlns:fo,attr"`
	XMLNSNumber     string          `xml:"xmlns:number,attr"`
	XMLNSCalcext    string          `xml:"xmlns:calcext,attr"`
	OfficeVersion   string          `xml:"office:version,attr"`
	OfficeMimetype  string          `xml:"office:mimetype,attr"`
	AutomaticStyles AutomaticStyles `xml:"office:automatic-styles"`
	Body            Body            `xml:"office:body"`
}

type Manifest

type Manifest struct {
	XMLName xml.Name    `xml:"manifest:manifest"`
	Version string      `xml:"manifest:version,attr"`
	XMLNS   string      `xml:"xmlns:manifest,attr"`
	Entries []FileEntry `xml:"manifest:file-entry"`
}

type Map

type Map struct {
	XMLName        xml.Name `xml:"style:map"`
	Condition      string   `xml:"style:condition,attr"`
	ApplyStyleName string   `xml:"style:apply-style-name,attr"`
}

type NamedExpressions

type NamedExpressions struct {
	NamedRanges []NamedRange `xml:"table:named-range"`
}

type NamedRange

type NamedRange struct {
	Name             string `xml:"table:name,attr"`
	BaseCellAddress  string `xml:"table:base-cell-address,attr"`
	CellRangeAddress string `xml:"table:cell-range-address,attr"`
}

type NumberElement

type NumberElement struct {
	XMLName          xml.Name `xml:"number:number"`
	DecimalPlaces    string   `xml:"number:decimal-places,attr"`
	MinDecimalPlaces string   `xml:"number:min-decimal-places,attr"`
	MinIntegerDigits string   `xml:"number:min-integer-digits,attr"`
	Grouping         string   `xml:"number:grouping,attr"`
	Language         string   `xml:"-"`
	Country          string   `xml:"-"`
}

type NumberElementDateDay

type NumberElementDateDay struct {
	XMLName xml.Name `xml:"number:day"`
	Style   string   `xml:"number:style,attr"`
}

type NumberElementDateMonth

type NumberElementDateMonth struct {
	XMLName xml.Name `xml:"number:month"`
	Style   string   `xml:"number:style,attr"`
}

type NumberElementDateYear

type NumberElementDateYear struct {
	XMLName xml.Name `xml:"number:year"`
	Style   string   `xml:"number:style,attr"`
}

type NumberFormat

type NumberFormat struct {
	DecimalPlaces    int  `xml:"number:decimal-places,attr"`
	MinDecimalPlaces int  `xml:"number:min-decimal-places,attr"`
	MinIntegerDigits int  `xml:"number:min-integer-digits,attr"`
	Grouping         bool `xml:"number:grouping,attr"`
}

type NumberStyle

type NumberStyle struct {
	XMLName        xml.Name        `xml:"number:number-style"`
	Name           string          `xml:"style:name,attr"`
	Volatile       string          `xml:"style:volatile,attr,omitempty"`
	Language       string          `xml:"number:language,attr,omitempty"`
	Country        string          `xml:"number:country,attr,omitempty"`
	Text           string          `xml:"number:text,omitempty"`
	TextProperties *TextProperties `xml:"style:text-properties,omitempty"`
	NumberElements []NumberElement `xml:",any"`
	Map            *Map            `xml:"style:map,omitempty"`
}

type OfficeDocumentContent

type OfficeDocumentContent struct {
	XMLName         xml.Name        `xml:"office:document-content"`
	XMLNSOffice     string          `xml:"xmlns:office,attr"`
	XMLNSTable      string          `xml:"xmlns:table,attr"`
	XMLNSText       string          `xml:"xmlns:text,attr"`
	XMLNSStyle      string          `xml:"xmlns:style,attr"`
	XMLNSFo         string          `xml:"xmlns:fo,attr"`
	XMLNSNumber     string          `xml:"xmlns:number,attr"`
	XMLNSCalcext    string          `xml:"xmlns:calcext,attr"`
	OfficeVersion   string          `xml:"office:version,attr"`
	AutomaticStyles AutomaticStyles `xml:"office:automatic-styles"`
	Body            Body            `xml:"office:body"`
}

type OfficeDocumentStyles

type OfficeDocumentStyles struct {
	XMLName         xml.Name        `xml:"office:document-styles"`
	XMLNSOffice     string          `xml:"xmlns:office,attr"`
	XMLNSTable      string          `xml:"xmlns:table,attr"`
	XMLNSText       string          `xml:"xmlns:text,attr"`
	XMLNSStyle      string          `xml:"xmlns:style,attr"`
	XMLNSFo         string          `xml:"xmlns:fo,attr"`
	XMLNSNumber     string          `xml:"xmlns:number,attr"`
	XMLNSSvg        string          `xml:"xmlns:svg,attr"`
	XMLNSCalcext    string          `xml:"xmlns:calcext,attr"`
	OfficeVersion   string          `xml:"office:version,attr"`
	AutomaticStyles AutomaticStyles `xml:"office:automatic-styles"`
}

type Row

type Row struct {
	XMLName xml.Name `xml:"table:table-row"`
	Cells   []Cell   `xml:"table:table-cell"`
}

type Spreadsheet

type Spreadsheet struct {
	XMLName          xml.Name         `xml:"office:spreadsheet"`
	Tables           []Table          `xml:"table:table"`
	NamedExpressions NamedExpressions `xml:"table:named-expressions"`
}

func MakeSpreadsheet

func MakeSpreadsheet(cells [][]Cell) Spreadsheet

type Style

type Style struct {
	XMLName         xml.Name `xml:"style:style"`
	Name            string   `xml:"style:name,attr"`
	Family          string   `xml:"style:family,attr"`
	ParentStyleName string   `xml:"style:parent-style-name,attr"`
	DataStyleName   string   `xml:"style:data-style-name,attr"`
}

type StyleMap

type StyleMap struct {
	Condition      string `xml:"style:condition,attr"`
	ApplyStyleName string `xml:"style:apply-style-name,attr"`
}

type Styles

type Styles struct {
	XMLName xml.Name      `xml:"styles"`
	Items   []interface{} `xml:",any"`
}

type Table

type Table struct {
	XMLName xml.Name `xml:"table:table"`
	Name    string   `xml:"table:name,attr"`
	Rows    []Row    `xml:"table:table-row"`
}

type TextElement

type TextElement struct {
	Content string `xml:",chardata"`
}

type TextProperties

type TextProperties struct {
	XMLName xml.Name `xml:"style:text-properties"`
	Color   string   `xml:"fo:color,attr,omitempty"`
}

Jump to

Keyboard shortcuts

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