add customXml in the order list for rebuild (#154)

* add customXml in the order list for rebuild to prevent error on remove sheet

* Remove the useless customXML type in order list ; in the sort function of rebuild, if the type of Relationship is not in the `order` list, go to the end ; adding test code
This commit is contained in:
Jonathan Durand
2022-05-29 20:22:33 +02:00
committed by GitHub
parent a4b42e4985
commit c514b3b0d4
3 changed files with 36 additions and 0 deletions
+6
View File
@@ -147,6 +147,12 @@ module.exports = (function() {
.sort(function(rel1, rel2){ //using order
var index1 = order.indexOf( path.basename(rel1.attrib.Type) );
var index2 = order.indexOf( path.basename(rel2.attrib.Type) );
// If the attrib.Type is not in the order list, go to the end of sort
// Maybe we can do it more gracefully with the boolean operator
if ( index1 < 0 && index2 >= 0 ) return 1; // rel1 go after rel2
if ( index1 >= 0 && index2 < 0 ) return -1; // rel1 go before rel2
if ( index1 < 0 && index2 < 0 ) return 0; // change nothing
if ((index1 + index2) == 0) {
if(rel1.attrib.Id && rel2.attrib.Id) return rel1.attrib.Id.substring(3) - rel2.attrib.Id.substring(3);
return rel1._id - rel2._id;
+30
View File
@@ -1243,4 +1243,34 @@ describe("CRUD operations", function() {
});
});
});
describe("Rebuild file", function() {
it("Rebuild archive file with custom Relationship workbook rels after delete a sheet", function (done) {
fs.readFile(path.join(__dirname, 'templates', 'custom-xml.xlsx'), function(err, buffer) {
expect(err).toBeNull();
// Create a template
var t = new XlsxTemplate(buffer);
t.deleteSheet("Sheet2");
var newData = t.generate();
var wbrels = etree.parse(t.archive.file("xl/_rels/workbook.xml.rels").asText()).getroot();
expect(wbrels.find("Relationship[@Id='rId1']").attrib.Target).toEqual("worksheets/sheet1.xml");
expect(wbrels.find("Relationship[@Id='rId1']").attrib.Type).toEqual("http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet");
expect(wbrels.find("Relationship[@Id='rId2']").attrib.Target).toEqual("worksheets/sheet3.xml");
expect(wbrels.find("Relationship[@Id='rId2']").attrib.Type).toEqual("http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet");
expect(wbrels.find("Relationship[@Id='rId3']").attrib.Target).toEqual("theme/theme1.xml");
expect(wbrels.find("Relationship[@Id='rId3']").attrib.Type).toEqual("http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme");
expect(wbrels.find("Relationship[@Id='rId4']").attrib.Target).toEqual("styles.xml");
expect(wbrels.find("Relationship[@Id='rId4']").attrib.Type).toEqual("http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles");
expect(wbrels.find("Relationship[@Id='rId5']").attrib.Target).toEqual("sharedStrings.xml");
expect(wbrels.find("Relationship[@Id='rId5']").attrib.Type).toEqual("http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings");
expect(wbrels.find("Relationship[@Id='rId6']").attrib.Target).toEqual("xmlMaps.xml");
expect(wbrels.find("Relationship[@Id='rId6']").attrib.Type).toEqual("http://schemas.openxmlformats.org/officeDocument/2006/relationships/xmlMaps");
expect(wbrels.find("Relationship[@Id='rId7']").attrib.Target).toEqual("connections.xml");
expect(wbrels.find("Relationship[@Id='rId7']").attrib.Type).toEqual("http://schemas.openxmlformats.org/officeDocument/2006/relationships/connections");
fs.writeFileSync('test/output/custom-xml.xlsx', newData, 'binary');
done();
});
});
});
});
Binary file not shown.