2014-02-07 10:53:40 +00:00
|
|
|
function parseZip(zip, opts) {
|
|
|
|
opts = opts || {};
|
|
|
|
fixopts(opts);
|
2014-02-05 13:39:21 +00:00
|
|
|
reset_cp();
|
2014-01-28 16:38:02 +00:00
|
|
|
var entries = Object.keys(zip.files);
|
|
|
|
var keys = entries.filter(function(x){return x.substr(-1) != '/';}).sort();
|
|
|
|
var dir = parseCT(getdata(getzipfile(zip, '[Content_Types].xml')));
|
|
|
|
var xlsb = false;
|
|
|
|
if(dir.workbooks.length === 0) {
|
|
|
|
var binname = "xl/workbook.bin";
|
|
|
|
if(!getzipfile(zip,binname)) throw new Error("Could not find workbook entry");
|
|
|
|
dir.workbooks.push(binname);
|
|
|
|
xlsb = true;
|
|
|
|
}
|
|
|
|
|
2014-02-13 06:22:42 +00:00
|
|
|
if(!opts.bookSheets) {
|
|
|
|
strs = {};
|
|
|
|
if(dir.sst) strs=parse_sst(getdata(getzipfile(zip, dir.sst.replace(/^\//,''))), dir.sst, opts);
|
|
|
|
|
|
|
|
styles = {};
|
|
|
|
if(dir.style) styles = parse_sty(getdata(getzipfile(zip, dir.style.replace(/^\//,''))),dir.style);
|
|
|
|
}
|
|
|
|
|
|
|
|
var wb = parse_wb(getdata(getzipfile(zip, dir.workbooks[0].replace(/^\//,''))), dir.workbooks[0], opts);
|
2014-01-28 16:38:02 +00:00
|
|
|
|
2014-02-04 00:00:44 +00:00
|
|
|
var props = {}, propdata = "";
|
|
|
|
try {
|
|
|
|
propdata = dir.coreprops.length !== 0 ? getdata(getzipfile(zip, dir.coreprops[0].replace(/^\//,''))) : "";
|
2014-01-28 16:38:02 +00:00
|
|
|
propdata += dir.extprops.length !== 0 ? getdata(getzipfile(zip, dir.extprops[0].replace(/^\//,''))) : "";
|
2014-02-04 00:00:44 +00:00
|
|
|
props = propdata !== "" ? parseProps(propdata) : {};
|
|
|
|
} catch(e) { }
|
2014-02-13 06:22:42 +00:00
|
|
|
|
|
|
|
if(opts.bookSheets) {
|
|
|
|
if(props.Worksheets && props.SheetNames.length > 0) return { SheetNames:props.SheetNames };
|
|
|
|
else if(wb.Sheets) return { SheetNames:wb.Sheets.map(function(x) { return x.name; }) };
|
|
|
|
}
|
|
|
|
|
2014-01-28 16:38:02 +00:00
|
|
|
var deps = {};
|
|
|
|
if(dir.calcchain) deps=parseDeps(getdata(getzipfile(zip, dir.calcchain.replace(/^\//,''))));
|
|
|
|
var sheets = {}, i=0;
|
|
|
|
var sheetRels = {};
|
2014-01-29 06:00:09 +00:00
|
|
|
var path, relsPath;
|
2014-01-28 16:38:02 +00:00
|
|
|
if(!props.Worksheets) {
|
|
|
|
/* Google Docs doesn't generate the appropriate metadata, so we impute: */
|
|
|
|
var wbsheets = wb.Sheets;
|
|
|
|
props.Worksheets = wbsheets.length;
|
|
|
|
props.SheetNames = [];
|
|
|
|
for(var j = 0; j != wbsheets.length; ++j) {
|
|
|
|
props.SheetNames[j] = wbsheets[j].name;
|
|
|
|
}
|
|
|
|
for(i = 0; i != props.Worksheets; ++i) {
|
|
|
|
try { /* TODO: remove these guards */
|
2014-01-29 06:00:09 +00:00
|
|
|
path = 'xl/worksheets/sheet' + (i+1) + (xlsb?'.bin':'.xml');
|
|
|
|
relsPath = path.replace(/^(.*)(\/)([^\/]*)$/, "$1/_rels/$3.rels");
|
2014-02-07 10:53:40 +00:00
|
|
|
sheets[props.SheetNames[i]]=parse_ws(getdata(getzipfile(zip, path)),path,opts);
|
2014-01-28 16:38:02 +00:00
|
|
|
sheetRels[props.SheetNames[i]]=parseRels(getdata(getzipfile(zip, relsPath)), path);
|
|
|
|
} catch(e) {}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for(i = 0; i != props.Worksheets; ++i) {
|
|
|
|
try {
|
|
|
|
//var path = dir.sheets[i].replace(/^\//,'');
|
2014-01-29 06:00:09 +00:00
|
|
|
path = 'xl/worksheets/sheet' + (i+1) + (xlsb?'.bin':'.xml');
|
|
|
|
relsPath = path.replace(/^(.*)(\/)([^\/]*)$/, "$1/_rels/$3.rels");
|
2014-02-07 10:53:40 +00:00
|
|
|
sheets[props.SheetNames[i]]=parse_ws(getdata(getzipfile(zip, path)),path,opts);
|
2014-01-28 16:38:02 +00:00
|
|
|
sheetRels[props.SheetNames[i]]=parseRels(getdata(getzipfile(zip, relsPath)), path);
|
|
|
|
} catch(e) {/*console.error(e);*/}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-12 06:09:42 +00:00
|
|
|
if(dir.comments) parse_comments(zip, dir.comments, sheets, sheetRels, opts);
|
2014-01-28 16:38:02 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
Directory: dir,
|
|
|
|
Workbook: wb,
|
|
|
|
Props: props,
|
|
|
|
Deps: deps,
|
|
|
|
Sheets: sheets,
|
|
|
|
SheetNames: props.SheetNames,
|
|
|
|
Strings: strs,
|
|
|
|
Styles: styles,
|
|
|
|
keys: keys,
|
|
|
|
files: zip.files
|
|
|
|
};
|
|
|
|
}
|