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.
go get -u "github.com/krestkrest/go-xlsx-templater"
import "github.com/krestkrest/go-xlsx-templater"
For slices use dot notation {{items.name}}
. When parser meets dot notation, it will repeat the contained row.
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,
},
},
},
},
}
Error processing is omitted in example.
doc := xlst.New()
doc.ReadTemplate("./template.xlsx")
doc.Render(ctx)
doc.Save("./report.xlsx")
type Xlst struct {
// contains filtered or unexported fields
}
Xlst Represents template struct
func New() *Xlst
New() creates new Xlst struct and returns pointer to it
func (m *Xlst) ReadTemplate(path string) error
ReadTemplate() reads template from disk and stores it in a struct
func (m *Xlst) Render(ctx map[string]interface{}) error
Render() renders report and stores it in a struct
func (m *Xlst) Save(path string) error
Save() saves generated report to disk
func (m *Xlst) Write(writer io.Writer) error
Write() writes generated report to provided writer