57_cmntxml.js 2.3 KB
Newer Older
S
SheetJS 已提交
1
/* 18.7 Comments */
S
SheetJS 已提交
2
function parse_comments_xml(data/*:string*/, opts)/*:Array<Comment>*/ {
S
SheetJS 已提交
3
	/* 18.7.6 CT_Comments */
4
	if(data.match(/<(?:\w+:)?comments *\/>/)) return [];
S
SheetJS 已提交
5 6
	var authors = [];
	var commentList = [];
S
SheetJS 已提交
7
	var authtag = data.match(/<(?:\w+:)?authors>([\s\S]*)<\/(?:\w+:)?authors>/);
S
SheetJS 已提交
8
	if(authtag && authtag[1]) authtag[1].split(/<\/\w*:?author>/).forEach(function(x) {
S
SheetJS 已提交
9
		if(x === "" || x.trim() === "") return;
S
SheetJS 已提交
10 11
		var a = x.match(/<(?:\w+:)?author[^>]*>(.*)/);
		if(a) authors.push(a[1]);
S
SheetJS 已提交
12
	});
S
SheetJS 已提交
13
	var cmnttag = data.match(/<(?:\w+:)?commentList>([\s\S]*)<\/(?:\w+:)?commentList>/);
S
SheetJS 已提交
14
	if(cmnttag && cmnttag[1]) cmnttag[1].split(/<\/\w*:?comment>/).forEach(function(x, index) {
S
SheetJS 已提交
15
		if(x === "" || x.trim() === "") return;
S
SheetJS 已提交
16 17 18
		var cm = x.match(/<(?:\w+:)?comment[^>]*>/);
		if(!cm) return;
		var y = parsexmltag(cm[0]);
S
SheetJS 已提交
19
		var comment/*:Comment*/ = ({ author: y.authorId && authors[y.authorId] || "sheetjsghost", ref: y.ref, guid: y.guid }/*:any*/);
S
SheetJS 已提交
20 21
		var cell = decode_cell(y.ref);
		if(opts.sheetRows && opts.sheetRows <= cell.r) return;
S
SheetJS 已提交
22
		var textMatch = x.match(/<(?:\w+:)?text>([\s\S]*)<\/(?:\w+:)?text>/);
S
TXT/PRN  
SheetJS 已提交
23
		var rt = !!textMatch && !!textMatch[1] && parse_si(textMatch[1]) || {r:"",t:"",h:""};
S
SheetJS 已提交
24
		comment.r = rt.r;
S
TXT/PRN  
SheetJS 已提交
25
		if(rt.r == "<t></t>") rt.t = rt.h = "";
S
SheetJS 已提交
26
		comment.t = rt.t.replace(/\r\n/g,"\n").replace(/\r/g,"\n");
S
SheetJS 已提交
27 28 29 30 31
		if(opts.cellHTML) comment.h = rt.h;
		commentList.push(comment);
	});
	return commentList;
}
S
SheetJS 已提交
32

S
SheetJS 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
var CMNT_XML_ROOT = writextag('comments', null, { 'xmlns': XMLNS.main[0] });
function write_comments_xml(data, opts) {
	var o = [XML_HEADER, CMNT_XML_ROOT];

	var iauthor = [];
	o.push("<authors>");
	data.map(function(x) { return x[1]; }).forEach(function(comment) {
		comment.map(function(x) { return escapexml(x.a); }).forEach(function(a) {
			if(iauthor.indexOf(a) > -1) return;
			iauthor.push(a);
			o.push("<author>" + a + "</author>");
		});
	});
	o.push("</authors>");
	o.push("<commentList>");
	data.forEach(function(d) {
		d[1].forEach(function(c) {
			/* 18.7.3 CT_Comment */
			o.push('<comment ref="' + d[0] + '" authorId="' + iauthor.indexOf(escapexml(c.a)) + '"><text>');
S
TXT/PRN  
SheetJS 已提交
52
			o.push(writetag("t", c.t == null ? "" : c.t));
S
SheetJS 已提交
53 54 55 56 57 58 59
			o.push('</text></comment>');
		});
	});
	o.push("</commentList>");
	if(o.length>2) { o[o.length] = ('</comments>'); o[1]=o[1].replace("/>",">"); }
	return o.join("");
}