/* 18.2 Workbook */
var wbnsregex = /<\w+:workbook/;
function parse_wb_xml(data, opts) {
var wb = { AppVersion:{}, WBProps:{}, WBView:[], Sheets:[], CalcPr:{}, xmlns: "" };
var pass = false, xmlns = "xmlns";
data.match(tagregex).forEach(function xml_wb(x) {
var y = parsexmltag(x);
switch(strip_ns(y[0])) {
case '': break;
/* 18.2.13 fileVersion CT_FileVersion ? */
case '': break;
/* 18.2.12 fileSharing CT_FileSharing ? */
case '': break;
/* 18.2.28 workbookPr CT_WorkbookPr ? */
case '': delete y[0]; wb.WBProps = y; break;
/* 18.2.29 workbookProtection CT_WorkbookProtection ? */
case '': break;
/* 18.2.1 bookViews CT_BookViews ? */
case '': case '': break;
/* 18.2.30 workbookView CT_BookView + */
case '': case '': break; // aggregate sheet
/* 18.2.19 sheet CT_Sheet + */
case '': break;
/* 18.2.14 functionGroup CT_FunctionGroup + */
case '': case '': break;
/* 18.2.8 externalReference CT_ExternalReference + */
case '': break;
case '': case '': pass=false; break;
/* 18.2.5 definedName CT_DefinedName + */
case '': case '': break;
/* 18.2.2 calcPr CT_CalcPr ? */
case '': delete y[0]; wb.CalcPr = y; break;
/* 18.2.16 oleSize CT_OleSize ? (ref required) */
case '': case '': case '': break;
/* 18.2.18 pivotCaches CT_PivotCaches ? */
case '': case '': case '': break;
/* 18.2.23 smartTagTypes CT_SmartTagTypes ? */
case '': case '': break;
/* 18.2.22 smartTagType CT_SmartTagType ? */
case '': break;
/* 18.2.11 fileRecoveryPr CT_FileRecoveryPr ? */
case '': break;
/* 18.2.26 webPublishObjects CT_WebPublishObjects ? */
case '': case '': break;
/* 18.2.25 webPublishObject CT_WebPublishObject ? */
case '': case '': case '': break;
/* 18.2.7 ext CT_Extension + */
case '': pass=false; break;
/* Others */
case '': pass=false; break;
default: if(!pass && opts.WTF) throw 'unrecognized ' + y[0] + ' in workbook';
}
});
if(XMLNS.main.indexOf(wb.xmlns) === -1) throw new Error("Unknown Namespace: " + wb.xmlns);
parse_wb_defaults(wb);
return wb;
}
var WB_XML_ROOT = writextag('workbook', null, {
'xmlns': XMLNS.main[0],
//'xmlns:mx': XMLNS.mx,
//'xmlns:s': XMLNS.main[0],
'xmlns:r': XMLNS.r
});
function safe1904(wb) {
/* TODO: store date1904 somewhere else */
try { return parsexmlbool(wb.Workbook.WBProps.date1904) ? "true" : "false"; } catch(e) { return "false"; }
}
function write_wb_xml(wb, opts) {
var o = [XML_HEADER];
o[o.length] = WB_XML_ROOT;
o[o.length] = (writextag('workbookPr', null, {date1904:safe1904(wb)}));
o[o.length] = "";
for(var i = 0; i != wb.SheetNames.length; ++i)
o[o.length] = (writextag('sheet',null,{name:wb.SheetNames[i].substr(0,31), sheetId:""+(i+1), "r:id":"rId"+(i+1)}));
o[o.length] = "";
var hasPrintHeaders = false;
for(var i = 0; i != wb.SheetNames.length; ++i) {
var sheetName = wb.SheetNames[i];
var sheet = wb.Sheets[sheetName]
if (sheet['!printHeader']) {
if (sheet['!printHeader'].length !== 2) {
throw "!printHeaders must be an array of length 2: "+sheet['!printHeader'];
}
hasPrintHeaders = true;
}
}
if (hasPrintHeaders) {
o[o.length] = '';
for(var i = 0; i != wb.SheetNames.length; ++i) {
var sheetName = wb.SheetNames[i];
var sheet = wb.Sheets[sheetName]
if (sheet['!printHeader'])
var range = "'" + sheetName + "'!" + sheet['!printHeader'];
console.log("!!!!"+range)
o[o.length] = (writextag('definedName', range, {
"name":"_xlnm.Print_Titles",
localSheetId : ''+i
}))
}
o[o.length] = '';
}
//
// Sheet1!$1:$1
// Sheet2!$1:$2
//
if(o.length>2){ o[o.length] = ''; o[1]=o[1].replace("/>",">"); }
return o.join("");
}