mirror of
https://github.com/optilude/xlsx-template.git
synced 2026-07-02 00:17:39 +08:00
substituteAll: Interpolate values for all the sheets using the given substitutions (#173)
This commit is contained in:
@@ -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).
|
||||
|
||||
@@ -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.
Reference in New Issue
Block a user