[{"id":"b66f8b1e-cc14-4e89-9679-abd5687d283d","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"569bb90a-ea68-46c6-96f1-ab151c120714","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"5eb52f08-2d1a-4362-9ffc-4871bdc10f3f","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"341dd607-b97d-4d70-bde2-53acda6b6c95","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"05349273-414f-4208-9ea2-c4fc8f4ea2cb","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"86089f76-b778-4d52-821e-6f27de3df613","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"b81e4fd6-1fc5-43a0-a258-b6e16a5cbec6","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"8aa8ce31-43e4-438e-951f-241608435260","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"37343f41-6ec2-4c7e-b21d-2cc18d5ce1e0","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"5915e52f-64f8-4146-b8bd-81bead6324a3","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"836ba889-af9e-460d-a4cc-c24d922795f2","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"4ae14b06-bb68-4394-a210-a46b8f028346","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"d8f42066-e9dc-4411-bdcf-43b1a203370c","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"7ef86f16-b1a0-49f7-9592-612b9be02b25","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"b29c7775-a9a4-451e-a1b5-01d19ed5ca5e","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"e56f3989-8f81-46af-90fa-a4813eeb976f","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"2526c963-f170-45a8-923e-91b0712a9810","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"24575cf0-501a-44f9-8426-c40f8f4b5552","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"1a6f8d7f-acd6-42be-8c4f-f464c6218381","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"82b176fd-5cab-498c-909e-8fa7d29c38d8","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"1708b3a9-4f37-44a8-8f0e-f9a2d2e5d940","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"c9ac246e-29fb-4bc4-8231-8439795bb590","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"94f2a5c3-2539-436a-af75-23fbbd1a3957","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"d99594f4-2d40-4df4-9419-ba2ca6aa3f7f","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"fd66e72d-0f10-4f57-9807-6db26290ab2e","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"8f344863-503d-4bc3-a594-3815e7d55f5c","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"9be82601-de9d-4c18-948a-23ab6f4dd431","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"6495f3c0-b463-47e0-b08a-ca949672211e","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"b4bede08-3f08-4839-ba4a-abc7ac195bde","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"4afcdfc0-3ff5-4f2b-a223-f8fc042a5bbe","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"422062c4-fa40-4771-a86f-008efe6d86e5","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"d87e8013-86a7-4840-8d25-6f62e14eb4ac","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"a6acedfd-4043-4c64-a5d1-aec3326df9e7","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"dbdc0b24-06c4-48b9-8d6c-7455119dc773","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"a3856849-954a-4cfc-96a6-382e530d3638","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"4a39306b-ffa5-433d-80a3-28e41f929b72","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]}]
        
(Showing Draft Content)

更改文件菜单内容

问题:V16版本推出了全新导入Excel文件的方式无需先转JSON再导入,那么如何使用原先的Excel IO导入导出Excel呢?


解决方案:

SpreadJS V16 设计器保留原有使用Excel IO导入导入导出Excel的方式,可以通过以下方式开启。

1. 获取文件菜单模板

let DesignerNS = GC.Spread.Sheets.Designer, fileMenuTemplateName = DesignerNS.TemplateNames.FileMenuPanelTemplate;
let template = DesignerNS.getTemplate(fileMenuTemplateName), targetNode = template.content[0].children[0].children[1];

2. 将导入导出Excel的选项和功能由新的导入方式value Excel替换为Excel IO

targetNode.children[1].children[1].children[0].children[0].items[0].value = "Excel IO"
targetNode.children[2].children[1].children[0].children[0].items[0].value = "Excel IO"

3. 由于Excel是一个选项,需要将导入导出方式的默认值也修改为Excel IO

context.getData("fileMenuSetting")中放的是用户上一次选择内容,如果选过就不替换了

4. 完整代码

// 由于导入Excel是第一个选项,需要修改一下初始的默认值,如果不是放到第一个,这个就不需要了
let getFileMenuOption = GC.Spread.Sheets.Designer.FileMenuHandler.getFileMenuOption;
GC.Spread.Sheets.Designer.FileMenuHandler.getFileMenuOption = function(context){
    let fileMenuSetting = context.getData("fileMenuSetting");
    let option = getFileMenuOption.call(this, context);
    console.log(option)
    if(!fileMenuSetting || !fileMenuSetting.activeCategory_import){
        option.activeCategory_import = "Excel IO"
    }
    if(!fileMenuSetting || !fileMenuSetting.activeCategory_export){
        option.activeCategory_export = "Excel IO"
    }
    return option;
}

let DesignerNS = GC.Spread.Sheets.Designer, fileMenuTemplateName = DesignerNS.TemplateNames.FileMenuPanelTemplate;
let template = DesignerNS.getTemplate(fileMenuTemplateName), targetNode = template.content[0].children[0].children[1];
// 默认value是Excel,使用新的导入导出选项,替换为Excel IO
targetNode.children[1].children[1].children[0].children[0].items[0].value = "Excel IO"
targetNode.children[2].children[1].children[0].children[0].items[0].value = "Excel IO"
// 也可以新增一个item 
// targetNode.children[1].children[1].children[0].children[0].items.splice(1,0,{text:"ExcelIO", value:"Excel IO"});
// targetNode.children[2].children[1].children[0].children[0].items.splice(1,0,{text:"ExcelIO", value:"Excel IO"});
DesignerNS.registerTemplate(fileMenuTemplateName, template);

也可以同时开启两种导入Excel的方式,这种情况不需要重写getFileMenuOption修改默认值了

targetNode.children[1].children[1].children[0].children[0].items.splice(1,0,{text:"ExcelIO", value:"Excel IO"});
targetNode.children[2].children[1].children[0].children[0].items.splice(1,0,{text:"ExcelIO", value:"Excel IO"});

image

使用Excel IO导入的方式可以监听FileLoading,获取到SpreadJS ssjson

designer.bind(GC.Spread.Sheets.Designer.Events.FileLoading, function(s, e){
    console.log(s, e);
});

问题:如何在文件菜单中添加像Excel一样的模板功能

背景:

我们都知道,在excel中有众多模板可以供选择。如何把这个功能加到SpreadJS在线表格编辑器中?

image

解决方案:

下面是我们实现的效果,我们来看下这个是如何实现的。

image

我们知道(不知道的看下前面的文章),编辑器左上角这个“文件”按钮点开之后会是一个特别大的Template,那么想要往里加东西,就需要先获取这个Template,然后按照里面的结构去添加,添加完成之后再注册回去。我们新增的Template是下面这样的一个结构。这个会在附件的custom-file-menu-panel.js文件中,记住这个“custom_item_action_1”bindingPath。

var listDisplayContainer =
    template["content"][0]["children"][0]["children"][1];

// 增加按钮
listDisplayContainer.children.push({
    type: "Container",
    visibleWhen: "activeCategory_main=CustomItem",
    children: [
        {
            type: "TextBlock",
            margin: "50px 0 15px 50px",
            text: "自定义行为",
            style: "font-size:36px;line-height:80px",
        },
        {
            type: "ColumnSet",
            margin: "0 0 0 50px",
            children: [
                {
                    type: "Column",
                    children: [
                        {
                            type: "List",
                            className: "file-menu-list",
                            items: [
                                {
                                    text: "打印",
                                    value: "print",
                                },
                                {
                                    text: "加载模板",
                                    value: "action_1",
                                },

                            ],
                            bindingPath: "custom_item",
                        },
                    ],
                    width: "350px",
                },
                {
                    type: "Container",
                    children: [
                        {
                            type: "Button",
                            margin: "20px 50px",
                            text: "打印",
                            width: 200,
                            height: 200,
                            iconClass: "icon-common1 icon-print",
                            bindingPath: "custom_item_print",
                            iconPosition: "top",
                        },
                    ],
                    visibleWhen: "custom_item=print",
                },
                {
                    type: "Container",
                    children: [
                        {
                            type: "Button",
                            margin: "20px 50px",
                            text: "编辑器模板",
                            width: 200,
                            height: 200,
                            iconClass: "icon-common1 icon-tmp",
                            bindingPath: "custom_item_action_1",
                            iconPosition: "top",
                        },
                    ],
                    visibleWhen: "custom_item=action_1",
                }
            ],
        },
    ],
});

上面的图片是怎么加进去的呢?就是通过iconClass将对应的css引入。其余大家可以通过下面的代码进行研究

.icon-tmp {
    /* 背景图片 */
    background-image: url("./jsonPic.png");
}

.icon-print {
    /* 打印的背景图片 */
    background-image: url("./print.png");
}


.icon-common1 {
    width: 150px;
    height: 150px;
}

设计完模板之后,我们就需要重写下面的这个命令了

GC.Spread.Sheets.Designer.getCommand(GC.Spread.Sheets.Designer.CommandNames.FileMenuPanel);

通过重写execute方法,在里面判断对应的bindingPath是否一致,一致则进到这个case里面包我想要的模板加载或者逻辑添加即可。

var fileMenuPanelCommand = GC.Spread.Sheets.Designer.getCommand(GC.Spread.Sheets.Designer.CommandNames.FileMenuPanel);// 获取文件按钮表盘
var oldExecuteFn = fileMenuPanelCommand.execute;// 执行按钮获取

// 重写执行逻辑
fileMenuPanelCommand.execute = function (context, propertyName, newValue) {
    // console.log(context)
    // console.log(propertyName)
    // console.log(newValue)
    oldExecuteFn.apply(this, arguments);
    switch (propertyName) {
        case 'custom_item_print':// 自定义打印
            var workbook = context.getWorkbook();// context就是designer
            workbook.print();// 打印
            break;
        case 'custom_item_action_1':// 自定义行为1
            var workbook = context.getWorkbook();
            workbook.fromJSON(tmp)// 导入设定好的模板
            GC.Spread.Sheets.Designer.getCommand('fileMenuPanel').execute(designer, "activeCategory_hide", true)// 执行指令
            break;
    }
}

最后,别忘了将新的模板和命令注册回去。

var config = GC.Spread.Sheets.Designer.DefaultConfig;
config.commandMap = {
    fileMenuPanel: fileMenuPanelCommand
}
var designer = new GC.Spread.Sheets.Designer.Designer("gc-designer-container", config);

下载附件请查看论坛精华帖

作者: Grapecity.China | 审核:Clark.Pan | 更新时间:2023.12.28