jsreport excel question for xlsxReplace
-
{{#each offer.obligorInfo}} {{#xlsxReplace "xl/worksheets/sheet2.xml" "worksheet.sheetData[0].row[2].c[2]"}} <c t="inlineStr" s="{{@root.$removedItem.c.[2].$.s}}"><is><t>{{basicObligorInfo.obligorName}}</t></is></c> {{/xlsxReplace}} {{/each}}
Consider the above, how can we even put variables inside? "xl/worksheets/sheet2.xml" "worksheet.sheetData[0].row[2].c[2]"
Such as "xl/worksheets/sheet2.xml" "worksheet.sheetData[0].row[2].c[${0}]"
I can't figure any way to do it with handlebars, the string param needs to be dynamic, not statically written.
Btw the documentation on jsReport's excel recipe is extremely poor.
-
You can create your own custom handlebars helper that dynamically constructs the
xlsxReplace
secondxmlPath
parameter and use it like this:{{#xlsxReplace "xl/worksheets/sheet1.xml" (replace "worksheet.sheetData[0].row[[object Object]].c[0]" "[object Object]" 1)}}
function replace(str, pattern, value) { return str.replace(pattern, value) }
However, the problem here is that the
xlsxReplace
doesn't put to the root context the$removedItem
you are trying to use for the style attribute. But it seems thexlsxMerge
is the right way to go here.Check the example I prepared for you
https://playground.jsreport.net/w/anon/O70zThsu
-
@jan_blaha thanks Jan.
I didn't know that you can pass a callback into a handlebar function with ( functionName ...params); thanks a lot.
I've tried something like the above (before you answer), like {{#xlsxReplace "xl/worksheets/sheet1.xml" functionName params}}; that was throwing an error, so it needs ( ).
-
Its documented here, just FYI https://handlebarsjs.com/guide/expressions.html#subexpressions
I wasn't aware of it also for quite some time.I updated docs to include the dynamic xmlPath calculation code.
-
Yeah, I am not that good with handlebars, never used it before ;p, I got it confused with angular's templating.