xlst

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2023 License: MIT Imports: 11 Imported by: 0

README

go-xlsx-templater

Simple .xlsx (Excel XML document) template based document generator using handlebars.

Takes input .xlsx documents with mustache sippets in it and renders new document with snippets replaced by provided context.

Thanks to github.com/qax-os/excelize and github.com/aymerick/raymond for useful libs.

Inspired by github.com/ivahaev/go-xlsx-templater, but made some improvements to prevent style modifications in the result file.

Installation

    go get -u "github.com/krestkrest/go-xlsx-templater"

Usage

Import to your project
    import "github.com/krestkrest/go-xlsx-templater"
Prepare template.xlsx template.

For slices use dot notation {{items.name}}. When parser meets dot notation, it will repeat the contained row.

Prepare context data
    ctx := map[string]interface{}{
        "name": "Github User",
        "groupHeader": "Group name",
        "nameHeader": "Item name",
        "quantityHeader": "Quantity",
        "groups": []map[string]interface{}{
            {
                "name":  "Work",
                "total": 3,
                "items": []map[string]interface{}{
                    {
                        "name":     "Pen",
                        "quantity": 2,
                    },
                    {
                        "name":     "Pencil",
                        "quantity": 1,
                    },
                },
            },
            {
                "name":  "Weekend",
                "total": 36,
                "items": []map[string]interface{}{
                    {
                        "name":     "Condom",
                        "quantity": 12,
                    },
                    {
                        "name":     "Beer",
                        "quantity": 24,
                    },
                },
            },
        },
    }
Read template, render with context and save to disk.

Error processing is omitted in example.

    doc := xlst.New()
	doc.ReadTemplate("./template.xlsx")
	doc.Render(ctx)
	doc.Save("./report.xlsx")

Documentation

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

Xlst Represents template struct

func New
func New() *Xlst

New() creates new Xlst struct and returns pointer to it

func (*Xlst) ReadTemplate
func (m *Xlst) ReadTemplate(path string) error

ReadTemplate() reads template from disk and stores it in a struct

func (*Xlst) Render
func (m *Xlst) Render(ctx map[string]interface{}) error

Render() renders report and stores it in a struct

func (*Xlst) Save
func (m *Xlst) Save(path string) error

Save() saves generated report to disk

func (*Xlst) Write
func (m *Xlst) Write(writer io.Writer) error

Write() writes generated report to provided writer

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func OptionUnescapeHTML added in v0.0.4

func OptionUnescapeHTML(o *options)

OptionUnescapeHTML remove '"' and other specific HTML strings from the result of substitution.

Types

type CellModification added in v0.0.2

type CellModification struct {
	Row int
	ColumnModification
}

type ColumnModification added in v0.0.2

type ColumnModification struct {
	Column int
	Value  string
}

type Modifications added in v0.0.2

type Modifications struct {
	CellModifications  []*CellModification
	RowInsertions      map[int][]*RowInsertion
	RowInsertionsTotal int
}

func NewModifications added in v0.0.2

func NewModifications() *Modifications

func (*Modifications) AddCellModification added in v0.0.2

func (m *Modifications) AddCellModification(cm *CellModification)

func (*Modifications) AddRowInsertion added in v0.0.2

func (m *Modifications) AddRowInsertion(row int, ri *RowInsertion)

type Option added in v0.0.4

type Option func(o *options)

Option is the interface of functor that applies options to the Render method.

type RowInsertion added in v0.0.2

type RowInsertion struct {
	Columns []*ColumnModification
}

type Xlst

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

Xlst Represents template struct

func New

func New() *Xlst

New creates new Xlst struct and returns pointer to it

func NewFromBinary

func NewFromBinary(content []byte) (*Xlst, error)

NewFromBinary creates new Xlst struct puts binary tempate into and returns pointer to it

func (*Xlst) ReadTemplate

func (m *Xlst) ReadTemplate(path string) error

ReadTemplate reads template from disk and stores it in a struct

func (*Xlst) Render

func (m *Xlst) Render(in interface{}) error

Render renders report and stores it in a struct

func (*Xlst) RenderWithOptions

func (m *Xlst) RenderWithOptions(in interface{}, opts ...Option) error

func (*Xlst) Save

func (m *Xlst) Save(path string) error

Save saves generated report to disk

func (*Xlst) Write

func (m *Xlst) Write(writer io.Writer) error

Write writes generated report to provided writer

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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