找回密码
 立即注册

QQ登录

只需一步,快速开始

dexteryao 讲师达人认证 悬赏达人认证 SpreadJS 开发认证

超级版主

123

主题

8927

帖子

1万

积分

超级版主

Rank: 8Rank: 8

积分
13532

讲师达人悬赏达人元老葡萄SpreadJS 认证SpreadJS 高级认证微信认证勋章

dexteryao 讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2020-10-31 18:15  /   查看:4724  /  回复:2
本帖最后由 dexteryao 于 2021-2-3 14:06 编辑

SpreadJS 新的组件版设计器采用了全新可配置设计,任何区域都采取json config 的配置方式。
通过修改默认的GC.Spread.Sheets.Designer.DefaultConfig,可以达到自定制功能
首先看一下如何自定义工具栏(Ribbon)并添加新的ribbon tab和按钮


1. 增加新Ribbon 选项卡
在浏览器Console中输入GC.Spread.Sheets.Designer.DefaultConfig即可查看默认ribbon选项卡配置


参考默认配置,自定义操作选项卡

var designerConfig = JSON.parse(JSON.stringify(GC.Spread.Sheets.Designer.DefaultConfig))
var customerRibbon = {
    "id": "operate",
    "text": "操作",
    "buttonGroups": [
    ]
};designerConfig.ribbon.push(customerRibbon);var designer = new GC.Spread.Sheets.Designer.Designer(
    document.getElementById('ssDesigner'), /**designer host */
    designerConfig, // designerConfigJson /**If you want to change Designer's layout or rewrite it, you can pass in your own Config JSON */ ,
    undefined /**If you want to use the spread you already created instead of generating a new spread, pass in */
);
2. 自定义button
在定义按钮之前,需要先定义按钮点击时的命令Commands,并将命令注册到config的commandMap属性上
var ribbonFileCommands = {
    "getTemplates": {
        iconClass: "ribbon-button-download",
        text: "加载",
        commandName: "getTemplates",
        execute: getTemplates,
    },
    "uploadFile":
    {
        iconClass: "ribbon-button-upload",
        text: "上传",
        commandName: "uploadFile",
        execute: uploadFile,
    },}

上面代码注册了getTemplates和uploadFile两个命令,
其中execute对应的时具体执行内容的function
iconClass为按钮样式,可以制定按钮图片
text为显示文字
commandName为命令名称,需要全局唯一
iconClass示例:
.ribbon-button-download {
    background-image: url(图片地址,可以是base64 svg)}


async function getTemplates(designer) {
    var spread = designer.getWorkbook();//使用getWorkbook获取Spread对象并操作}async function uploadFile(designer) {
    var spread = designer.getWorkbook();//使用getWorkbook获取Spread对象并操作}

有了命令就可以添加对应button 的config了
var ribbonFileConfig =
{
    "label": "文件操作",
    "thumbnailClass": "ribbon-thumbnail-spreadsettings",
    "commandGroup": {
        "children": [
            {
                "direction": "vertical",
                "commands": ["getTemplates", "uploadFile"]
            },
            {
                "type": "separator"
            }        ]
    }
};

在designer的config中加入自定义的命令和按钮

designerConfig.commandMap = {}
Object.assign(designerConfig.commandMap, ribbonFileCommands)
customerRibbon.buttonGroups.push(ribbonFileConfig);designerConfig.ribbon.push(customerRibbon);
var designer = new GC.Spread.Sheets.Designer.Designer(
    document.getElementById('ssDesigner'), /**designer host */
    designerConfig, // designerConfigJson /**If you want to change Designer's layout or rewrite it, you can pass in your own Config JSON */ ,
    undefined /**If you want to use the spread you already created instead of generating a new spread, pass in */
);


这样我们新建的按钮就可以正常工作了。

image.png756971435.png

image.png325417479.png
image.png745974120.png

2 个回复

倒序浏览
sumyy
注册会员   /  发表于:2023-8-16 14:17:02
沙发
"direction": "vertical",这个值是唯一的嘛?还是说有其他值或者说有文档列举?
回复 使用道具 举报
Joestar.XuSpreadJS 开发认证
超级版主   /  发表于:2023-8-16 14:22:16
板凳
sumyy 发表于 2023-8-16 14:17
"direction": "vertical",这个值是唯一的嘛?还是说有其他值或者说有文档列举?

您好,direction可以是vertical也可以是horizontal。
SpreadJS 17.0.8 | GcExcel 7.1.0 已发布~
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部