Dynamic height and weight in table
-
Hello:
No. I store the templates in JSReport, but I don´t store the template content, or the data (I use API call) This configuration works fine in my project. The problem is: I don´t know how to add the "hackMerge" function in the API call. I have tried the last solution and it results in error. How can I solve this problem?
Example to the API call:
{ template: { content: dataContent, recipe: 'xlsx', engine: "handlebars", phantom: optionsPhantom, xlsxTemplate: { shortid: idXLSXTemplate } }, data : dataReport }
-
The helpers are stored in the
template.helpers
property, so you need to send it there, if you don't store it in the template. This body works{ "template": { "name": "xlsxtest", "helpers": "function hackMerge(fileName, path, opts) {\n \/\/ workaround for this bug\n \/\/ https:\/\/github.com\/jsreport\/jsreport-xlsx\/issues\/20\n opts.data.root.$xlsxTemplate[fileName].hack = function () {\n return new Function('obj', 'return obj' + path)(opts.data.root.$xlsxTemplate[fileName]);\n }\n \n xlsxMerge.call(this, fileName, 'hack()', opts)\n}", "content": "{{#hackMerge \"xl\/workbook.xml\" \".workbook.sheets[0].sheet[0]\"}}\n <sheet name=\"My Sheet Name\"\/>\n{{\/hackMerge}}{{{xlsxPrint}}}" } }
-
I tried your solution:
{ template: { content: dataContent, recipe: 'xlsx', engine: "handlebars", phantom: optionsPhantom, xlsxTemplate: { shortid: idXLSXTemplate }, "helpers": "function hackMerge(fileName, path, opts) {\n \/\/ workaround for this bug\n \/\/ https:\/\/github.com\/jsreport\/jsreport-xlsx\/issues\/20\n opts.data.root.$xlsxTemplate[fileName].hack = function () {\n return new Function('obj', 'return obj' + path)(opts.data.root.$xlsxTemplate[fileName]);\n }\n \n xlsxMerge.call(this, fileName, 'hack()', opts)\n}" }, data : dataReport }
And I get this error:
Error during rendering report:
Unexpected identifier evalmachine.<anonymous>:409 function hackMerge(fileName, path, opts) { opts.data.root.$xlsxTemplate[fileName].hack = function () { return new Function('obj', 'return obj' + path)(opts.data.root.$xlsxTemplate[fileName]); } xlsxMerge.call(this, fileName, 'hack()', opts) } ^^^^^^^^^ SyntaxError: Unexpected identifier xlsXmerge it´s not declared
-
Did you alter the default jsreport server configuration? What you have in
tasks.strategy
?
-
I have not altered the default jsreport server configuration and my tasks.strategy file is empty.
-
Hm, it works for me...
Can you run like this other helpers, for example in html recipe?
What jsreport version you have?
Do you send the json correctly escaped?
Maybe you could add aconsole.log(request.template.helpers)
here to see if jsreport gets what you expect https://github.com/jsreport/jsreport-templates/blob/master/lib/templates.js#L76
-
btw the latest release of jsreport includes fix for complex paths in xlsx helpers. So you can use standard xlsxMerge there.
-
Hello:
Thank you very much for your help.
I solved the error, it was silly. Attribute "helpers" in the API call work correctly. The error was in the string content of the attribute "helpers." You and I put:
"function hackMerge(fileName, path, opts) { opts.data.root.$xlsxTemplate[fileName].hack = function () { return new Function('obj', 'return obj' + path)(opts.data.root.$xlsxTemplate[fileName]); } xlsxMerge.call(this, fileName, 'hack()', opts) }"
And the correct way is:
"function hackMerge(fileName, path, opts) { opts.data.root.$xlsxTemplate[fileName].hack = function () { return new Function('obj', 'return obj' + path)(opts.data.root.$xlsxTemplate[fileName]); }; xlsxMerge.call(this, fileName, 'hack()', opts) }"
With special attention to semicolon (";") before the if-else statement.
I have worked very hard creating tables in JSReport. I have created an adapted example for anyone who has the same needs as I did. The example is this (https://playground.jsreport.net/studio/workspace/HyqJPOI3x/21).
-
Ah, I can imagine this was easy to overlook. Glad you found it in the end.
Btw here you can find how to do code highlights for your next posts
https://help.github.com/articles/creating-and-highlighting-code-blocks/
-
Code highlighted ;-)
-
Hello:
One question more. Now, I want do a report with bars and lines in the graph. I am triying this (https://playground.jsreport.net/studio/workspace/HyqJPOI3x/23). Any ideas?
-
Try to click
Download
in theRun
context and open it in the excel. It will fix the xml and gives you protocol where you find what was wrong.