excel

package
v0.0.0-...-9c1b276 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2022 License: GPL-3.0 Imports: 16 Imported by: 0

README

excel

Based on my own xml parser, https://github.com/xianhammer/excel/xml, this repo allow for reading of OOXML based Excel files.

Document creation is only basically supported.

Documentation

Overview

package excel contain a excel document.

Index

Constants

View Source
const (
	Spreadsheet               = "http://schemas.openxmlformats.org/spreadsheetml/2006/main"
	Relationships             = "http://schemas.openxmlformats.org/package/2006/relationships"
	RelationshipsDoc          = "http://schemas.openxmlformats.org/officeDocument/2006/relationships"
	OfficeDocument            = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"
	RelationshipSharedstrings = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings"
	RelationshipStyles        = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"
	RelationshipsWorksheet    = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet"
	RelationshipsTheme        = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme"
	ContentTypes              = "http://schemas.openxmlformats.org/package/2006/content-types"
	CoreProperties            = "http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties"
	ExtendedProperties        = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties"
	MarkupCompatibility       = "http://schemas.openxmlformats.org/markup-compatibility/2006"
	X14ac                     = "http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac"
	X16r2                     = "http://schemas.microsoft.com/office/spreadsheetml/2015/02/main"
	X15                       = "http://schemas.microsoft.com/office/spreadsheetml/2010/11/main"

	AppVersion     = "16.0300"
	Creator        = "User"  // TODO Set to proper value at startup
	LastModifiedBy = Creator // TODO Set to proper value at startup

)
View Source
const (
	Boolean Type = 'b'
	Date         = 'd'
	Error        = 'e'
	Inline       = 'i'
	Number       = 'n'
	String       = 's'
	Formula      = 'f'
)

Variables

View Source
var (
	// See https://docs.microsoft.com/en-us/office/open-xml/structure-of-a-spreadsheetml-document
	EmbeddedExcel = regexp.MustCompile(`\.xlsx$`) // Only process top levele EXCEL files.
	EmbeddedZIP   = regexp.MustCompile(`\.zip$`)  // Only process top levele EXCEL files.
)
View Source
var (
	ErrUnknownFile          = errors.New("Missing _rels/.rels file")
	ErrMissingRels          = errors.New("Missing _rels/.rels file")
	ErrMissingWorkbook      = errors.New("Missing workbook file")
	ErrMissingSharedstrings = errors.New("Missing sharedstrings file")
	ErrMissingStyles        = errors.New("Missing styles file")

	ErrInvalidSpans     = errors.New("Invalid spans attribute format")
	ErrInvalidDimension = errors.New("Invalid dimension attribute format")
	ErrInvalidReference = errors.New("Invalid reference attribute format")

	ErrDuplicateSheet = errors.New("Duplicate sheet")
	ErrUnknownSheet   = errors.New("Unknown sheet")

	ErrArgumentInconsictency = errors.New("Inconsistent arguments")
)

Functions

func FormatDatetime

func FormatDatetime(f *numFmt, data []byte) (out string)

func FormatDefault

func FormatDefault(f *numFmt, data []byte) (out string)

func FormatDimension

func FormatDimension(row, column int) (s string)

func FormatFloat

func FormatFloat(f *numFmt, data []byte) (out string)

func FormatInteger

func FormatInteger(f *numFmt, data []byte) (out string)

func FormatStandard

func FormatStandard(f *numFmt, data []byte) (out string)

func NewCellStyleXf

func NewCellStyleXf(nf *numFmt) (f *cellStyleXf)

func NewCellXf

func NewCellXf(nf *numFmt) (f *cellXf)

func NewNumFmt

func NewNumFmt(numFmtId, code, goFormat string, formatter func(f *numFmt, data []byte) (out string)) (f *numFmt)

func NewSimpleWriter

func NewSimpleWriter(doc *Document) (w *simplewriter)

func NodeContent

func NodeContent(r io.Reader, tag []byte) (contents []string, err error)

func ParseReference

func ParseReference(value []byte) (column, row int, err error)

func WriteSimple

func WriteSimple(outputFile string, doc *Document) (err error)

Types

type Attributes

type Attributes map[string]string

type Cell

type Cell struct {
	// contains filtered or unexported fields
}

func (*Cell) From

func (c *Cell) From(other *Cell)

func (*Cell) SetCellStyleXf

func (c *Cell) SetCellStyleXf(xf *cellStyleXf) (out *Cell)

func (*Cell) SetCellXf

func (c *Cell) SetCellXf(xf *cellXf) (out *Cell)

func (*Cell) SetType

func (c *Cell) SetType(t Type) (out *Cell)

func (*Cell) SetValue

func (c *Cell) SetValue(ss *SharedStrings, v string) (out *Cell)

func (*Cell) Value

func (c *Cell) Value(ss *SharedStrings, applyStyle bool) (cell string)

type ColumnUpdate

type ColumnUpdate func(row, column int, c *Cell) (newValue string)

type ContentType

type ContentType string

type Dimension

type Dimension struct {
	ColumnStart, RowStart, ColumnEnd, RowEnd int
}

func ParseDimension

func ParseDimension(value []byte) (d Dimension, err error)

func (Dimension) Columns

func (d Dimension) Columns() int

func (Dimension) End

func (d Dimension) End() (s string)

func (Dimension) Rows

func (d Dimension) Rows() int

func (Dimension) Start

func (d Dimension) Start() (s string)

func (Dimension) String

func (d Dimension) String() (s string)

type Document

type Document struct {
	Workbook *Workbook
	// contains filtered or unexported fields
}

func NewDocument

func NewDocument() (doc *Document)

func Open

func Open(r io.ReaderAt, size int64) (doc *Document, err error)

func OpenFile

func OpenFile(path string) (doc *Document, err error)

OpenFile open a zip container

func (*Document) Close

func (d *Document) Close() (err error)

Close the document

type Elements

type Elements []Attributes

func ChildAttributes

func ChildAttributes(r io.Reader, tag, parentTag []byte) (attributes Elements, err error)

func NodeAttributes

func NodeAttributes(r io.Reader, tag []byte) (attributes Elements, err error)

func (Elements) Get

func (e Elements) Get(key, value string) (element Attributes)

type File

type File zip.File

func (*File) Open

func (f *File) Open() (io.ReadCloser, error)

func (*File) QuerySelectorAll

func (f *File) QuerySelectorAll(tagname string) (elements []Attributes, err error)

type Files

type Files map[string]*File

type Row

type Row []Cell

func (Row) Cell

func (r Row) Cell(idx int) (c *Cell)

func (Row) Indeces

func (r Row) Indeces(pick []string, ss *SharedStrings, applyStyle bool, translate map[string]string) (indeces []int, err error)

func (Row) Pick

func (r Row) Pick(pick []int, ss *SharedStrings, applyStyle bool) (row []string)

func (Row) Value

func (r Row) Value(ss *SharedStrings, applyStyle bool) (row []string)

type SharedStrings

type SharedStrings struct {
	// contains filtered or unexported fields
}

func (*SharedStrings) Get

func (s *SharedStrings) Get(idx int) (v string)

func (*SharedStrings) GetFromValue

func (s *SharedStrings) GetFromValue(value []byte) (v string)

func (*SharedStrings) GetIdx

func (s *SharedStrings) GetIdx(v string) (idx int)

GetIdx return 1-offset shared strings index. If 0 is returned no string matched.

type Sheet

type Sheet struct {
	Attributes Attributes
	Dimension  Dimension
	Rows       [][]Cell
	// contains filtered or unexported fields
}

Sheet of an Excel workbook, providing access to Cells and Rows.

func (*Sheet) AddColumns

func (s *Sheet) AddColumns(cols []ColumnUpdate)

AddColumns add new column(s) to the current sheet.

func (*Sheet) AppendEmptyRow

func (s *Sheet) AppendEmptyRow(width int) (r []Cell)

func (*Sheet) AppendRow

func (s *Sheet) AppendRow() (r *newrow)

func (*Sheet) Cell

func (s *Sheet) Cell(row, col int) (c *Cell)

Cell return specified cell.

func (*Sheet) CellByRef

func (s *Sheet) CellByRef(ref string) (cell *Cell, err error)

CellByRef return cell by Excel reference, like "C3"

func (*Sheet) ColumnByTitle

func (s *Sheet) ColumnByTitle(row int, title string) (column int, err error)

ColumnByTitle

func (*Sheet) ReadFrom

func (s *Sheet) ReadFrom(r io.Reader) (n int64, err error)

ReadFrom implement the io.ReaderFrom interface.

func (*Sheet) Row

func (s *Sheet) Row(row int) (r Row)

Row return indexed row.

func (*Sheet) SharedStrings

func (s *Sheet) SharedStrings() (ss *SharedStrings)

SharedStrings return a reference to the sharedstrings(.xml)

func (*Sheet) Styles

func (s *Sheet) Styles() (ss *Styles)

Styles return a reference to the style(.xml)

func (*Sheet) WriteTo

func (s *Sheet) WriteTo(w io.Writer) (n int64, err error)

WriteTo implement the io.WriterTo interface. TODO Values below are fixed on purpose (In current version at least).

type Styles

type Styles struct {
	// contains filtered or unexported fields
}

Styles represent Excel styles(.xml)

func (*Styles) AddCellStyleXf

func (s *Styles) AddCellStyleXf(xf *cellStyleXf)

func (*Styles) AddCellXf

func (s *Styles) AddCellXf(xf *cellXf)

func (*Styles) AddNumFmt

func (s *Styles) AddNumFmt(nf *numFmt)

func (*Styles) GetCellStyleXf

func (s *Styles) GetCellStyleXf(idx int) (xf *cellStyleXf)

func (*Styles) GetCellStyleXfByFormat

func (s *Styles) GetCellStyleXfByFormat(format string) (xf *cellStyleXf)

func (*Styles) GetCellXf

func (s *Styles) GetCellXf(idx int) (xf *cellXf)

func (*Styles) GetCellXfByFormat

func (s *Styles) GetCellXfByFormat(format string) (xf *cellXf)

func (*Styles) GetNumFmt

func (s *Styles) GetNumFmt(id string) (nf *numFmt)

func (*Styles) GetNumFmtByFormat

func (s *Styles) GetNumFmtByFormat(format string) (nf *numFmt)

type Type

type Type byte

type Workbook

type Workbook struct {
	// contains filtered or unexported fields
}

Workbook represent an Excel Workbook(.xml).

func (*Workbook) AddSheet

func (w *Workbook) AddSheet(name string) (s *Sheet, err error)

AddSheet return a new named sheet.

func (*Workbook) ImportSheet

func (w *Workbook) ImportSheet(name string, s *Sheet, addCols []ColumnUpdate) (t *Sheet, err error)

Import sheet (deep clone).

func (*Workbook) SharedStrings

func (w *Workbook) SharedStrings() (ss *SharedStrings)

SharedStrings return a reference to the sharedstrings(.xml)

func (*Workbook) Sheet

func (w *Workbook) Sheet(name string) (s *Sheet, n int64, err error)

Sheet return a named sheet, the sheet file size or an error.

func (*Workbook) Sheets

func (w *Workbook) Sheets() (s []*Sheet)

Sheets return all sheets

func (*Workbook) Styles

func (w *Workbook) Styles() (ss *Styles)

Styles return a reference to the style(.xml)

Jump to

Keyboard shortcuts

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