UNPKG

hyperformula-dc

Version:

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

883 lines (751 loc) 36.6 kB
"use strict"; require("core-js/modules/es.array.slice.js"); require("core-js/modules/es.object.to-string.js"); require("core-js/modules/es.function.name.js"); require("core-js/modules/es.array.from.js"); require("core-js/modules/es.string.iterator.js"); require("core-js/modules/es.symbol.js"); require("core-js/modules/es.symbol.description.js"); require("core-js/modules/es.symbol.iterator.js"); require("core-js/modules/es.array.iterator.js"); require("core-js/modules/web.dom-collections.iterator.js"); exports.__esModule = true; exports.CrudOperations = void 0; require("core-js/modules/es.array.concat.js"); require("core-js/modules/es.array.sort.js"); require("core-js/modules/es.array.map.js"); require("core-js/modules/es.array.reduce.js"); require("core-js/modules/es.number.is-integer.js"); require("core-js/modules/es.number.constructor.js"); var _AbsoluteCellRange = require("./AbsoluteCellRange"); var _Cell = require("./Cell"); var _CellContentParser = require("./CellContentParser"); var _errors = require("./errors"); var _NamedExpressions = require("./NamedExpressions"); var _Operations = require("./Operations"); var _Sheet = require("./Sheet"); var _Span = require("./Span"); var _UndoRedo = require("./UndoRedo"); 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 _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 _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; } 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 _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; } var CrudOperations = /*#__PURE__*/function () { function CrudOperations(config, operations, undoRedo, clipboardOperations, dependencyGraph, columnSearch, parser, cellContentParser, lazilyTransformingAstService, namedExpressions) { _classCallCheck(this, CrudOperations); this.operations = operations; this.undoRedo = undoRedo; this.clipboardOperations = clipboardOperations; this.dependencyGraph = dependencyGraph; this.columnSearch = columnSearch; this.parser = parser; this.cellContentParser = cellContentParser; this.lazilyTransformingAstService = lazilyTransformingAstService; this.namedExpressions = namedExpressions; this.maxRows = config.maxRows; this.maxColumns = config.maxColumns; } _createClass(CrudOperations, [{ key: "addRows", value: function addRows(sheet) { for (var _len = arguments.length, indexes = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { indexes[_key - 1] = arguments[_key]; } var addRowsCommand = new _Operations.AddRowsCommand(sheet, indexes); this.ensureItIsPossibleToAddRows.apply(this, [sheet].concat(indexes)); this.undoRedo.clearRedoStack(); this.clipboardOperations.abortCut(); this.operations.addRows(addRowsCommand); this.undoRedo.saveOperation(new _UndoRedo.AddRowsUndoEntry(addRowsCommand)); } }, { key: "removeRows", value: function removeRows(sheet) { for (var _len2 = arguments.length, indexes = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { indexes[_key2 - 1] = arguments[_key2]; } var removeRowsCommand = new _Operations.RemoveRowsCommand(sheet, indexes); this.ensureItIsPossibleToRemoveRows.apply(this, [sheet].concat(indexes)); this.undoRedo.clearRedoStack(); this.clipboardOperations.abortCut(); var rowsRemovals = this.operations.removeRows(removeRowsCommand); this.undoRedo.saveOperation(new _UndoRedo.RemoveRowsUndoEntry(removeRowsCommand, rowsRemovals)); } }, { key: "addColumns", value: function addColumns(sheet) { for (var _len3 = arguments.length, indexes = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) { indexes[_key3 - 1] = arguments[_key3]; } var addColumnsCommand = new _Operations.AddColumnsCommand(sheet, indexes); this.ensureItIsPossibleToAddColumns.apply(this, [sheet].concat(indexes)); this.undoRedo.clearRedoStack(); this.clipboardOperations.abortCut(); this.operations.addColumns(addColumnsCommand); this.undoRedo.saveOperation(new _UndoRedo.AddColumnsUndoEntry(addColumnsCommand)); } }, { key: "removeColumns", value: function removeColumns(sheet) { for (var _len4 = arguments.length, indexes = new Array(_len4 > 1 ? _len4 - 1 : 0), _key4 = 1; _key4 < _len4; _key4++) { indexes[_key4 - 1] = arguments[_key4]; } var removeColumnsCommand = new _Operations.RemoveColumnsCommand(sheet, indexes); this.ensureItIsPossibleToRemoveColumns.apply(this, [sheet].concat(indexes)); this.undoRedo.clearRedoStack(); this.clipboardOperations.abortCut(); var columnsRemovals = this.operations.removeColumns(removeColumnsCommand); this.undoRedo.saveOperation(new _UndoRedo.RemoveColumnsUndoEntry(removeColumnsCommand, columnsRemovals)); } }, { key: "moveCells", value: function moveCells(sourceLeftCorner, width, height, destinationLeftCorner) { this.undoRedo.clearRedoStack(); this.clipboardOperations.abortCut(); var _this$operations$move = this.operations.moveCells(sourceLeftCorner, width, height, destinationLeftCorner), version = _this$operations$move.version, overwrittenCellsData = _this$operations$move.overwrittenCellsData, addedGlobalNamedExpressions = _this$operations$move.addedGlobalNamedExpressions; this.undoRedo.saveOperation(new _UndoRedo.MoveCellsUndoEntry(sourceLeftCorner, width, height, destinationLeftCorner, overwrittenCellsData, addedGlobalNamedExpressions, version)); } }, { key: "moveRows", value: function moveRows(sheet, startRow, numberOfRows, targetRow) { this.ensureItIsPossibleToMoveRows(sheet, startRow, numberOfRows, targetRow); this.undoRedo.clearRedoStack(); this.clipboardOperations.abortCut(); var version = this.operations.moveRows(sheet, startRow, numberOfRows, targetRow); this.undoRedo.saveOperation(new _UndoRedo.MoveRowsUndoEntry(sheet, startRow, numberOfRows, targetRow, version)); } }, { key: "moveColumns", value: function moveColumns(sheet, startColumn, numberOfColumns, targetColumn) { this.ensureItIsPossibleToMoveColumns(sheet, startColumn, numberOfColumns, targetColumn); this.undoRedo.clearRedoStack(); var version = this.operations.moveColumns(sheet, startColumn, numberOfColumns, targetColumn); this.undoRedo.saveOperation(new _UndoRedo.MoveColumnsUndoEntry(sheet, startColumn, numberOfColumns, targetColumn, version)); } }, { key: "cut", value: function cut(sourceLeftCorner, width, height) { this.clipboardOperations.cut(sourceLeftCorner, width, height); } }, { key: "ensureItIsPossibleToCopy", value: function ensureItIsPossibleToCopy(sourceLeftCorner, width, height) { if (!isPositiveInteger(width)) { throw new _errors.InvalidArgumentsError('width to be positive integer.'); } if (!isPositiveInteger(height)) { throw new _errors.InvalidArgumentsError('height to be positive integer.'); } } }, { key: "copy", value: function copy(sourceLeftCorner, width, height) { this.ensureItIsPossibleToCopy(sourceLeftCorner, width, height); this.clipboardOperations.copy(sourceLeftCorner, width, height); } }, { key: "paste", value: function paste(targetLeftCorner) { var clipboard = this.clipboardOperations.clipboard; if (clipboard === undefined) { throw new _errors.NothingToPasteError(); } else if (this.clipboardOperations.isCutClipboard()) { this.moveCells(clipboard.sourceLeftCorner, clipboard.width, clipboard.height, targetLeftCorner); } else if (this.clipboardOperations.isCopyClipboard()) { this.clipboardOperations.ensureItIsPossibleToCopyPaste(targetLeftCorner); var targetRange = _AbsoluteCellRange.AbsoluteCellRange.spanFrom(targetLeftCorner, clipboard.width, clipboard.height); var oldContent = this.operations.getRangeClipboardCells(targetRange); this.undoRedo.clearRedoStack(); var addedGlobalNamedExpressions = this.operations.restoreClipboardCells(clipboard.sourceLeftCorner.sheet, clipboard.getContent(targetLeftCorner)); this.undoRedo.saveOperation(new _UndoRedo.PasteUndoEntry(targetLeftCorner, oldContent, clipboard.content, addedGlobalNamedExpressions)); } } }, { key: "beginUndoRedoBatchMode", value: function beginUndoRedoBatchMode() { this.undoRedo.beginBatchMode(); } }, { key: "commitUndoRedoBatchMode", value: function commitUndoRedoBatchMode() { this.undoRedo.commitBatchMode(); } }, { key: "isClipboardEmpty", value: function isClipboardEmpty() { return this.clipboardOperations.clipboard === undefined; } }, { key: "clearClipboard", value: function clearClipboard() { this.clipboardOperations.clear(); } }, { key: "addSheet", value: function addSheet(name) { if (name !== undefined) { this.ensureItIsPossibleToAddSheet(name); } this.undoRedo.clearRedoStack(); var addedSheetName = this.operations.addSheet(name); this.undoRedo.saveOperation(new _UndoRedo.AddSheetUndoEntry(addedSheetName)); return addedSheetName; } }, { key: "removeSheet", value: function removeSheet(sheetId) { this.ensureScopeIdIsValid(sheetId); this.undoRedo.clearRedoStack(); this.clipboardOperations.abortCut(); var originalName = this.sheetMapping.fetchDisplayName(sheetId); var oldSheetContent = this.operations.getSheetClipboardCells(sheetId); var _this$operations$remo = this.operations.removeSheet(sheetId), version = _this$operations$remo.version, scopedNamedExpressions = _this$operations$remo.scopedNamedExpressions; this.undoRedo.saveOperation(new _UndoRedo.RemoveSheetUndoEntry(originalName, sheetId, oldSheetContent, scopedNamedExpressions, version)); } }, { key: "renameSheet", value: function renameSheet(sheetId, newName) { this.ensureItIsPossibleToRenameSheet(sheetId, newName); var oldName = this.operations.renameSheet(sheetId, newName); if (oldName !== undefined) { this.undoRedo.clearRedoStack(); this.undoRedo.saveOperation(new _UndoRedo.RenameSheetUndoEntry(sheetId, oldName, newName)); } return oldName; } }, { key: "clearSheet", value: function clearSheet(sheetId) { this.ensureScopeIdIsValid(sheetId); this.undoRedo.clearRedoStack(); this.clipboardOperations.abortCut(); var oldSheetContent = this.operations.getSheetClipboardCells(sheetId); this.operations.clearSheet(sheetId); this.undoRedo.saveOperation(new _UndoRedo.ClearSheetUndoEntry(sheetId, oldSheetContent)); } }, { key: "setCellContents", value: function setCellContents(topLeftCornerAddress, cellContents) { if (!(cellContents instanceof Array)) { cellContents = [[cellContents]]; } else { for (var i = 0; i < cellContents.length; i++) { if (!(cellContents[i] instanceof Array)) { throw new _errors.InvalidArgumentsError('an array of arrays or a raw cell value.'); } } } this.ensureItIsPossibleToChangeCellContents(topLeftCornerAddress, cellContents); this.undoRedo.clearRedoStack(); var oldContents = []; for (var _i = 0; _i < cellContents.length; _i++) { for (var j = 0; j < cellContents[_i].length; j++) { var address = { sheet: topLeftCornerAddress.sheet, row: topLeftCornerAddress.row + _i, col: topLeftCornerAddress.col + j }; var newContent = cellContents[_i][j]; this.clipboardOperations.abortCut(); var oldContent = this.operations.setCellContent(address, newContent); oldContents.push({ address: address, newContent: newContent, oldContent: oldContent }); } } this.undoRedo.saveOperation(new _UndoRedo.SetCellContentsUndoEntry(oldContents)); } }, { key: "setSheetContent", value: function setSheetContent(sheetId, values) { this.ensureScopeIdIsValid(sheetId); this.ensureItIsPossibleToChangeSheetContents(sheetId, values); (0, _Sheet.validateAsSheet)(values); this.undoRedo.clearRedoStack(); this.clipboardOperations.abortCut(); var oldSheetContent = this.operations.getSheetClipboardCells(sheetId); this.operations.setSheetContent(sheetId, values); this.undoRedo.saveOperation(new _UndoRedo.SetSheetContentUndoEntry(sheetId, oldSheetContent, values)); } }, { key: "setRowOrder", value: function setRowOrder(sheetId, rowMapping) { this.validateSwapRowIndexes(sheetId, rowMapping); this.testRowOrderForArrays(sheetId, rowMapping); this.undoRedo.clearRedoStack(); this.clipboardOperations.abortCut(); var oldContent = this.operations.setRowOrder(sheetId, rowMapping); this.undoRedo.saveOperation(new _UndoRedo.SetRowOrderUndoEntry(sheetId, rowMapping, oldContent)); } }, { key: "validateSwapRowIndexes", value: function validateSwapRowIndexes(sheetId, rowMapping) { if (!this.sheetMapping.hasSheetWithId(sheetId)) { throw new _errors.NoSheetWithIdError(sheetId); } this.validateRowOrColumnMapping(sheetId, rowMapping, 'row'); } }, { key: "testColumnOrderForArrays", value: function testColumnOrderForArrays(sheetId, columnMapping) { var _iterator = _createForOfIteratorHelper(columnMapping), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var _step$value = _slicedToArray(_step.value, 2), source = _step$value[0], target = _step$value[1]; if (source !== target) { var rowRange = _AbsoluteCellRange.AbsoluteCellRange.spanFrom({ sheet: sheetId, col: source, row: 0 }, 1, Infinity); if (this.dependencyGraph.arrayMapping.isFormulaArrayInRange(rowRange)) { throw new _errors.SourceLocationHasArrayError(); } } } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } } }, { key: "setColumnOrder", value: function setColumnOrder(sheetId, columnMapping) { this.validateSwapColumnIndexes(sheetId, columnMapping); this.testColumnOrderForArrays(sheetId, columnMapping); this.undoRedo.clearRedoStack(); this.clipboardOperations.abortCut(); var oldContent = this.operations.setColumnOrder(sheetId, columnMapping); this.undoRedo.saveOperation(new _UndoRedo.SetColumnOrderUndoEntry(sheetId, columnMapping, oldContent)); } }, { key: "validateSwapColumnIndexes", value: function validateSwapColumnIndexes(sheetId, columnMapping) { if (!this.sheetMapping.hasSheetWithId(sheetId)) { throw new _errors.NoSheetWithIdError(sheetId); } this.validateRowOrColumnMapping(sheetId, columnMapping, 'column'); } }, { key: "testRowOrderForArrays", value: function testRowOrderForArrays(sheetId, rowMapping) { var _iterator2 = _createForOfIteratorHelper(rowMapping), _step2; try { for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { var _step2$value = _slicedToArray(_step2.value, 2), source = _step2$value[0], target = _step2$value[1]; if (source !== target) { var rowRange = _AbsoluteCellRange.AbsoluteCellRange.spanFrom({ sheet: sheetId, col: 0, row: source }, Infinity, 1); if (this.dependencyGraph.arrayMapping.isFormulaArrayInRange(rowRange)) { throw new _errors.SourceLocationHasArrayError(); } } } } catch (err) { _iterator2.e(err); } finally { _iterator2.f(); } } }, { key: "mappingFromOrder", value: function mappingFromOrder(sheetId, newOrder, rowOrColumn) { if (!this.sheetMapping.hasSheetWithId(sheetId)) { throw new _errors.NoSheetWithIdError(sheetId); } var limit = rowOrColumn === 'row' ? this.dependencyGraph.getSheetHeight(sheetId) : this.dependencyGraph.getSheetWidth(sheetId); if (newOrder.length !== limit) { throw new _errors.InvalidArgumentsError("number of ".concat(rowOrColumn, "s provided to be sheet ").concat(rowOrColumn === 'row' ? 'height' : 'width', ".")); } var ret = []; for (var i = 0; i < limit; i++) { if (newOrder[i] !== i) { ret.push([i, newOrder[i]]); } } return ret; } }, { key: "validateRowOrColumnMapping", value: function validateRowOrColumnMapping(sheetId, rowMapping, rowOrColumn) { var limit = rowOrColumn === 'row' ? this.dependencyGraph.getSheetHeight(sheetId) : this.dependencyGraph.getSheetWidth(sheetId); var sources = rowMapping.map(function (_ref) { var _ref2 = _slicedToArray(_ref, 2), a = _ref2[0], _ = _ref2[1]; return a; }).sort(function (a, b) { return a - b; }); var targets = rowMapping.map(function (_ref3) { var _ref4 = _slicedToArray(_ref3, 2), _ = _ref4[0], b = _ref4[1]; return b; }).sort(function (a, b) { return a - b; }); for (var i = 0; i < sources.length; i++) { if (!isNonnegativeInteger(sources[i]) || sources[i] >= limit) { throw new _errors.InvalidArgumentsError("".concat(rowOrColumn, " numbers to be nonnegative integers and less than sheet ").concat(rowOrColumn === 'row' ? 'height' : 'width', ".")); } if (sources[i] === sources[i + 1]) { throw new _errors.InvalidArgumentsError("source ".concat(rowOrColumn, " numbers to be unique.")); } if (sources[i] !== targets[i]) { throw new _errors.InvalidArgumentsError("target ".concat(rowOrColumn, " numbers to be permutation of source ").concat(rowOrColumn, " numbers.")); } } } }, { key: "undo", value: function undo() { if (this.undoRedo.isUndoStackEmpty()) { throw new _errors.NoOperationToUndoError(); } this.clipboardOperations.abortCut(); this.undoRedo.undo(); } }, { key: "redo", value: function redo() { if (this.undoRedo.isRedoStackEmpty()) { throw new _errors.NoOperationToRedoError(); } this.clipboardOperations.abortCut(); this.undoRedo.redo(); } }, { key: "addNamedExpression", value: function addNamedExpression(expressionName, expression, sheetId, options) { this.ensureItIsPossibleToAddNamedExpression(expressionName, expression, sheetId); this.operations.addNamedExpression(expressionName, expression, sheetId, options); this.undoRedo.clearRedoStack(); this.clipboardOperations.abortCut(); this.undoRedo.saveOperation(new _UndoRedo.AddNamedExpressionUndoEntry(expressionName, expression, sheetId, options)); } }, { key: "changeNamedExpressionExpression", value: function changeNamedExpressionExpression(expressionName, sheetId, newExpression, options) { this.ensureItIsPossibleToChangeNamedExpression(expressionName, newExpression, sheetId); var _this$operations$chan = this.operations.changeNamedExpressionExpression(expressionName, newExpression, sheetId, options), _this$operations$chan2 = _slicedToArray(_this$operations$chan, 2), oldNamedExpression = _this$operations$chan2[0], content = _this$operations$chan2[1]; this.undoRedo.clearRedoStack(); this.clipboardOperations.abortCut(); this.undoRedo.saveOperation(new _UndoRedo.ChangeNamedExpressionUndoEntry(oldNamedExpression, newExpression, content, sheetId, options)); } }, { key: "removeNamedExpression", value: function removeNamedExpression(expressionName, sheetId) { this.ensureScopeIdIsValid(sheetId); var _this$operations$remo2 = this.operations.removeNamedExpression(expressionName, sheetId), _this$operations$remo3 = _slicedToArray(_this$operations$remo2, 2), namedExpression = _this$operations$remo3[0], content = _this$operations$remo3[1]; this.undoRedo.clearRedoStack(); this.clipboardOperations.abortCut(); this.undoRedo.saveOperation(new _UndoRedo.RemoveNamedExpressionUndoEntry(namedExpression, content, sheetId)); return namedExpression; } }, { key: "ensureItIsPossibleToAddNamedExpression", value: function ensureItIsPossibleToAddNamedExpression(expressionName, expression, sheetId) { this.ensureScopeIdIsValid(sheetId); this.ensureNamedExpressionNameIsValid(expressionName, sheetId); this.ensureNamedExpressionIsValid(expression); } }, { key: "ensureItIsPossibleToChangeNamedExpression", value: function ensureItIsPossibleToChangeNamedExpression(expressionName, expression, sheetId) { this.ensureScopeIdIsValid(sheetId); if (this.namedExpressions.namedExpressionForScope(expressionName, sheetId) === undefined) { throw new _errors.NamedExpressionDoesNotExistError(expressionName); } this.ensureNamedExpressionIsValid(expression); } }, { key: "isItPossibleToRemoveNamedExpression", value: function isItPossibleToRemoveNamedExpression(expressionName, sheetId) { this.ensureScopeIdIsValid(sheetId); if (this.namedExpressions.namedExpressionForScope(expressionName, sheetId) === undefined) { throw new _errors.NamedExpressionDoesNotExistError(expressionName); } } }, { key: "ensureItIsPossibleToAddRows", value: function ensureItIsPossibleToAddRows(sheet) { if (!this.sheetMapping.hasSheetWithId(sheet)) { throw new _errors.NoSheetWithIdError(sheet); } var sheetHeight = this.dependencyGraph.getSheetHeight(sheet); for (var _len5 = arguments.length, indexes = new Array(_len5 > 1 ? _len5 - 1 : 0), _key5 = 1; _key5 < _len5; _key5++) { indexes[_key5 - 1] = arguments[_key5]; } var newRowsCount = indexes.map(function (index) { return index[1]; }).reduce(function (a, b) { return a + b; }, 0); if (sheetHeight + newRowsCount > this.maxRows) { throw new _errors.SheetSizeLimitExceededError(); } for (var _i2 = 0, _indexes = indexes; _i2 < _indexes.length; _i2++) { var _indexes$_i = _slicedToArray(_indexes[_i2], 2), row = _indexes$_i[0], numberOfRowsToAdd = _indexes$_i[1]; if (!isNonnegativeInteger(row) || !isPositiveInteger(numberOfRowsToAdd)) { throw new _errors.InvalidArgumentsError('row number to be nonnegative and number of rows to add to be positive.'); } } } }, { key: "ensureItIsPossibleToRemoveRows", value: function ensureItIsPossibleToRemoveRows(sheet) { for (var _len6 = arguments.length, indexes = new Array(_len6 > 1 ? _len6 - 1 : 0), _key6 = 1; _key6 < _len6; _key6++) { indexes[_key6 - 1] = arguments[_key6]; } for (var _i3 = 0, _indexes2 = indexes; _i3 < _indexes2.length; _i3++) { var _indexes2$_i = _slicedToArray(_indexes2[_i3], 2), rowStart = _indexes2$_i[0], numberOfRows = _indexes2$_i[1]; var rowEnd = rowStart + numberOfRows - 1; if (!isNonnegativeInteger(rowStart) || !isNonnegativeInteger(rowEnd)) { throw new _errors.InvalidArgumentsError('starting and ending row to be nonnegative.'); } if (rowEnd < rowStart) { throw new _errors.InvalidArgumentsError('starting row to be smaller than the ending row.'); } if (!this.sheetMapping.hasSheetWithId(sheet)) { throw new _errors.NoSheetWithIdError(sheet); } } } }, { key: "ensureItIsPossibleToAddColumns", value: function ensureItIsPossibleToAddColumns(sheet) { if (!this.sheetMapping.hasSheetWithId(sheet)) { throw new _errors.NoSheetWithIdError(sheet); } var sheetWidth = this.dependencyGraph.getSheetWidth(sheet); for (var _len7 = arguments.length, indexes = new Array(_len7 > 1 ? _len7 - 1 : 0), _key7 = 1; _key7 < _len7; _key7++) { indexes[_key7 - 1] = arguments[_key7]; } var newColumnsCount = indexes.map(function (index) { return index[1]; }).reduce(function (a, b) { return a + b; }, 0); if (sheetWidth + newColumnsCount > this.maxColumns) { throw new _errors.SheetSizeLimitExceededError(); } for (var _i4 = 0, _indexes3 = indexes; _i4 < _indexes3.length; _i4++) { var _indexes3$_i = _slicedToArray(_indexes3[_i4], 2), column = _indexes3$_i[0], numberOfColumnsToAdd = _indexes3$_i[1]; if (!isNonnegativeInteger(column) || !isPositiveInteger(numberOfColumnsToAdd)) { throw new _errors.InvalidArgumentsError('column number to be nonnegative and number of columns to add to be positive.'); } } } }, { key: "ensureItIsPossibleToRemoveColumns", value: function ensureItIsPossibleToRemoveColumns(sheet) { for (var _len8 = arguments.length, indexes = new Array(_len8 > 1 ? _len8 - 1 : 0), _key8 = 1; _key8 < _len8; _key8++) { indexes[_key8 - 1] = arguments[_key8]; } for (var _i5 = 0, _indexes4 = indexes; _i5 < _indexes4.length; _i5++) { var _indexes4$_i = _slicedToArray(_indexes4[_i5], 2), columnStart = _indexes4$_i[0], numberOfColumns = _indexes4$_i[1]; var columnEnd = columnStart + numberOfColumns - 1; if (!isNonnegativeInteger(columnStart) || !isNonnegativeInteger(columnEnd)) { throw new _errors.InvalidArgumentsError('starting and ending column to be nonnegative.'); } if (columnEnd < columnStart) { throw new _errors.InvalidArgumentsError('starting column to be smaller than the ending column.'); } if (!this.sheetMapping.hasSheetWithId(sheet)) { throw new _errors.NoSheetWithIdError(sheet); } } } }, { key: "ensureItIsPossibleToMoveRows", value: function ensureItIsPossibleToMoveRows(sheet, startRow, numberOfRows, targetRow) { this.ensureItIsPossibleToAddRows(sheet, [targetRow, numberOfRows]); var sourceStart = (0, _Cell.simpleCellAddress)(sheet, 0, startRow); var targetStart = (0, _Cell.simpleCellAddress)(sheet, 0, targetRow); if (!this.sheetMapping.hasSheetWithId(sheet) || (0, _Cell.invalidSimpleCellAddress)(sourceStart) || (0, _Cell.invalidSimpleCellAddress)(targetStart) || !isPositiveInteger(numberOfRows) || targetRow <= startRow + numberOfRows && targetRow >= startRow) { throw new _errors.InvalidArgumentsError('a valid range of rows to move.'); } var width = this.dependencyGraph.getSheetWidth(sheet); var sourceRange = _AbsoluteCellRange.AbsoluteCellRange.spanFrom(sourceStart, width, numberOfRows); if (this.dependencyGraph.arrayMapping.isFormulaArrayInRange(sourceRange)) { throw new _errors.SourceLocationHasArrayError(); } if (targetRow > 0 && this.dependencyGraph.arrayMapping.isFormulaArrayInAllRows(_Span.RowsSpan.fromNumberOfRows(sheet, targetRow - 1, 2))) { throw new _errors.TargetLocationHasArrayError(); } } }, { key: "ensureItIsPossibleToMoveColumns", value: function ensureItIsPossibleToMoveColumns(sheet, startColumn, numberOfColumns, targetColumn) { this.ensureItIsPossibleToAddColumns(sheet, [targetColumn, numberOfColumns]); var sourceStart = (0, _Cell.simpleCellAddress)(sheet, startColumn, 0); var targetStart = (0, _Cell.simpleCellAddress)(sheet, targetColumn, 0); if (!this.sheetMapping.hasSheetWithId(sheet) || (0, _Cell.invalidSimpleCellAddress)(sourceStart) || (0, _Cell.invalidSimpleCellAddress)(targetStart) || !isPositiveInteger(numberOfColumns) || targetColumn <= startColumn + numberOfColumns && targetColumn >= startColumn) { throw new _errors.InvalidArgumentsError('a valid range of columns to move.'); } var sheetHeight = this.dependencyGraph.getSheetHeight(sheet); var sourceRange = _AbsoluteCellRange.AbsoluteCellRange.spanFrom(sourceStart, numberOfColumns, sheetHeight); if (this.dependencyGraph.arrayMapping.isFormulaArrayInRange(sourceRange)) { throw new _errors.SourceLocationHasArrayError(); } if (targetColumn > 0 && this.dependencyGraph.arrayMapping.isFormulaArrayInAllColumns(_Span.ColumnsSpan.fromNumberOfColumns(sheet, targetColumn - 1, 2))) { throw new _errors.TargetLocationHasArrayError(); } } }, { key: "ensureItIsPossibleToAddSheet", value: function ensureItIsPossibleToAddSheet(name) { if (this.sheetMapping.hasSheetWithName(name)) { throw new _errors.SheetNameAlreadyTakenError(name); } } }, { key: "ensureItIsPossibleToRenameSheet", value: function ensureItIsPossibleToRenameSheet(sheetId, name) { if (!this.sheetMapping.hasSheetWithId(sheetId)) { throw new _errors.NoSheetWithIdError(sheetId); } var existingSheetId = this.sheetMapping.get(name); if (existingSheetId !== undefined && existingSheetId !== sheetId) { throw new _errors.SheetNameAlreadyTakenError(name); } } }, { key: "ensureItIsPossibleToChangeContent", value: function ensureItIsPossibleToChangeContent(address) { if ((0, _Cell.invalidSimpleCellAddress)(address)) { throw new _errors.InvalidAddressError(address); } if (!this.sheetMapping.hasSheetWithId(address.sheet)) { throw new _errors.NoSheetWithIdError(address.sheet); } } }, { key: "ensureItIsPossibleToChangeCellContents", value: function ensureItIsPossibleToChangeCellContents(inputAddress, content) { var boundaries = (0, _Sheet.findBoundaries)(content); var targetRange = _AbsoluteCellRange.AbsoluteCellRange.spanFrom(inputAddress, boundaries.width, boundaries.height); this.ensureRangeInSizeLimits(targetRange); var _iterator3 = _createForOfIteratorHelper(targetRange.addresses(this.dependencyGraph)), _step3; try { for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) { var address = _step3.value; this.ensureItIsPossibleToChangeContent(address); } } catch (err) { _iterator3.e(err); } finally { _iterator3.f(); } } }, { key: "ensureItIsPossibleToChangeSheetContents", value: function ensureItIsPossibleToChangeSheetContents(sheetId, content) { var boundaries = (0, _Sheet.findBoundaries)(content); var targetRange = _AbsoluteCellRange.AbsoluteCellRange.spanFrom((0, _Cell.simpleCellAddress)(sheetId, 0, 0), boundaries.width, boundaries.height); this.ensureRangeInSizeLimits(targetRange); } }, { key: "ensureRangeInSizeLimits", value: function ensureRangeInSizeLimits(range) { if (range.exceedsSheetSizeLimits(this.maxColumns, this.maxRows)) { throw new _errors.SheetSizeLimitExceededError(); } } }, { key: "isThereSomethingToUndo", value: function isThereSomethingToUndo() { return !this.undoRedo.isUndoStackEmpty(); } }, { key: "isThereSomethingToRedo", value: function isThereSomethingToRedo() { return !this.undoRedo.isRedoStackEmpty(); } }, { key: "getAndClearContentChanges", value: function getAndClearContentChanges() { return this.operations.getAndClearContentChanges(); } }, { key: "ensureScopeIdIsValid", value: function ensureScopeIdIsValid(scopeId) { if (scopeId !== undefined && !this.sheetMapping.hasSheetWithId(scopeId)) { throw new _errors.NoSheetWithIdError(scopeId); } } }, { key: "sheetMapping", get: function get() { return this.dependencyGraph.sheetMapping; } }, { key: "ensureNamedExpressionNameIsValid", value: function ensureNamedExpressionNameIsValid(expressionName, sheetId) { if (!this.namedExpressions.isNameValid(expressionName)) { throw new _errors.NamedExpressionNameIsInvalidError(expressionName); } if (!this.namedExpressions.isNameAvailable(expressionName, sheetId)) { throw new _errors.NamedExpressionNameIsAlreadyTakenError(expressionName); } } }, { key: "ensureNamedExpressionIsValid", value: function ensureNamedExpressionIsValid(expression) { var parsedExpression = this.cellContentParser.parse(expression); if (parsedExpression instanceof _CellContentParser.CellContent.Formula) { var parsingResult = this.parser.parse(parsedExpression.formula, (0, _Cell.simpleCellAddress)(-1, 0, 0)); if ((0, _NamedExpressions.doesContainRelativeReferences)(parsingResult.ast)) { throw new _errors.NoRelativeAddressesAllowedError(); } } } }]); return CrudOperations; }(); exports.CrudOperations = CrudOperations; function isPositiveInteger(x) { return Number.isInteger(x) && x > 0; } function isNonnegativeInteger(x) { return Number.isInteger(x) && x >= 0; }