Skip to content

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

License

Notifications You must be signed in to change notification settings

irbgeo/excel-templater

Repository files navigation

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

type key default value type of value in data for filling describe
simple " " any type the value will be inserted along the path from the placeholder
qr code qr_code " " not empty string the 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 array qr_code_row array of not empty strings the array of qr codes will be inserted in a row, starting with the placeholder cell
image image transparent pixel base64-encoded PNG
table table array 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/geoirb/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
	r, err := templater.FillIn(templateFile, payload)
	if err != nil {
		panic(err)
	}
}

Gratitude

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