找回密码
 立即注册

QQ登录

只需一步,快速开始

硕㏒

金牌服务用户

48

主题

139

帖子

441

积分

金牌服务用户

积分
441
硕㏒
金牌服务用户   /  发表于:2024-10-24 15:51  /   查看:67  /  回复:5
100金币
image.png102026976.png

附件中有ssjson,目前的场景是有三个sheet,原材料附表,水泥混凝土配合比设计,报告,每个sheet都有一个table;
初始化时,调用后端接口,拿到数据后,给三个sheet页中的table设置数据,在原材料附表中没有问题;
在水泥混凝土配合比设计,报告这两个报告中,绑定了数据,发现有些字段赋值不成功:
image.png606543883.png
同时,在table下面的行,行高发生了变化:
image.png722166747.png
以下是我的代码部分
  1. processQualification() {
  2.       let arr = this.form.sampleInfoMaterialRelationDtoList;
  3.       if (arr && arr.length) {
  4.         arr.forEach((item, index) => {
  5.           item.index = index + 1;
  6.           if (item.qualificationName) {
  7.             // 使用JSON.parse将字符串转换为对象
  8.             const qualificationObject = JSON.parse(item.qualificationName);
  9.             // 使用Object.keys获取对象的所有可枚举属性名
  10.             const keys = Object.keys(qualificationObject);
  11.             // 创建一个空数组来存储键值对字符串
  12.             let keyValuePairs = keys.map(
  13.               (key) => `${key}:${qualificationObject[key]}`
  14.             );
  15.             item.qualificationName = keyValuePairs.join(";");
  16.           }
  17.         });
  18.       }
  19.       return arr;
  20.     },
  21.     processMaterialSheet(sheet, spreadNS, arr, data) {
  22.       let table = sheet.tables.findByName("Table1");
  23.       if (!table) {
  24.         console.error("未找到名为 'Table1' 的表格");
  25.         return;
  26.       }
  27.       let totalRowCount = sheet.getRowCount(); // 获取当前工作表的总行数
  28.       let rowCountToAdd = arr.length - 1; // 要添加的行数
  29.       // 尝试获取basicrow名称管理器的范围
  30.       let customName = sheet.getCustomName("basicrow");
  31.       // 检查basicrow是否存在
  32.       if (!customName) {
  33.         console.error("命名范围 'basicrow' 未找到。");

  34.         return;
  35.       }

  36.       // 获取basicrow的表达式
  37.       let expression = customName.getExpression();
  38.       // 获取basicrow的表达式

  39.       if (!expression) {
  40.         console.error("命名范围 'basicrow' 的表达式未定义。");

  41.         return;
  42.       }

  43.       // 获取basicrow的范围
  44.       let basicrow = expression.getRange();
  45.       // 获取basicrow的范围

  46.       if (!basicrow) {
  47.         console.error("无法获取 'basicrow' 的范围。");

  48.         return;
  49.       }
  50.       let startRow = basicrow.row; // basicrow的起始行
  51.       let rowHeight = sheet.getRowHeight(startRow); // 获取basicrow行高
  52.       let tableRange = table.range(); // 获取表格范围
  53.       let currentRowCount = tableRange.rowCount; // 当前表格行数
  54.       let row = tableRange.row; // 表格起始行
  55.       // 在工作表末尾添加行
  56.       sheet.addRows(totalRowCount, rowCountToAdd * basicrow.rowCount);
  57.       // 在表格中插入新行
  58.       table.insertRows(
  59.         currentRowCount - 2,
  60.         rowCountToAdd * basicrow.rowCount,
  61.         true
  62.       );
  63.       // 设置新行的行高
  64.       for (let i = 0; i <= rowCountToAdd * basicrow.rowCount + 3; i++) {
  65.         console.log(currentRowCount + row + i, rowHeight);
  66.         sheet.setRowHeight(currentRowCount + row + i, rowHeight);
  67.       }
  68.       for (let i = 0; i < rowCountToAdd; i++) {
  69.         sheet.copyTo(
  70.           startRow,
  71.           basicrow.col,
  72.           currentRowCount + row + basicrow.rowCount * i,
  73.           basicrow.col,
  74.           basicrow.rowCount,
  75.           basicrow.colCount,
  76.           GC.Spread.Sheets.CopyToOptions.style |
  77.             GC.Spread.Sheets.CopyToOptions.formula |
  78.             GC.Spread.Sheets.CopyToOptions.span
  79.         );
  80.       }
  81.       // table.expandBoundRows(true);
  82.       // 创建表格列对象
  83.       const columnNames = [
  84.         "index",
  85.         "experienceItemName",
  86.         "sampleCode",
  87.         "experienceItemParamNames",
  88.         "qualificationName",
  89.         "sampleNum",
  90.         "manufacturer",
  91.         "origin",
  92.         "batchNumber",
  93.         "sampleDesc",
  94.         "recommendDosage",
  95.       ];

  96.       let tableColumns = columnNames.map(
  97.         (columnName) => new spreadNS.Tables.TableColumn(columnName, columnName)
  98.       );

  99.       // 设置表格不自动生成列
  100.       table.autoGenerateColumns(false);

  101.       // 绑定数据
  102.       table.bind(tableColumns, "sales", data);
  103.     },
  104.     processReportSheet(sheet, spreadNS, arr, data) {
  105.       let table = sheet.tables.findByName("reportRecordProportion");
  106.       if (!table) {
  107.         console.error("未找到名为 'reportRecordProportion' 的表格");
  108.         return;
  109.       }
  110.       let totalRowCount = sheet.getRowCount(); // 获取当前工作表的总行数
  111.       let rowCountToAdd = arr.length - 1; // 要添加的行数
  112.       // 尝试获取basicrow名称管理器的范围
  113.       let customName = sheet.getCustomName("basicrow");
  114.       // 检查basicrow是否存在
  115.       if (!customName) {
  116.         console.error("命名范围 'basicrow' 未找到。");

  117.         return;
  118.       }

  119.       // 获取basicrow的表达式
  120.       let expression = customName.getExpression();
  121.       // 获取basicrow的表达式

  122.       if (!expression) {
  123.         console.error("命名范围 'basicrow' 的表达式未定义。");

  124.         return;
  125.       }

  126.       // 获取basicrow的范围
  127.       let basicrow = expression.getRange();
  128.       // 获取basicrow的范围

  129.       if (!basicrow) {
  130.         console.error("无法获取 'basicrow' 的范围。");

  131.         return;
  132.       }
  133.       let startRow = basicrow.row; // basicrow的起始行
  134.       let rowHeight = sheet.getRowHeight(startRow); // 获取basicrow行高
  135.       let tableRange = table.range(); // 获取表格范围
  136.       let currentRowCount = tableRange.rowCount; // 当前表格行数
  137.       let row = tableRange.row; // 表格起始行
  138.       // 在工作表末尾添加行
  139.       sheet.addRows(totalRowCount, rowCountToAdd * basicrow.rowCount);
  140.       // 在表格中插入新行
  141.       table.insertRows(
  142.         currentRowCount - 2,
  143.         rowCountToAdd * basicrow.rowCount,
  144.         true
  145.       );
  146.       // 设置新行的行高
  147.       for (let i = 0; i <= rowCountToAdd * basicrow.rowCount + 3; i++) {
  148.         console.log(currentRowCount + row + i, rowHeight);
  149.         sheet.setRowHeight(currentRowCount + row + i, rowHeight);
  150.       }
  151.       for (let i = 0; i < rowCountToAdd; i++) {
  152.         sheet.copyTo(
  153.           startRow,
  154.           basicrow.col,
  155.           currentRowCount + row + basicrow.rowCount * i,
  156.           basicrow.col,
  157.           basicrow.rowCount,
  158.           basicrow.colCount,
  159.           GC.Spread.Sheets.CopyToOptions.style |
  160.             GC.Spread.Sheets.CopyToOptions.formula |
  161.             GC.Spread.Sheets.CopyToOptions.span
  162.         );
  163.       }
  164.       // table.expandBoundRows(true);
  165.       // 创建表格列对象
  166.       const columnNames = [
  167.         "experienceItemName",
  168.         "qualificationName",
  169.         "manufacturer",
  170.         "sampleCode",
  171.       ];

  172.       let tableColumns = columnNames.map(
  173.         (columnName) => new spreadNS.Tables.TableColumn(columnName, columnName)
  174.       );

  175.       // 设置表格不自动生成列
  176.       table.autoGenerateColumns(false);

  177.       // 绑定数据
  178.       table.bind(tableColumns, "sales", data);
  179.     },
  180.     processOriginalSheet(sheet, spreadNS, arr, data) {
  181.       let table = sheet.tables.findByName("originalRecordProportion");
  182.       if (!table) {
  183.         console.error("未找到名为 'originalRecordProportion' 的表格");
  184.         return;
  185.       }
  186.       let totalRowCount = sheet.getRowCount(); // 获取当前工作表的总行数
  187.       let rowCountToAdd = arr.length - 1; // 要添加的行数
  188.       // 尝试获取basicrow名称管理器的范围
  189.       let customName = sheet.getCustomName("basicrow");
  190.       // 检查basicrow是否存在
  191.       if (!customName) {
  192.         console.error("命名范围 'basicrow' 未找到。");

  193.         return;
  194.       }

  195.       // 获取basicrow的表达式
  196.       let expression = customName.getExpression();
  197.       // 获取basicrow的表达式

  198.       if (!expression) {
  199.         console.error("命名范围 'basicrow' 的表达式未定义。");

  200.         return;
  201.       }

  202.       // 获取basicrow的范围
  203.       let basicrow = expression.getRange();
  204.       // 获取basicrow的范围

  205.       if (!basicrow) {
  206.         console.error("无法获取 'basicrow' 的范围。");

  207.         return;
  208.       }
  209.       let startRow = basicrow.row; // basicrow的起始行
  210.       let rowHeight = sheet.getRowHeight(startRow); // 获取basicrow行高
  211.       let tableRange = table.range(); // 获取表格范围
  212.       let currentRowCount = tableRange.rowCount; // 当前表格行数
  213.       let row = tableRange.row; // 表格起始行
  214.       // 在工作表末尾添加行
  215.       sheet.addRows(totalRowCount, rowCountToAdd * basicrow.rowCount);
  216.       // 在表格中插入新行
  217.       table.insertRows(
  218.         currentRowCount - 2,
  219.         rowCountToAdd * basicrow.rowCount,
  220.         true
  221.       );
  222.       // 设置新行的行高
  223.       for (let i = 0; i <= rowCountToAdd * basicrow.rowCount + 3; i++) {
  224.         console.log(currentRowCount + row + i, rowHeight);
  225.         sheet.setRowHeight(currentRowCount + row + i, rowHeight);
  226.       }
  227.       for (let i = 0; i < rowCountToAdd; i++) {
  228.         sheet.copyTo(
  229.           startRow,
  230.           basicrow.col,
  231.           currentRowCount + row + basicrow.rowCount * i,
  232.           basicrow.col,
  233.           basicrow.rowCount,
  234.           basicrow.colCount,
  235.           GC.Spread.Sheets.CopyToOptions.style |
  236.             GC.Spread.Sheets.CopyToOptions.formula |
  237.             GC.Spread.Sheets.CopyToOptions.span
  238.         );
  239.       }
  240.       // table.expandBoundRows(true);
  241.       // 创建表格列对象
  242.       const columnNames = [
  243.         "experienceItemName",
  244.         "qualificationName",
  245.         "manufacturer",
  246.         "sampleCode",
  247.       ];

  248.       let tableColumns = columnNames.map(
  249.         (columnName) => new spreadNS.Tables.TableColumn(columnName, columnName)
  250.       );

  251.       // 设置表格不自动生成列
  252.       table.autoGenerateColumns(false);

  253.       // 绑定数据
  254.       table.bind(tableColumns, "sales", data);
  255.     },
  256.     processingMaterialInfo(spread) {
  257.       let spreadNS = GC.Spread.Sheets;
  258.       const sheetCount = spread.getSheetCount();
  259.       let arr = this.processQualification();
  260.       let data = {
  261.         name: "Jones",
  262.         region: "East",
  263.         sales: arr,
  264.       };
  265.       for (let i = 0; i < sheetCount; i++) {
  266.         const sheet = spread.getSheet(i);
  267.         let sheetName = sheet.name();
  268.         if (sheetName === "原材料附表") {
  269.           this.processMaterialSheet(sheet, spreadNS, arr, data);
  270.         } else if (sheetName === "报告") {
  271.           this.processReportSheet(sheet, spreadNS, arr, data);
  272.         } else if (sheetName.includes("配合比")) {
  273.           this.processOriginalSheet(sheet, spreadNS, arr, data);
  274.         }

  275.         console.log(sheetName);
  276.       }
  277.     },
复制代码



示例.ssjson

203.45 KB, 下载次数: 9

5 个回复

倒序浏览
Ellia.DuanSpreadJS 开发认证
超级版主   /  发表于:4 天前
沙发
本帖最后由 Ellia.Duan 于 2024-10-24 17:50 编辑

您好,还缺少form.sampleInfoMaterialRelationDtoList数据,请上传到附件demo中。

以及检查是否还有其他变量缺失,补全后使文件在浏览器打开后,可以复现您的问题。

index_2.html

218.72 KB, 下载次数: 9

回复 使用道具 举报
硕㏒
金牌服务用户   /  发表于:3 天前
板凳
本帖最后由 硕㏒ 于 2024-10-25 09:22 编辑
Ellia.Duan 发表于 2024-10-24 17:49
您好,还缺少form.sampleInfoMaterialRelationDtoList数据,请上传到附件demo中。

以及检查是否还有其他 ...

附件中增加了数据,变量我检查了一遍是没有错的

image.png725490112.png
image.png48551024.png

index_2.html

363.83 KB, 下载次数: 9

回复 使用道具 举报
Ellia.DuanSpreadJS 开发认证
超级版主   /  发表于:3 天前
地板
您好,

第一个问题:为什么【原材料附表】可以数据展示完整,而【水泥混凝土配合比设计】和【报告】数据展示不完整?
因为【原材料附表】没有进行单元格合并,一列对应一个字段。
而【水泥混凝土配合比设计】和【报告】均有单元格合并,以【水泥混凝土配合比设计】为例,为前四列赋值Column字段,
所以A列显示experienceItemName字段,而D列(第二列标题)显示的是第四个字段:sampleCode。如下图所示
image.png608840026.png
所以正确的绑定字段是为A、D、F、K列绑定字段
image.png856642150.png

关于第二个问题,目前看到行高有差别的,再深入调研下原因,
image.png383606566.png

回复 使用道具 举报
硕㏒
金牌服务用户   /  发表于:3 天前
5#
Ellia.Duan 发表于 2024-10-25 09:33
您好,

第一个问题:为什么【原材料附表】可以数据展示完整,而【水泥混凝土配合比设计】和【报告】数据 ...

问题已定位到,谢谢支持
回复 使用道具 举报
Ellia.DuanSpreadJS 开发认证
超级版主   /  发表于:3 天前
6#
不客气,第二个问题可以开启table的自动扩展行,删除新增行的相关代码来解决。
那本帖就先结贴啦。有问题欢迎发新帖交流。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部