@protobi/exceljs
Version:
Excel Workbook Manager - Temporary fork with pivot table enhancements and bug fixes pending upstream merge
284 lines (278 loc) • 12.8 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 _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 _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); }
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 _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); }
var _ = require('../../../utils/under-dash');
var utils = require('../../../utils/utils');
var colCache = require('../../../utils/col-cache');
var BaseXform = require('../base-xform');
var Range = require('../../../doc/range');
function assign(definedName, attributes, name, defaultValue) {
var value = attributes[name];
if (value !== undefined) {
definedName[name] = value;
} else if (defaultValue !== undefined) {
definedName[name] = defaultValue;
}
}
function assignBool(definedName, attributes, name, defaultValue) {
var value = attributes[name];
if (value !== undefined) {
definedName[name] = utils.parseBoolean(value);
} else if (defaultValue !== undefined) {
definedName[name] = defaultValue;
}
}
function optimiseDataValidations(model) {
// Squeeze alike data validations together into rectangular ranges
// to reduce file size and speed up Excel load time
var dvList = _.map(model, function (dataValidation, address) {
return {
address: address,
dataValidation: dataValidation,
marked: false
};
}).sort(function (a, b) {
return _.strcmp(a.address, b.address);
});
var dvMap = _.keyBy(dvList, 'address');
var matchCol = function matchCol(addr, height, col) {
for (var i = 0; i < height; i++) {
var otherAddress = colCache.encodeAddress(addr.row + i, col);
if (!model[otherAddress] || !_.isEqual(model[addr.address], model[otherAddress])) {
return false;
}
}
return true;
};
return dvList.map(function (dv) {
if (!dv.marked) {
var addr = colCache.decodeEx(dv.address);
if (addr.dimensions) {
dvMap[addr.dimensions].marked = true;
return _objectSpread(_objectSpread({}, dv.dataValidation), {}, {
sqref: dv.address
});
}
// iterate downwards - finding matching cells
var height = 1;
var otherAddress = colCache.encodeAddress(addr.row + height, addr.col);
while (model[otherAddress] && _.isEqual(dv.dataValidation, model[otherAddress])) {
height++;
otherAddress = colCache.encodeAddress(addr.row + height, addr.col);
}
// iterate rightwards...
var width = 1;
while (matchCol(addr, height, addr.col + width)) {
width++;
}
// mark all included addresses
for (var i = 0; i < height; i++) {
for (var j = 0; j < width; j++) {
otherAddress = colCache.encodeAddress(addr.row + i, addr.col + j);
dvMap[otherAddress].marked = true;
}
}
if (height > 1 || width > 1) {
var bottom = addr.row + (height - 1);
var right = addr.col + (width - 1);
return _objectSpread(_objectSpread({}, dv.dataValidation), {}, {
sqref: "".concat(dv.address, ":").concat(colCache.encodeAddress(bottom, right))
});
}
return _objectSpread(_objectSpread({}, dv.dataValidation), {}, {
sqref: dv.address
});
}
return null;
}).filter(Boolean);
}
var DataValidationsXform = /*#__PURE__*/function (_BaseXform) {
_inherits(DataValidationsXform, _BaseXform);
var _super = _createSuper(DataValidationsXform);
function DataValidationsXform() {
_classCallCheck(this, DataValidationsXform);
return _super.apply(this, arguments);
}
_createClass(DataValidationsXform, [{
key: "tag",
get: function get() {
return 'dataValidations';
}
}, {
key: "render",
value: function render(xmlStream, model) {
var optimizedModel = optimiseDataValidations(model);
if (optimizedModel.length) {
xmlStream.openNode('dataValidations', {
count: optimizedModel.length
});
optimizedModel.forEach(function (value) {
xmlStream.openNode('dataValidation');
if (value.type !== 'any') {
xmlStream.addAttribute('type', value.type);
if (value.operator && value.type !== 'list' && value.operator !== 'between') {
xmlStream.addAttribute('operator', value.operator);
}
if (value.allowBlank) {
xmlStream.addAttribute('allowBlank', '1');
}
}
if (value.showInputMessage) {
xmlStream.addAttribute('showInputMessage', '1');
}
if (value.promptTitle) {
xmlStream.addAttribute('promptTitle', value.promptTitle);
}
if (value.prompt) {
xmlStream.addAttribute('prompt', value.prompt);
}
if (value.showErrorMessage) {
xmlStream.addAttribute('showErrorMessage', '1');
}
if (value.errorStyle) {
xmlStream.addAttribute('errorStyle', value.errorStyle);
}
if (value.errorTitle) {
xmlStream.addAttribute('errorTitle', value.errorTitle);
}
if (value.error) {
xmlStream.addAttribute('error', value.error);
}
xmlStream.addAttribute('sqref', value.sqref);
(value.formulae || []).forEach(function (formula, index) {
xmlStream.openNode("formula".concat(index + 1));
if (value.type === 'date') {
xmlStream.writeText(utils.dateToExcel(new Date(formula)));
} else {
xmlStream.writeText(formula);
}
xmlStream.closeNode();
});
xmlStream.closeNode();
});
xmlStream.closeNode();
}
}
}, {
key: "parseOpen",
value: function parseOpen(node) {
switch (node.name) {
case 'dataValidations':
this.model = {};
return true;
case 'dataValidation':
{
this._address = node.attributes.sqref;
var dataValidation = {
type: node.attributes.type || 'any',
formulae: []
};
if (node.attributes.type) {
assignBool(dataValidation, node.attributes, 'allowBlank');
}
assignBool(dataValidation, node.attributes, 'showInputMessage');
assignBool(dataValidation, node.attributes, 'showErrorMessage');
switch (dataValidation.type) {
case 'any':
case 'list':
case 'custom':
break;
default:
assign(dataValidation, node.attributes, 'operator', 'between');
break;
}
assign(dataValidation, node.attributes, 'promptTitle');
assign(dataValidation, node.attributes, 'prompt');
assign(dataValidation, node.attributes, 'errorStyle');
assign(dataValidation, node.attributes, 'errorTitle');
assign(dataValidation, node.attributes, 'error');
this._dataValidation = dataValidation;
return true;
}
case 'formula1':
case 'formula2':
this._formula = [];
return true;
default:
return false;
}
}
}, {
key: "parseText",
value: function parseText(text) {
if (this._formula) {
this._formula.push(text);
}
}
}, {
key: "parseClose",
value: function parseClose(name) {
var _this = this;
switch (name) {
case 'dataValidations':
return false;
case 'dataValidation':
{
if (!this._dataValidation.formulae || !this._dataValidation.formulae.length) {
delete this._dataValidation.formulae;
delete this._dataValidation.operator;
}
// The four known cases: 1. E4:L9 N4:U9 2.E4 L9 3. N4:U9 4. E4
var list = this._address.split(/\s+/g) || [];
list.forEach(function (addr) {
if (addr.includes(':')) {
var range = new Range(addr);
range.forEachAddress(function (address) {
_this.model[address] = _this._dataValidation;
});
} else {
_this.model[addr] = _this._dataValidation;
}
});
return true;
}
case 'formula1':
case 'formula2':
{
var formula = this._formula.join('');
switch (this._dataValidation.type) {
case 'whole':
case 'textLength':
formula = parseInt(formula, 10);
break;
case 'decimal':
formula = parseFloat(formula);
break;
case 'date':
formula = utils.excelToDate(parseFloat(formula));
break;
default:
break;
}
this._dataValidation.formulae.push(formula);
this._formula = undefined;
return true;
}
default:
return true;
}
}
}]);
return DataValidationsXform;
}(BaseXform);
module.exports = DataValidationsXform;
//# sourceMappingURL=data-validations-xform.js.map