xlst

package module
v0.0.0-...-94cf377 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2022 License: MIT Imports: 9 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/tealeg/xlsx and github.com/aymerick/raymond for useful libs.

About package in russian

Godoc

Installation

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

Usage

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

Filename may be any of course. For slices use dot notation {{items.name}}. When parser meets dot notation it will repeats contains row. If you need repeat few rows, or have nested slices, you can use range start {{range groups}} and {{end}}.

Sample document image

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 omited in example.

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

Report image

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

View Source
var (
	TimeLayout = "15:04"
)

Functions

This section is empty.

Types

type InParam

type InParam struct {
	In        interface{}
	SheetName string
}

InParam 输入参数

type Options

type Options struct {
	WrapTextInAllCells bool
}

Options for render has only one property WrapTextInAllCells for wrapping text

type TimeSpan

type TimeSpan struct {
	Start time.Time
	End   time.Time
	Text  string
}

func (*TimeSpan) Has

func (t *TimeSpan) Has(timeStr string) bool

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) MergeTimeSpan

func (m *Xlst) MergeTimeSpan(timeSpans []*TimeSpan)

MergeTimeSpan 根据时间合并行

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) RenderMultipleSheet

func (m *Xlst) RenderMultipleSheet(options *Options, in ...InParam) error

RenderMultipleSheet renders multipleSheet

func (*Xlst) RenderWithOptions

func (m *Xlst) RenderWithOptions(in interface{}, options *Options) error

RenderWithOptions renders report with options provided 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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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