UNPKG

hyperformula

Version:

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

192 lines (190 loc) 7.39 kB
"use strict"; exports.__esModule = true; exports.invalidSimpleRowAddress = exports.invalidSimpleColumnAddress = exports.invalidSimpleCellAddress = exports.getCellValueType = exports.getCellValueFormat = exports.getCellValueDetailedType = exports.getCellType = exports.equalSimpleCellAddress = exports.addressKey = exports.absoluteSheetReference = exports.ErrorType = exports.CellValueTypeOrd = exports.CellValueType = exports.CellValueNoNumber = exports.CellValueJustNumber = exports.CellValueDetailedType = exports.CellType = exports.CellError = void 0; exports.isSimpleCellAddress = isSimpleCellAddress; exports.simpleRowAddress = exports.simpleColumnAddress = exports.simpleCellAddress = exports.movedSimpleCellAddress = void 0; var _DependencyGraph = require("./DependencyGraph"); var _errorMessage = require("./error-message"); var _InterpreterValue = require("./interpreter/InterpreterValue"); var _SimpleRangeValue = require("./SimpleRangeValue"); /** * @license * Copyright (c) 2025 Handsoncode. All rights reserved. */ /** * Possible errors returned by our interpreter. */ var ErrorType; (function (ErrorType) { /** Division by zero. */ ErrorType["DIV_BY_ZERO"] = "DIV_BY_ZERO"; /** Unknown function name. */ ErrorType["NAME"] = "NAME"; ErrorType["VALUE"] = "VALUE"; ErrorType["NUM"] = "NUM"; ErrorType["NA"] = "NA"; /** Cyclic dependency. */ ErrorType["CYCLE"] = "CYCLE"; /** Wrong address reference. */ ErrorType["REF"] = "REF"; /** Array spill error. */ ErrorType["SPILL"] = "SPILL"; /** Invalid/missing licence error. */ ErrorType["LIC"] = "LIC"; /** Generic error */ ErrorType["ERROR"] = "ERROR"; })(ErrorType || (exports.ErrorType = ErrorType = {})); var CellType; (function (CellType) { CellType["FORMULA"] = "FORMULA"; CellType["VALUE"] = "VALUE"; CellType["ARRAY"] = "ARRAY"; CellType["EMPTY"] = "EMPTY"; CellType["ARRAYFORMULA"] = "ARRAYFORMULA"; })(CellType || (exports.CellType = CellType = {})); const getCellType = (vertex, address) => { if (vertex instanceof _DependencyGraph.ArrayVertex) { if (vertex.isLeftCorner(address)) { return CellType.ARRAYFORMULA; } else { return CellType.ARRAY; } } if (vertex instanceof _DependencyGraph.FormulaCellVertex || vertex instanceof _DependencyGraph.ParsingErrorVertex) { return CellType.FORMULA; } if (vertex instanceof _DependencyGraph.ValueCellVertex) { return CellType.VALUE; } return CellType.EMPTY; }; exports.getCellType = getCellType; var CellValueNoNumber; (function (CellValueNoNumber) { CellValueNoNumber["EMPTY"] = "EMPTY"; CellValueNoNumber["NUMBER"] = "NUMBER"; CellValueNoNumber["STRING"] = "STRING"; CellValueNoNumber["BOOLEAN"] = "BOOLEAN"; CellValueNoNumber["ERROR"] = "ERROR"; })(CellValueNoNumber || (exports.CellValueNoNumber = CellValueNoNumber = {})); var CellValueJustNumber; (function (CellValueJustNumber) { CellValueJustNumber["NUMBER"] = "NUMBER"; })(CellValueJustNumber || (exports.CellValueJustNumber = CellValueJustNumber = {})); const CellValueType = exports.CellValueType = Object.assign(Object.assign({}, CellValueNoNumber), CellValueJustNumber); const CellValueDetailedType = exports.CellValueDetailedType = Object.assign(Object.assign({}, CellValueNoNumber), _InterpreterValue.NumberType); const CellValueTypeOrd = arg => { switch (arg) { case CellValueType.EMPTY: return 0; case CellValueType.NUMBER: return 1; case CellValueType.STRING: return 2; case CellValueType.BOOLEAN: return 3; case CellValueType.ERROR: return 4; } throw new Error('Cell value not computed'); }; exports.CellValueTypeOrd = CellValueTypeOrd; const getCellValueType = cellValue => { if (cellValue === _InterpreterValue.EmptyValue) { return CellValueType.EMPTY; } if (cellValue instanceof CellError || cellValue instanceof _SimpleRangeValue.SimpleRangeValue) { return CellValueType.ERROR; } if (typeof cellValue === 'string') { return CellValueType.STRING; } else if ((0, _InterpreterValue.isExtendedNumber)(cellValue)) { return CellValueType.NUMBER; } else if (typeof cellValue === 'boolean') { return CellValueType.BOOLEAN; } throw new Error('Cell value not computed'); }; exports.getCellValueType = getCellValueType; const getCellValueDetailedType = cellValue => { if ((0, _InterpreterValue.isExtendedNumber)(cellValue)) { return (0, _InterpreterValue.getTypeOfExtendedNumber)(cellValue); } else { return getCellValueType(cellValue); } }; exports.getCellValueDetailedType = getCellValueDetailedType; const getCellValueFormat = cellValue => { if ((0, _InterpreterValue.isExtendedNumber)(cellValue)) { return (0, _InterpreterValue.getFormatOfExtendedNumber)(cellValue); } else { return undefined; } }; exports.getCellValueFormat = getCellValueFormat; class CellError { constructor(type, message, root) { this.type = type; this.message = message; this.root = root; } /** * Returns a CellError with a given message. * @param {string} detailedMessage - message to be displayed */ static parsingError(detailedMessage) { return new CellError(ErrorType.ERROR, `${_errorMessage.ErrorMessage.ParseError}${detailedMessage ? ' ' + detailedMessage : ''}`); } attachRootVertex(vertex) { if (this.root === undefined) { return new CellError(this.type, this.message, vertex); } else { return this; } } } exports.CellError = CellError; const simpleRowAddress = (sheet, row) => ({ sheet, row }); exports.simpleRowAddress = simpleRowAddress; const invalidSimpleRowAddress = address => address.row < 0; exports.invalidSimpleRowAddress = invalidSimpleRowAddress; const simpleColumnAddress = (sheet, col) => ({ sheet, col }); exports.simpleColumnAddress = simpleColumnAddress; const invalidSimpleColumnAddress = address => address.col < 0; exports.invalidSimpleColumnAddress = invalidSimpleColumnAddress; const simpleCellAddress = (sheet, col, row) => ({ sheet, col, row }); exports.simpleCellAddress = simpleCellAddress; const invalidSimpleCellAddress = address => address.col < 0 || address.row < 0; exports.invalidSimpleCellAddress = invalidSimpleCellAddress; const movedSimpleCellAddress = (address, toSheet, toRight, toBottom) => { return simpleCellAddress(toSheet, address.col + toRight, address.row + toBottom); }; exports.movedSimpleCellAddress = movedSimpleCellAddress; const addressKey = address => `${address.sheet},${address.row},${address.col}`; /** * Checks if the object is a simple cell address. */ exports.addressKey = addressKey; function isSimpleCellAddress(obj) { var _a, _b, _c; return obj && (typeof obj === 'object' || typeof obj === 'function') && typeof ((_a = obj) === null || _a === void 0 ? void 0 : _a.sheet) === 'number' && typeof ((_b = obj) === null || _b === void 0 ? void 0 : _b.col) === 'number' && typeof ((_c = obj) === null || _c === void 0 ? void 0 : _c.row) === 'number'; } const absoluteSheetReference = (address, baseAddress) => { var _a; return (_a = address.sheet) !== null && _a !== void 0 ? _a : baseAddress.sheet; }; exports.absoluteSheetReference = absoluteSheetReference; const equalSimpleCellAddress = (left, right) => { return left.sheet === right.sheet && left.col === right.col && left.row === right.row; }; exports.equalSimpleCellAddress = equalSimpleCellAddress;