@protobi/exceljs
Version:
Excel Workbook Manager - Temporary fork with pivot table enhancements and bug fixes pending upstream merge
155 lines (149 loc) • 10.6 kB
JavaScript
;
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 ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
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); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
function _createSuper(t) { var r = _isNativeReflectConstruct(); return function () { var e, o = _getPrototypeOf(t); if (r) { var s = _getPrototypeOf(this).constructor; e = Reflect.construct(o, arguments, s); } else e = o.apply(this, arguments); return _possibleConstructorReturn(this, e); }; }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
var _require = require('uuid'),
uuidv4 = _require.v4;
var XmlStream = require('../../../utils/xml-stream');
var BaseXform = require('../base-xform');
var PivotTableXform = /*#__PURE__*/function (_BaseXform) {
_inherits(PivotTableXform, _BaseXform);
var _super = _createSuper(PivotTableXform);
function PivotTableXform() {
var _this;
_classCallCheck(this, PivotTableXform);
_this = _super.call(this);
_this.map = {};
return _this;
}
_createClass(PivotTableXform, [{
key: "prepare",
value: function prepare(model) {
// TK
}
}, {
key: "tag",
get: function get() {
// http://www.datypic.com/sc/ooxml/e-ssml_pivotTableDefinition.html
return 'pivotTableDefinition';
}
}, {
key: "render",
value: function render(xmlStream, model) {
// eslint-disable-next-line no-unused-vars
var rows = model.rows,
columns = model.columns,
values = model.values,
metric = model.metric,
cacheFields = model.cacheFields,
cacheId = model.cacheId,
applyWidthHeightFormats = model.applyWidthHeightFormats;
// Examples
// --------
// rows: [0, 1], // only 2 items possible for now
// columns: [2], // only 1 item possible for now
// values: [4], // only 1 item possible for now
// metric: 'sum', // only 'sum' possible for now
//
// the numbers are indices into `cacheFields`.
// Generate unique UID for each pivot table
var uniqueUid = "{".concat(uuidv4(), "}").toUpperCase();
xmlStream.openXml(XmlStream.StdDocAttributes);
xmlStream.openNode(this.tag, _objectSpread(_objectSpread({}, PivotTableXform.PIVOT_TABLE_ATTRIBUTES), {}, {
'xr:uid': uniqueUid,
name: 'PivotTable2',
cacheId: cacheId,
applyNumberFormats: '0',
applyBorderFormats: '0',
applyFontFormats: '0',
applyPatternFormats: '0',
applyAlignmentFormats: '0',
applyWidthHeightFormats: applyWidthHeightFormats,
dataCaption: 'Values',
updatedVersion: '8',
minRefreshableVersion: '3',
useAutoFormatting: '1',
itemPrintTitles: '1',
createdVersion: '8',
indent: '0',
compact: '0',
compactData: '0',
multipleFieldFilters: '0'
}));
// Note: keeping this pretty-printed and verbose for now to ease debugging.
//
// location: ref="A3:E15"
// pivotFields
// rowFields and rowItems
// colFields and colItems
// dataFields
// pivotTableStyleInfo
xmlStream.writeXml("\n <location ref=\"A3:E15\" firstHeaderRow=\"1\" firstDataRow=\"2\" firstDataCol=\"1\" />\n <pivotFields count=\"".concat(cacheFields.length, "\">\n ").concat(renderPivotFields(model), "\n </pivotFields>\n <rowFields count=\"").concat(rows.length, "\">\n ").concat(rows.map(function (rowIndex) {
return "<field x=\"".concat(rowIndex, "\" />");
}).join('\n '), "\n </rowFields>\n <rowItems count=\"1\">\n <i t=\"grand\"><x /></i>\n </rowItems>\n <colFields count=\"").concat(columns.length, "\">\n ").concat(columns.map(function (columnIndex) {
return "<field x=\"".concat(columnIndex, "\" />");
}).join('\n '), "\n </colFields>\n <colItems count=\"1\">\n <i t=\"grand\"><x /></i>\n </colItems>\n <dataFields count=\"").concat(values.length, "\">\n <dataField\n name=\"").concat(metric === 'count' ? 'Count' : 'Sum', " of ").concat(cacheFields[values[0]].name, "\"\n fld=\"").concat(values[0], "\"\n baseField=\"0\"\n baseItem=\"0\"\n ").concat(metric === 'count' ? 'subtotal="count"' : '', "\n />\n </dataFields>\n <pivotTableStyleInfo\n name=\"PivotStyleLight16\"\n showRowHeaders=\"1\"\n showColHeaders=\"1\"\n showRowStripes=\"0\"\n showColStripes=\"0\"\n showLastColumn=\"1\"\n />\n <extLst>\n <ext\n uri=\"{962EF5D1-5CA2-4c93-8EF4-DBF5C05439D2}\"\n xmlns:x14=\"http://schemas.microsoft.com/office/spreadsheetml/2009/9/main\"\n >\n <x14:pivotTableDefinition\n hideValuesRow=\"1\"\n xmlns:xm=\"http://schemas.microsoft.com/office/excel/2006/main\"\n />\n </ext>\n <ext\n uri=\"{747A6164-185A-40DC-8AA5-F01512510D54}\"\n xmlns:xpdl=\"http://schemas.microsoft.com/office/spreadsheetml/2016/pivotdefaultlayout\"\n >\n <xpdl:pivotTableDefinition16\n EnabledSubtotalsDefault=\"0\"\n SubtotalsOnTopDefault=\"0\"\n />\n </ext>\n </extLst>\n "));
xmlStream.closeNode();
}
}, {
key: "parseOpen",
value: function parseOpen(node) {
// TK
}
}, {
key: "parseText",
value: function parseText(text) {
// TK
}
}, {
key: "parseClose",
value: function parseClose(name) {
// TK
}
}, {
key: "reconcile",
value: function reconcile(model, options) {
// TK
}
}]);
return PivotTableXform;
}(BaseXform); // Helpers
function renderPivotFields(pivotTable) {
/* eslint-disable no-nested-ternary */
return pivotTable.cacheFields.map(function (cacheField, fieldIndex) {
var fieldType = pivotTable.rows.indexOf(fieldIndex) >= 0 ? 'row' : pivotTable.columns.indexOf(fieldIndex) >= 0 ? 'column' : pivotTable.values.indexOf(fieldIndex) >= 0 ? 'value' : null;
return renderPivotField(fieldType, cacheField.sharedItems);
}).join('');
}
function renderPivotField(fieldType, sharedItems) {
// fieldType: 'row', 'column', 'value', null
var defaultAttributes = 'compact="0" outline="0" showAll="0" defaultSubtotal="0"';
if (fieldType === 'row' || fieldType === 'column') {
var axis = fieldType === 'row' ? 'axisRow' : 'axisCol';
return "\n <pivotField axis=\"".concat(axis, "\" ").concat(defaultAttributes, ">\n <items count=\"").concat(sharedItems.length + 1, "\">\n ").concat(sharedItems.map(function (item, index) {
return "<item x=\"".concat(index, "\" />");
}).join('\n '), "\n </items>\n </pivotField>\n ");
}
return "\n <pivotField\n ".concat(fieldType === 'value' ? 'dataField="1"' : '', "\n ").concat(defaultAttributes, "\n />\n ");
}
PivotTableXform.PIVOT_TABLE_ATTRIBUTES = {
xmlns: 'http://schemas.openxmlformats.org/spreadsheetml/2006/main',
'xmlns:mc': 'http://schemas.openxmlformats.org/markup-compatibility/2006',
'mc:Ignorable': 'xr',
'xmlns:xr': 'http://schemas.microsoft.com/office/spreadsheetml/2014/revision'
};
module.exports = PivotTableXform;
//# sourceMappingURL=pivot-table-xform.js.map