您好,我写了一个简单的Demo请参考:
- 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();
- window.designer = designer;
- window.spread = spread;
- window.sheet = sheet;
- let character = 0;
- let addSignSetting = {
- canUndo: true,
- name: "addSignSetting",
- execute: function (context, options, isUndo) {
- let 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.setTag(
- sheet.getActiveRowIndex(),
- sheet.getActiveColumnIndex(),
- JSON.stringify({ sign: true })
- );
- Commands.endTransaction(context, options);
- return true;
- }
- },
- };
- let setSign = {
- canUndo: true,
- name: "setSign",
- execute: function (context, options, isUndo) {
- let 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.setValue(
- sheet.getActiveRowIndex(),
- sheet.getActiveColumnIndex(),
- "sign"
- );
- Commands.endTransaction(context, options);
- return true;
- }
- },
- };
- spread.commandManager().register("addSignSetting", addSignSetting);
- spread.commandManager().register("setSign", setSign);
- let oldOpenMenu = spread.contextMenu.onOpenMenu;
- spread.contextMenu.onOpenMenu = function (
- menuData,
- itemsDataForShown,
- hitInfo,
- spread
- ) {
- oldOpenMenu.apply(this, arguments);
- console.log(itemsDataForShown);
- if (character == 1) {
- itemsDataForShown.push({
- text: "addSignSetting",
- name: "addSignSetting",
- command: "addSignSetting",
- workArea: "viewport",
- });
- }
- if (character == 2) {
- let { row, col } = hitInfo.worksheetHitInfo;
- if (JSON.parse(sheet.getTag(row, col)).sign === true) {
- itemsDataForShown.push({
- text: "setSign",
- name: "setSign",
- command: "setSign",
- workArea: "viewport",
- });
- }
- }
- };
- document.querySelector("#button1").addEventListener("click", function () {
- character = 1;
- });
- document.querySelector("#button2").addEventListener("click", function () {
- character = 2;
- });
- };
复制代码
|