sheet-comments-writer.js 3.7 KB

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