这个问题发现了一点端倪,是当给列设置了celltype之后,就会造成JSON.stringify报错。我把我们其中一个celltype贴出来。
- import '@grapecity/spread-sheets/styles/gc.spread.sheets.excel2016colorful.css'
- import GC from '@grapecity/spread-sheets'
- import '@grapecity/spread-sheets-vue'
- import '@grapecity/spread-sheets-resources-zh'
- /**
- * 数字类型单元格
- * @param {*} length
- * @param {*} precision
- */
- import {isNull} from "../../util";
- let CZAMTCellType = function CZAMTCellType(_parent, columnInfo, length, precision, flag, MoneyUnits) {
- this._parent=_parent;
- this.columnInfo=columnInfo;
- this.length = length;
- this.precision = precision;
- this.flag = flag;
- this.MoneyUnits = MoneyUnits;
- this.Init(length, precision, flag);
- }
-
- CZAMTCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
- CZAMTCellType.prototype.deactivateEditor = async function (editorContext, context) {
- let row = context.row;
- let col = context.col;
- let gridhelper = new GridHelper(this._parent.spread);
- let curValue = gridhelper.GetCellValue(row, col);
- }
-
- CZAMTCellType.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
- style.hAlign = GcSpread.Sheets.HorizontalAlign.right;
- if (value) {
- value = Number(value);
- if (!(typeof value === 'number' && !isNaN(value))) {
- value = 0;
- }
- let textValue = (new Decimal(value).div(new Decimal(this.MoneyUnits))).toFixed(2);
-
- if(textValue<0){//负数时,显示值改为整数,且颜色变红。
- textValue = Math.abs(textValue);
- style.foreColor = "red";
- }
- GcSpread.Sheets.CustomCellType.prototype.paint.apply(this, [ctx, this.format(textValue), x, y, w, h, style, options]);
-
- } else {
- value = 0;
- GcSpread.Sheets.CustomCellType.prototype.paint.apply(this, [ctx, this.format(value), x, y, w, h, style, options]);
- }
- };
-
- CZAMTCellType.prototype.Init = function (length, precision, flag) {
- this.maxvalue = Math.pow(10, length - precision - 1)
- if (this.maxvalue < 99999999999999) {
- this.maxvalue -= 1
- } else {
- this.maxvalue = 99999999999999
- }
- if (flag) {
- this.minValue = 0
- } else {
- this.minValue = 0 - this.maxvalue
- }
- };
-
- CZAMTCellType.prototype.setEditorValue = function (editorContext, value) {
- // if (editorContext) {
- // const textArea = editorContext.children[0];
- // if (isNull(value)) {
- // textArea.value = 0;
- // } else {
- // if (typeof value === 'string') {
- // value = value.replace(',', '')
- // }
- // value = Number(value);
- // if (!(typeof value === 'number' && !isNaN(value))) {
- // textArea.value = 0;
- // } else {
- // let textValue = (new Decimal(value).div(new Decimal(this.MoneyUnits)));
- // textArea.value = textValue;
- // }
- // }
- // }
- if (editorContext) {
- // const textArea = editorContext.children[0]
- const textArea = editorContext // .innerText
- if (this.isNull(value)) {
- textArea.innerText = 0
- } else {
- if (typeof value === 'string') {
- value = value.replace(',', '')
- }
- value = Number(value);
- if (!(typeof value === 'number' && !isNaN(value))) {
- textArea.innerText = 0
- } else {
- let textValue = new Decimal(value).div(new Decimal(this.MoneyUnits))
- textArea.innerText = textValue
- }
- }
- }
- };
-
- CZAMTCellType.prototype.getEditorValue = function (editorContext) {
- if (editorContext) {
- // const textArea = editorContext.children[0];
- let editorValue = editorContext.innerText;
- if (!isNull(editorValue)) {
- editorValue = Number(this.delcommafy(editorValue));
- if (!(typeof editorValue === 'number' && !isNaN(editorValue))) {
- return 0;
- }
- editorValue = new Decimal(editorValue).mul(new Decimal(this.MoneyUnits));
- if (editorValue > this.maxvalue) {
- editorValue = this.maxvalue;
- }
- if (editorValue < this.minValue) {
- editorValue = this.minValue;
- }
- let index = editorValue.toString().indexOf(".");
- if (index == -1) {
- return Number(editorValue);
- } else {
- var result = editorValue.toString().substring(index + 1);
- if (2 < result.length) {
- editorValue = this.delcommafy(editorValue);
- }
- return Number(editorValue);
- }
- } else {
- return 0;
- }
- }
- };
-
- CZAMTCellType.prototype.isNull = function (val) {
- if (val) {
- if (
- val === "" ||
- val === null ||
- val === "null" ||
- val === undefined ||
- val === "undefined"
- ) {
- return true;
- } else {
- return false;
- }
- } else {
- return true;
- }
- };
- //格式化为千分位
- CZAMTCellType.prototype.format=function (num) {
-
- return (num+'').replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g,'$1,');
-
- }
- CZAMTCellType.prototype.delcommafy=function (num){
- if((num+"").trim()==""){
- return"";
- }
- num=num.replace(/,/gi,'');
- return num;
- }
- window.CZAMTCellType = CZAMTCellType;
-
复制代码 |