sheet-rels-writer.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. "use strict";
  2. /* eslint-disable max-classes-per-file */
  3. const utils = require('../../utils/utils');
  4. const RelType = require('../../xlsx/rel-type');
  5. class HyperlinksProxy {
  6. constructor(sheetRelsWriter) {
  7. this.writer = sheetRelsWriter;
  8. }
  9. push(hyperlink) {
  10. this.writer.addHyperlink(hyperlink);
  11. }
  12. }
  13. class SheetRelsWriter {
  14. constructor(options) {
  15. // in a workbook, each sheet will have a number
  16. this.id = options.id;
  17. // count of all relationships
  18. this.count = 0;
  19. // keep record of all hyperlinks
  20. this._hyperlinks = [];
  21. this._workbook = options.workbook;
  22. }
  23. get stream() {
  24. if (!this._stream) {
  25. // eslint-disable-next-line no-underscore-dangle
  26. this._stream = this._workbook._openStream(`/xl/worksheets/_rels/sheet${this.id}.xml.rels`);
  27. }
  28. return this._stream;
  29. }
  30. get length() {
  31. return this._hyperlinks.length;
  32. }
  33. each(fn) {
  34. return this._hyperlinks.forEach(fn);
  35. }
  36. get hyperlinksProxy() {
  37. return this._hyperlinksProxy || (this._hyperlinksProxy = new HyperlinksProxy(this));
  38. }
  39. addHyperlink(hyperlink) {
  40. // Write to stream
  41. const relationship = {
  42. Target: hyperlink.target,
  43. Type: RelType.Hyperlink,
  44. TargetMode: 'External'
  45. };
  46. const rId = this._writeRelationship(relationship);
  47. // store sheet stuff for later
  48. this._hyperlinks.push({
  49. rId,
  50. address: hyperlink.address
  51. });
  52. }
  53. addMedia(media) {
  54. return this._writeRelationship(media);
  55. }
  56. addRelationship(rel) {
  57. return this._writeRelationship(rel);
  58. }
  59. commit() {
  60. if (this.count) {
  61. // write xml utro
  62. this._writeClose();
  63. // and close stream
  64. this.stream.end();
  65. }
  66. }
  67. // ================================================================================
  68. _writeOpen() {
  69. this.stream.write(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  70. <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">`);
  71. }
  72. _writeRelationship(relationship) {
  73. if (!this.count) {
  74. this._writeOpen();
  75. }
  76. const rId = `rId${++this.count}`;
  77. if (relationship.TargetMode) {
  78. this.stream.write(`<Relationship Id="${rId}"` + ` Type="${relationship.Type}"` + ` Target="${utils.xmlEncode(relationship.Target)}"` + ` TargetMode="${relationship.TargetMode}"` + '/>');
  79. } else {
  80. this.stream.write(`<Relationship Id="${rId}" Type="${relationship.Type}" Target="${relationship.Target}"/>`);
  81. }
  82. return rId;
  83. }
  84. _writeClose() {
  85. this.stream.write('</Relationships>');
  86. }
  87. }
  88. module.exports = SheetRelsWriter;
  89. //# sourceMappingURL=sheet-rels-writer.js.map