找回密码
 立即注册

QQ登录

只需一步,快速开始

Ellia.Duan SpreadJS 开发认证

超级版主

67

主题

4654

帖子

7185

积分

超级版主

Rank: 8Rank: 8

积分
7185

SpreadJS 认证SpreadJS 高级认证

Ellia.Duan SpreadJS 开发认证
超级版主   /  发表于:2024-7-26 13:59  /   查看:150  /  回复:0
本帖最后由 Ellia.Duan 于 2024-7-29 13:34 编辑

在上一篇文章中,我们介绍到可以利用"Wingdings 2 "在表格中插入一些符号。但是对于普通用户来说,让他们去记忆符号对应的键盘值,无疑是困难的。

为此,我们可以在工具栏中添加图标下拉选择,选中后设置富文本,添加这个符号。
我们想要实现这个效果,如下图所示,该如何实现呢?
image.png676062621.png
我们观察上图,除了 image.png623613891.png 其余部分是可以输入文字实现的。我们接下来处理,如何插入复选框符号。

1、定义菜单
image.png73023070.png
我们先来实现如上图所示的一个工具栏菜单。
  1.         var config = JSON.parse(JSON.stringify(GC.Spread.Sheets.Designer.DefaultConfig));
  2.         let fontIconConfig = {
  3.             label: "字符集",
  4.             commandGroup: {
  5.                 commands: ["setFontIcon"],
  6.             }
  7.         };
  8.         config.ribbon[1].buttonGroups.splice(config.ribbon[1].buttonGroups.length - 1, 0, fontIconConfig);
  9.         let fontIconCammands = {
  10.             setFontIcon: {
  11.                 bigButton: "=ribbonHeight>toolbarHeight",
  12.                 commandName: "setFontIcon",
  13.                 direction: "=IF(ribbonHeight<toolbarHeight,"horizontal","vertical")",
  14.                 dropdownList: [
  15.                     {
  16.                         groupName: "Wingdings 2",
  17.                         groupItems: [{
  18.                             iconClass: "iconfont icon-checkbox",
  19.                             iconHeight: 20,
  20.                             iconWidth: 14,
  21.                             tip: "勾选",
  22.                             value: "R"
  23.                         }, {
  24.                             iconClass: "iconfont icon-uncheckbox",
  25.                             iconHeight: 20,
  26.                             iconWidth: 14,
  27.                             tip: "不勾选",
  28.                             value: "&#163;"
  29.                         }
  30.                         ],
  31.                     },
  32.                 ],
  33.                 // iconClass: "ribbon-button-shapes",
  34.                 text: "字符",
  35.                 title: "插入字符",
  36.                 type: "dropdown",
  37.                 iconClass: "icon-custom-special-symbol",

  38.                 execute: async (context, selectValue) => {
  39.                           console.log(selectValue)
  40.                 },
  41.             },
  42.         };
  43.         config.commandMap = {}
  44.         Object.assign(config.commandMap, fontIconCammands)
  45.         var designer = new GC.Spread.Sheets.Designer.Designer("gc-designer-container", config)
复制代码
通过上述代码,我们便实现了插入符号的菜单功能,这里就不详细介绍具体的实现细节,具体可以参考这篇文章
2、重写execute
  1. if (selectValue != null && selectValue != undefined) {
  2.                         let sheet = context.getWorkbook().getActiveSheet(),
  3.                             row = sheet.getActiveRowIndex(),
  4.                             col = sheet.getActiveColumnIndex()
  5.                         let value = sheet.getValue(row, col, GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.ValueType.richText)

  6.                         let richText = [
  7.                             {
  8.                                 style: {
  9.                                     font: '16px "Wingdings 2"',
  10.                                     foreColor: "rgb(0, 0, 0)",
  11.                                     textDecoration: 0
  12.                                 },
  13.                                 text: selectValue,
  14.                             }
  15.                         ]
  16.                         if (value && value.richText) {
  17.                             richText.unshift(...value.richText)
  18.                         } else {
  19.                             richText.unshift({text: sheet.getValue(row, col) == null ? '' : sheet.getValue(row, col) + ''})
  20.                         }
  21.                         let text = richText.map(item => item.text)
  22.                         let newValue = {richText, text: text.join("")}
  23.                         sheet.setValue(row, col, newValue, GC.Spread.Sheets.SheetArea.viewport);
  24.                     }
复制代码

在上述代码中,我们重写了富文本,添加了字符样式,并重新setValue
3、双击打开富文本编辑器
在双击事件中,判断如果当前单元格存在富文本,则打开富文本编辑器,如下代码:
  1.   sheet.bind(GC.Spread.Sheets.Events.CellDoubleClick, function (e, info) {
  2.             let value = sheet.getValue(info.row, info.col, GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.ValueType.richText)
  3.             if (value && value.richText) {
  4.                 spread.commandManager().execute({
  5.                     cmd: "richText",
  6.                     sheetName: sheet.name(),
  7.                     row: info.row,
  8.                     col: info.col
  9.                 });
  10.             }

  11.         });
复制代码


最后,由于菜单中无法获取单元格的焦点,所以会在单元格文本内容最后插入符号,如果有在文本中间内容插入符号的需求,可以剪切到相应位置。
完整代码可以参考附件的demo: insert-符号.html (14.78 KB, 下载次数: 7)

0 个回复

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