mirror of
https://github.com/optilude/xlsx-template.git
synced 2026-07-02 08:27:39 +08:00
Merge pull request #69 from optilude/bug-64
Do not replace text in the shared strings
This commit is contained in:
@@ -746,10 +746,6 @@ module.exports = (function() {
|
||||
Workbook.prototype.substituteScalar = function(cell, string, placeholder, substitution) {
|
||||
var self = this;
|
||||
|
||||
if(placeholder.full && typeof(substitution) === "string") {
|
||||
self.replaceString(string, substitution);
|
||||
}
|
||||
|
||||
if(placeholder.full) {
|
||||
return self.insertCellValue(cell, substitution);
|
||||
} else {
|
||||
|
||||
@@ -662,5 +662,37 @@ describe("CRUD operations", function() {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("Multiple sheets", function() {
|
||||
it("Each sheet should take each name", function (done) {
|
||||
fs.readFile(path.join(__dirname, 'templates', 'multple-sheets-arrays.xlsx'), function(err, data) {
|
||||
buster.expect(err).toBeNull();
|
||||
|
||||
// Create a template
|
||||
var t = new XlsxTemplate(data);
|
||||
for (let sheetNumber = 1; sheetNumber <= 2; sheetNumber++) {
|
||||
// Set up some placeholder values matching the placeholders in the template
|
||||
var values = {
|
||||
page: 'page: ' + sheetNumber
|
||||
};
|
||||
|
||||
// Perform substitution
|
||||
t.substitute(sheetNumber, values);
|
||||
}
|
||||
|
||||
// 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();
|
||||
buster.expect(sheet1).toBeDefined();
|
||||
buster.expect(sheet2).toBeDefined();
|
||||
buster.expect(getSharedString(sharedStrings, sheet1, "A1")).toEqual("page: 1");
|
||||
buster.expect(getSharedString(sharedStrings, sheet2, "A1")).toEqual("page: 2");
|
||||
|
||||
fs.writeFileSync('test/output/multple-sheets-arrays.xlsx', newData, 'binary');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
+33
-2
@@ -467,8 +467,39 @@ describe("Helpers", function() {
|
||||
t.addSharedString(string);
|
||||
buster.expect(t.substituteScalar(col, string, placeholder, substitution)).toEqual("bar");
|
||||
buster.expect(col.attrib.t).toEqual("s");
|
||||
buster.expect(String(val.text)).toEqual("0");
|
||||
buster.expect(t.sharedStrings).toEqual(["bar"]);
|
||||
buster.expect(String(val.text)).toEqual("1");
|
||||
buster.expect(t.sharedStrings).toEqual(["${foo}", "bar"]);
|
||||
});
|
||||
|
||||
it("Substitution of shared simple string values", function() {
|
||||
var t = new XlsxTemplate(),
|
||||
string = "${foo}",
|
||||
substitution = "bar",
|
||||
placeholder = {
|
||||
full: true,
|
||||
key: undefined,
|
||||
name: "foo",
|
||||
placeholder: "${foo}",
|
||||
type: "normal"
|
||||
},
|
||||
val = {
|
||||
text: "0"
|
||||
},
|
||||
col = {
|
||||
attrib: {t: "s"},
|
||||
find: function() {
|
||||
return val;
|
||||
}
|
||||
};
|
||||
|
||||
t.addSharedString(string);
|
||||
buster.expect(t.substituteScalar(col, string, placeholder, substitution)).toEqual("bar");
|
||||
|
||||
// Explicitly share substritution strings if they could be reused.
|
||||
buster.expect(t.substituteScalar(col, string, placeholder, substitution)).toEqual("bar");
|
||||
buster.expect(col.attrib.t).toEqual("s");
|
||||
buster.expect(String(val.text)).toEqual("1");
|
||||
buster.expect(t.sharedStrings).toEqual(["${foo}", "bar"]);
|
||||
});
|
||||
|
||||
it("can substitute simple numeric values", function() {
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user