找回密码
 立即注册

QQ登录

只需一步,快速开始

Richard.Ma 讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2023-6-30 14:53  /   查看:956  /  回复:0
本帖最后由 Richard.Ma 于 2023-6-30 15:12 编辑

当前组件版编辑器支持用户选择纸张类型,但是如果要自定义纸张的宽高,目前没有提供对应的UI界面。页面设置对话框中也无法设置。

这篇教程介绍如何给组件版编辑器添加一个自定义纸张尺寸的对话框

先来看一下UI具体的效果,
image.png339556884.png

image.png242967833.png

通过这个简单的对话框,在填写宽高然后点击确认后,即可给当前sheet设置纸张的宽高

下面是实现此功能的具体步骤

1.添加一个对话框模板template

对话框模板定义了对话框中的元素。可以看到里面添加了2个NumberEditor,并且bindingPath分别为width和height,用于绑定宽高变量
  1. var CustomPaperSizeTemplate = {
  2.       templateName: "CustomPaperSizeTemplate",
  3.       title: "设置自定义纸张尺寸",
  4.       width: 300,
  5.       height: 200,
  6.       content: [{
  7.             type: "FlexContainer",
  8.             children: [{
  9.                         type: "TextBlock",
  10.                         text: "设置纸张自定义宽高",
  11.                         margin: "5px 10px"
  12.                   },
  13.                   {
  14.                         type: "Column",
  15.                         width: "auto",
  16.                         children: [{
  17.                                     type: "ColumnSet",
  18.                                     children: [{
  19.                                                 "type": "Column",
  20.                                                 "width": "50px",
  21.                                                 "children": [{
  22.                                                       "type": "TextBlock",
  23.                                                       "margin": "5px 10px -5px 10px",
  24.                                                       "text": "宽:"
  25.                                                 }]
  26.                                           },
  27.                                           {
  28.                                                 "type": "Column",
  29.                                                 "width": "auto",
  30.                                                 "children": [{
  31.                                                       type: "NumberEditor",
  32.                                                       bindingPath: "width",
  33.                                                       margin: "5px 10px"
  34.                                                 }]
  35.                                           }
  36.                                     ]
  37.                               },
  38.                               {
  39.                                     type: "ColumnSet",
  40.                                     width: "160px",
  41.                                     children: [{
  42.                                                 "type": "Column",
  43.                                                 "width": "50px",
  44.                                                 "children": [{
  45.                                                       "type": "TextBlock",
  46.                                                       "margin": "5px 10px",
  47.                                                       "text": "高:"
  48.                                                 }]
  49.                                           },
  50.                                           {
  51.                                                 "type": "Column",
  52.                                                 "width": "auto",
  53.                                                 "children": [{
  54.                                                       type: "NumberEditor",
  55.                                                       bindingPath: "height",
  56.                                                       margin: "5px 10px"
  57.                                                 }]
  58.                                           }
  59.                                     ]
  60.                               },
  61.                         ]
  62.                   },
  63.             ]
  64.       }]
  65. };
复制代码



2新建一个命令,用于弹出对话框

这段代码新建了一个命令,通过调用showDialog方法,传入了宽高参数,弹出刚刚创建的对话框,并且在对话框确认关闭后拿到修改的数据来进行应用。
  1. var paperSizeDialog = {
  2.       title: "自定义纸张尺寸",
  3.       text: "自定义纸张尺寸",
  4.       "direction": "horizontal",
  5.       commandName: "cmdPaperSizeDialog",
  6.       iconClass: "ribbon-button-pageSetup-size",
  7.       bigButton: true,
  8.       type: 'button', // 单击按钮命令时,将调用执行函数。
  9.       execute: (context, propertyName) => {
  10.             var custompapersize = designer.getData("custompapersize")
  11.             GC.Spread.Sheets.Designer.showDialog("CustomPaperSize", {
  12.                   width: custompapersize.width,
  13.                   height: custompapersize.height
  14.             }, (result) => {
  15.                   console.log(result);

  16.                   let {
  17.                         width,
  18.                         height
  19.                   } = result; // 结果的数据结构.
  20.                   designer.setData("custompapersize", {
  21.                         width: width,
  22.                         height: height
  23.                   })
  24.                   let spread = context.getWorkbook();
  25.                   let sheet = spread.getActiveSheet();
  26.                   sheet.printInfo().paperSize(new GC.Spread.Sheets.Print.PaperSize(width, height));
  27.             }, (e) => { // 错误回调
  28.                   console.log(e);
  29.             }, (result) => { // 有效回调,用于检查结果值是否有效。如果没有,对话框将无法关闭。
  30.                   return result;
  31.             });
  32.       }
  33. }
复制代码


3,应用上述模板和命令到编辑器中

  1. function adddlg(config) {
  2.       config.commandMap = {};
  3.       GC.Spread.Sheets.Designer.registerTemplate("CustomPaperSize", CustomPaperSizeTemplate);
  4.       var config = GC.Spread.Sheets.Designer.ToolBarModeConfig;
  5.       config.commandMap["cmdPaperSizeDialog"] = paperSizeDialog;

  6.       var command = {
  7.             text: "纸张尺寸",
  8.             id: "time",
  9.             buttonGroups: [{
  10.                   commandGroup: {
  11.                         "children": [
  12.                               "cmdPaperSizeDialog"
  13.                         ]

  14.                   }
  15.             }]
  16.       };
  17.       config.ribbon.panels[2].buttonGroups[0].commandGroup.children.splice(4, 0, "cmdPaperSizeDialog");
  18. }
复制代码
  1.       config = GC.Spread.Sheets.Designer.ToolBarModeConfig;
  2.       designer.setData("custompapersize", {
  3.             width: 50,
  4.             height: 50
  5.       })
  6.       adddlg(config);
  7.       designer.setConfig(config);
  8.       designer.refresh();
复制代码

4.处理页面设置对话框重置纸张宽高的问题

由于编辑器本身不支持自定义纸张尺寸,所以页面设置对话框在关闭时会把sheet的自定义纸张宽高重置为0,我们需要监听这个操作来避免此问题
  1.       spread = designer.getWorkbook();
  2.       spread.commandManager().addListener('', function (args) {
  3.             var command = args.command;
  4.             if (command.cmd == "Designer.setPageLayout" && command.paperKind == 0) {
  5.                   var custompapersize = designer.getData("custompapersize");
  6.                   spread.getActiveSheet().printInfo().paperSize(new GC.Spread.Sheets.Print.PaperSize(custompapersize.width, custompapersize.height));
  7.             }
  8.       });
复制代码




最后,例行附上此示例的项目代码
custompapersize.zip (3.6 KB, 下载次数: 26)

0 个回复

您需要登录后才可以回帖 登录 | 立即注册
返回顶部