请选择 进入手机版 | 继续访问电脑版
 找回密码
 立即注册

QQ登录

只需一步,快速开始

IT-Weaver

初级会员

20

主题

76

帖子

238

积分

初级会员

积分
238
IT-Weaver
初级会员   /  发表于:2021-11-15 20:29  /   查看:1208  /  回复:3
1金币



  1. import GC from '@grapecity/spread-sheets'

  2. /**
  3. * 数字类型单元格
  4. * @param {*} length
  5. * @param {*} precision
  6. */
  7. let CZAMTCellTypeV12 = function CZAMTCellTypeV12(length, precision, flag, MoneyUnits) {
  8.     this.length = length
  9.     this.precision = precision
  10.     this.flag = flag
  11.     this.MoneyUnits = MoneyUnits
  12.     this.Init(length, precision, flag)
  13. }

  14. CZAMTCellTypeV12.prototype = new GC.Spread.Sheets.CellTypes.Text(); // new GC.Spread.Sheets.TextCellType()

  15. CZAMTCellTypeV12.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
  16.     try {
  17.         //alert(value)
  18.         style.hAlign = GC.Spread.Sheets.HorizontalAlign.right
  19.         if (value) {
  20.             value = Number(value)
  21.             if (!(typeof value === 'number' && !isNaN(value))) {
  22.                 value = 0
  23.             }
  24.             let textValue = (new Decimal(value).div(new Decimal(this.MoneyUnits))).toFixed(2);
  25.             GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, [ctx, this.format(textValue), x, y, w, h, style, options]);
  26.         } else {
  27.             GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, [ctx, this.format(value), x, y, w, h, style, options]);
  28.         }
  29.     } catch (e) {
  30.         console.log(e);
  31.     }
  32. }

  33. CZAMTCellTypeV12.prototype.Init = function (length, precision, flag) {
  34.     this.maxvalue = Math.pow(10, length - precision - 1)
  35.     if (this.maxvalue < 99999999999999) {
  36.         this.maxvalue -= 1
  37.     } else {
  38.         this.maxvalue = 99999999999999
  39.     }
  40.     if (flag) {
  41.         this.minValue = 0
  42.     } else {
  43.         this.minValue = 0 - this.maxvalue
  44.     }
  45. }

  46. CZAMTCellTypeV12.prototype.selectAll = function (editorContext, context) {
  47.     // console.log("selectAll", editorContext);
  48.     // console.log("context", context);
  49. }

  50. //
  51. // CZAMTCellTypeV12.prototype.createEditorElement = function (context) {
  52. //     var div = document.createElement("div");
  53. //     // var $div = document.getElementsByTagNameNS('div');
  54. //     var $div = div;
  55. //     $div.gcUIElement = "gcEditingInput";
  56. //     $div.style['background-color'] = "white";
  57. //     $div.style.position = "absolute";
  58. //     $div.style.overflow = "hidden";
  59. //     $div.style.border = "2px #5292f7 solid";
  60. //     var $span1 = document.createElement("span");
  61. //     $span1.style.display = "block";
  62. //     var $input1 = document.createElement("input");
  63. //     $input1.type = 'text';
  64. //     $div.append($span1);
  65. //     $div.append($input1);
  66. //     return div;
  67. // };

  68. CZAMTCellTypeV12.prototype.setEditorValue = function (editorContext, value) {
  69.     if (editorContext && !this.isNull(editorContext.children) && !this.isNull(editorContext.children[0])) {
  70.         if (editorContext.children[0].children && editorContext.children[0].children[0]) {
  71.             const textArea = editorContext.children[0].children[0];
  72.             if (this.isNull(value)) {
  73.                 textArea.innerHTML = 0;
  74.             } else {
  75.                 if (typeof value === 'string') {
  76.                     value = value.replace(',', '')
  77.                 }
  78.                 value = Number(value);
  79.                 if (!(typeof value === 'number' && !isNaN(value))) {
  80.                     textArea.innerHTML = 0;
  81.                 } else {
  82.                     let textValue = (new Decimal(value).div(new Decimal(this.MoneyUnits)));
  83.                     textArea.innerHTML = textValue;
  84.                 }
  85.             }
  86.         }
  87.     }
  88. };

  89. CZAMTCellTypeV12.prototype.createEditorElement = function(context) {
  90.     var editor = GC.Spread.Sheets.CellTypes.Text.prototype.createEditorElement.call(this, context);
  91.     //var textarea = editor.firstChild;
  92.     editor.onkeypress = function(event) {
  93.         return event.keyCode >= 48 && event.keyCode <= 57 || event.keyCode == 46 || event.keyCode == 45
  94.     }
  95.     editor.onkeyup = function(event) {
  96.         // let value = this.children[0].children[0].innerHTML;
  97.         // this.children[0].children[0].innerHTML = value.replace(/[\u4e00-\u9fa5]/g, '').replace(/\D/g, '');
  98.     }
  99.     editor.onpaste = function(event) {
  100.         var clipData = event.clipboardData;
  101.         return !clipData.getData('text').match(/\D/);
  102.     }
  103.     editor.ondragenter = function(event) {
  104.         return false;
  105.     }
  106.     return editor;
  107. };

  108. CZAMTCellTypeV12.prototype.getEditorValue = function (editorContext) {
  109.     if (editorContext && editorContext.children && editorContext.children[0]) {
  110.         if (editorContext.children[0].children && editorContext.children[0].children[0]) {
  111.             const textArea = editorContext.children[0].children[0]
  112.             let editorValue = textArea.innerHTML
  113.             if (!this.isNull(editorValue)) {
  114.                 editorValue = Number(this.delcommafy(editorValue));
  115.                 if (!(typeof editorValue === 'number' && !isNaN(editorValue))) {
  116.                     return 0
  117.                 }

  118.                 editorValue = new Decimal(editorValue).mul(new Decimal(this.MoneyUnits)).toNumber()
  119.                 if (editorValue > this.maxvalue) {
  120.                     editorValue = this.maxvalue
  121.                 }
  122.                 if (editorValue < this.minValue) {
  123.                     editorValue = this.minValue
  124.                 }
  125.                 let index = editorValue.toString().indexOf('.')
  126.                 if (index === -1) {
  127.                     return Number(editorValue)
  128.                 } else {
  129.                     var result = editorValue.toString().substring(index + 1)
  130.                     if (result.length > 2) {
  131.                         editorValue = this.delcommafy(editorValue);
  132.                     }
  133.                     return Number(editorValue)
  134.                 }
  135.             } else {
  136.                 return 0
  137.             }
  138.         }else{
  139.             return 0;
  140.         }
  141.     }else{
  142.         return 0;
  143.     }
  144. }
  145. //
  146. //
  147. CZAMTCellTypeV12.prototype.isNull = function (val) {
  148.     if (val) {
  149.         if (
  150.             val === '' ||
  151.             val === null ||
  152.             val === 'null' ||
  153.             val === undefined ||
  154.             val === 'undefined'
  155.         ) {
  156.             return true
  157.         } else {
  158.             return false
  159.         }
  160.     } else {
  161.         return true
  162.     }
  163. }
  164. //格式化为千分位
  165. CZAMTCellTypeV12.prototype.format=function (num) {
  166.     return (num+'').replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g,'$1,');
  167. }
  168. CZAMTCellTypeV12.prototype.delcommafy=function (num){
  169.     if((num+"").trim()==""){
  170.         return"";
  171.     }
  172.     num=num.replace(/,/gi,'');
  173.     return num;
  174. }

  175. window.CZAMTCellTypeV12 = CZAMTCellTypeV12
复制代码


我金额单元格设置的是这个celltype,初次刷新时,列宽如果比要显示的数字小时,数字不能正确的按系统预设的金额单位换算。

初始化时:
1636979276(1).png694552611.png

不做任何其他操作,只时修改列宽:
1636979310(1).png128432616.png

再次将列宽拉小:
1636979337(1).png699960632.png


但是如果编辑过一次金额列,反复拖拉就不会出现这个情况,跟踪了celltype相关事件,发现初始化时没有进入任何事件。

最佳答案

查看完整内容

问题已复现,已在另一个帖子中标记回复,有进展会在下方链接更新 https://gcdn.grapecity.com.cn/forum.php?mod=viewthread&tid=136356 关于本帖的问题若成功复现后,也请提供给我们这边做进一步调研。

3 个回复

倒序浏览
最佳答案
最佳答案
Derrick.Jiao讲师达人认证 悬赏达人认证 SpreadJS 开发认证
论坛元老   /  发表于:2021-11-15 20:30:00
来自 4#
IT-Weaver 发表于 2021-11-16 11:21
本帖子这个情况确实在纯净环境下没有问题,我们再查一下本地项目原因。

另外一个问题,已经在文件 ...

问题已复现,已在另一个帖子中标记回复,有进展会在下方链接更新
https://gcdn.grapecity.com.cn/fo ... read&tid=136356

关于本帖的问题若成功复现后,也请提供给我们这边做进一步调研。
回复 使用道具 举报
Derrick.Jiao讲师达人认证 悬赏达人认证 SpreadJS 开发认证
论坛元老   /  发表于:2021-11-16 10:40:29
2#
你好,为了更加准确复现你的问题,请在附件的demo上复现你的问题,这边复现后直接在demo上调整。

online.html

10.51 KB, 下载次数: 17

回复 使用道具 举报
IT-Weaver
初级会员   /  发表于:2021-11-16 11:21:12
3#
Derrick.Jiao 发表于 2021-11-16 10:40
你好,为了更加准确复现你的问题,请在附件的demo上复现你的问题,这边复现后直接在demo上调整。



本帖子这个情况确实在纯净环境下没有问题,我们再查一下本地项目原因。

另外一个问题,已经在文件中复现,就是在输入时,一直输入相反。

online.rar

2.44 KB, 下载次数: 15

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部