找回密码
 立即注册

QQ登录

只需一步,快速开始

zyh2123606

金牌服务用户

13

主题

30

帖子

143

积分

金牌服务用户

积分
143
zyh2123606
金牌服务用户   /  发表于:2024-5-27 10:37  /   查看:1446  /  回复:4
1金币
本帖最后由 Richard.Huang 于 2024-5-27 16:24 编辑

产品:SpreadJS
版本:V17.0.4

designer自定义celltype当单元格为空和没有任何改变时也会触发editCell image.png135351256.png ,希望自定义celltype和默认保持一致,当值没有变化时不触发editcell,下面是自定义cellType代码
  1. import * as GC from '@grapecity/spread-sheets'

  2. /**
  3. * 自定义单元格类型
  4. */
  5. type cellType = 'number' | 'integer' | 'identifier' | 'string'
  6. class CustomCellType extends GC.Spread.Sheets.CellTypes.Base {
  7.   type: cellType
  8.   precision: number
  9.   constructor(type: cellType = 'string', precision: number = 2) {
  10.     super()
  11.     this.typeName = 'CustomCellType'
  12.     this.type = type
  13.     this.precision = precision
  14.   }
  15.   paint(ctx: CanvasRenderingContext2D, value: any, x: number, y: number, w: number, h: number, style: GC.Spread.Sheets.Style, context?: any): void {
  16.     GC.Spread.Sheets.CellTypes.Base.prototype.paint.apply(this, [ctx, value, x, y, w, h, style, context])
  17.   }
  18.   createEditorElement(context?: any): HTMLInputElement {
  19.     const input = document.createElement('input')
  20.     input.setAttribute('data-col', context.col)
  21.     input.setAttribute('data-row', context.row)
  22.     return input
  23.   }
  24.   activateEditor(editorContext: HTMLInputElement, cellStyle: GC.Spread.Sheets.Style, cellRect: GC.Spread.Sheets.Rect, context?: any): void {
  25.     if (editorContext) {
  26.       GC.Spread.Sheets.CellTypes.Base.prototype.activateEditor.apply(this, [editorContext, cellStyle, cellRect, context])
  27.       editorContext.style.position = 'absolute'
  28.       const format = () => {
  29.         const value = editorContext.value
  30.         if (this.type == 'number') editorContext.value = value.replace(new RegExp(`^(-?\\d*\\.?\\d{0,${this.precision}}).*`), '$1')
  31.         if (this.type == 'integer') editorContext.value = value.replace(/\D/g, '')
  32.         if (this.type == 'identifier') editorContext.value = value.replace(/[^A-Za-z0-9/g, '')
  33.       }
  34.       editorContext.addEventListener('input', format)
  35.       editorContext.addEventListener('change', format)
  36.     }
  37.   }
  38.   deactivateEditor(editorContext: HTMLInputElement, context?: any): void {
  39.     GC.Spread.Sheets.CellTypes.Base.prototype.deactivateEditor.apply(this, [editorContext, context])
  40.   }
  41.   getEditorValue(editorContext: HTMLInputElement, context?: any): string {
  42.     return editorContext.value
  43.   }
  44.   setEditorValue(editorContext: HTMLInputElement, value: any, context?: any): void {
  45.     if (value) editorContext.value = value
  46.   }
  47.   updateEditor(editorContext: HTMLInputElement, cellStyle: GC.Spread.Sheets.Style, cellRect: GC.Spread.Sheets.Rect, context?: any): GC.Spread.Sheets.Rect | any {
  48.     if (editorContext) {
  49.       editorContext.style.width = '1000px'
  50.       editorContext.style.height = '1000px'
  51.     }
  52.   }
  53. }
  54. export default CustomCellType
复制代码

麻烦帮忙排查问题,不要再建议监听其他事件了,就editCell

最佳答案

查看完整内容

您好,经过测试调研,我们发现您没有写isEditingValueChanged这个方法,导致每次进入编辑后退出时,该方法都return一个true,然后会触发一次editCell这个指令,您只要重写一下这个方法并在里面最对应的判断即可:

4 个回复

倒序浏览
最佳答案
最佳答案
Richard.HuangSpreadJS 开发认证
超级版主   /  发表于:2024-5-27 10:37:01
来自 3#
您好,经过测试调研,我们发现您没有写isEditingValueChanged这个方法,导致每次进入编辑后退出时,该方法都return一个true,然后会触发一次editCell这个指令,您只要重写一下这个方法并在里面最对应的判断即可:
  1. CustomCellType.prototype.isEditingValueChanged = function (oldValue, newValue) {
  2.     if (newValue != oldValue) {
  3.         return true;
  4.     }
  5.     return false;
  6. };
复制代码

回复 使用道具 举报
Richard.HuangSpreadJS 开发认证
超级版主   /  发表于:2024-5-27 18:25:13
2#
收到您的自定义单元格类型代码了,我们将进行深入调研,会尽快给您进行回复
回复 使用道具 举报
zyh2123606
金牌服务用户   /  发表于:2024-5-28 15:29:05
4#
好的,非常感谢
回复 使用道具 举报
Richard.HuangSpreadJS 开发认证
超级版主   /  发表于:2024-5-28 16:14:10
5#

好的,本帖子的问题解决,这里就先结帖了。后续如果您有新的问题,也欢迎创建新的求助帖。请记得设置最佳答案。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部