Merge pull request #4 from cartuchogl/descendants

Partial support for descendants properties ...
This commit is contained in:
Andrey Kurdyumov
2016-11-04 15:55:44 +06:00
committed by GitHub
3 changed files with 143 additions and 6 deletions
+20 -6
View File
@@ -30,6 +30,21 @@ module.exports = (function() {
}
};
/**
* Based on http://stackoverflow.com/questions/8051975
* Mimic https://lodash.com/docs#get
*/
var _get = function(obj, desc, defaultValue) {
var arr = desc.split('.');
try {
while (arr.length && (obj = obj[arr.shift()])) {}
} catch(ex) {
/* invalid chain */
obj = undefined;
}
return obj === undefined ? defaultValue : obj;
}
/**
* Delete unused sheets if needed
*/
@@ -155,12 +170,8 @@ module.exports = (function() {
self.extractPlaceholders(string).forEach(function(placeholder) {
// Only substitute things for which we have a substitution
var substitution = substitutions[placeholder.name],
var substitution = _get(substitutions, placeholder.name, ''),
newCellsInserted = 0;
if(substitution === undefined) {
//return;
substitution = '';
}
if(placeholder.full && placeholder.type === "table" && substitution instanceof Array) {
newCellsInserted = self.substituteTable(
@@ -186,6 +197,9 @@ module.exports = (function() {
self.pushRight(self.workbook, sheet.root, cell.attrib.r, newCellsInserted);
}
} else {
if (placeholder.key) {
substitution = _get(substitutions, placeholder.name + '.' + placeholder.key);
}
string = self.substituteScalar(cell, string, placeholder, substitution);
}
});
@@ -749,7 +763,7 @@ module.exports = (function() {
var newRow, newCell,
newCellsInsertedOnNewRow = 0,
newCells = [],
value = element[key];
value = _get(element, key, '');
if(idx === 0) { // insert in the row where the placeholders are
+123
View File
@@ -181,6 +181,129 @@ describe("CRUD operations", function() {
});
it("can substitute values with descendant properties and generate a file", function(done) {
fs.readFile(path.join(__dirname, 'templates', 't2.xlsx'), function(err, data) {
buster.expect(err).toBeNull();
var t = new XlsxTemplate(data);
t.substitute(1, {
demo: { extractDate: new Date("2013-01-02") },
revision: 10,
dates: [new Date("2013-01-01"), new Date("2013-01-02"), new Date("2013-01-03")],
planData: [
{
name: "John Smith",
role: { name: "Developer" },
days: [8, 8, 4]
}, {
name: "James Smith",
role: { name: "Analyst" },
days: [4, 4, 4]
}, {
name: "Jim Smith",
role: { name: "Manager" },
days: [4, 4, 4]
}
]
});
var newData = t.generate(),
archive = new zip(newData, {base64: false, checkCRC32: true});
var sharedStrings = etree.parse(t.archive.file("xl/sharedStrings.xml").asText()).getroot(),
sheet1 = etree.parse(t.archive.file("xl/worksheets/sheet1.xml").asText()).getroot();
// extract date placeholder - interpolated into string referenced at B4
buster.expect(sheet1.find("./sheetData/row/c[@r='B4']").attrib.t).toEqual("s");
buster.expect(
sharedStrings.findall("./si")[
parseInt(sheet1.find("./sheetData/row/c[@r='B4']/v").text, 10)
].find("t").text
).toEqual("Extracted on 41276");
// revision placeholder - cell C4 changed from string to number
buster.expect(sheet1.find("./sheetData/row/c[@r='C4']").attrib.t).toEqual("n");
buster.expect(sheet1.find("./sheetData/row/c[@r='C4']/v").text).toEqual("10");
// dates placeholder - added cells
buster.expect(sheet1.find("./sheetData/row/c[@r='D6']").attrib.t).toEqual("d");
buster.expect(sheet1.find("./sheetData/row/c[@r='E6']").attrib.t).toEqual("d");
buster.expect(sheet1.find("./sheetData/row/c[@r='F6']").attrib.t).toEqual("d");
buster.expect(sheet1.find("./sheetData/row/c[@r='D6']/v").text).toEqual("41275");
buster.expect(sheet1.find("./sheetData/row/c[@r='E6']/v").text).toEqual("41276");
buster.expect(sheet1.find("./sheetData/row/c[@r='F6']/v").text).toEqual("41277");
// planData placeholder - added rows and cells
buster.expect(sheet1.find("./sheetData/row/c[@r='B7']").attrib.t).toEqual("s");
buster.expect(
sharedStrings.findall("./si")[
parseInt(sheet1.find("./sheetData/row/c[@r='B7']/v").text, 10)
].find("t").text
).toEqual("John Smith");
buster.expect(sheet1.find("./sheetData/row/c[@r='B8']").attrib.t).toEqual("s");
buster.expect(
sharedStrings.findall("./si")[
parseInt(sheet1.find("./sheetData/row/c[@r='B8']/v").text, 10)
].find("t").text
).toEqual("James Smith");
buster.expect(sheet1.find("./sheetData/row/c[@r='B9']").attrib.t).toEqual("s");
buster.expect(
sharedStrings.findall("./si")[
parseInt(sheet1.find("./sheetData/row/c[@r='B9']/v").text, 10)
].find("t").text
).toEqual("Jim Smith");
buster.expect(sheet1.find("./sheetData/row/c[@r='C7']").attrib.t).toEqual("s");
buster.expect(
sharedStrings.findall("./si")[
parseInt(sheet1.find("./sheetData/row/c[@r='C7']/v").text, 10)
].find("t").text
).toEqual("Developer");
buster.expect(sheet1.find("./sheetData/row/c[@r='C8']").attrib.t).toEqual("s");
buster.expect(
sharedStrings.findall("./si")[
parseInt(sheet1.find("./sheetData/row/c[@r='C8']/v").text, 10)
].find("t").text
).toEqual("Analyst");
buster.expect(sheet1.find("./sheetData/row/c[@r='C9']").attrib.t).toEqual("s");
buster.expect(
sharedStrings.findall("./si")[
parseInt(sheet1.find("./sheetData/row/c[@r='C9']/v").text, 10)
].find("t").text
).toEqual("Manager");
buster.expect(sheet1.find("./sheetData/row/c[@r='D7']").attrib.t).toEqual("n");
buster.expect(sheet1.find("./sheetData/row/c[@r='D7']/v").text).toEqual("8");
buster.expect(sheet1.find("./sheetData/row/c[@r='D8']").attrib.t).toEqual("n");
buster.expect(sheet1.find("./sheetData/row/c[@r='D8']/v").text).toEqual("4");
buster.expect(sheet1.find("./sheetData/row/c[@r='D9']").attrib.t).toEqual("n");
buster.expect(sheet1.find("./sheetData/row/c[@r='D9']/v").text).toEqual("4");
buster.expect(sheet1.find("./sheetData/row/c[@r='E7']").attrib.t).toEqual("n");
buster.expect(sheet1.find("./sheetData/row/c[@r='E7']/v").text).toEqual("8");
buster.expect(sheet1.find("./sheetData/row/c[@r='E8']").attrib.t).toEqual("n");
buster.expect(sheet1.find("./sheetData/row/c[@r='E8']/v").text).toEqual("4");
buster.expect(sheet1.find("./sheetData/row/c[@r='E9']").attrib.t).toEqual("n");
buster.expect(sheet1.find("./sheetData/row/c[@r='E9']/v").text).toEqual("4");
buster.expect(sheet1.find("./sheetData/row/c[@r='F7']").attrib.t).toEqual("n");
buster.expect(sheet1.find("./sheetData/row/c[@r='F7']/v").text).toEqual("4");
buster.expect(sheet1.find("./sheetData/row/c[@r='F8']").attrib.t).toEqual("n");
buster.expect(sheet1.find("./sheetData/row/c[@r='F8']/v").text).toEqual("4");
buster.expect(sheet1.find("./sheetData/row/c[@r='F9']").attrib.t).toEqual("n");
buster.expect(sheet1.find("./sheetData/row/c[@r='F9']/v").text).toEqual("4");
// XXX: For debugging only
// fs.writeFileSync('test.xlsx', newData, 'binary');
done();
});
});
it("moves columns left or right when filling lists", function(done) {
fs.readFile(path.join(__dirname, 'templates', 'test-cols.xlsx'), function(err, data) {
Binary file not shown.