大家在使用SpreadJS的隐藏功能的时候会发现SpreadJS的隐藏效果和Excel的隐藏效果是不一样的,如下图所示:
SpreadJS:
Excel:
那么如果想要在SpreadJS中实现类似Excel的隐藏效果应该如何实现呢?
在SpreadJS中,隐藏行并不是将它的行高设置为0,而是从视图上直接隐藏了这一行,但是Excel中则是将它的行高设置为了0,所以要实现这个需求的核心在于如何重写隐藏行的命令使其能够将行高设置为0。
以下是一个简单的示例,供大家参考:
- window.onload = function () {
- let designerConfig = JSON.parse(
- JSON.stringify(GC.Spread.Sheets.Designer.DefaultConfig)
- );
- let designer = new GC.Spread.Sheets.Designer.Designer(
- "gc-designer-container",
- designerConfig
- );
- let spread = designer.getWorkbook();
- let sheet = spread.getActiveSheet();
- let hideRow = {
- canUndo: true,
- name: "hideRow",
- execute: function (context, options, isUndo) {
- var Commands = GC.Spread.Sheets.Commands;
- if (isUndo) {
- Commands.undoTransaction(context, options);
- return true;
- } else {
- Commands.startTransaction(context, options);
- let sheet = context.getSheetFromName(options.sheetName);
- sheet.suspendPaint();
- let selections = options.selections;
- for (let i = 0; i < selections.length; i++) {
- let selection = selections[i];
- for (let j = 0; j < selection.rowCount; j++) {
- sheet.setRowHeight(selection.row + j, 0);
- }
- }
- sheet.resumePaint();
- Commands.endTransaction(context, options);
- return true;
- }
- },
- };
- spread.commandManager().register("hideRow", hideRow);
- let oldOpenMenu = spread.contextMenu.onOpenMenu;
- spread.contextMenu.onOpenMenu = function (
- menuData,
- itemsDataForShown,
- hitInfo,
- spread
- ) {
- oldOpenMenu.apply(this, arguments);
- itemsDataForShown.forEach(function (item, index) {
- if (item && item.name === "gc.spread.contextMenu.hideRows") {
- item.command = "hideRow";
- }
- });
- };
- };
复制代码
|
|