轉帖|使用教程|編輯:王香|2019-03-13 13:09:42.000|閱讀 374 次
概述:Spread JS V11開始已有了原生右鍵菜單,右鍵菜單中的項可以靈活的自定義(參考擴展上下文菜單)。當現有的菜單不滿足我們需求時候,我們可以通過自定義Command的方式拓展功能。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Spread JS V11開始已有了原生右鍵菜單,右鍵菜單中的項可以靈活的自定義(參考擴展上下文菜單)。當現有的菜單不滿足我們需求時候,我們可以通過自定義Command的方式拓展功能。
在Excel中我們常用的一個操作是插入行,并希望在插入行后自動復制上一行的樣式,合并單元格,那么已這個需求為例,講解如何重定義現有插入菜單
Command中使用Transaction實現Undo和Redo,exectue時先調用原有“gc.spread.contextMenu.insertRows”命令插入行,然后復制插入行前樣式。
var 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); var sheet = context.getSheetFromName(options.sheetName); sheet.suspendPaint(); options.cmd = "gc.spread.contextMenu.insertRows" context.commandManager().execute(options); options.cmd = "insertRowsCopyStyle"; var beforeRowCount = 0; if (options.selections && options.selections.length) { var selections = getSortedRowSelections(options.selections) for (var i = 0; i < selections.length; i++) { var selection = selections[i]; if (selection.row > 0) { for (var row = selection.row + beforeRowCount; row < selection.row + beforeRowCount + selection.rowCount; row++) { sheet.copyTo(selection.row + beforeRowCount - 1, -1, row, -1, 1, -1, GC.Spread.Sheets.CopyToOptions.style | GC.Spread.Sheets.CopyToOptions.span); } } beforeRowCount += selection.rowCount; } } sheet.resumePaint(); Commands.endTransaction(context, options); return true; } } };
getSortedRowSelections為對selections按照row Index排序的方法。
function getSortedRowSelections(selections) { var sortedRanges = selections; for (var i = 0; i < sortedRanges.length - 1; i++) { for (var j = i + 1; j < sortedRanges.length; j++) { if (sortedRanges[i].row > sortedRanges[j].row) { var temp = sortedRanges[i]; sortedRanges[i] = sortedRanges[j]; sortedRanges[j] = temp; } } } return sortedRanges; }
spread.commandManager().register("insertRowsCopyStyle", insertRowsCopyStyle);
function MyContextMenu() {} MyContextMenu.prototype = new GC.Spread.Sheets.ContextMenu.ContextMenu(spread); MyContextMenu.prototype.onOpenMenu = function (menuData, itemsDataForShown, hitInfo, spread) { itemsDataForShown.forEach(function (item, index) { if (item && item.name === "gc.spread.insertRows") { item.command = "insertRowsCopyStyle" } }); }; var contextMenu = new MyContextMenu(); spread.contextMenu = contextMenu;
在onOpenMenu 方法中,可以拿到當前點擊區域可操作的所有右鍵菜單內容,對itemsDataForShown進行修改,可以更改最終呈現出來右鍵菜單的內容。比如刪除某一項,或者設置disable=true來禁止。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn