[{"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)

使用区域模板

需求:如何在表格绑定中使用区域模板


背景

我们在之前的FAQ中讲到了单元格中使用区域模板,同理在表格绑定中也能使用区域模板

区域模板实际上就是一个特殊的 CellType

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

在下面示例中,给表格第一行设置了CellType,然后使用 copyTableStyle 方法将内容复制到表格其他行,样式和公式都可以复制。

表格绑定的数据也无法使用sheet绑定中的value方法返回。需要在构建数据源时,数组中设置 RangeTemplate 的属性,就是一个对象

实现步骤

重新构建数据:

{
  list: [
    {
      detail: data[0],
    },
    {
      detail: data[1],
    },
    {
      detail: data[2],
    },
  ];
}//可以循环构建

核心代码:

var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"));
var dialogSS;
var editorDialog = document.getElementById("editorDialog");
var dialogOK = document.getElementById("dialogOK");
var dialogCancel = document.getElementById("dialogCancel");

var renderSheet = spread.getActiveSheet();
renderSheet.defaults.rowHeight = 207;

var templateSheet = new GC.Spread.Sheets.Worksheet();
templateSheet.fromJSON(templatesheetjson);
spread.addSheet(1, templateSheet);
templateSheet.setFormatter(2, 2, "=IMAGE(@)");

renderSheet.suspendPaint();
var celltype = new GC.Spread.Sheets.CellTypes.RangeTemplate(templateSheet);

// renderSheet.setCellType(0, 1, celltype);
// renderSheet.setBindingPath(0, 1, "detail")

renderSheet.setRowHeight(0, 10);
renderSheet.setColumnWidth(0, 440);
// renderSheet.setColumnWidth(1, 440)
var dataSource = new GC.Spread.Sheets.Bindings.CellBindingSource({
  list: [
    {
      detail: data[0],
    },
    {
      detail: data[1],
    },
    {
      detail: data[2],
    },
  ];
});

renderSheet.setRowHeight(1, 30);

// 添加表格
var table = renderSheet.tables.add("tableRecordds", 1, 0, 4, 1);
table.autoGenerateColumns(false);

var tableColumn1 = new GC.Spread.Sheets.Tables.TableColumn();
tableColumn1.name("Detail");
tableColumn1.dataField("detail");
table.bindColumns([tableColumn1]);
table.bindingPath("list");

// 数据绑定
renderSheet.setDataSource(dataSource);
renderSheet.getRange(2, 0).cellType(celltype);
// 设定style
copyTableStyle(renderSheet, table);
renderSheet.resumePaint();

function copyTableStyle(sheet, table) {
  var range = table.dataRange();
  var rowHeight = sheet.getRowHeight(range.row);
  for (var i = 1; i < range.rowCount; i++) {
    // 复制样式
    sheet.copyTo(
      range.row + i - 1,
      range.col,
      range.row + i,
      range.col,
      1,
      range.colCount,
      GC.Spread.Sheets.CopyToOptions.style
    );
    // 复制公式
    sheet.copyTo(
      range.row + i - 1,
      range.col,
      range.row + i,
      range.col,
      1,
      range.colCount,
      GC.Spread.Sheets.CopyToOptions.formula
    );
    // 复制单元格合并
    sheet.copyTo(
      range.row + i - 1,
      range.col,
      range.row + i,
      range.col,
      1,
      range.colCount,
      GC.Spread.Sheets.CopyToOptions.span
    );
    // 设置行高
    sheet.setRowHeight(range.row + i, rowHeight);
  }
}

// 双击单元格
spread.bind(GC.Spread.Sheets.Events.CellDoubleClick, function (s, e) {
  var sheet = e.sheet,
    row = e.row,
    col = e.col;
  var cellType = sheet.getCellType(row, col);
  if (
    cellType &&
    cellType instanceof GC.Spread.Sheets.CellTypes.RangeTemplate
  ) {
    // 如果是我们自己的区域模板
    editRangeCell(sheet, row, col);
  }
});

// 初始化子Spread
function initDialogSpread() {
  if (!dialogSS) {
    dialogSS = new GC.Spread.Sheets.Workbook(
      document.getElementById("dialogSS")
    );
  }

  dialogSS.options.tabStripVisible = false;
  dialogSS.refresh();
}

function editRangeCell(sheet, row, col) {
  setTimeout(function () {
    initDialogSpread();
    setEditorValue(sheet, row, col);
  }, 10);
  editorDialog.style.display = "block";
}

function setEditorValue(sheet, row, col) {
  var cellType = sheet.getCellType(row, col);
  if (dialogSS && cellType) {
    var json = cellType.toJSON();
    var editorSheet = dialogSS.getActiveSheet();
    editorSheet.fromJSON(JSON.parse(json.resolvor.templatejson));
    console.log(JSON.parse(json.resolvor.templatejson));
    var value = JSON.parse(JSON.stringify(sheet.getValue(row, col)));
    var dataSource = new GC.Spread.Sheets.Bindings.CellBindingSource(value);
    editorSheet.setDataSource(dataSource);
    console.log(editorSheet.recalcAll);
    editorSheet.recalcAll(true);
  }
}

// 关闭子图
function closeDialog() {
  editorDialog.style.display = "none";
}
// 取消按钮逻辑
dialogCancel.onclick = function () {
  closeDialog();
};
// 确认按钮逻辑
dialogOK.onclick = function () {
  var editorSheet = dialogSS.getActiveSheet();
  var cellValue = editorSheet.getDataSource().getSource();// 获取绑定数据

  var renderSheet = spread.getActiveSheet();
  renderSheet.setValue(// 为单元格赋值
    renderSheet.getActiveRowIndex(),
    renderSheet.getActiveColumnIndex(),
    cellValue
  );
  closeDialog();
  console.log(JSON.stringify(renderSheet.getDataSource().getSource()));
};
<!doctype html>
<html>

<head>
    <title>SpreadJS in TypeScript</title>
    <link rel="stylesheet" type="text/css"
        href="node_modules/@grapecity-software/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
    <script src="node_modules/systemjs/dist/system.src.js"></script>
    <script src="systemjs.config.js"></script>

    <script>
        System.import('./src/app');
    </script>
    <style>
        #editorDialog {
            position: fixed;
            width: 100%;
            height: 100%;
            background-color: #aaaaaaaa;
            display: none;
            top: 0;
        }
    </style>
</head>

<body>
    <div id="ss" style="width:100%;height:98vh;border:1px solid darkgray"></div>
    <div id="editorDialog">
        <div style="height: 80%; width: 80%;margin: 50px auto;background-color:white">
            <button id='dialogOK'>确定</button>
            <button id='dialogCancel'>取消</button>
            <div id='dialogSS' style="width:100%;height:90%;border:1px solid darkgray"></div>
        </div>
    </div>
</body>

</html>

完整版Demo可以参考论坛精华贴:在表格绑定中使用区域模板(Range Template)

作者: Dexter.Yao | 审核:AlexZ | 更新时间:2023.09.24