您好,可以通过重写Command来实现这个需求,以下是一个简单的例子:
- let insertRowCheckTag = {
- canUndo: true,
- name: "insertRowCheckTag",
- execute: function (context, options, isUndo) {
- var Commands = GC.Spread.Sheets.Commands;
- if (isUndo) {
- Commands.undoTransaction(context, options);
- return true;
- } else {
- let sheet = context.getSheetFromName(options.sheetName);
- let selection = sheet.getSelections()[0];
- let rowIndex = selection.row;
- let columnCount = sheet.getColumnCount();
- for (let i = 0; i < columnCount; i++) {
- let tag = sheet.getTag(rowIndex, i);
- if (tag) {
- console.log("tag exist, cannot add row");
- return false;
- }
- }
- Commands.startTransaction(context, options);
- sheet.addRows(rowIndex, 1)
- Commands.endTransaction(context, options);
- return true;
- }
- },
- };
- spread.commandManager().register("insertRowCheckTag", insertRowCheckTag);
- 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.insertRows") {
- item.command = "insertRowCheckTag";
- }
- });
- };
复制代码 |