UNPKG

react-microspreadsheet

Version:

A pluggable spreadsheet component.

105 lines (92 loc) 2.93 kB
// Generated by CoffeeScript 1.8.0 var Mori, calculated, flush, getCalcResult, getCalcResultAt, getMatrixValues, parseStr, parser, store, utils; Mori = require('mori'); utils = require('./utils'); store = require('./cells-store'); calculated = {}; flush = function() { return calculated = {}; }; module.exports.getUtils = function() { return { getCalcResultAt: function() { return getCalcResultAt.apply(this, arguments); }, getMatrixValues: function() { return getMatrixValues.apply(this, arguments); } }; }; module.exports.recalc = function() { var addr, calcRes, i, j, raw, _i, _j, _ref, _ref1; flush(); for (i = _i = 0, _ref = Mori.count(store.cells) - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) { for (j = _j = 0, _ref1 = Mori.count(Mori.get(store.cells, 0)) - 1; 0 <= _ref1 ? _j <= _ref1 : _j >= _ref1; j = 0 <= _ref1 ? ++_j : --_j) { addr = utils.getAddressFromCoord([i, j]); raw = Mori.get_in(store.cells, [i, j, 'raw']); calcRes = raw[0] === '=' && raw.slice(1) ? getCalcResult(raw) : raw; if (typeof calcRes === 'number') { calculated[addr.toUpperCase()] = calcRes; } store.cells = Mori.assoc_in(store.cells, [i, j, 'calc'], calcRes); } } store.redoStates = Mori.list(); store.undoStates = Mori.conj(store.undoStates, store.cells); store.canUndo = true; return store.canRedo = false; }; getCalcResultAt = function(addr) { var coord, raw; addr = addr.toUpperCase(); if (addr in calculated) { return calculated[addr]; } else { coord = utils.getCoordFromAddress(addr); raw = Mori.get_in(store.cells, coord.concat('raw'), ''); return getCalcResult(raw); } }; getCalcResult = function(raw) { var e; if (raw[0] === '=' && raw.length > 1) { try { return parser.parse(raw.slice(1)); } catch (_error) { e = _error; return parseStr(raw.toString()); } } else { return parseStr(raw.toString()); } }; parseStr = function(raw) { var f; if (!raw.length) { return 0; } f = parseFloat(raw); if (!isNaN(f) && isFinite(raw)) { return f; } else { return raw; } }; getMatrixValues = function(start, end) { var addr, colEnd, colStart, i, j, matrix, rowArray, rowEnd, rowStart, _i, _j; colStart = start[0].toUpperCase().charCodeAt(0) - 65; colEnd = end[0].toUpperCase().charCodeAt(0) - 65; rowStart = start.slice(1) - 1; rowEnd = end.slice(1) - 1; matrix = []; for (i = _i = rowStart; rowStart <= rowEnd ? _i <= rowEnd : _i >= rowEnd; i = rowStart <= rowEnd ? ++_i : --_i) { rowArray = []; for (j = _j = colStart; colStart <= colEnd ? _j <= colEnd : _j >= colEnd; j = colStart <= colEnd ? ++_j : --_j) { addr = utils.getAddressFromCoord([i, j]); rowArray.push(getCalcResultAt(addr)); } matrix.push(rowArray); } return matrix; }; parser = require('./formula-parser');