| 
									
										
										
										
											2014-05-03 18:51:10 +00:00
										 |  |  | function decode_row(rowstr) { return Number(unfix_row(rowstr)) - 1; } | 
					
						
							|  |  |  | function encode_row(row) { return "" + (row + 1); } | 
					
						
							|  |  |  | function fix_row(cstr) { return cstr.replace(/([A-Z]|^)([0-9]+)$/,"$1$$$2"); } | 
					
						
							|  |  |  | function unfix_row(cstr) { return cstr.replace(/\$([0-9]+)$/,"$1"); } | 
					
						
							| 
									
										
										
										
											2013-03-31 22:56:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-03 18:51:10 +00:00
										 |  |  | function decode_col(colstr) { var c = unfix_col(colstr), d = 0, i = 0; for(; i !== c.length; ++i) d = 26*d + c.charCodeAt(i) - 64; return d - 1; } | 
					
						
							| 
									
										
										
										
											2013-03-31 22:56:45 +00:00
										 |  |  | function encode_col(col) { var s=""; for(++col; col; col=Math.floor((col-1)/26)) s = _chr(((col-1)%26) + 65) + s; return s; } | 
					
						
							| 
									
										
										
										
											2014-05-03 18:51:10 +00:00
										 |  |  | function fix_col(cstr) { return cstr.replace(/^([A-Z])/,"$$$1"); } | 
					
						
							|  |  |  | function unfix_col(cstr) { return cstr.replace(/^\$([A-Z])/,"$1"); } | 
					
						
							| 
									
										
										
										
											2013-03-31 22:56:45 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | function split_cell(cstr) { return cstr.replace(/(\$?[A-Z]*)(\$?[0-9]*)/,"$1,$2").split(","); } | 
					
						
							|  |  |  | function decode_cell(cstr) { var splt = split_cell(cstr); return { c:decode_col(splt[0]), r:decode_row(splt[1]) }; } | 
					
						
							| 
									
										
										
										
											2014-05-03 18:51:10 +00:00
										 |  |  | function encode_cell(cell) { return encode_col(cell.c) + encode_row(cell.r); } | 
					
						
							|  |  |  | function fix_cell(cstr) { return fix_col(fix_row(cstr)); } | 
					
						
							|  |  |  | function unfix_cell(cstr) { return unfix_col(unfix_row(cstr)); } | 
					
						
							| 
									
										
										
										
											2013-03-31 22:56:45 +00:00
										 |  |  | function decode_range(range) { var x =range.split(":").map(decode_cell); return {s:x[0],e:x[x.length-1]}; } | 
					
						
							| 
									
										
										
										
											2014-05-03 18:51:10 +00:00
										 |  |  | function encode_range(cs,ce) { | 
					
						
							|  |  |  | 	if(typeof ce === 'undefined' || typeof ce === 'number') return encode_range(cs.s, cs.e); | 
					
						
							|  |  |  | 	if(typeof cs !== 'string') cs = encode_cell(cs); if(typeof ce !== 'string') ce = encode_cell(ce); | 
					
						
							|  |  |  | 	return cs == ce ? cs : cs + ":" + ce; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function format_cell(cell, v) { | 
					
						
							|  |  |  | 	if(!cell || !cell.t) return ""; | 
					
						
							|  |  |  | 	if(typeof cell.w !== 'undefined') return cell.w; | 
					
						
							|  |  |  | 	if(typeof v === 'undefined') v = cell.v; | 
					
						
							|  |  |  | 	if(typeof cell.z !== 'undefined') try { return (cell.w = SSF.format(cell.z, v)); } catch(e) { } | 
					
						
							|  |  |  | 	if(!cell.XF) return v; | 
					
						
							|  |  |  | 	try { return (cell.w = SSF.format(cell.XF.ifmt||0, v)); } catch(e) { return v; } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-03-31 22:56:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-29 22:30:03 +00:00
										 |  |  | function sheet_to_json(sheet, opts){ | 
					
						
							|  |  |  | 	var val, row, range, header, offset = 1, r, hdr = {}, isempty, R, C, v; | 
					
						
							| 
									
										
										
										
											2014-01-23 06:20:19 +00:00
										 |  |  | 	var out = []; | 
					
						
							|  |  |  | 	opts = opts || {}; | 
					
						
							| 
									
										
										
										
											2014-01-23 15:55:07 +00:00
										 |  |  | 	if(!sheet || !sheet["!ref"]) return out; | 
					
						
							| 
									
										
										
										
											2014-05-29 22:30:03 +00:00
										 |  |  | 	range = opts.range || sheet["!ref"]; | 
					
						
							|  |  |  | 	header = opts.header || ""; | 
					
						
							|  |  |  | 	switch(typeof range) { | 
					
						
							|  |  |  | 		case 'string': r = decode_range(range); break; | 
					
						
							|  |  |  | 		case 'number': r = decode_range(sheet["!ref"]); r.s.r = range; break; | 
					
						
							|  |  |  | 		default: r = range; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if(header) offset = 0; | 
					
						
							| 
									
										
										
										
											2014-01-23 06:20:19 +00:00
										 |  |  | 	for(R=r.s.r, C = r.s.c; C <= r.e.c; ++C) { | 
					
						
							|  |  |  | 		val = sheet[encode_cell({c:C,r:R})]; | 
					
						
							| 
									
										
										
										
											2014-05-29 22:30:03 +00:00
										 |  |  | 		if(header === "A") hdr[C] = encode_col(C); | 
					
						
							|  |  |  | 		else if(header === 1) hdr[C] = C; | 
					
						
							|  |  |  | 		else if(Array.isArray(header)) hdr[C] = header[C - r.s.c]; | 
					
						
							|  |  |  | 		else if(!val) continue; | 
					
						
							|  |  |  | 		else hdr[C] = format_cell(val); | 
					
						
							| 
									
										
										
										
											2014-01-23 06:20:19 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-03-31 22:56:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-29 22:30:03 +00:00
										 |  |  | 	for (R = r.s.r + offset; R <= r.e.r; ++R) { | 
					
						
							| 
									
										
										
										
											2014-01-23 06:20:19 +00:00
										 |  |  | 		isempty = true; | 
					
						
							| 
									
										
										
										
											2014-05-29 22:30:03 +00:00
										 |  |  | 		row = header === 1 ? [] : Object.create({ __rowNum__ : R }); | 
					
						
							| 
									
										
										
										
											2014-01-23 06:20:19 +00:00
										 |  |  | 		for (C = r.s.c; C <= r.e.c; ++C) { | 
					
						
							|  |  |  | 			val = sheet[encode_cell({c: C,r: R})]; | 
					
						
							| 
									
										
										
										
											2014-01-23 15:55:07 +00:00
										 |  |  | 			if(!val || !val.t) continue; | 
					
						
							| 
									
										
										
										
											2014-05-03 18:51:10 +00:00
										 |  |  | 			v = (val || {}).v; | 
					
						
							|  |  |  | 			switch(val.t){ | 
					
						
							|  |  |  | 				case 'e': continue; | 
					
						
							|  |  |  | 				case 's': case 'str': break; | 
					
						
							|  |  |  | 				case 'b': case 'n': break; | 
					
						
							| 
									
										
										
										
											2014-01-23 06:20:19 +00:00
										 |  |  | 				default: throw 'unrecognized type ' + val.t; | 
					
						
							| 
									
										
										
										
											2013-03-31 22:56:45 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-05-03 18:51:10 +00:00
										 |  |  | 			if(typeof v !== 'undefined') { | 
					
						
							|  |  |  | 				row[hdr[C]] = opts.raw ? v||val.v : format_cell(val,v); | 
					
						
							|  |  |  | 				isempty = false; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-03-31 22:56:45 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-01-23 06:20:19 +00:00
										 |  |  | 		if(!isempty) out.push(row); | 
					
						
							| 
									
										
										
										
											2013-03-31 22:56:45 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-01-23 06:20:19 +00:00
										 |  |  | 	return out; | 
					
						
							| 
									
										
										
										
											2013-03-31 22:56:45 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-29 22:30:03 +00:00
										 |  |  | function sheet_to_row_object_array(sheet, opts) { if(!opts) opts = {}; delete opts.range; return sheet_to_json(sheet, opts); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-23 06:20:19 +00:00
										 |  |  | function sheet_to_csv(sheet, opts) { | 
					
						
							| 
									
										
										
										
											2014-02-22 21:36:28 +00:00
										 |  |  | 	var out = [], txt = ""; | 
					
						
							| 
									
										
										
										
											2014-01-23 06:20:19 +00:00
										 |  |  | 	opts = opts || {}; | 
					
						
							| 
									
										
										
										
											2014-02-22 21:36:28 +00:00
										 |  |  | 	if(!sheet || !sheet["!ref"]) return ""; | 
					
						
							| 
									
										
										
										
											2014-05-03 18:51:10 +00:00
										 |  |  | 	var r = decode_range(sheet["!ref"]); | 
					
						
							| 
									
										
										
										
											2014-02-12 06:09:42 +00:00
										 |  |  | 	var fs = opts.FS||",", rs = opts.RS||"\n"; | 
					
						
							| 
									
										
										
										
											2014-02-17 08:44:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-23 06:20:19 +00:00
										 |  |  | 	for(var R = r.s.r; R <= r.e.r; ++R) { | 
					
						
							|  |  |  | 		var row = []; | 
					
						
							|  |  |  | 		for(var C = r.s.c; C <= r.e.c; ++C) { | 
					
						
							| 
									
										
										
										
											2014-05-03 18:51:10 +00:00
										 |  |  | 			var val = sheet[encode_cell({c:C,r:R})]; | 
					
						
							| 
									
										
										
										
											2014-01-23 06:20:19 +00:00
										 |  |  | 			if(!val) { row.push(""); continue; } | 
					
						
							| 
									
										
										
										
											2014-05-03 18:51:10 +00:00
										 |  |  | 			txt = String(format_cell(val)); | 
					
						
							| 
									
										
										
										
											2014-02-12 06:09:42 +00:00
										 |  |  | 			if(txt.indexOf(fs)!==-1 || txt.indexOf(rs)!==-1 || txt.indexOf('"')!==-1) | 
					
						
							|  |  |  | 				txt = "\"" + txt.replace(/"/g, '""') + "\""; | 
					
						
							|  |  |  | 			row.push(txt); | 
					
						
							| 
									
										
										
										
											2013-03-31 22:56:45 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-02-22 21:36:28 +00:00
										 |  |  | 		out.push(row.join(fs)); | 
					
						
							| 
									
										
										
										
											2013-03-31 22:56:45 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-22 21:36:28 +00:00
										 |  |  | 	return out.join(rs) + (out.length ? rs : ""); | 
					
						
							| 
									
										
										
										
											2013-03-31 22:56:45 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2013-10-30 22:26:31 +00:00
										 |  |  | var make_csv = sheet_to_csv; | 
					
						
							| 
									
										
										
										
											2013-03-31 22:56:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-13 17:00:01 +00:00
										 |  |  | function get_formulae(ws) { | 
					
						
							|  |  |  | 	var cmds = []; | 
					
						
							| 
									
										
										
										
											2013-04-17 01:32:20 +00:00
										 |  |  | 	for(var y in ws) if(y[0] !=='!' && ws.hasOwnProperty(y)) { | 
					
						
							|  |  |  | 		var x = ws[y]; | 
					
						
							| 
									
										
										
										
											2013-04-13 17:00:01 +00:00
										 |  |  | 		var val = ""; | 
					
						
							|  |  |  | 		if(x.f) val = x.f; | 
					
						
							| 
									
										
										
										
											2014-02-17 08:44:22 +00:00
										 |  |  | 		else if(typeof x.w !== 'undefined') val = "'" + x.w; | 
					
						
							| 
									
										
										
										
											2014-02-06 22:02:11 +00:00
										 |  |  | 		else if(typeof x.v === 'undefined') continue; | 
					
						
							| 
									
										
										
										
											2013-04-13 17:00:01 +00:00
										 |  |  | 		else val = x.v; | 
					
						
							|  |  |  | 		cmds.push(y + "=" + val); | 
					
						
							| 
									
										
										
										
											2013-04-17 01:32:20 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-04-13 17:00:01 +00:00
										 |  |  | 	return cmds; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-03 18:51:10 +00:00
										 |  |  | var utils = { | 
					
						
							| 
									
										
										
										
											2013-03-31 22:56:45 +00:00
										 |  |  | 	encode_col: encode_col, | 
					
						
							|  |  |  | 	encode_row: encode_row, | 
					
						
							|  |  |  | 	encode_cell: encode_cell, | 
					
						
							|  |  |  | 	encode_range: encode_range, | 
					
						
							|  |  |  | 	decode_col: decode_col, | 
					
						
							|  |  |  | 	decode_row: decode_row, | 
					
						
							|  |  |  | 	split_cell: split_cell, | 
					
						
							|  |  |  | 	decode_cell: decode_cell, | 
					
						
							|  |  |  | 	decode_range: decode_range, | 
					
						
							|  |  |  | 	sheet_to_csv: sheet_to_csv, | 
					
						
							| 
									
										
										
										
											2013-10-30 22:26:31 +00:00
										 |  |  | 	make_csv: sheet_to_csv, | 
					
						
							| 
									
										
										
										
											2014-05-29 22:30:03 +00:00
										 |  |  | 	make_json: sheet_to_json, | 
					
						
							| 
									
										
										
										
											2013-04-13 17:00:01 +00:00
										 |  |  | 	get_formulae: get_formulae, | 
					
						
							| 
									
										
										
										
											2014-05-03 18:51:10 +00:00
										 |  |  | 	format_cell: format_cell, | 
					
						
							| 
									
										
										
										
											2014-05-29 22:30:03 +00:00
										 |  |  | 	sheet_to_json: sheet_to_json, | 
					
						
							| 
									
										
										
										
											2013-03-31 22:56:45 +00:00
										 |  |  | 	sheet_to_row_object_array: sheet_to_row_object_array | 
					
						
							|  |  |  | }; |