substituteAll: Interpolate values for all the sheets using the given substitutions (#173)

This commit is contained in:
Jonathan Keebler
2022-10-04 12:16:19 -04:00
committed by GitHub
parent 27f5fa8700
commit b379121e36
3 changed files with 37 additions and 0 deletions
+12
View File
@@ -223,6 +223,18 @@ module.exports = (function() {
}
};
/**
* Interpolate values for all the sheets using the given substitutions
* (an object).
*/
Workbook.prototype.substituteAll = function(substitutions) {
var self = this;
var sheets = self.loadSheets(self.prefix, self.workbook, self.workbookRels);
sheets.forEach(function(sheet) {
self.substitute(sheet.id, substitutions);
});
};
/**
* Interpolate values for the sheet with the given number (1-based) or
* name (if a string) using the given substitutions (an object).
+25
View File
@@ -1248,6 +1248,31 @@ describe("CRUD operations", function() {
done();
});
});
it("Each sheet should be replaced", function (done) {
fs.readFile(path.join(__dirname, 'templates', 'multple-sheet-substitution.xlsx'), function(err, data) {
expect(err).toBeNull();
// Create a template
var t = new XlsxTemplate(data);
t.substituteAll({
foo: "bar"
});
// Get binary data
var newData = t.generate();
var sharedStrings = etree.parse(t.archive.file("xl/sharedStrings.xml").asText()).getroot();
var sheet1 = etree.parse(t.archive.file("xl/worksheets/sheet1.xml").asText()).getroot();
var sheet2 = etree.parse(t.archive.file("xl/worksheets/sheet2.xml").asText()).getroot();
expect(sheet1).toBeDefined();
expect(sheet2).toBeDefined();
expect(getSharedString(sharedStrings, sheet1, "A1")).toEqual("bar");
expect(getSharedString(sharedStrings, sheet2, "A1")).toEqual("bar");
fs.writeFileSync('test/output/multple-sheet-substitution.xlsx', newData, 'binary');
done();
});
});
});
describe("Copy sheets", function() {
Binary file not shown.