重写插入行指令实现插入行并复制样式
本帖最后由 Joestar.Xu 于 2024-5-15 17:24 编辑在使用SpreadJS的过程中我们会需要再某一行的样式的基础上再插入一样的一行,但是直接插入并不会保留样式,这样的需求应该如何实现呢?
只需要重写一下插入行的命令即可实现,参考以下代码:
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 insertRowsCopyStyle = {
canUndo: true,
name: "insertRowsCopyStyle",
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);
options.cmd = "gc.spread.contextMenu.insertRows";
context.commandManager().execute(options);
options.cmd = "insertRowsCopyStyle";
sheet.copyTo(
sheet.getActiveRowIndex() + 1,
0,
sheet.getActiveRowIndex(),
0,
1,
sheet.getColumnCount(),
GC.Spread.Sheets.CopyToOptions.style,
)
Commands.endTransaction(context, options);
return true;
}
},
};
spread.commandManager().register("insertRowsCopyStyle", insertRowsCopyStyle);
spread.contextMenu.menuData.forEach(item => {
if (item.name === "gc.spread.insertRows") {
item.command = "insertRowsCopyStyle"
}
})
页:
[1]