UNPKG

hyperformula-dc

Version:

HyperFormula is a JavaScript engine for efficient processing of spreadsheet-like data and formulas

357 lines (309 loc) 13.8 kB
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } import "core-js/modules/es.symbol.js"; import "core-js/modules/es.symbol.description.js"; import "core-js/modules/es.object.to-string.js"; import "core-js/modules/es.symbol.iterator.js"; import "core-js/modules/es.array.iterator.js"; import "core-js/modules/es.string.iterator.js"; import "core-js/modules/web.dom-collections.iterator.js"; import "core-js/modules/es.array.slice.js"; import "core-js/modules/es.function.name.js"; import "core-js/modules/es.array.from.js"; import "core-js/modules/es.object.get-prototype-of.js"; import "core-js/modules/es.reflect.construct.js"; function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } /** * @license * Copyright (c) 2021 Handsoncode. All rights reserved. */ import { AbsoluteCellRange } from '../AbsoluteCellRange'; import { ArraySize } from '../ArraySize'; import { ArrayValue, ErroredArray, NotComputedArray } from '../ArrayValue'; import { CellError, equalSimpleCellAddress, ErrorType } from '../Cell'; import { ErrorMessage } from '../error-message'; import { EmptyValue, getRawValue } from '../interpreter/InterpreterValue'; import { ColumnsSpan, RowsSpan } from '../Span'; export var FormulaVertex = /*#__PURE__*/function () { function FormulaVertex(formula, cellAddress, version) { _classCallCheck(this, FormulaVertex); this.formula = formula; this.cellAddress = cellAddress; this.version = version; } _createClass(FormulaVertex, [{ key: "width", get: function get() { return 1; } }, { key: "height", get: function get() { return 1; } /** * Returns formula stored in this vertex */ }, { key: "getFormula", value: function getFormula(updatingService) { this.ensureRecentData(updatingService); return this.formula; } }, { key: "ensureRecentData", value: function ensureRecentData(updatingService) { if (this.version != updatingService.version()) { var _updatingService$appl = updatingService.applyTransformations(this.formula, this.cellAddress, this.version), _updatingService$appl2 = _slicedToArray(_updatingService$appl, 3), newAst = _updatingService$appl2[0], newAddress = _updatingService$appl2[1], newVersion = _updatingService$appl2[2]; this.formula = newAst; this.cellAddress = newAddress; this.version = newVersion; } } /** * Returns address of the cell associated with vertex */ }, { key: "getAddress", value: function getAddress(updatingService) { this.ensureRecentData(updatingService); return this.cellAddress; } }], [{ key: "fromAst", value: function fromAst(formula, address, size, version) { if (size.isScalar()) { return new FormulaCellVertex(formula, address, version); } else { return new ArrayVertex(formula, address, size, version); } } }]); return FormulaVertex; }(); export var ArrayVertex = /*#__PURE__*/function (_FormulaVertex) { _inherits(ArrayVertex, _FormulaVertex); var _super = _createSuper(ArrayVertex); function ArrayVertex(formula, cellAddress, size) { var _this; var version = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0; _classCallCheck(this, ArrayVertex); _this = _super.call(this, formula, cellAddress, version); if (size.isRef) { _this.array = new ErroredArray(new CellError(ErrorType.REF, ErrorMessage.NoSpaceForArrayResult), ArraySize.error()); } else { _this.array = new NotComputedArray(size); } return _this; } _createClass(ArrayVertex, [{ key: "width", get: function get() { return this.array.width(); } }, { key: "height", get: function get() { return this.array.height(); } }, { key: "sheet", get: function get() { return this.cellAddress.sheet; } }, { key: "leftCorner", get: function get() { return this.cellAddress; } }, { key: "setCellValue", value: function setCellValue(value) { if (value instanceof CellError) { this.setErrorValue(value); return value; } var array = ArrayValue.fromInterpreterValue(value); array.resize(this.array.size); this.array = array; return value; } }, { key: "getCellValue", value: function getCellValue() { if (this.array instanceof NotComputedArray) { throw Error('Array not computed yet.'); } return this.array.simpleRangeValue(); } }, { key: "valueOrUndef", value: function valueOrUndef() { if (this.array instanceof NotComputedArray) { return undefined; } return this.array.simpleRangeValue(); } }, { key: "getArrayCellValue", value: function getArrayCellValue(address) { var col = address.col - this.cellAddress.col; var row = address.row - this.cellAddress.row; try { return this.array.get(col, row); } catch (e) { return new CellError(ErrorType.REF); } } }, { key: "getArrayCellRawValue", value: function getArrayCellRawValue(address) { var val = this.getArrayCellValue(address); if (val instanceof CellError || val === EmptyValue) { return undefined; } else { return getRawValue(val); } } }, { key: "setArrayCellValue", value: function setArrayCellValue(address, value) { var col = address.col - this.cellAddress.col; var row = address.row - this.cellAddress.row; if (this.array instanceof ArrayValue) { this.array.set(col, row, value); } } }, { key: "setNoSpace", value: function setNoSpace() { this.array = new ErroredArray(new CellError(ErrorType.SPILL, ErrorMessage.NoSpaceForArrayResult), ArraySize.error()); return this.getCellValue(); } }, { key: "getRange", value: function getRange() { return AbsoluteCellRange.spanFrom(this.cellAddress, this.width, this.height); } }, { key: "setAddress", value: function setAddress(address) { this.cellAddress = address; } }, { key: "setFormula", value: function setFormula(newFormula) { this.formula = newFormula; } }, { key: "spansThroughSheetRows", value: function spansThroughSheetRows(sheet, startRow) { var endRow = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : startRow; return this.cellAddress.sheet === sheet && this.cellAddress.row <= endRow && startRow < this.cellAddress.row + this.height; } }, { key: "spansThroughSheetColumn", value: function spansThroughSheetColumn(sheet, col) { var columnEnd = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : col; return this.cellAddress.sheet === sheet && this.cellAddress.col <= columnEnd && col < this.cellAddress.col + this.width; } }, { key: "isComputed", value: function isComputed() { return !(this.array instanceof NotComputedArray); } }, { key: "columnsFromArray", value: function columnsFromArray() { return ColumnsSpan.fromNumberOfColumns(this.cellAddress.sheet, this.cellAddress.col, this.width); } }, { key: "rowsFromArray", value: function rowsFromArray() { return RowsSpan.fromNumberOfRows(this.cellAddress.sheet, this.cellAddress.row, this.height); } /** * No-op as array vertices are transformed eagerly. * */ }, { key: "ensureRecentData", value: function ensureRecentData(_updatingService) {} }, { key: "isLeftCorner", value: function isLeftCorner(address) { return equalSimpleCellAddress(this.cellAddress, address); } }, { key: "setErrorValue", value: function setErrorValue(error) { this.array = new ErroredArray(error, this.array.size); } }]); return ArrayVertex; }(FormulaVertex); /** * Represents vertex which keeps formula */ export var FormulaCellVertex = /*#__PURE__*/function (_FormulaVertex2) { _inherits(FormulaCellVertex, _FormulaVertex2); var _super2 = _createSuper(FormulaCellVertex); function FormulaCellVertex( /** Formula in AST format */ formula, /** Address which this vertex represents */ address, version) { _classCallCheck(this, FormulaCellVertex); return _super2.call(this, formula, address, version); } _createClass(FormulaCellVertex, [{ key: "valueOrUndef", value: function valueOrUndef() { return this.cachedCellValue; } /** * Sets computed cell value stored in this vertex */ }, { key: "setCellValue", value: function setCellValue(cellValue) { this.cachedCellValue = cellValue; return this.cachedCellValue; } /** * Returns cell value stored in vertex */ }, { key: "getCellValue", value: function getCellValue() { if (this.cachedCellValue !== undefined) { return this.cachedCellValue; } else { throw Error('Value of the formula cell is not computed.'); } } }, { key: "isComputed", value: function isComputed() { return this.cachedCellValue !== undefined; } }]); return FormulaCellVertex; }(FormulaVertex);