| 
									
										
										
										
											2014-01-28 16:38:02 +00:00
										 |  |  | /* 18.2 Workbook */ | 
					
						
							| 
									
										
										
										
											2014-06-05 07:06:20 +00:00
										 |  |  | function parse_wb_xml(data, opts) { | 
					
						
							| 
									
										
										
										
											2014-01-28 16:38:02 +00:00
										 |  |  | 	var wb = { AppVersion:{}, WBProps:{}, WBView:[], Sheets:[], CalcPr:{}, xmlns: "" }; | 
					
						
							| 
									
										
										
										
											2014-04-03 22:51:54 +00:00
										 |  |  | 	var pass = false, xmlns = "xmlns"; | 
					
						
							| 
									
										
										
										
											2014-01-28 16:38:02 +00:00
										 |  |  | 	data.match(/<[^>]*>/g).forEach(function(x) { | 
					
						
							|  |  |  | 		var y = parsexmltag(x); | 
					
						
							| 
									
										
										
										
											2014-06-05 07:06:20 +00:00
										 |  |  | 		switch(y[0].replace(/<(\/?)\w+:/,"<$1")) { | 
					
						
							| 
									
										
										
										
											2014-01-28 16:38:02 +00:00
										 |  |  | 			case '<?xml': break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			/* 18.2.27 workbook CT_Workbook 1 */ | 
					
						
							| 
									
										
										
										
											2014-04-03 22:51:54 +00:00
										 |  |  | 			case '<workbook': | 
					
						
							|  |  |  | 				if(x.match(/<\w+:workbook/)) xmlns = "xmlns" + x.match(/<(\w+):/)[1]; | 
					
						
							|  |  |  | 				wb.xmlns = y[xmlns]; | 
					
						
							|  |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2014-01-28 16:38:02 +00:00
										 |  |  | 			case '</workbook>': break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			/* 18.2.13 fileVersion CT_FileVersion ? */ | 
					
						
							|  |  |  | 			case '<fileVersion': delete y[0]; wb.AppVersion = y; break; | 
					
						
							|  |  |  | 			case '<fileVersion/>': break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			/* 18.2.12 fileSharing CT_FileSharing ? */ | 
					
						
							|  |  |  | 			case '<fileSharing': case '<fileSharing/>': break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			/* 18.2.28 workbookPr CT_WorkbookPr ? */ | 
					
						
							|  |  |  | 			case '<workbookPr': delete y[0]; wb.WBProps = y; break; | 
					
						
							|  |  |  | 			case '<workbookPr/>': delete y[0]; wb.WBProps = y; break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			/* 18.2.29 workbookProtection CT_WorkbookProtection ? */ | 
					
						
							| 
									
										
										
										
											2014-06-05 07:06:20 +00:00
										 |  |  | 			case '<workbookProtection': break; | 
					
						
							| 
									
										
										
										
											2014-01-28 16:38:02 +00:00
										 |  |  | 			case '<workbookProtection/>': break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			/* 18.2.1  bookViews CT_BookViews ? */ | 
					
						
							|  |  |  | 			case '<bookViews>': case '</bookViews>': break; | 
					
						
							|  |  |  | 			/* 18.2.30   workbookView CT_BookView + */ | 
					
						
							|  |  |  | 			case '<workbookView': delete y[0]; wb.WBView.push(y); break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			/* 18.2.20 sheets CT_Sheets 1 */ | 
					
						
							|  |  |  | 			case '<sheets>': case '</sheets>': break; // aggregate sheet
 | 
					
						
							|  |  |  | 			/* 18.2.19   sheet CT_Sheet + */ | 
					
						
							|  |  |  | 			case '<sheet': delete y[0]; y.name = utf8read(y.name); wb.Sheets.push(y); break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			/* 18.2.15 functionGroups CT_FunctionGroups ? */ | 
					
						
							|  |  |  | 			case '<functionGroups': case '<functionGroups/>': break; | 
					
						
							|  |  |  | 			/* 18.2.14   functionGroup CT_FunctionGroup + */ | 
					
						
							|  |  |  | 			case '<functionGroup': break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			/* 18.2.9  externalReferences CT_ExternalReferences ? */ | 
					
						
							| 
									
										
										
										
											2014-06-05 07:06:20 +00:00
										 |  |  | 			case '<externalReferences': case '</externalReferences>': case '<externalReferences>': break; | 
					
						
							| 
									
										
										
										
											2014-01-28 16:38:02 +00:00
										 |  |  | 			/* 18.2.8    externalReference CT_ExternalReference + */ | 
					
						
							|  |  |  | 			case '<externalReference': break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			/* 18.2.6  definedNames CT_DefinedNames ? */ | 
					
						
							|  |  |  | 			case '<definedNames/>': break; | 
					
						
							| 
									
										
										
										
											2014-06-05 07:06:20 +00:00
										 |  |  | 			case '<definedNames>': case '<definedNames': pass=true; break; | 
					
						
							| 
									
										
										
										
											2014-01-28 16:38:02 +00:00
										 |  |  | 			case '</definedNames>': pass=false; break; | 
					
						
							|  |  |  | 			/* 18.2.5    definedName CT_DefinedName + */ | 
					
						
							|  |  |  | 			case '<definedName': case '<definedName/>': case '</definedName>': break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			/* 18.2.2  calcPr CT_CalcPr ? */ | 
					
						
							|  |  |  | 			case '<calcPr': delete y[0]; wb.CalcPr = y; break; | 
					
						
							|  |  |  | 			case '<calcPr/>': delete y[0]; wb.CalcPr = y; break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			/* 18.2.16 oleSize CT_OleSize ? (ref required) */ | 
					
						
							|  |  |  | 			case '<oleSize': break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			/* 18.2.4  customWorkbookViews CT_CustomWorkbookViews ? */ | 
					
						
							|  |  |  | 			case '<customWorkbookViews>': case '</customWorkbookViews>': case '<customWorkbookViews': break; | 
					
						
							|  |  |  | 			/* 18.2.3    customWorkbookView CT_CustomWorkbookView + */ | 
					
						
							|  |  |  | 			case '<customWorkbookView': case '</customWorkbookView>': break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			/* 18.2.18 pivotCaches CT_PivotCaches ? */ | 
					
						
							|  |  |  | 			case '<pivotCaches>': case '</pivotCaches>': case '<pivotCaches': break; | 
					
						
							|  |  |  | 			/* 18.2.17 pivotCache CT_PivotCache ? */ | 
					
						
							|  |  |  | 			case '<pivotCache': break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			/* 18.2.21 smartTagPr CT_SmartTagPr ? */ | 
					
						
							|  |  |  | 			case '<smartTagPr': case '<smartTagPr/>': break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			/* 18.2.23 smartTagTypes CT_SmartTagTypes ? */ | 
					
						
							|  |  |  | 			case '<smartTagTypes': case '<smartTagTypes>': case '</smartTagTypes>': break; | 
					
						
							|  |  |  | 			/* 18.2.22   smartTagType CT_SmartTagType ? */ | 
					
						
							|  |  |  | 			case '<smartTagType': break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			/* 18.2.24 webPublishing CT_WebPublishing ? */ | 
					
						
							|  |  |  | 			case '<webPublishing': case '<webPublishing/>': break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			/* 18.2.11 fileRecoveryPr CT_FileRecoveryPr ? */ | 
					
						
							|  |  |  | 			case '<fileRecoveryPr': case '<fileRecoveryPr/>': break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			/* 18.2.26 webPublishObjects CT_WebPublishObjects ? */ | 
					
						
							|  |  |  | 			case '<webPublishObjects>': case '<webPublishObjects': case '</webPublishObjects>': break; | 
					
						
							|  |  |  | 			/* 18.2.25 webPublishObject CT_WebPublishObject ? */ | 
					
						
							|  |  |  | 			case '<webPublishObject': break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			/* 18.2.10 extLst CT_ExtensionList ? */ | 
					
						
							|  |  |  | 			case '<extLst>': case '</extLst>': case '<extLst/>': break; | 
					
						
							|  |  |  | 			/* 18.2.7    ext CT_Extension + */ | 
					
						
							|  |  |  | 			case '<ext': pass=true; break; //TODO: check with versions of excel
 | 
					
						
							|  |  |  | 			case '</ext>': pass=false; break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			/* Others */ | 
					
						
							| 
									
										
										
										
											2014-06-05 07:06:20 +00:00
										 |  |  | 			case '<ArchID': break; | 
					
						
							|  |  |  | 			case '<AlternateContent': pass=true; break; | 
					
						
							|  |  |  | 			case '</AlternateContent>': pass=false; break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			default: if(!pass && opts.WTF) throw 'unrecognized ' + y[0] + ' in workbook'; | 
					
						
							| 
									
										
										
										
											2014-01-28 16:38:02 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-05-16 00:33:34 +00:00
										 |  |  | 	if(XMLNS.main.indexOf(wb.xmlns) === -1) throw new Error("Unknown Namespace: " + wb.xmlns); | 
					
						
							| 
									
										
										
										
											2014-01-28 16:38:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	var z; | 
					
						
							|  |  |  | 	/* defaults */ | 
					
						
							|  |  |  | 	for(z in WBPropsDef) if(typeof wb.WBProps[z] === 'undefined') wb.WBProps[z] = WBPropsDef[z]; | 
					
						
							|  |  |  | 	for(z in CalcPrDef) if(typeof wb.CalcPr[z] === 'undefined') wb.CalcPr[z] = CalcPrDef[z]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	wb.WBView.forEach(function(w){for(var z in WBViewDef) if(typeof w[z] === 'undefined') w[z]=WBViewDef[z]; }); | 
					
						
							|  |  |  | 	wb.Sheets.forEach(function(w){for(var z in SheetDef) if(typeof w[z] === 'undefined') w[z]=SheetDef[z]; }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	_ssfopts.date1904 = parsexmlbool(wb.WBProps.date1904, 'date1904'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return wb; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-16 00:33:34 +00:00
										 |  |  | var WB_XML_ROOT = writextag('workbook', null, { | 
					
						
							|  |  |  | 	'xmlns': XMLNS.main[0], | 
					
						
							|  |  |  | 	//'xmlns:mx': XMLNS.mx,
 | 
					
						
							|  |  |  | 	//'xmlns:s': XMLNS.main[0],
 | 
					
						
							|  |  |  | 	'xmlns:r': XMLNS.r | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-02 05:19:07 +00:00
										 |  |  | function safe1904(wb) { | 
					
						
							|  |  |  | 	/* TODO: store date1904 somewhere else */ | 
					
						
							|  |  |  | 	try { return parsexmlbool(wb.Workbook.WBProps.date1904) ? "true" : "false"; } catch(e) { return "false"; } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-16 00:33:34 +00:00
										 |  |  | var write_wb_xml = function(wb, opts) { | 
					
						
							|  |  |  | 	var o = []; | 
					
						
							|  |  |  | 	o.push(XML_HEADER); | 
					
						
							|  |  |  | 	o.push(WB_XML_ROOT); | 
					
						
							| 
									
										
										
										
											2014-06-02 05:19:07 +00:00
										 |  |  | 	o.push(writextag('workbookPr', null, {date1904:safe1904(wb)})); | 
					
						
							| 
									
										
										
										
											2014-05-16 00:33:34 +00:00
										 |  |  | 	o.push("<sheets>"); | 
					
						
							|  |  |  | 	var i = 1; | 
					
						
							|  |  |  | 	wb.SheetNames.forEach(function(s) { | 
					
						
							| 
									
										
										
										
											2014-05-31 15:44:56 +00:00
										 |  |  | 		o.push(writextag('sheet',null,{name:s.substr(0,31), sheetId:String(i), "r:id":"rId"+i})); | 
					
						
							| 
									
										
										
										
											2014-05-16 00:33:34 +00:00
										 |  |  | 		++i; | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	o.push("</sheets>"); | 
					
						
							|  |  |  | 	if(o.length>2){ o.push('</workbook>'); o[1]=o[1].replace("/>",">"); } | 
					
						
							|  |  |  | 	return o.join(""); | 
					
						
							|  |  |  | }; |