| 
									
										
										
										
											2014-01-28 16:38:02 +00:00
										 |  |  | /* 18.7.3 CT_Comment */ | 
					
						
							| 
									
										
										
										
											2014-02-12 06:09:42 +00:00
										 |  |  | function parse_comments_xml(data, opts) { | 
					
						
							|  |  |  | 	if(data.match(/<comments *\/>/)) return []; | 
					
						
							| 
									
										
										
										
											2014-01-28 16:38:02 +00:00
										 |  |  | 	var authors = []; | 
					
						
							|  |  |  | 	var commentList = []; | 
					
						
							|  |  |  | 	data.match(/<authors>([^\u2603]*)<\/authors>/m)[1].split('</author>').forEach(function(x) { | 
					
						
							|  |  |  | 		if(x === "" || x.trim() === "") return; | 
					
						
							|  |  |  | 		authors.push(x.match(/<author[^>]*>(.*)/)[1]); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	data.match(/<commentList>([^\u2603]*)<\/commentList>/m)[1].split('</comment>').forEach(function(x, index) { | 
					
						
							|  |  |  | 		if(x === "" || x.trim() === "") return; | 
					
						
							|  |  |  | 		var y = parsexmltag(x.match(/<comment[^>]*>/)[0]); | 
					
						
							|  |  |  | 		var comment = { author: y.authorId && authors[y.authorId] ? authors[y.authorId] : undefined, ref: y.ref, guid: y.guid }; | 
					
						
							|  |  |  | 		var textMatch = x.match(/<text>([^\u2603]*)<\/text>/m); | 
					
						
							|  |  |  | 		if (!textMatch || !textMatch[1]) return; // a comment may contain an empty text tag.
 | 
					
						
							|  |  |  | 		var rt = parse_si(textMatch[1]); | 
					
						
							|  |  |  | 		comment.r = rt.r; | 
					
						
							| 
									
										
										
										
											2014-02-05 13:39:21 +00:00
										 |  |  | 		comment.t = rt.t; | 
					
						
							| 
									
										
										
										
											2014-02-12 06:09:42 +00:00
										 |  |  | 		if(opts.cellHTML) comment.h = rt.h; | 
					
						
							| 
									
										
										
										
											2014-01-28 16:38:02 +00:00
										 |  |  | 		commentList.push(comment); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	return commentList; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-12 06:09:42 +00:00
										 |  |  | function parse_comments(zip, dirComments, sheets, sheetRels, opts) { | 
					
						
							| 
									
										
										
										
											2014-01-28 16:38:02 +00:00
										 |  |  | 	for(var i = 0; i != dirComments.length; ++i) { | 
					
						
							|  |  |  | 		var canonicalpath=dirComments[i]; | 
					
						
							| 
									
										
										
										
											2014-02-15 05:08:18 +00:00
										 |  |  | 		var comments=parse_comments_xml(getzipdata(zip, canonicalpath.replace(/^\//,''), true), opts); | 
					
						
							|  |  |  | 		if(!comments || !comments.length) return; | 
					
						
							| 
									
										
										
										
											2014-01-28 16:38:02 +00:00
										 |  |  | 		// find the sheets targeted by these comments
 | 
					
						
							|  |  |  | 		var sheetNames = Object.keys(sheets); | 
					
						
							|  |  |  | 		for(var j = 0; j != sheetNames.length; ++j) { | 
					
						
							|  |  |  | 			var sheetName = sheetNames[j]; | 
					
						
							|  |  |  | 			var rels = sheetRels[sheetName]; | 
					
						
							| 
									
										
										
										
											2014-02-12 06:09:42 +00:00
										 |  |  | 			if(rels) { | 
					
						
							| 
									
										
										
										
											2014-01-28 16:38:02 +00:00
										 |  |  | 				var rel = rels[canonicalpath]; | 
					
						
							| 
									
										
										
										
											2014-02-12 06:09:42 +00:00
										 |  |  | 				if(rel) insertCommentsIntoSheet(sheetName, sheets[sheetName], comments); | 
					
						
							| 
									
										
										
										
											2014-01-28 16:38:02 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-01-29 06:00:09 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-01-28 16:38:02 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function insertCommentsIntoSheet(sheetName, sheet, comments) { | 
					
						
							|  |  |  | 	comments.forEach(function(comment) { | 
					
						
							|  |  |  | 		var cell = sheet[comment.ref]; | 
					
						
							|  |  |  | 		if (!cell) { | 
					
						
							|  |  |  | 			cell = {}; | 
					
						
							|  |  |  | 			sheet[comment.ref] = cell; | 
					
						
							|  |  |  | 			var range = decode_range(sheet["!ref"]); | 
					
						
							|  |  |  | 			var thisCell = decode_cell(comment.ref); | 
					
						
							|  |  |  | 			if(range.s.r > thisCell.r) range.s.r = thisCell.r; | 
					
						
							|  |  |  | 			if(range.e.r < thisCell.r) range.e.r = thisCell.r; | 
					
						
							|  |  |  | 			if(range.s.c > thisCell.c) range.s.c = thisCell.c; | 
					
						
							|  |  |  | 			if(range.e.c < thisCell.c) range.e.c = thisCell.c; | 
					
						
							|  |  |  | 			var encoded = encode_range(range); | 
					
						
							|  |  |  | 			if (encoded !== sheet["!ref"]) sheet["!ref"] = encoded; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-12 06:09:42 +00:00
										 |  |  | 		if (!cell.c) cell.c = []; | 
					
						
							|  |  |  | 		var o = {a: comment.author, t: comment.t, r: comment.r}; | 
					
						
							|  |  |  | 		if(comment.h) o.h = comment.h; | 
					
						
							|  |  |  | 		cell.c.push(o); | 
					
						
							| 
									
										
										
										
											2014-01-28 16:38:02 +00:00
										 |  |  | 	}); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 |