Fix special case of array substitution

Fix #47
This commit is contained in:
Andrey Kyrdyumov
2017-10-01 02:38:17 +03:00
parent 26198d7637
commit f8a6f94c79
4 changed files with 32 additions and 3 deletions
+8 -3
View File
@@ -182,7 +182,7 @@ module.exports = (function() {
// Only substitute things for which we have a substitution
var substitution = _get(substitutions, placeholder.name, ''),
newCellsInserted = 0;
//console.log(substitutions, substitution, placeholder);
if(placeholder.full && placeholder.type === "table" && substitution instanceof Array) {
newCellsInserted = self.substituteTable(
row, newTableRows,
@@ -190,9 +190,14 @@ module.exports = (function() {
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) {
appendCell = false; // don't double-insert cells
cellsInserted += newCellsInserted;
self.pushRight(self.workbook, sheet.root, cell.attrib.r, newCellsInserted);
}
@@ -709,7 +714,7 @@ module.exports = (function() {
var newCellsInserted = -1, // we technically delete one before we start adding back
currentCell = cell.attrib.r;
// add a cell for each element in the list
// add a cell for each element in the list
substitution.forEach(function(element) {
++newCellsInserted;
+24
View File
@@ -637,6 +637,30 @@ describe("CRUD operations", function() {
done();
});
});
it("Arrays with single element", function(done) {
fs.readFile(path.join(__dirname, 'templates', 'test-nested-arrays.xlsx'), function(err, data) {
buster.expect(err).toBeNull();
var t = new XlsxTemplate(data);
var data = { "sales": [ { "payments": [123], } ] };
t.substitute(1, data);
var newData = t.generate();
var sharedStrings = etree.parse(t.archive.file("xl/sharedStrings.xml").asText()).getroot(),
sheet1 = etree.parse(t.archive.file("xl/worksheets/sheet1.xml").asText()).getroot();
buster.expect(sheet1).toBeDefined();
var a1 = sheet1.find("./sheetData/row/c[@r='A1']/v");
var firstElement = sheet1.findall("./sheetData/row/c[@r='A1']");
buster.expect(a1).not.toBeNull();
buster.expect(a1.text).toEqual("123");
buster.expect(firstElement).not.toBeNull();
buster.expect(firstElement.length).toEqual(1);
fs.writeFileSync('test/output/test-nested-arrays.xlsx', newData, 'binary');
done();
});
});
});
});
Binary file not shown.
Binary file not shown.