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();
|
2014-02-15 05:08:18 +00:00
|
|
|
var dir = parseCT(getzipdata(zip, '[Content_Types].xml'));
|
2014-01-28 16:38:02 +00:00
|
|
|
var xlsb = false;
|
2014-02-15 05:08:18 +00:00
|
|
|
var sheets;
|
2014-01-28 16:38:02 +00:00
|
|
|
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-14 06:25:46 +00:00
|
|
|
if(!opts.bookSheets && !opts.bookProps) {
|
2014-02-13 06:22:42 +00:00
|
|
|
strs = {};
|
2014-02-15 05:08:18 +00:00
|
|
|
if(dir.sst) strs=parse_sst(getzipdata(zip, dir.sst.replace(/^\//,'')), dir.sst, opts);
|
2014-02-13 06:22:42 +00:00
|
|
|
|
|
|
|
styles = {};
|
2014-03-23 21:30:00 +00:00
|
|
|
if(dir.style) styles = parse_sty(getzipdata(zip, dir.style.replace(/^\//,'')),dir.style, opts);
|
2014-02-13 06:22:42 +00:00
|
|
|
}
|
|
|
|
|
2014-02-15 05:08:18 +00:00
|
|
|
var wb = parse_wb(getzipdata(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 {
|
2014-02-15 05:08:18 +00:00
|
|
|
propdata = dir.coreprops.length !== 0 ? getzipdata(zip, dir.coreprops[0].replace(/^\//,'')) : "";
|
|
|
|
propdata += dir.extprops.length !== 0 ? getzipdata(zip, dir.extprops[0].replace(/^\//,'')) : "";
|
2014-02-04 00:00:44 +00:00
|
|
|
props = propdata !== "" ? parseProps(propdata) : {};
|
|
|
|
} catch(e) { }
|
2014-02-14 06:25:46 +00:00
|
|
|
|
2014-02-14 03:39:03 +00:00
|
|
|
var custprops = {};
|
2014-02-14 06:25:46 +00:00
|
|
|
if(!opts.bookSheets || opts.bookProps) {
|
2014-02-15 05:08:18 +00:00
|
|
|
if (dir.custprops.length !== 0) {
|
|
|
|
propdata = getzipdata(zip, dir.custprops[0].replace(/^\//,''), true);
|
|
|
|
if(propdata) custprops = parseCustomProps(propdata);
|
|
|
|
}
|
2014-02-14 03:39:03 +00:00
|
|
|
}
|
2014-02-13 06:22:42 +00:00
|
|
|
|
2014-02-14 06:25:46 +00:00
|
|
|
var out = {};
|
|
|
|
if(opts.bookSheets || opts.bookProps) {
|
|
|
|
if(props.Worksheets && props.SheetNames.length > 0) sheets=props.SheetNames;
|
|
|
|
else if(wb.Sheets) sheets = wb.Sheets.map(function(x){ return x.name; });
|
|
|
|
if(opts.bookProps) { out.Props = props; out.Custprops = custprops; }
|
|
|
|
if(typeof sheets !== 'undefined') out.SheetNames = sheets;
|
|
|
|
if(opts.bookSheets ? out.SheetNames : opts.bookProps) return out;
|
2014-02-13 06:22:42 +00:00
|
|
|
}
|
2014-02-15 05:08:18 +00:00
|
|
|
sheets = {};
|
2014-02-13 06:22:42 +00:00
|
|
|
|
2014-01-28 16:38:02 +00:00
|
|
|
var deps = {};
|
2014-03-29 22:53:15 +00:00
|
|
|
if(opts.bookDeps && dir.calcchain) deps=parse_cc(getzipdata(zip, dir.calcchain.replace(/^\//,'')),dir.calcchain,opts);
|
2014-02-15 05:08:18 +00:00
|
|
|
|
|
|
|
var i=0;
|
2014-01-28 16:38:02 +00:00
|
|
|
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;
|
|
|
|
}
|
2014-02-21 16:41:37 +00:00
|
|
|
}
|
|
|
|
/* Numbers iOS hack TODO: parse workbook rels to get names */
|
|
|
|
var nmode = (getzipdata(zip,"xl/worksheets/sheet.xml",true))?1:0;
|
|
|
|
for(i = 0; i != props.Worksheets; ++i) {
|
|
|
|
try {
|
|
|
|
//path = dir.sheets[i].replace(/^\//,'');
|
|
|
|
path = 'xl/worksheets/sheet'+(i+1-nmode)+(xlsb?'.bin':'.xml');
|
|
|
|
path = path.replace(/sheet0\./,"sheet.");
|
|
|
|
relsPath = path.replace(/^(.*)(\/)([^\/]*)$/, "$1/_rels/$3.rels");
|
|
|
|
sheets[props.SheetNames[i]]=parse_ws(getzipdata(zip, path),path,opts);
|
|
|
|
sheetRels[props.SheetNames[i]]=parseRels(getzipdata(zip, relsPath, true), path);
|
|
|
|
} catch(e) { if(opts.WTF) throw e; }
|
2014-01-28 16:38:02 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2014-02-17 08:44:22 +00:00
|
|
|
out = {
|
2014-01-28 16:38:02 +00:00
|
|
|
Directory: dir,
|
|
|
|
Workbook: wb,
|
|
|
|
Props: props,
|
2014-02-14 03:39:03 +00:00
|
|
|
Custprops: custprops,
|
2014-01-28 16:38:02 +00:00
|
|
|
Deps: deps,
|
|
|
|
Sheets: sheets,
|
|
|
|
SheetNames: props.SheetNames,
|
|
|
|
Strings: strs,
|
|
|
|
Styles: styles,
|
|
|
|
};
|
2014-02-17 08:44:22 +00:00
|
|
|
if(opts.bookFiles) {
|
2014-02-21 16:41:37 +00:00
|
|
|
out.keys = keys;
|
|
|
|
out.files = zip.files;
|
2014-02-17 08:44:22 +00:00
|
|
|
}
|
|
|
|
return out;
|
2014-01-28 16:38:02 +00:00
|
|
|
}
|