找回密码
 立即注册

QQ登录

只需一步,快速开始

dexteryao 讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2022-1-28 10:04  /   查看:2774  /  回复:2
本帖最后由 dexteryao 于 2022-1-30 08:34 编辑

前面介绍了单元格中使用区域模板,同理在表格绑定中也能使用区域模板。

由于表格绑定目前还不支持列设置celltype,我们需要在表格绑定后,通过代码给指定区域设置celltype

在下面示例中,给表格第一行设置了celltype,然后使用copyTableStyle方法将内容复制到表格其他行,样式和公式都可以复制。
表格绑定的数据也无法使用sheet绑定中的value方法返回。需要在构建数据源时,数组中设置Range Template的属性,就是一个对象

demo中重新构建了数据,
{list:[{"detail": data[0]}, {"detail": data[1]}, {"detail": data[2]} //可以循环构建


demo 如下,打卡有点慢,请耐心等待 主要代码:
  1. import * as GC from "@grapecity/spread-sheets";
  2. import "@grapecity/spread-sheets-tablesheet";
  3. import "./license.js";
  4. import "./templateSheet.js";
  5. import "./data.js";
  6. var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"));
  7. var dialogSS;
  8. var editorDialog = document.getElementById("editorDialog");
  9. var dialogOK = document.getElementById("dialogOK");
  10. var dialogCancel = document.getElementById("dialogCancel");
  11. var renderSheet = spread.getActiveSheet();
  12. renderSheet.defaults.rowHeight = 207;
  13. var templateSheet = new GC.Spread.Sheets.Worksheet();
  14. templateSheet.fromJSON(templatesheetjson);
  15. spread.addSheet(1, templateSheet);
  16. templateSheet.setFormatter(2, 2, "=IMAGE(@)");
  17. renderSheet.suspendPaint();
  18. var celltype = new GC.Spread.Sheets.CellTypes.RangeTemplate(templateSheet);
  19. // renderSheet.setCellType(0, 1, celltype);
  20. // renderSheet.setBindingPath(0, 1, "detail")
  21. renderSheet.setRowHeight(0, 10)
  22. renderSheet.setColumnWidth(0, 440)
  23. // renderSheet.setColumnWidth(1, 440)
  24. var dataSource = new GC.Spread.Sheets.Bindings.CellBindingSource({ //"detail": data[10],
  25. list: [{ "detail": data[0] }, { "detail": data[1] }, { "detail": data[2] }]
  26. });
  27. renderSheet.setRowHeight(1, 30)
  28. var table = renderSheet.tables.add("tableRecordds", 1, 0, 4, 1);
  29. table.autoGenerateColumns(false);
  30. var tableColumn1 = new GC.Spread.Sheets.Tables.TableColumn();
  31. tableColumn1.name("Detail");
  32. tableColumn1.dataField("detail");
  33. table.bindColumns([tableColumn1]);
  34. table.bindingPath("list")
  35. renderSheet.setDataSource(dataSource)
  36. renderSheet.getRange(2, 0).cellType(celltype)
  37. copyTableStyle(renderSheet, table)
  38. renderSheet.resumePaint();
  39. function copyTableStyle(sheet, table) {
  40. var range = table.dataRange();
  41. var rowHeight = sheet.getRowHeight(range.row);
  42. for (var i = 1; i < range.rowCount; i++) {
  43. // Copy Style
  44. sheet.copyTo(range.row + i - 1, range.col, range.row + i, range.col, 1, range.colCount, GC.Spread.Sheets.CopyToOptions.style);
  45. // Copy Formula
  46. sheet.copyTo(range.row + i - 1, range.col, range.row + i, range.col, 1, range.colCount, GC.Spread.Sheets.CopyToOptions.formula);
  47. // Copy Span
  48. sheet.copyTo(range.row + i - 1, range.col, range.row + i, range.col, 1, range.colCount, GC.Spread.Sheets.CopyToOptions.span);
  49. // Set Row Height
  50. sheet.setRowHeight(range.row + i, rowHeight);
  51. }
  52. }
  53. spread.bind(GC.Spread.Sheets.Events.CellDoubleClick, function (s, e) {
  54. var sheet = e.sheet, row = e.row, col = e.col;
  55. var cellType = sheet.getCellType(row, col);
  56. if (cellType && cellType instanceof GC.Spread.Sheets.CellTypes.RangeTemplate) {
  57. editRangeCell(sheet, row, col);
  58. }
  59. })
  60. function initDialogSpread() {
  61. if (!dialogSS) {
  62. dialogSS = new GC.Spread.Sheets.Workbook(document.getElementById("dialogSS"));
  63. }
  64. dialogSS.options.tabStripVisible = false;
  65. dialogSS.refresh();
  66. }
  67. function setEditorValue(sheet, row, col) {
  68. var cellType = sheet.getCellType(row, col);
  69. if (dialogSS && cellType) {
  70. var json = cellType.toJSON();
  71. var editorSheet = dialogSS.getActiveSheet()
  72. editorSheet.fromJSON(JSON.parse(json.resolvor.templatejson));
  73. console.log(JSON.parse(json.resolvor.templatejson));
  74. var value = JSON.parse(JSON.stringify(sheet.getValue(row, col)))
  75. var dataSource = new GC.Spread.Sheets.Bindings.CellBindingSource(value);
  76. editorSheet.setDataSource(dataSource);
  77. console.log(editorSheet.recalcAll)
  78. editorSheet.recalcAll(true);
  79. }
  80. }
  81. function editRangeCell(sheet, row, col) {
  82. setTimeout(function () {
  83. initDialogSpread()
  84. setEditorValue(sheet, row, col)
  85. }, 10)
  86. editorDialog.style.display = "block"
  87. }
  88. function closeDialog() {
  89. editorDialog.style.display = "none"
  90. }
  91. dialogCancel.onclick = function () {
  92. closeDialog();
  93. }
  94. dialogOK.onclick = function () {
  95. var editorSheet = dialogSS.getActiveSheet()
  96. var cellValue = editorSheet.getDataSource().getSource()
  97. var renderSheet = spread.getActiveSheet();
  98. renderSheet.setValue(renderSheet.getActiveRowIndex(), renderSheet.getActiveColumnIndex(), cellValue)
  99. closeDialog();
  100. console.log(JSON.stringify(renderSheet.getDataSource().getSource()));
  101. }
复制代码
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>SpreadJS in TypeScript</title>
  5. <link rel="stylesheet" type="text/css" href="node_modules/@grapecity/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
  6. <script src="node_modules/systemjs/dist/system.src.js"></script>
  7. <script src="systemjs.config.js"></script>
  8. <script>
  9. System.import('./src/app');
  10. </script>
  11. <style>
  12. #editorDialog{
  13. position: fixed;
  14. width: 100%;
  15. height: 100%;
  16. background-color: #aaaaaaaa;
  17. display: none;
  18. top: 0;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div id="ss" style="width:100%;height:98vh;border:1px solid darkgray"></div>
  24. <div id="editorDialog">
  25. <div style="height: 80%; width: 80%;margin: 50px auto;background-color:white">
  26. <button id='dialogOK'>确定</button>
  27. <button id='dialogCancel'>取消</button>
  28. <div id='dialogSS' style="width:100%;height:90%;border:1px solid darkgray"></div>
  29. </div>
  30. </div>
  31. </body>
  32. </html>
复制代码

2 个回复

倒序浏览
有心
中级会员   /  发表于:2022-1-30 08:29:40
沙发
一直加载中,看不到。
回复 使用道具 举报
dexteryao讲师达人认证 悬赏达人认证 SpreadJS 开发认证
超级版主   /  发表于:2022-1-30 08:35:06
板凳
有心 发表于 2022-1-30 08:29
一直加载中,看不到。

主要代码贴出来了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部