- import GC from '@grapecity/spread-sheets'
- /**
- * 数字类型单元格
- * @param {*} length
- * @param {*} precision
- */
- let CZAMTCellTypeV12 = function CZAMTCellTypeV12(length, precision, flag, MoneyUnits) {
- this.length = length
- this.precision = precision
- this.flag = flag
- this.MoneyUnits = MoneyUnits
- this.Init(length, precision, flag)
- }
- CZAMTCellTypeV12.prototype = new GC.Spread.Sheets.CellTypes.Text(); // new GC.Spread.Sheets.TextCellType()
- CZAMTCellTypeV12.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
- try {
- //alert(value)
- style.hAlign = GC.Spread.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);
- GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, [ctx, this.format(textValue), x, y, w, h, style, options]);
- } else {
- GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, [ctx, this.format(value), x, y, w, h, style, options]);
- }
- } catch (e) {
- console.log(e);
- }
- }
- CZAMTCellTypeV12.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
- }
- }
- CZAMTCellTypeV12.prototype.selectAll = function (editorContext, context) {
- // console.log("selectAll", editorContext);
- // console.log("context", context);
- }
- //
- // CZAMTCellTypeV12.prototype.createEditorElement = function (context) {
- // var div = document.createElement("div");
- // // var $div = document.getElementsByTagNameNS('div');
- // var $div = div;
- // $div.gcUIElement = "gcEditingInput";
- // $div.style['background-color'] = "white";
- // $div.style.position = "absolute";
- // $div.style.overflow = "hidden";
- // $div.style.border = "2px #5292f7 solid";
- // var $span1 = document.createElement("span");
- // $span1.style.display = "block";
- // var $input1 = document.createElement("input");
- // $input1.type = 'text';
- // $div.append($span1);
- // $div.append($input1);
- // return div;
- // };
- CZAMTCellTypeV12.prototype.setEditorValue = function (editorContext, value) {
- if (editorContext && !this.isNull(editorContext.children) && !this.isNull(editorContext.children[0])) {
- if (editorContext.children[0].children && editorContext.children[0].children[0]) {
- const textArea = editorContext.children[0].children[0];
- if (this.isNull(value)) {
- textArea.innerHTML = 0;
- } else {
- if (typeof value === 'string') {
- value = value.replace(',', '')
- }
- value = Number(value);
- if (!(typeof value === 'number' && !isNaN(value))) {
- textArea.innerHTML = 0;
- } else {
- let textValue = (new Decimal(value).div(new Decimal(this.MoneyUnits)));
- textArea.innerHTML = textValue;
- }
- }
- }
- }
- };
- CZAMTCellTypeV12.prototype.createEditorElement = function(context) {
- var editor = GC.Spread.Sheets.CellTypes.Text.prototype.createEditorElement.call(this, context);
- //var textarea = editor.firstChild;
- editor.onkeypress = function(event) {
- return event.keyCode >= 48 && event.keyCode <= 57 || event.keyCode == 46 || event.keyCode == 45
- }
- editor.onkeyup = function(event) {
- // let value = this.children[0].children[0].innerHTML;
- // this.children[0].children[0].innerHTML = value.replace(/[\u4e00-\u9fa5]/g, '').replace(/\D/g, '');
- }
- editor.onpaste = function(event) {
- var clipData = event.clipboardData;
- return !clipData.getData('text').match(/\D/);
- }
- editor.ondragenter = function(event) {
- return false;
- }
- return editor;
- };
- CZAMTCellTypeV12.prototype.getEditorValue = function (editorContext) {
- if (editorContext && editorContext.children && editorContext.children[0]) {
- if (editorContext.children[0].children && editorContext.children[0].children[0]) {
- const textArea = editorContext.children[0].children[0]
- let editorValue = textArea.innerHTML
- if (!this.isNull(editorValue)) {
- editorValue = Number(this.delcommafy(editorValue));
- if (!(typeof editorValue === 'number' && !isNaN(editorValue))) {
- return 0
- }
- editorValue = new Decimal(editorValue).mul(new Decimal(this.MoneyUnits)).toNumber()
- 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 (result.length > 2) {
- editorValue = this.delcommafy(editorValue);
- }
- return Number(editorValue)
- }
- } else {
- return 0
- }
- }else{
- return 0;
- }
- }else{
- return 0;
- }
- }
- //
- //
- CZAMTCellTypeV12.prototype.isNull = function (val) {
- if (val) {
- if (
- val === '' ||
- val === null ||
- val === 'null' ||
- val === undefined ||
- val === 'undefined'
- ) {
- return true
- } else {
- return false
- }
- } else {
- return true
- }
- }
- //格式化为千分位
- CZAMTCellTypeV12.prototype.format=function (num) {
- return (num+'').replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g,'$1,');
- }
- CZAMTCellTypeV12.prototype.delcommafy=function (num){
- if((num+"").trim()==""){
- return"";
- }
- num=num.replace(/,/gi,'');
- return num;
- }
- window.CZAMTCellTypeV12 = CZAMTCellTypeV12
复制代码
我金额单元格设置的是这个celltype,初次刷新时,列宽如果比要显示的数字小时,数字不能正确的按系统预设的金额单位换算。
初始化时:
不做任何其他操作,只时修改列宽:
再次将列宽拉小:
但是如果编辑过一次金额列,反复拖拉就不会出现这个情况,跟踪了celltype相关事件,发现初始化时没有进入任何事件。
|