Categorygithub.com/chenghonour/excel-templater
modulepackage
0.0.0-20231225075205-6f15ca619a58
Repository: https://github.com/chenghonour/excel-templater.git
Documentation: pkg.go.dev

# README

EXCEL-TEMPLATER

Description

The simple templater for filling in excel (.xlsx .xls) file with data.

Templater supports:

  • Insert simple values
  • Insert tabels
  • Insert qr codes
  • Insert list of qr codes
  • Insert png images.
  • Defaults values (turn on by flag)
  • Fill in multipage file

The filled in data must be in serializing format.

Placeholders

For correct input data in the excel file the file must have placeholders. The placeholder shows the template engine where and what data will be inserted and sets the style of inserted data.

The placeholder is the string between { }, string consisting of keys([_a-zA-Z0-9]+), keys are separated by :. The placeholder shows the template engine:

  • style of inserted data - stile of placeholder
  • value of inserted data - set of keys in placeholder must be a path to the value in data for filling in
  • type of inserted data - last key

Supported types of inserted data

typekeydefault valuetype of value in data for fillingdescribe
simple" "any typethe value will be inserted along the path from the placeholder
qr codeqr_code" "not empty stringthe qr code will be generated from the value, and it will be inserted in a cell, height of qr code will be the equal height of the cell with the placeholder
qr code arrayqr_code_rowarray of not empty stringsthe array of qr codes will be inserted in a row, starting with the placeholder cell
imageimagetransparent pixelbase64-encoded PNG
tabletablearray of objects!!! row with this placeholder will be deleted!!! Each object from the array will be converted to the generated table row. For right converting in the next row after a row with table a placeholder's cell must have a placeholders row, this placeholders needs for inserting values in columns of the generated table, the templater finds value in each object of the array.

Examples

Simple:

Data for filling

{
  "field_1": "value_for_A1",
  "field_2": "value_for_A2",
  "group_1": {
    "field_1": "value_for_D2",
    "subgroup": {
      "field": "value_for_C4"
    }
}

Template

simple_value_template

Result

simple_value_result

Qr code:

Data for filling

{
  	"urls": [
		"https://t.me/geoirb",
		"https://github.com/geoirb",
		"https://github.com/geoirb/excel-templater/blob/master/README.md"
	],
	"url": "https://t.me/geoirb"
}

Template

qr_code_template

Result

qr_code_result

Table:

Data for filling

{
	"group_1": {
		"table_example": [
			{
				"column_1": 1.1,
				"column_2": "first_row",
				"column_3": "first_row",
				"column_4": 0.0
			},
			{
				"column_1": 2.2,
				"column_2": "second_row",
				"column_3": "second_row",
				"column_4": 0.1
			},
			{
				"column_1": 3.3,
				"column_2": "third_row",
				"column_3": "third_row",
				"column_4": 0.2
			}
		]
	}
}

Template

table_template

Result

table_result

Get start


import (
...

	"github.com/chenghonour/excel-templater"
...
)

func main() {
	// flag useDefault turn on default values
	templater := excel.NewTemplater(useDefault)

	var payload interface{}
	// deserializing inserted data 
	if err := json.Unmarshal(data, &payload); err != nil {
		panic(err)
	}

	// templateFile - path to template
	fileByte, err := templater.FillIn(templateFile, payload)
	if err != nil {
		panic(err)
	}
}

Gratitude

  • github.com/qax-os/excelize
  • github.com/skip2/go-qrcode

# Packages

No description provided by the author

# Functions

NewTemplater ..