diff --git a/src/index.js b/src/index.js index 707dd73..ec8ed12 100755 --- a/src/index.js +++ b/src/index.js @@ -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; diff --git a/test/crud-test.ts b/test/crud-test.ts index 3557a62..ed4822c 100644 --- a/test/crud-test.ts +++ b/test/crud-test.ts @@ -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(); + }); + }); + }); }); diff --git a/test/templates/custom-xml.xlsx b/test/templates/custom-xml.xlsx new file mode 100644 index 0000000..66bffa1 Binary files /dev/null and b/test/templates/custom-xml.xlsx differ