Update index.js

Add "deleteSheet" function
This commit is contained in:
SkyKilla
2016-08-30 17:10:27 +07:00
committed by GitHub
parent 7a75b769c8
commit ddf79ca02a
+30
View File
@@ -30,6 +30,36 @@ module.exports = (function() {
}
};
/**
* Delete unused sheets if needed
*/
Workbook.prototype.deleteSheet = function(sheetName){
var self = this;
var sheet = self.loadSheet(sheetName);
Object.keys(self.archive.files).forEach(key=>{
if (key.indexOf(sheet.id) != -1)
delete self.archive.files[key];
})
var i = self.sheets.findIndex(el=> el.id == sheet.id);
self.sheets.splice(i, 1);
var el = self.workbook._children.find(item=>item.tag==='sheets');
if (!el) throw new Error("Template file contain errors");
// sheet.rId = el._children[i].attrib['r:id'];
el._children.splice(i,1);
el = self.workbook._children.find(item=>item.tag==='definedNames');
if (el) {
el._children.splice(i,1);
el._children.forEach((item, index)=>{item.attrib.localSheetId = String(index)}) // fix to avoid opening errors
}
self.archive.file(sheet.filename, etree.tostring(sheet.root));
self.archive.file(self.workbookPath, etree.tostring(self.workbook));
};
/**
* Load a .xlsx file from a byte array.
*/