找回密码
 立即注册

QQ登录

只需一步,快速开始

Richard.Huang SpreadJS 开发认证
超级版主   /  发表于:2024-3-11 18:17  /   查看:299  /  回复:0
背景
我们的autoFitColumn和autoFitRow这两个方法的作用是对执行行列设置成自适应行高列宽的,但是这两个代码的会触发ColumnWidthChanged、RowHeightChanged事件,这是因为autoFitColumn是一个较老的API了,之前我们也发现了,但是如果现在更改这个API的逻辑会造成老用户的一些使用习惯以及代码兼容性上出现问题,因此目前我们无法判断是代码还是UI双击行列触发了ColumnWidthChanged、RowHeightChanged事件。可是会有用户存在业务需求,仅仅对UI操作的事件进行监听和逻辑的执行,对通过API触发的事件做其他处理,这该怎么办呢?

代码实现
实际上,我们的autoFitRow方法和autoFitColumn方法是借助指令来实现的,因此我们可以像重写其他指令一样重写这两个方法调用的指令,为指令传递一个布尔值flag,代码执行时,传入这个flag(UI操作肯定是不会携带这个我们自定义的参数的),从而判断自适应行高列宽的具体是代码操作还是UI操作
  1. var autoFitColumnCommand = {
  2.     canUndo: true,
  3.     execute: function (spread, options, isUndo) {
  4.         var Commands = GC.Spread.Sheets.Commands;
  5.         if (isUndo) {
  6.             Commands.undoTransaction(spread, options);
  7.             return true;
  8.         } else {
  9.             Commands.startTransaction(spread, options);
  10.             if (options.flag) {
  11.                 console.log("代码操作")
  12.             } else {
  13.                 console.log("UI操作")
  14.             }
  15.             GC.Spread.Sheets.Commands.autoFitColumn.execute(spread, options, isUndo)
  16.             Commands.endTransaction(spread, options);
  17.             return true;
  18.         }
  19.     }
  20. };
  21. var commandManager = spread.commandManager();
  22. commandManager.register('autoFitColumn', autoFitColumnCommand);
  23. commandManager.execute({ cmd: "autoFitColumn", sheetName: "Sheet1", columns: [{ column: 0 }], isRowHeader: false, autoFitType: GC.Spread.Sheets.AutoFitType.cell, flag: true });
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 立即注册
返回顶部