Skip to content

Tables

Basic Table filling with range

Input file template.docx: Input file

The range open and close actions are inside their own rows to avoid creating different tables each iteration.

Golang code:

go
package main

import (
	"fmt"

	gotemplatedocx "github.com/JJJJJJack/go-template-docx"
)

type ID struct {
	ID       int
	IsActive bool
}

type ExampleStruct struct {
	IDs []ID
}

func main() {
	docxTemplate, err := gotemplatedocx.NewDocxTemplateFromFilename("template.docx")
	if err != nil {
		fmt.Println("Error loading template:", err)
		return
	}

	templateValues := ExampleStruct{
		IDs: []ID{
			{ID: 1, IsActive: true},
			{ID: 2, IsActive: false},
			{ID: 3, IsActive: true},
		},
	}

	err = docxTemplate.Apply(templateValues)
	if err != nil {
		fmt.Println("Error applying template values:", err)
		return
	}

	err = docxTemplate.Save("template_output.docx")
	if err != nil {
		fmt.Println("Error saving output file:", err)
		return
	}
}

Output file template_output.docx: Output file

Change table cell color

Input file template.docx: Table cells bg color input file

Basic function syntax

{{tableCellBgColor "#FF0000"}}

Golang code:

same as above

Output file template_output.docx: Table cells bg color output file