@protobi/exceljs
Version:
Excel Workbook Manager - Temporary fork with pivot table enhancements and bug fixes pending upstream merge
130 lines (124 loc) • 4.95 kB
JavaScript
"use strict";
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
/* eslint-disable max-classes-per-file */
var utils = require('../../utils/utils');
var RelType = require('../../xlsx/rel-type');
var HyperlinksProxy = /*#__PURE__*/function () {
function HyperlinksProxy(sheetRelsWriter) {
_classCallCheck(this, HyperlinksProxy);
this.writer = sheetRelsWriter;
}
_createClass(HyperlinksProxy, [{
key: "push",
value: function push(hyperlink) {
this.writer.addHyperlink(hyperlink);
}
}]);
return HyperlinksProxy;
}();
var SheetRelsWriter = /*#__PURE__*/function () {
function SheetRelsWriter(options) {
_classCallCheck(this, SheetRelsWriter);
// in a workbook, each sheet will have a number
this.id = options.id;
// count of all relationships
this.count = 0;
// keep record of all hyperlinks
this._hyperlinks = [];
this._workbook = options.workbook;
}
_createClass(SheetRelsWriter, [{
key: "stream",
get: function get() {
if (!this._stream) {
// eslint-disable-next-line no-underscore-dangle
this._stream = this._workbook._openStream("/xl/worksheets/_rels/sheet".concat(this.id, ".xml.rels"));
}
return this._stream;
}
}, {
key: "length",
get: function get() {
return this._hyperlinks.length;
}
}, {
key: "each",
value: function each(fn) {
return this._hyperlinks.forEach(fn);
}
}, {
key: "hyperlinksProxy",
get: function get() {
return this._hyperlinksProxy || (this._hyperlinksProxy = new HyperlinksProxy(this));
}
}, {
key: "addHyperlink",
value: function addHyperlink(hyperlink) {
// Write to stream
var relationship = {
Target: hyperlink.target,
Type: RelType.Hyperlink,
TargetMode: 'External'
};
var rId = this._writeRelationship(relationship);
// store sheet stuff for later
this._hyperlinks.push({
rId: rId,
address: hyperlink.address
});
}
}, {
key: "addMedia",
value: function addMedia(media) {
return this._writeRelationship(media);
}
}, {
key: "addRelationship",
value: function addRelationship(rel) {
return this._writeRelationship(rel);
}
}, {
key: "commit",
value: function commit() {
if (this.count) {
// write xml utro
this._writeClose();
// and close stream
this.stream.end();
}
}
// ================================================================================
}, {
key: "_writeOpen",
value: function _writeOpen() {
this.stream.write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n <Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">");
}
}, {
key: "_writeRelationship",
value: function _writeRelationship(relationship) {
if (!this.count) {
this._writeOpen();
}
var rId = "rId".concat(++this.count);
if (relationship.TargetMode) {
this.stream.write("<Relationship Id=\"".concat(rId, "\"") + " Type=\"".concat(relationship.Type, "\"") + " Target=\"".concat(utils.xmlEncode(relationship.Target), "\"") + " TargetMode=\"".concat(relationship.TargetMode, "\"") + '/>');
} else {
this.stream.write("<Relationship Id=\"".concat(rId, "\" Type=\"").concat(relationship.Type, "\" Target=\"").concat(relationship.Target, "\"/>"));
}
return rId;
}
}, {
key: "_writeClose",
value: function _writeClose() {
this.stream.write('</Relationships>');
}
}]);
return SheetRelsWriter;
}();
module.exports = SheetRelsWriter;
//# sourceMappingURL=sheet-rels-writer.js.map