找回密码
 立即注册

QQ登录

只需一步,快速开始

Joestar.Xu SpreadJS 开发认证
超级版主   /  发表于:2024-4-2 09:37  /   查看:235  /  回复:1
大家在使用SpreadJS的隐藏功能的时候会发现SpreadJS的隐藏效果和Excel的隐藏效果是不一样的,如下图所示:


SpreadJS:
image.png915828246.png

Excel:
image.png717362975.png

那么如果想要在SpreadJS中实现类似Excel的隐藏效果应该如何实现呢?

在SpreadJS中,隐藏行并不是将它的行高设置为0,而是从视图上直接隐藏了这一行,但是Excel中则是将它的行高设置为了0,所以要实现这个需求的核心在于如何重写隐藏行的命令使其能够将行高设置为0。

以下是一个简单的示例,供大家参考:

  1. window.onload = function () {
  2.   let designerConfig = JSON.parse(
  3.     JSON.stringify(GC.Spread.Sheets.Designer.DefaultConfig)
  4.   );

  5.   let designer = new GC.Spread.Sheets.Designer.Designer(
  6.     "gc-designer-container",
  7.     designerConfig
  8.   );

  9.   let spread = designer.getWorkbook();

  10.   let sheet = spread.getActiveSheet();

  11.   let hideRow = {
  12.     canUndo: true,
  13.     name: "hideRow",
  14.     execute: function (context, options, isUndo) {
  15.       var Commands = GC.Spread.Sheets.Commands;
  16.       if (isUndo) {
  17.         Commands.undoTransaction(context, options);
  18.         return true;
  19.       } else {
  20.         Commands.startTransaction(context, options);
  21.         let sheet = context.getSheetFromName(options.sheetName);
  22.         sheet.suspendPaint();
  23.         let selections = options.selections;
  24.         for (let i = 0; i < selections.length; i++) {
  25.           let selection = selections[i];
  26.           for (let j = 0; j < selection.rowCount; j++) {
  27.             sheet.setRowHeight(selection.row + j, 0);
  28.           }
  29.         }
  30.         sheet.resumePaint();
  31.         Commands.endTransaction(context, options);
  32.         return true;
  33.       }
  34.     },
  35.   };

  36.   spread.commandManager().register("hideRow", hideRow);

  37.   let oldOpenMenu = spread.contextMenu.onOpenMenu;
  38.   spread.contextMenu.onOpenMenu = function (
  39.     menuData,
  40.     itemsDataForShown,
  41.     hitInfo,
  42.     spread
  43.   ) {
  44.     oldOpenMenu.apply(this, arguments);

  45.     itemsDataForShown.forEach(function (item, index) {
  46.       if (item && item.name === "gc.spread.contextMenu.hideRows") {
  47.         item.command = "hideRow";
  48.       }
  49.     });
  50.   };
  51. };
复制代码

SpreadJS 17.0.8 | GcExcel 7.1.0 已发布~

0 个回复

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