exceltemplar

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 28, 2025 License: MIT Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Logo

ExcelTemplar

ExcelTemplar is a lightweight Excel templating engine for Go. It lets you design .xlsx templates in Excel using simple directives and render them with JSON data.

  • Minimal, dependency-light core
  • Works on top of excelize
  • Explicit template syntax with {{ }} blocks

Repository: https://github.com/nikitaxru/exceltemplar

Installation

go get github.com/nikitaxru/exceltemplar

Quick Start

package main

import (
    "log"

    excel "github.com/nikitaxru/exceltemplar"
)

func main() {
    // Path to an .xlsx file containing template directives
    templatePath := "./template.xlsx"
    outputPath := "./output.xlsx"

    // One or more JSON strings providing data for rendering
    data := []string{
        `{"tasks":[{"name":"Implement feature X","priority":"high"}]}`,
    }

    if err := excel.WriteResultsWithTemplate(templatePath, outputPath, data); err != nil {
        log.Fatalf("render failed: %v", err)
    }
}

Template Syntax (in Excel cells)

  • Expression: {{= expr}}
  • Each (list): {{#each $.items as $it i=$i}} ... {{/each}}
  • Each (object): {{#each-obj $.dict as $k $v}} ... {{/each-obj}}
  • If/Else: {{#if expr}} ... {{else}} ... {{/if}}
  • Built-ins: len(), exists(), join()

Examples (place in cells):

  • {{= $.title }}
  • {{#if len($.items) > 0}} ... {{/if}}
  • Inside each: {{= $it.name}}

API

  • LoadTemplate(path string) (*Template, error)
  • (*Template).Render(outputs []string) error — render with one or more JSON strings
  • (*Template).Save(destPath string) error
  • Convenience: WriteResultsWithTemplate(templatePath, destPath string, outputs []string) error

Utility:

  • NormalizeForExcel(jsonStrings []string) []string — normalizes JSON for predictable rendering

Full documentation

Complete documentation is available in the docs/ folder:

Both versions contain:

  • Template syntax reference
  • Path anchors and context explanation
  • Practical examples with JSON data
  • Excel template best practices
  • Programmatic API usage
  • Behavior with missing data

Testing

go test ./...

License

MIT © 2025 Nikita Samoylov

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NormalizeForExcel

func NormalizeForExcel(jsonStrings []string) []string

NormalizeForExcel приводит JSON строки к более предсказуемой форме: - добавляет отсутствующие поля пустыми значениями - сортирует массивы объектов по ключу name/code при наличии - удаляет явные дубликаты объектов (по сериализованному виду)

func WriteResultsWithTemplate

func WriteResultsWithTemplate(templatePath, destPath string, outputs []string) error

WriteResultsWithTemplate оставляем без изменений

Types

type Template

type Template struct {
	// contains filtered or unexported fields
}

func LoadTemplate

func LoadTemplate(path string) (*Template, error)

LoadTemplate строит AST для каждого листа

func (*Template) Render

func (t *Template) Render(outputs []string) error

func (*Template) Save

func (t *Template) Save(destPath string) error

Save сохраняет файл

Directories

Path Synopsis
examples
conditions command
each_obj command
inline_iif command
iterator command
join_fields command
nested_groups command
simple_array command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址