UNPKG

ze-react-component-library

Version:
134 lines (107 loc) 3.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = exports.columnIndexToLabel = void 0; var _hyperformula = require("hyperformula"); 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); } var __assign = void 0 && (void 0).__assign || function () { __assign = Object.assign || function (t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) { if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } } return t; }; return __assign.apply(this, arguments); }; var columnIndexToLabel = function columnIndexToLabel(col) { var colString = ""; var column = col; while (column >= 0) { colString = String.fromCharCode(column % 26 + 97) + colString; column = Math.floor(column / 26) - 1; } return colString.toUpperCase(); }; exports.columnIndexToLabel = columnIndexToLabel; var calcGridData = function calcGridData(data) { // build an instance with defined options and data var hfInstance = _hyperformula.HyperFormula.buildFromArray(data, { licenseKey: "gpl-v3" }); var values = hfInstance.getSheetValues(0); return values; }; /** * 将本系统自定义的函数转化为hyperformula支持的函数 * 目前支持 "@RC" * @param data */ var preprocessFormula = function preprocessFormula(data) { for (var rowIndex = 0; rowIndex < data.length; rowIndex++) { var row = data[rowIndex]; for (var colIndex = 0; colIndex < row.length; colIndex++) { var col = row[colIndex]; // 一种是col,一种是col.v if (typeof col === "string" && col.startsWith("=") && col.indexOf("@RC") > 0) { row[colIndex] = col.replace(/@RC/g, "" + columnIndexToLabel(colIndex) + (rowIndex + 1)); } else if (col && _typeof(col) === "object" && typeof col.v === "string" && col.v.startsWith("=") && col.v.indexOf("@RC") > 0) { row[colIndex] = __assign(__assign({}, col), { v: col.v.replace(/@RC/g, "" + columnIndexToLabel(colIndex) + (rowIndex + 1)) }); } } } }; /** * hyperformula的errorWrapper * @param v * @returns */ var errorWrapper = function errorWrapper(v) { if (v && _typeof(v) === "object" && v.address && v.type) { return "-"; } return v; }; var _default = function _default(source, showFormula) { var data = source.map(function (row) { return row.map(function (cell) { if (_typeof(cell) === "object") { var cellCopy = __assign({}, cell); delete cellCopy.node; return JSON.parse(JSON.stringify(cellCopy)); } return cell; }); }); if (data.length > 0) { preprocessFormula(data); var calc_1 = calcGridData(data.map(function (gridItem) { return gridItem.map(function (i) { return i && _typeof(i) === "object" ? i.v : i; }); })); // 把数据填回去,保留format data.forEach(function (gridItem, r) { gridItem.forEach(function (i, c) { if (i) { if (_typeof(i) === "object" && "v" in i) { if (!showFormula) { i.v = errorWrapper(calc_1[r][c]); } else { delete i.formatter; } } else if (typeof i === "string" && i.startsWith("=")) { if (!showFormula) { data[r][c] = errorWrapper(calc_1[r][c]); } } } }); }); // console.log(calc); } return data; }; exports.default = _default;