找回密码
 立即注册

QQ登录

只需一步,快速开始

搬砖小李

注册会员

6

主题

36

帖子

96

积分

注册会员

积分
96
最新发帖
搬砖小李
注册会员   /  发表于:2023-11-1 14:21  /   查看:1424  /  回复:19
本帖最后由 Richard.Huang 于 2023-11-7 12:12 编辑

产品:SpreadJS
版本:16.1.4

现在想重写合并单元格的默认行为,在用户操作合并单元格的时候给出一个弹窗提示,用户点击确定后才进行下一步操作,为了保持ui的一致,所以希望弹出如图所示风格的弹窗。想知道是否有api可以支持此操作。

image.png345267028.png

index3.html.zip

2.47 KB, 下载次数: 56

19 个回复

倒序浏览
Joestar.XuSpreadJS 开发认证
超级版主   /  发表于:2023-11-1 17:30:49
沙发
您好,可以参考这个链接中的内容来实现弹出对话框的功能:https://demo.grapecity.com.cn/sp ... gner#showmessagebox
SpreadJS 17.0.10 | GcExcel 7.1.2 已发布~
回复 使用道具 举报
搬砖小李
注册会员   /  发表于:2023-11-1 17:37:28
板凳
Joestar.Xu 发表于 2023-11-1 17:30
您好,可以参考这个链接中的内容来实现弹出对话框的功能:https://demo.grapecity.com.cn/spreadjs/help/ap ...

好的 我试试
回复 使用道具 举报
Joestar.XuSpreadJS 开发认证
超级版主   /  发表于:2023-11-1 17:51:04
地板
SpreadJS 17.0.10 | GcExcel 7.1.2 已发布~
回复 使用道具 举报
搬砖小李
注册会员   /  发表于:2023-11-1 21:19:53
5#
本帖最后由 搬砖小李 于 2023-11-1 21:21 编辑

我现在重写合并单元格的逻辑,加入了一些确认操作,使用弹窗回调执行合并后,发现撤销操作导致单元格状态不对了
  1. {
  2.       const oldAddSpan = GC.Spread.Sheets.Worksheet.prototype.addSpan
  3.       GC.Spread.Sheets.Worksheet.prototype.addSpan = function newAddSpan(...args) {
  4.         const [row, col, rowCount, colCount] = args
  5.         const sheet = spread.getActiveSheet()

  6.         // 保持和Excel行为一致,禁止表格区域合并
  7.         for(const table of sheet.tables.all()) {
  8.           if(isOverlapWithTable({ row, col, rowCount, colCount}, table)) {
  9.             GC.Spread.Sheets.Designer.showMessageBox('禁止表格内的单元格合并', 'SpreadJS 设计器', GC.Spread.Sheets.Designer.MessageBoxIcon.warning)
  10.             return
  11.           }
  12.         }

  13.         // 保持和Excel行为一致
  14.         // 合并前:[空][公式1][公式2] 合并后:[公式1],取消合并后:[公式1][空][空]
  15.         // 合并前:[值1][公式1][公式2] 合并后:[值1],取消合并后:[值1][空][空]
  16.         let firstFormula = null
  17.         let firstValue = null
  18.         let firstFind = false
  19.         let valueCellCount = 0 // 有值的单元格个数

  20.         for(let rowIndex = row; rowIndex <= row + rowCount - 1;  rowIndex++) {
  21.           for(let colIndex = col; colIndex <= col + colCount - 1; colIndex++) {
  22.             const formula = sheet.getFormula(rowIndex, colIndex)
  23.             const value = sheet.getValue(rowIndex, colIndex)
  24.             if(!firstFind) {
  25.               if(formula) {
  26.                 firstFormula = formula
  27.                 firstFind = true
  28.               } else if(value !== null && value !== undefined) {
  29.                 firstValue = value
  30.                 firstFind = true
  31.               }
  32.             }

  33.             if(formula || value) {
  34.               valueCellCount++
  35.             }
  36.           }
  37.         }

  38.         function doSpan() {
  39.           for(let rowIndex = row; rowIndex <= row + rowCount - 1;  rowIndex++) {
  40.             for(let colIndex = col; colIndex <= col + colCount - 1; colIndex++) {
  41.               sheet.setFormula(rowIndex, colIndex, null)
  42.               sheet.setValue(rowIndex, colIndex, null)
  43.             }
  44.           }
  45.           // sheet.clear(row, col, rowCount, colCount)
  46.           if(firstFormula) {
  47.             sheet.setFormula(row, col, firstFormula)
  48.           } else {
  49.             sheet.setValue(row, col, firstValue)
  50.           }

  51.           oldAddSpan.apply(sheet, args)
  52.         }

  53.         if(firstFind && valueCellCount > 1) {
  54.           GC.Spread.Sheets.Designer.showMessageBox(
  55.             '合并单元格时,仅保留左上角的值,而放弃其他值。',
  56.             'SpreadJS 设计器',
  57.             GC.Spread.Sheets.Designer.MessageBoxIcon.warning,
  58.             (action) => {
  59.               if(action === GC.Spread.Sheets.Designer.MessageBoxResult.ok) {
  60.                 doSpan()
  61.               }
  62.             },
  63.             () => {},
  64.             GC.Spread.Sheets.Designer.MessageBoxButtons.okCancel
  65.           )
  66.         } else {
  67.           doSpan()
  68.         }

  69.       }
复制代码


回复 使用道具 举报
Joestar.XuSpreadJS 开发认证
超级版主   /  发表于:2023-11-2 11:20:40
6#
您好,您说的“撤销操作导致单元格状态不对了”,具体指的是什么情况?
SpreadJS 17.0.10 | GcExcel 7.1.2 已发布~
回复 使用道具 举报
搬砖小李
注册会员   /  发表于:2023-11-2 11:23:23
7#
搬砖小李 发表于 2023-11-1 21:19
我现在重写合并单元格的逻辑,加入了一些确认操作,使用弹窗回调执行合并后,发现撤销操作导致单元格状态 ...

我发现如果多个选区合并会弹多个窗,现在我考虑使用重写【MergeCells】、【MergeCenter】、【MergeAcross】方法来实现我的逻辑,正向功能实现了,但是当ctrl+z回退时,发现相当于只执行了取消合并的操作,单元格的状态无法恢复,代码如下:
  1. export function rewriteMergeCell(commandName){

  2.   let newCommand=GC.Spread.Sheets.Designer.getCommand(commandName)
  3.   let oldExecute = newCommand.execute

  4.   newCommand.execute = function (context, options = {}, isMerge) {
  5.     console.log(context, options, isMerge)
  6.     if(!isMerge) {
  7.       return oldExecute.call(this, context, options, isMerge)
  8.     }

  9.     const sheet= context.Spread.getActiveSheet()
  10.     const selections=sheet.getSelections()
  11.     const ranges = []

  12.     function doSpan() {

  13.       // GC.Spread.Sheets.Command.startTransaction(context.Spread, options)
  14.       for (const range of ranges) {
  15.         const {row, rowCount, col, colCount, firstFormula, firstValue} = range
  16.         sheet.clear(row, col, rowCount, colCount, GC.Spread.Sheets.SheetArea.viewport,GC.Spread.Sheets.StorageType.data)
  17.         if(firstFormula) {
  18.           sheet.setFormula(row, col, firstFormula)
  19.         } else {
  20.           sheet.setValue(row, col, firstValue)
  21.         }
  22.       }

  23.       oldExecute.call(this, context, options, isMerge)
  24.       // GC.Spread.Sheets.Command.endTransaction(context.Spread, options)
  25.     }

  26.     for(const range of  selections) {
  27.       const { row, col, rowCount, colCount } = range

  28.       // 保持和Excel行为一致,禁止表格区域合并
  29.       for(const table of sheet.tables.all()) {
  30.         if(isOverlapWithTable({ row, col, rowCount, colCount}, table)) {
  31.           GC.Spread.Sheets.Designer.showMessageBox('禁止表格内的单元格合并', 'SpreadJS 设计器', GC.Spread.Sheets.Designer.MessageBoxIcon.warning)
  32.           return
  33.         }
  34.       }

  35.       // 保持和Excel行为一致
  36.       // 合并前:[空][公式1][公式2] 合并后:[公式1],取消合并后:[公式1][空][空]
  37.       // 合并前:[值1][公式1][公式2] 合并后:[值1],取消合并后:[值1][空][空]
  38.       let firstFormula = null
  39.       let firstValue = null
  40.       let firstFind = false
  41.       let valueCellCount = 0 // 有值的单元格个数

  42.       for(let rowIndex = row; rowIndex <= row + rowCount - 1;  rowIndex++) {
  43.         for(let colIndex = col; colIndex <= col + colCount - 1; colIndex++) {
  44.           const formula = sheet.getFormula(rowIndex, colIndex)
  45.           const value = sheet.getValue(rowIndex, colIndex)
  46.           if(!firstFind) {
  47.             if(formula) {
  48.               firstFormula = formula
  49.               firstFind = true
  50.             } else if(value !== null && value !== undefined) {
  51.               firstValue = value
  52.               firstFind = true
  53.             }
  54.           }

  55.           if(formula || value) {
  56.             valueCellCount++
  57.           }
  58.         }
  59.       }

  60.       ranges.push({row, rowCount, col, colCount, firstFormula, firstValue, valueCellCount, firstFind})
  61.     }

  62.     // 查询每个选区域,看看是否有的选区范围内有多个值
  63.     let valueCellCount = 0
  64.     for(const range of ranges) {
  65.       valueCellCount = Math.max(range.valueCellCount, valueCellCount)
  66.     }

  67.     if(valueCellCount > 1) {
  68.       GC.Spread.Sheets.Designer.showMessageBox(
  69.         '合并单元格时,仅保留左上角的值,而放弃其他值。',
  70.         'SpreadJS 设计器',
  71.         GC.Spread.Sheets.Designer.MessageBoxIcon.warning,
  72.         (action) => {
  73.           if(action === GC.Spread.Sheets.Designer.MessageBoxResult.ok) {
  74.             doSpan()
  75.           }
  76.         },
  77.         () => {},
  78.         GC.Spread.Sheets.Designer.MessageBoxButtons.okCancel
  79.       )
  80.     } else {
  81.       doSpan()
  82.     }
  83.   }

  84.   return newCommand
  85. }
复制代码
回复 使用道具 举报
搬砖小李
注册会员   /  发表于:2023-11-2 11:31:41
8#
Joestar.Xu 发表于 2023-11-2 11:20
您好,您说的“撤销操作导致单元格状态不对了”,具体指的是什么情况?

哈喽 ,我修改了逻辑,麻烦看下我下边的逻辑,然后执行结果是这样的
[空][公式1][空]        合并后          [公式1 _____________          crtl+z              [公式1][空][空]
[空][空][空]       ===========》  ___________________       ===========》 [空][空][空]   
[空][公式2][空]                           __________________]                                [空][空][空]   
回复 使用道具 举报
搬砖小李
注册会员   /  发表于:2023-11-2 11:36:31
9#
image.png359013671.png
回复 使用道具 举报
Joestar.XuSpreadJS 开发认证
超级版主   /  发表于:2023-11-2 11:49:03
10#
您这边提供的代码我无法运行,缺少commandName变量,我尝试修改但仍然无法重现您的逻辑效果,请您提供一个完整的Demo,这边复现后调研一下。
SpreadJS 17.0.10 | GcExcel 7.1.2 已发布~
回复 使用道具 举报
12下一页
您需要登录后才可以回帖 登录 | 立即注册
返回顶部