Git Product home page Git Product logo

go-xlsx-templater's Introduction

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

go-xlsx-templater's People

Contributors

kcasctiv avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.