Mercurial > gemma
changeset 5354:6a94ab82eaaa extented-report
XLSX templater: Added type support for cell expansion.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Tue, 22 Jun 2021 14:40:27 +0200 |
parents | a352a695b69c |
children | 16c026da50b1 |
files | pkg/xlsx/templater.go |
diffstat | 1 files changed, 38 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/pkg/xlsx/templater.go Mon Jun 21 18:10:49 2021 +0200 +++ b/pkg/xlsx/templater.go Tue Jun 22 14:40:27 2021 +0200 @@ -303,8 +303,8 @@ } // Copy over expanded text - if s, err := e.expand(area.mc.GetCellValue(), vars); err == nil { - e.template.SetCellStr(e.destinationSheet, dst, s) + if v, err := e.typedExpand(area.mc.GetCellValue(), vars); err == nil { + e.template.SetCellValue(e.destinationSheet, dst, v) } // Finally merge the cells @@ -362,8 +362,8 @@ e.template.SetCellFormula(e.destinationSheet, dst, s) } if s, err := e.template.GetCellValue(e.sourceSheet, src); err == nil { - if s, err = e.expand(s, vars); err == nil { - e.template.SetCellStr(e.destinationSheet, dst, s) + if v, err := e.expand(s, vars); err == nil { + e.template.SetCellValue(e.destinationSheet, dst, v) } } } @@ -585,3 +585,37 @@ str = handlebars(str, replace) return str, err } + +func (e *executor) typedExpand(str string, vars map[string]interface{}) (interface{}, error) { + + var ( + err error + repCount int + last interface{} + ) + + replace := func(s string) string { + if err != nil { + return "" + } + var eval gval.Evaluable + if eval, err = e.expr(strings.TrimSpace(s)); err != nil { + return "" + } + repCount++ + last, err = eval(e.ctx, vars) + return fmt.Sprintf("%v", last) + } + + nstr := handlebars(str, replace) + + if err != nil { + return nil, err + } + + if repCount == 1 && strings.HasPrefix(str, "{{") && strings.HasSuffix(str, "}}") { + // log.Printf("typed eval: %T\n", last) + return last, nil + } + return nstr, nil +}