/* 18.7 Comments */ function parse_comments_xml(data/*:string*/, opts)/*:Array*/ { /* 18.7.6 CT_Comments */ if(data.match(/<(?:\w+:)?comments *\/>/)) return []; var authors/*:Array*/ = []; var commentList/*:Array*/ = []; var authtag = str_match_xml_ns(data, "authors"); if(authtag && authtag[1]) authtag[1].split(/<\/\w*:?author>/).forEach(function(x) { if(x === "" || x.trim() === "") return; var a = x.match(/<(?:\w+:)?author[^<>]*>(.*)/); if(a) authors.push(a[1]); }); var cmnttag = str_match_xml_ns(data, "commentList"); if(cmnttag && cmnttag[1]) cmnttag[1].split(/<\/\w*:?comment>/).forEach(function(x) { if(x === "" || x.trim() === "") return; var cm = x.match(/<(?:\w+:)?comment[^<>]*>/); if(!cm) return; var y = parsexmltag(cm[0]); var comment/*:RawComment*/ = ({ author: y.authorId && authors[y.authorId] || "sheetjsghost", ref: y.ref, guid: y.guid }/*:any*/); var cell = decode_cell(y.ref); if(opts.sheetRows && opts.sheetRows <= cell.r) return; var textMatch = str_match_xml_ns(x, "text"); var rt = !!textMatch && !!textMatch[1] && parse_si(textMatch[1]) || {r:"",t:"",h:""}; comment.r = rt.r; if(rt.r == "") rt.t = rt.h = ""; comment.t = (rt.t||"").replace(/\r\n/g,"\n").replace(/\r/g,"\n"); if(opts.cellHTML) comment.h = rt.h; commentList.push(comment); }); return commentList; } function write_comments_xml(data/*::, opts*/) { var o = [XML_HEADER, writextag('comments', null, { 'xmlns': XMLNS_main[0] })]; var iauthor/*:Array*/ = []; o.push(""); data.forEach(function(x) { x[1].forEach(function(w) { var a = escapexml(w.a); if(iauthor.indexOf(a) == -1) { iauthor.push(a); o.push("" + a + ""); } if(w.T && w.ID && iauthor.indexOf("tc=" + w.ID) == -1) { iauthor.push("tc=" + w.ID); o.push("" + "tc=" + w.ID + ""); } }); }); if(iauthor.length == 0) { iauthor.push("SheetJ5"); o.push("SheetJ5"); } o.push(""); o.push(""); data.forEach(function(d) { /* 18.7.3 CT_Comment */ var lastauthor = 0, ts = [], tcnt = 0; if(d[1][0] && d[1][0].T && d[1][0].ID) lastauthor = iauthor.indexOf("tc=" + d[1][0].ID); d[1].forEach(function(c) { if(c.a) lastauthor = iauthor.indexOf(escapexml(c.a)); if(c.T) ++tcnt; ts.push(c.t == null ? "" : escapexml(c.t)); }); if(tcnt === 0) { d[1].forEach(function(c) { o.push(''); o.push(writetag("t", c.t == null ? "" : escapexml(c.t))); o.push(''); }); } else { if(d[1][0] && d[1][0].T && d[1][0].ID) lastauthor = iauthor.indexOf("tc=" + d[1][0].ID); /* based on Threaded Comments -> Comments projection */ o.push(''); var t = "Comment:\n " + (ts[0]) + "\n"; for(var i = 1; i < ts.length; ++i) t += "Reply:\n " + ts[i] + "\n"; o.push(writetag("t", escapexml(t))); o.push(''); } }); o.push(""); if(o.length>2) { o[o.length] = (''); o[1]=o[1].replace("/>",">"); } return o.join(""); } /* [MS-XLSX] 2.1.17 */ function parse_tcmnt_xml(data/*:string*/, opts)/*:Array*/ { var out = []; var pass = false, comment = {}, tidx = 0; data.replace(tagregex, function xml_tcmnt(x, idx) { var y/*:any*/ = parsexmltag(x); switch(strip_ns(y[0])) { case '': break; /* 2.6.205 threadedComment CT_ThreadedComment */ case '': if(comment.t != null) out.push(comment); break; case '': case '': comment.t = data.slice(tidx, idx).replace(/\r\n/g, "\n").replace(/\r/g, "\n"); break; /* 2.6.206 mentions CT_ThreadedCommentMentions TODO */ case '': pass = true; break; case '': pass = false; break; /* 2.6.202 mention CT_Mention TODO */ /* 18.2.10 extLst CT_ExtensionList ? */ case '': case '': case '': break; /* 18.2.7 ext CT_Extension + */ case '': pass=false; break; default: if(!pass && opts.WTF) throw new Error('unrecognized ' + y[0] + ' in threaded comments'); } return x; }); return out; } function write_tcmnt_xml(comments, people, opts) { var o = [XML_HEADER, writextag('ThreadedComments', null, { 'xmlns': XMLNS.TCMNT }).replace(/[\/]>/, ">")]; comments.forEach(function(carr) { var rootid = ""; (carr[1] || []).forEach(function(c, idx) { if(!c.T) { delete c.ID; return; } if(c.a && people.indexOf(c.a) == -1) people.push(c.a); var tcopts = { ref: carr[0], id: "{54EE7951-7262-4200-6969-" + ("000000000000" + opts.tcid++).slice(-12) + "}" }; if(idx == 0) rootid = tcopts.id; else tcopts.parentId = rootid; c.ID = tcopts.id; if(c.a) tcopts.personId = "{54EE7950-7262-4200-6969-" + ("000000000000" + people.indexOf(c.a)).slice(-12) + "}"; o.push(writextag('threadedComment', writetag('text', c.t||""), tcopts)); }); }); o.push(''); return o.join(""); } /* [MS-XLSX] 2.1.18 */ function parse_people_xml(data/*:string*/, opts) { var out = []; var pass = false; data.replace(tagregex, function xml_tcmnt(x) { var y/*:any*/ = parsexmltag(x); switch(strip_ns(y[0])) { case '': break; /* 2.6.203 person CT_Person TODO: providers */ case '': break; /* 18.2.10 extLst CT_ExtensionList ? */ case '': case '': case '': break; /* 18.2.7 ext CT_Extension + */ case '': pass=false; break; default: if(!pass && opts.WTF) throw new Error('unrecognized ' + y[0] + ' in threaded comments'); } return x; }); return out; } function write_people_xml(people/*, opts*/) { var o = [XML_HEADER, writextag('personList', null, { 'xmlns': XMLNS.TCMNT, 'xmlns:x': XMLNS_main[0] }).replace(/[\/]>/, ">")]; people.forEach(function(person, idx) { o.push(writextag('person', null, { displayName: person, id: "{54EE7950-7262-4200-6969-" + ("000000000000" + idx).slice(-12) + "}", userId: person, providerId: "None" })); }); o.push(""); return o.join(""); }