sheet-comments-writer.js 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. "use strict";
  2. const XmlStream = require('../../utils/xml-stream');
  3. const RelType = require('../../xlsx/rel-type');
  4. const colCache = require('../../utils/col-cache');
  5. const CommentXform = require('../../xlsx/xform/comment/comment-xform');
  6. const VmlShapeXform = require('../../xlsx/xform/comment/vml-shape-xform');
  7. class SheetCommentsWriter {
  8. constructor(worksheet, sheetRelsWriter, options) {
  9. // in a workbook, each sheet will have a number
  10. this.id = options.id;
  11. this.count = 0;
  12. this._worksheet = worksheet;
  13. this._workbook = options.workbook;
  14. this._sheetRelsWriter = sheetRelsWriter;
  15. }
  16. get commentsStream() {
  17. if (!this._commentsStream) {
  18. // eslint-disable-next-line no-underscore-dangle
  19. this._commentsStream = this._workbook._openStream(`/xl/comments${this.id}.xml`);
  20. }
  21. return this._commentsStream;
  22. }
  23. get vmlStream() {
  24. if (!this._vmlStream) {
  25. // eslint-disable-next-line no-underscore-dangle
  26. this._vmlStream = this._workbook._openStream(`xl/drawings/vmlDrawing${this.id}.vml`);
  27. }
  28. return this._vmlStream;
  29. }
  30. _addRelationships() {
  31. const commentRel = {
  32. Type: RelType.Comments,
  33. Target: `../comments${this.id}.xml`
  34. };
  35. this._sheetRelsWriter.addRelationship(commentRel);
  36. const vmlDrawingRel = {
  37. Type: RelType.VmlDrawing,
  38. Target: `../drawings/vmlDrawing${this.id}.vml`
  39. };
  40. this.vmlRelId = this._sheetRelsWriter.addRelationship(vmlDrawingRel);
  41. }
  42. _addCommentRefs() {
  43. this._workbook.commentRefs.push({
  44. commentName: `comments${this.id}`,
  45. vmlDrawing: `vmlDrawing${this.id}`
  46. });
  47. }
  48. _writeOpen() {
  49. this.commentsStream.write('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' + '<comments xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">' + '<authors><author>Author</author></authors>' + '<commentList>');
  50. this.vmlStream.write('<?xml version="1.0" encoding="UTF-8"?>' + '<xml xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:x="urn:schemas-microsoft-com:office:excel">' + '<o:shapelayout v:ext="edit">' + '<o:idmap v:ext="edit" data="1" />' + '</o:shapelayout>' + '<v:shapetype id="_x0000_t202" coordsize="21600,21600" o:spt="202" path="m,l,21600r21600,l21600,xe">' + '<v:stroke joinstyle="miter" />' + '<v:path gradientshapeok="t" o:connecttype="rect" />' + '</v:shapetype>');
  51. }
  52. _writeComment(comment, index) {
  53. const commentXform = new CommentXform();
  54. const commentsXmlStream = new XmlStream();
  55. commentXform.render(commentsXmlStream, comment);
  56. this.commentsStream.write(commentsXmlStream.xml);
  57. const vmlShapeXform = new VmlShapeXform();
  58. const vmlXmlStream = new XmlStream();
  59. vmlShapeXform.render(vmlXmlStream, comment, index);
  60. this.vmlStream.write(vmlXmlStream.xml);
  61. }
  62. _writeClose() {
  63. this.commentsStream.write('</commentList></comments>');
  64. this.vmlStream.write('</xml>');
  65. }
  66. addComments(comments) {
  67. if (comments && comments.length) {
  68. if (!this.startedData) {
  69. this._worksheet.comments = [];
  70. this._writeOpen();
  71. this._addRelationships();
  72. this._addCommentRefs();
  73. this.startedData = true;
  74. }
  75. comments.forEach(item => {
  76. item.refAddress = colCache.decodeAddress(item.ref);
  77. });
  78. comments.forEach(comment => {
  79. this._writeComment(comment, this.count);
  80. this.count += 1;
  81. });
  82. }
  83. }
  84. commit() {
  85. if (this.count) {
  86. this._writeClose();
  87. this.commentsStream.end();
  88. this.vmlStream.end();
  89. }
  90. }
  91. }
  92. module.exports = SheetCommentsWriter;
  93. //# sourceMappingURL=sheet-comments-writer.js.map