2014-06-05 07:06:20 +00:00
|
|
|
function safe_parse_wbrels(wbrels, sheets) {
|
|
|
|
if(!wbrels) return 0;
|
|
|
|
try {
|
2014-06-29 18:29:45 +00:00
|
|
|
wbrels = sheets.map(function pwbr(w) { return [w.name, wbrels['!id'][w.id].Target]; });
|
2014-06-05 07:06:20 +00:00
|
|
|
} catch(e) { return null; }
|
|
|
|
return !wbrels || wbrels.length === 0 ? null : wbrels;
|
|
|
|
}
|
|
|
|
|
2017-02-10 19:23:01 +00:00
|
|
|
function safe_parse_ws(zip, path/*:string*/, relsPath/*:string*/, sheet, sheetRels, sheets, opts) {
|
2014-06-05 07:06:20 +00:00
|
|
|
try {
|
2017-02-10 19:23:01 +00:00
|
|
|
sheetRels[sheet]=parse_rels(getzipstr(zip, relsPath, true), path);
|
2014-06-05 07:06:20 +00:00
|
|
|
sheets[sheet]=parse_ws(getzipdata(zip, path),path,opts,sheetRels[sheet]);
|
|
|
|
} catch(e) { if(opts.WTF) throw e; }
|
|
|
|
}
|
|
|
|
|
2017-02-10 19:23:01 +00:00
|
|
|
var nodirs = function nodirs(x/*:string*/)/*:boolean*/{return x.substr(-1) != '/';};
|
|
|
|
function parse_zip(zip/*:ZIP*/, opts/*:?ParseOpts*/)/*:Workbook*/ {
|
2014-05-16 00:33:34 +00:00
|
|
|
make_ssf(SSF);
|
2014-02-07 10:53:40 +00:00
|
|
|
opts = opts || {};
|
2014-05-16 00:33:34 +00:00
|
|
|
fix_read_opts(opts);
|
2014-02-05 13:39:21 +00:00
|
|
|
reset_cp();
|
2014-10-10 02:22:38 +00:00
|
|
|
|
|
|
|
/* OpenDocument Part 3 Section 2.2.1 OpenDocument Package */
|
|
|
|
if(safegetzipfile(zip, 'META-INF/manifest.xml')) return parse_ods(zip, opts);
|
|
|
|
|
2014-06-29 18:29:45 +00:00
|
|
|
var entries = keys(zip.files).filter(nodirs).sort();
|
2017-02-10 19:23:01 +00:00
|
|
|
var dir = parse_ct((getzipstr(zip, '[Content_Types].xml')/*:?any*/), opts);
|
2014-01-28 16:38:02 +00:00
|
|
|
var xlsb = false;
|
2014-04-03 22:51:54 +00:00
|
|
|
var sheets, binname;
|
2014-01-28 16:38:02 +00:00
|
|
|
if(dir.workbooks.length === 0) {
|
2014-04-03 22:51:54 +00:00
|
|
|
binname = "xl/workbook.xml";
|
|
|
|
if(getzipdata(zip,binname, true)) dir.workbooks.push(binname);
|
|
|
|
}
|
|
|
|
if(dir.workbooks.length === 0) {
|
|
|
|
binname = "xl/workbook.bin";
|
|
|
|
if(!getzipfile(zip,binname,true)) throw new Error("Could not find workbook");
|
2014-01-28 16:38:02 +00:00
|
|
|
dir.workbooks.push(binname);
|
|
|
|
xlsb = true;
|
|
|
|
}
|
2014-05-29 22:30:03 +00:00
|
|
|
if(dir.workbooks[0].substr(-3) == "bin") xlsb = true;
|
2014-05-22 12:16:51 +00:00
|
|
|
if(xlsb) set_cp(1200);
|
2014-01-28 16:38:02 +00:00
|
|
|
|
2014-02-14 06:25:46 +00:00
|
|
|
if(!opts.bookSheets && !opts.bookProps) {
|
2014-05-16 00:33:34 +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-05-29 06:18:23 +00:00
|
|
|
|
|
|
|
themes = {};
|
2014-06-05 07:06:20 +00:00
|
|
|
if(opts.cellStyles && dir.themes.length) themes = parse_theme(getzipdata(zip, dir.themes[0].replace(/^\//,''), true),dir.themes[0], 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 = "";
|
2014-05-16 00:33:34 +00:00
|
|
|
|
|
|
|
if(dir.coreprops.length !== 0) {
|
2017-02-10 19:23:01 +00:00
|
|
|
propdata = getzipstr(zip, dir.coreprops[0].replace(/^\//,''), true);
|
2014-05-16 00:33:34 +00:00
|
|
|
if(propdata) props = parse_core_props(propdata);
|
|
|
|
if(dir.extprops.length !== 0) {
|
2017-02-10 19:23:01 +00:00
|
|
|
propdata = getzipstr(zip, dir.extprops[0].replace(/^\//,''), true);
|
2014-05-16 00:33:34 +00:00
|
|
|
if(propdata) parse_ext_props(propdata, props);
|
|
|
|
}
|
|
|
|
}
|
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) {
|
2017-02-10 19:23:01 +00:00
|
|
|
propdata = getzipstr(zip, dir.custprops[0].replace(/^\//,''), true);
|
2014-05-16 00:33:34 +00:00
|
|
|
if(propdata) custprops = parse_cust_props(propdata, opts);
|
2014-02-15 05:08:18 +00:00
|
|
|
}
|
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;
|
2014-06-29 18:29:45 +00:00
|
|
|
else if(wb.Sheets) sheets = wb.Sheets.map(function pluck(x){ return x.name; });
|
2014-02-14 06:25:46 +00:00
|
|
|
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;
|
2017-02-10 19:23:01 +00:00
|
|
|
var sheetRels = ({}/*:any*/);
|
2014-01-29 06:00:09 +00:00
|
|
|
var path, relsPath;
|
2014-01-28 16:38:02 +00:00
|
|
|
if(!props.Worksheets) {
|
|
|
|
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
|
|
|
}
|
2014-05-22 12:16:51 +00:00
|
|
|
|
2014-05-29 22:30:03 +00:00
|
|
|
var wbext = xlsb ? "bin" : "xml";
|
|
|
|
var wbrelsfile = 'xl/_rels/workbook.' + wbext + '.rels';
|
2017-02-10 19:23:01 +00:00
|
|
|
var wbrels = parse_rels(getzipstr(zip, wbrelsfile, true), wbrelsfile);
|
2014-06-05 07:06:20 +00:00
|
|
|
if(wbrels) wbrels = safe_parse_wbrels(wbrels, wb.Sheets);
|
2014-05-22 12:16:51 +00:00
|
|
|
/* Numbers iOS hack */
|
2014-02-21 16:41:37 +00:00
|
|
|
var nmode = (getzipdata(zip,"xl/worksheets/sheet.xml",true))?1:0;
|
|
|
|
for(i = 0; i != props.Worksheets; ++i) {
|
2017-02-10 19:23:01 +00:00
|
|
|
if(wbrels && wbrels[i]) path = 'xl/' + (wbrels[i][1]).replace(/[\/]?xl\//, "");
|
2014-06-05 07:06:20 +00:00
|
|
|
else {
|
|
|
|
path = 'xl/worksheets/sheet'+(i+1-nmode)+"." + wbext;
|
|
|
|
path = path.replace(/sheet0\./,"sheet.");
|
|
|
|
}
|
|
|
|
relsPath = path.replace(/^(.*)(\/)([^\/]*)$/, "$1/_rels/$3.rels");
|
|
|
|
safe_parse_ws(zip, path, relsPath, props.SheetNames[i], sheetRels, sheets, opts);
|
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
|
|
|
|
2017-02-10 19:23:01 +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-05-29 06:18:23 +00:00
|
|
|
Themes: themes,
|
2014-05-16 00:33:34 +00:00
|
|
|
SSF: SSF.get_table()
|
2017-02-10 19:23:01 +00:00
|
|
|
}/*:any*/);
|
2014-02-17 08:44:22 +00:00
|
|
|
if(opts.bookFiles) {
|
2014-05-16 00:33:34 +00:00
|
|
|
out.keys = entries;
|
2014-02-21 16:41:37 +00:00
|
|
|
out.files = zip.files;
|
2014-02-17 08:44:22 +00:00
|
|
|
}
|
2014-04-03 22:51:54 +00:00
|
|
|
if(opts.bookVBA) {
|
|
|
|
if(dir.vba.length > 0) out.vbaraw = getzipdata(zip,dir.vba[0],true);
|
2017-02-10 19:23:01 +00:00
|
|
|
else if(dir.defaults && dir.defaults.bin === 'application/vnd.ms-office.vbaProject') out.vbaraw = getzipdata(zip,'xl/vbaProject.bin',true);
|
2014-04-03 22:51:54 +00:00
|
|
|
}
|
2014-02-17 08:44:22 +00:00
|
|
|
return out;
|
2014-01-28 16:38:02 +00:00
|
|
|
}
|