xlsx-template
xlsx-template copied to clipboard
Replacing with a 1 cell-sized array not working?
Hi,
First, thanks for the project, it's great :)
I'm trying to substitute a placeholder to put an array, but i've been in trouble when inserting a 1 cell array. Here is an exemple : My excel template is just : | ${table:sales.payments} | My code :
var tpl = fs.readFileSync('tpl.xlsx');
tpl = new xls(tpl);
tpl.substitute(1, data);
const result = tpl.generate();
The strange thing is, with data = { "sales": [ { "payments": [123,456], } ] };
, it works perfectly, but with data = { "sales": [ { "payments": [666], } ] };
, it does not replace the placeholder (${table:sales.payments} is written in the output file).
It also works for data = { "sales": [ { "payments": 123, } ] };
Is this a normal behaviour ? It's pretty annoying to have to check the length of my arrays and convert them when their length is 1...
Thanks !
For start - Sorry for long delay. Original author was on vacation and only now I accept his invitation.
This is definitely an issue. I will take a look at it and try to reproduce.
@tsc-aymeric Could you try xlsx-template@next
and use ${table:sales[0].payments}
inside your templates. I recently add ability to use array indexer for simple cases, but did not specifically check your case. Possible that it will work, but I don't strictly test for such scenario.
Let me know how it is goes.
Hey, thanks for the attention you gave to my issue. :-)
I can't test right now but i'll do it as soon as possible. However, the problem I had was my array does not have a fixed size. It's generally larger than 1 cell, and there is no problem is such cases. But it happens that its size is 1, and then the substitution is not made. So I can't change my template to ${table:sales[0].payments}
with my problem.
I think I finish with your issue @tac-aymeric , could you please try npm i xlxs-template@next
. Hopefully it will fix your issue since I reproduce your problem.
I ran into the same error, in my project (data representation is quite different). In most cases, database query return more than one row. So, table substitution works fine. But if query returns exactly one row, placeholders rewrites with empty values, and generated report shows nothing. xlxs-template@next dont fix this. My report data looks like:
var data = {
t1:[
{ ROWNUM: 1, C_10: '27.10.2017', C_5: 85000000, DATE_BEGIN: '27.10.2017', PRC: 8 }
]
}
and I use this temporary solution to fix this bug
if (data.t1.length === 1)
data.t1.push({})
UPD: there is no such error in older version "version": "0.0.3"
Similar issue here, created a new ticket for this! #71
I tried to examine the source code and found the condition that leads to this bug.
if(placeholder.full && placeholder.type === "table" && substitution instanceof Array) {
newCellsInserted = self.substituteTable(
row, newTableRows,
cells, cell,
namedTables, substitution, placeholder.key
);
// don't double-insert cells
if (newCellsInserted !== 0
|| substitution.length <= 1) {
appendCell = false;
}
// Did we insert new columns (array values)?
if(newCellsInserted !== 0) {
cellsInserted += newCellsInserted;
self.pushRight(self.workbook, sheet.root, cell.attrib.r, newCellsInserted);
}
}
here it must be replaced by substitution.length <= 0
// don't double-insert cells
if (newCellsInserted !== 0
|| substitution.length <= 0) {
appendCell = false;
}
I do not know how this will affect the rest of the functionality, but it is necessary that the developer looked!!
@Sedjj We've been running a forked version with that same fix for a few months now without issues.