Skip to content

Simple xlsx template based document generator

License

Notifications You must be signed in to change notification settings

sjqzhang/go-xlsx-templater

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

Godoc

Installation

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

Usage

Import to your project

    import "github.com/sjqzhang/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

Cell Merge Example

if you want to merge cells, the data ctx key must be endswith _merge and the content must be same, for example:

{
  "accident": [
    {
      "description": "title",
      "level": "level",
      "title_merge": "线上事故"
    },
    {
      "description": "title",
      "level": "level",
      "title_merge": "线上事故"
    },
    {
      "description": "title",
      "level": "level",
      "title_merge": "线上事故"
    }
  ],
  "group": "Company Group",
  "manager": "",
  "name": "name-cn-sls-mf-leader",
  "okrs": [
    {
      "category_merge": "Business Project Goals",
      "completion": "",
      "info_merge": "O1: Object1",
      "keyResult": "KR1:what is the name",
      "remark": "",
      "score": 0,
      "weight": 0,
      "weightScore": 0
    },
    {
      "category_merge": "Business Project Goals",
      "completion": "",
      "info_merge": "O1: Object1",
      "keyResult": "KR2:what is the second key result",
      "remark": "",
      "score": 0,
      "weight": 0,
      "weightScore": 0
    },
    {
      "category_merge": "Business Project Goals",
      "completion": "",
      "info_merge": "O2: Object2",
      "keyResult": "KR1:what is the KR1",
      "remark": "",
      "score": 0,
      "weight": 0,
      "weightScore": 0
    },
    {
      "category_merge": "Business Project Goals",
      "completion": "",
      "info_merge": "O2: Object2",
      "keyResult": "KR1:this is okr1",
      "remark": "",
      "score": 0,
      "weight": 0,
      "weightScore": 0
    },
    {
      "category_merge": "Internal Project Goals",
      "completion": "",
      "info_merge": "O3: Object3",
      "keyResult": "KR1: my name is okr1",
      "remark": "",
      "score": 0,
      "weight": 0,
      "weightScore": 0
    },
    {
      "category_merge": "Internal Project Goals",
      "completion": "",
      "info_merge": "O3: Object3",
      "keyResult": "KR2: what is this?",
      "remark": "",
      "score": 0,
      "weight": 0,
      "weightScore": 0
    }
  ],
  "quarter": "Q1",
  "reviewOverall": "",
  "reviewResult": "",
  "role": "Tech Leader",
  "team": "",
  "totalScore": 0
}

Report image

    doc := xlst.New()
    doc.ReadTemplate("./demo/okrTemplate.xlsx")
    var ctx map[string]interface{}
    json.Unmarshal([]byte(js), &ctx)
    doc.Render(ctx)
    doc.Save("./demo/report.xlsx")

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

About

Simple xlsx template based document generator

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%