UNPKG

hyperformula-dc

Version:

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

277 lines (227 loc) 10.7 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.array.map.js"; import "core-js/modules/es.array.find.js"; import "core-js/modules/es.object.get-prototype-of.js"; import "core-js/modules/es.reflect.construct.js"; 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"; 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; } 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); } /** * @license * Copyright (c) 2021 Handsoncode. All rights reserved. */ import { AbsoluteCellRange } from '../../AbsoluteCellRange'; import { CellError, ErrorType, simpleCellAddress } from '../../Cell'; import { ErrorMessage } from '../../error-message'; import { RowSearchStrategy } from '../../Lookup/RowSearchStrategy'; import { StatType } from '../../statistics'; import { zeroIfEmpty } from '../ArithmeticHelper'; import { SimpleRangeValue } from '../SimpleRangeValue'; import { ArgumentTypes, FunctionPlugin } from './FunctionPlugin'; export var LookupPlugin = /*#__PURE__*/function (_FunctionPlugin) { _inherits(LookupPlugin, _FunctionPlugin); var _super = _createSuper(LookupPlugin); function LookupPlugin() { var _this; _classCallCheck(this, LookupPlugin); _this = _super.apply(this, arguments); _this.rowSearch = new RowSearchStrategy(_this.config, _this.dependencyGraph); return _this; } /** * Corresponds to VLOOKUP(key, range, index, [sorted]) * * @param ast * @param state */ _createClass(LookupPlugin, [{ key: "vlookup", value: function vlookup(ast, state) { var _this2 = this; return this.runFunction(ast.args, state, this.metadata('VLOOKUP'), function (key, rangeValue, index, sorted) { var range = rangeValue.range; if (range === undefined) { return new CellError(ErrorType.VALUE, ErrorMessage.WrongType); } if (index < 1) { return new CellError(ErrorType.VALUE, ErrorMessage.LessThanOne); } if (index > range.width()) { return new CellError(ErrorType.REF, ErrorMessage.IndexLarge); } return _this2.doVlookup(zeroIfEmpty(key), rangeValue, index - 1, sorted); }); } /** * Corresponds to HLOOKUP(key, range, index, [sorted]) * * @param ast * @param formulaAddress */ }, { key: "hlookup", value: function hlookup(ast, state) { var _this3 = this; return this.runFunction(ast.args, state, this.metadata('HLOOKUP'), function (key, rangeValue, index, sorted) { var range = rangeValue.range; if (range === undefined) { return new CellError(ErrorType.VALUE, ErrorMessage.WrongType); } if (index < 1) { return new CellError(ErrorType.VALUE, ErrorMessage.LessThanOne); } if (index > range.height()) { return new CellError(ErrorType.REF, ErrorMessage.IndexLarge); } return _this3.doHlookup(zeroIfEmpty(key), rangeValue, index - 1, sorted); }); } }, { key: "match", value: function match(ast, state) { var _this4 = this; return this.runFunction(ast.args, state, this.metadata('MATCH'), function (key, rangeValue, sorted) { return _this4.doMatch(zeroIfEmpty(key), rangeValue, sorted); }); } }, { key: "doVlookup", value: function doVlookup(key, rangeValue, index, sorted) { this.dependencyGraph.stats.start(StatType.VLOOKUP); var range = rangeValue.range; var searchedRange; if (range === undefined) { searchedRange = SimpleRangeValue.onlyValues(rangeValue.data.map(function (arg) { return [arg[0]]; })); } else { searchedRange = SimpleRangeValue.onlyRange(AbsoluteCellRange.spanFrom(range.start, 1, range.height()), this.dependencyGraph); } var rowIndex = this.searchInRange(key, searchedRange, sorted, this.columnSearch); this.dependencyGraph.stats.end(StatType.VLOOKUP); if (rowIndex === -1) { return new CellError(ErrorType.NA, ErrorMessage.ValueNotFound); } var value; if (range === undefined) { value = rangeValue.data[rowIndex][index]; } else { var address = simpleCellAddress(range.sheet, range.start.col + index, range.start.row + rowIndex); value = this.dependencyGraph.getCellValue(address); } if (value instanceof SimpleRangeValue) { return new CellError(ErrorType.VALUE, ErrorMessage.WrongType); } return value; } }, { key: "doHlookup", value: function doHlookup(key, rangeValue, index, sorted) { var range = rangeValue.range; var searchedRange; if (range === undefined) { searchedRange = SimpleRangeValue.onlyValues([rangeValue.data[0]]); } else { searchedRange = SimpleRangeValue.onlyRange(AbsoluteCellRange.spanFrom(range.start, range.width(), 1), this.dependencyGraph); } var colIndex = this.searchInRange(key, searchedRange, sorted, this.rowSearch); if (colIndex === -1) { return new CellError(ErrorType.NA, ErrorMessage.ValueNotFound); } var value; if (range === undefined) { value = rangeValue.data[index][colIndex]; } else { var address = simpleCellAddress(range.sheet, range.start.col + colIndex, range.start.row + index); value = this.dependencyGraph.getCellValue(address); } if (value instanceof SimpleRangeValue) { return new CellError(ErrorType.VALUE, ErrorMessage.WrongType); } return value; } }, { key: "doMatch", value: function doMatch(key, rangeValue, sorted) { if (rangeValue.width() > 1 && rangeValue.height() > 1) { return new CellError(ErrorType.NA); } if (rangeValue.width() === 1) { var index = this.columnSearch.find(key, rangeValue, sorted !== 0); if (index === -1) { return new CellError(ErrorType.NA, ErrorMessage.ValueNotFound); } return index + 1; } else { var _index = this.rowSearch.find(key, rangeValue, sorted !== 0); if (_index === -1) { return new CellError(ErrorType.NA, ErrorMessage.ValueNotFound); } return _index + 1; } } }, { key: "searchInRange", value: function searchInRange(key, range, sorted, searchStrategy) { if (!sorted && typeof key === 'string' && this.arithmeticHelper.requiresRegex(key)) { return searchStrategy.advancedFind(this.arithmeticHelper.eqMatcherFunction(key), range); } else { return searchStrategy.find(key, range, sorted); } } }]); return LookupPlugin; }(FunctionPlugin); LookupPlugin.implementedFunctions = { 'VLOOKUP': { method: 'vlookup', parameters: [{ argumentType: ArgumentTypes.NOERROR }, { argumentType: ArgumentTypes.RANGE }, { argumentType: ArgumentTypes.NUMBER }, { argumentType: ArgumentTypes.BOOLEAN, defaultValue: true }] }, 'HLOOKUP': { method: 'hlookup', parameters: [{ argumentType: ArgumentTypes.NOERROR }, { argumentType: ArgumentTypes.RANGE }, { argumentType: ArgumentTypes.NUMBER }, { argumentType: ArgumentTypes.BOOLEAN, defaultValue: true }] }, 'MATCH': { method: 'match', parameters: [{ argumentType: ArgumentTypes.NOERROR }, { argumentType: ArgumentTypes.RANGE }, { argumentType: ArgumentTypes.NUMBER, defaultValue: 1 }] } };